Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a ServiceMonitor for *Kubernetes* client (not openshift) #2740

Closed
zenbones opened this issue Jan 20, 2021 · 13 comments
Closed

Creating a ServiceMonitor for *Kubernetes* client (not openshift) #2740

zenbones opened this issue Jan 20, 2021 · 13 comments
Labels
question Waiting on feedback Issues that require feedback from User/Other community members

Comments

@zenbones
Copy link

Is there any way to create a ServiceMonitor, as in..

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor

If it isn't directly wrapped by the API, can I use the client to create/install an arbitrary type from string or JsonNode or... something?

@rohanKanojia
Copy link
Member

@manusa manusa added question Waiting on feedback Issues that require feedback from User/Other community members labels Jan 21, 2021
@zenbones
Copy link
Author

Yes, I know I can, and that does work. I had opened this ticket before, which you responded to with the very nice work on ServiceMonitor you did for the OpenShift client. The problem is that I'm using the Kubernetes client. I guess I should bother to read the code and create a proper k8s version of ServiceMonitor, but my time is limited and the raw api does work fine, but the heart wants what the heart wants.

@rohanKanojia
Copy link
Member

rohanKanojia commented Jan 21, 2021

ohk, I see. I don't think it's possible to add ServiceMonitor in KubernetesClient Jar. Best I can think of is providing a prometheus extension that can have all POJOs for structs in github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1 package.

If you just want to use ServiceMonitor with KubernetesClient using typed custom resource API. You can add this additional dependency in project which contains all monitoring model including ServiceMonitor:

<dependency>
  <groupId>io.fabric8</groupId>
  <artifactId>openshift-model-monitoring</artifactId>
  <version>5.0.0</version>
</dependency>

and then create a ServiceMonitor client like this:

try (KubernetesClient client = new DefaultKubernetesClient()) {
   // Create ServiceMonitor Client
   MixedOperation<ServiceMonitor, ServiceMonitorList, Resource<ServiceMonitor>> svcMonitorClient = client.customResources(ServiceMonitor.class);
   
   // Use Client for ServiceMonitor related operations
   svcMonitorClient.inNamespace("foo").list();

}

If you don't want to add additional dependency you can copy-paste ServiceMonitor POJOs into your project too(You can find them while building the project):

kubernetes-client : $ find . -iname ServiceMonitor*.java
./kubernetes-model-generator/openshift-model-monitoring/target/generated-sources/io/fabric8/openshift/api/model/monitoring/v1/ServiceMonitor.java
./kubernetes-model-generator/openshift-model-monitoring/target/generated-sources/io/fabric8/openshift/api/model/monitoring/v1/ServiceMonitorList.java
./kubernetes-model-generator/openshift-model-monitoring/target/generated-sources/io/fabric8/openshift/api/model/monitoring/v1/ServiceMonitorSpec.java

@zenbones
Copy link
Author

Thank you, that's helpful. I'll poke around when I get a chance and see what works best. Feel free to close this ticket. Adding a small section in docs with advice similar to the above might be helpful for others as well.

@manusa manusa closed this as completed Feb 8, 2021
@shouldnotappearcalm
Copy link

ohk, I see. I don't think it's possible to add ServiceMonitor in KubernetesClient Jar. Best I can think of is providing a prometheus extension that can have all POJOs for structs in github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1 package.

If you just want to use ServiceMonitor with KubernetesClient using typed custom resource API. You can add this additional dependency in project which contains all monitoring model including ServiceMonitor:

<dependency>
  <groupId>io.fabric8</groupId>
  <artifactId>openshift-model-monitoring</artifactId>
  <version>5.0.0</version>
</dependency>

and then create a ServiceMonitor client like this:

try (KubernetesClient client = new DefaultKubernetesClient()) {
   // Create ServiceMonitor Client
   MixedOperation<ServiceMonitor, ServiceMonitorList, Resource<ServiceMonitor>> svcMonitorClient = client.customResources(ServiceMonitor.class);
   
   // Use Client for ServiceMonitor related operations
   svcMonitorClient.inNamespace("foo").list();

}

If you don't want to add additional dependency you can copy-paste ServiceMonitor POJOs into your project too(You can find them while building the project):

kubernetes-client : $ find . -iname ServiceMonitor*.java
./kubernetes-model-generator/openshift-model-monitoring/target/generated-sources/io/fabric8/openshift/api/model/monitoring/v1/ServiceMonitor.java
./kubernetes-model-generator/openshift-model-monitoring/target/generated-sources/io/fabric8/openshift/api/model/monitoring/v1/ServiceMonitorList.java
./kubernetes-model-generator/openshift-model-monitoring/target/generated-sources/io/fabric8/openshift/api/model/monitoring/v1/ServiceMonitorSpec.java

Hello i have a question,openshift-model-monitoring ServiceMonitor not extend CustomResource,how to use client.customResources(ServiceMonitor.class) directly

@rohanKanojia
Copy link
Member

rohanKanojia commented Mar 15, 2021

@shouldnotappearcalm: Hmm, I'm not sure if depending upon current CustomResource API you would be able to use it. Could you please create an issue for adding support for Prometheus Extension? It should be easy to implement.

@shouldnotappearcalm
Copy link

shouldnotappearcalm commented Mar 15, 2021

@rohanKanojia: This is how I use it now,but <T extends HasMetadata, L extends KubernetesResourceList<T>> MixedOperation<T, L, Resource<T>> customResources(CustomResourceDefinitionContext crdContext, Class<T> resourceType, Class<L> listClass) method is deprecated in 5.1.0

Config config = new ConfigBuilder().build();
KubernetesClient kubernetesClient = new DefaultKubernetesClient(config);
String crdName = "servicemonitors.monitoring.coreos.com";
CustomResourceDefinition crd = new ApiextensionsAPIGroupClient().v1beta1().customResourceDefinitions().withName(crdName).get();
MixedOperation<ServiceMonitor, ServiceMonitorList, Resource<ServiceMonitor>> svcMonitorClient = kubernetesClient.customResources(CustomResourceDefinitionContext.fromCrd(crd), ServiceMonitor.class, ServiceMonitorList.class);
ServiceMonitorList serviceMonitorList = svcMonitorClient.inNamespace("default").list();

@rohanKanojia
Copy link
Member

Hmm, I see. We're discussing whether this should be part of API in future versions or not here #2738 . I think it should stay there since this can allow users to use same POJO for multiple versions too.

@shouldnotappearcalm
Copy link

Okay, the way I currently use can also achieve my needs

@rohanKanojia
Copy link
Member

@shouldnotappearcalm : Bdw, I would appreciate it if you could add your comment on #2738 to make this method not deprecated . It would help us in making the right decision.

@shouldnotappearcalm
Copy link

shouldnotappearcalm commented Mar 15, 2021

I have added a comment to 2738

@iocanel
Copy link
Member

iocanel commented Mar 16, 2021

@shouldnotappearcalm @rohanKanojia @zenbones: FWIW, we do have a client extension as part of dekorate: https://github.com/dekorateio/dekorate/tree/master/annotations/prometheus-annotations

The extension bits:

  • client
  • model

package should move to the client as extension, or at least that was the intention.
If anyone is up to the task, I wouldn't mind moving them there.

@rohanKanojia
Copy link
Member

@iocanel : Could you please open a separate issue for this? I think we should do it. Right now our extensions also have mock module too but I think it should be easy to implement too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Waiting on feedback Issues that require feedback from User/Other community members
Projects
None yet
Development

No branches or pull requests

5 participants