Search in sources :

Example 6 with V1ObjectMeta

use of io.kubernetes.client.openapi.models.V1ObjectMeta in project java by kubernetes-client.

the class InformerExample method main.

public static void main(String[] args) throws Exception {
    CoreV1Api coreV1Api = new CoreV1Api();
    ApiClient apiClient = coreV1Api.getApiClient();
    OkHttpClient httpClient = apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
    apiClient.setHttpClient(httpClient);
    SharedInformerFactory factory = new SharedInformerFactory(apiClient);
    // Node informer
    SharedIndexInformer<V1Node> nodeInformer = factory.sharedIndexInformerFor(// the informer-factory.
    (CallGeneratorParams params) -> {
        return coreV1Api.listNodeCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
    }, V1Node.class, V1NodeList.class);
    nodeInformer.addEventHandler(new ResourceEventHandler<V1Node>() {

        @Override
        public void onAdd(V1Node node) {
            System.out.printf("%s node added!\n", node.getMetadata().getName());
        }

        @Override
        public void onUpdate(V1Node oldNode, V1Node newNode) {
            System.out.printf("%s => %s node updated!\n", oldNode.getMetadata().getName(), newNode.getMetadata().getName());
        }

        @Override
        public void onDelete(V1Node node, boolean deletedFinalStateUnknown) {
            System.out.printf("%s node deleted!\n", node.getMetadata().getName());
        }
    });
    factory.startAllRegisteredInformers();
    V1Node nodeToCreate = new V1Node();
    V1ObjectMeta metadata = new V1ObjectMeta();
    metadata.setName("noxu");
    nodeToCreate.setMetadata(metadata);
    V1Node createdNode = coreV1Api.createNode(nodeToCreate, null, null, null, null);
    Thread.sleep(3000);
    Lister<V1Node> nodeLister = new Lister<V1Node>(nodeInformer.getIndexer());
    V1Node node = nodeLister.get("noxu");
    System.out.printf("noxu created! %s\n", node.getMetadata().getCreationTimestamp());
    factory.stopAllRegisteredInformers();
    Thread.sleep(3000);
    System.out.println("informer stopped..");
}
Also used : OkHttpClient(okhttp3.OkHttpClient) V1Node(io.kubernetes.client.openapi.models.V1Node) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) Lister(io.kubernetes.client.informer.cache.Lister) ApiClient(io.kubernetes.client.openapi.ApiClient) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Example 7 with V1ObjectMeta

use of io.kubernetes.client.openapi.models.V1ObjectMeta in project java by kubernetes-client.

the class ConfigMapLock method create.

@Override
public boolean create(LeaderElectionRecord record) {
    try {
        V1ConfigMap configMap = new V1ConfigMap();
        V1ObjectMeta objectMeta = new V1ObjectMeta();
        objectMeta.setName(name);
        objectMeta.setNamespace(namespace);
        Map<String, String> annotations = new HashMap<>();
        annotations.put(LeaderElectionRecordAnnotationKey, coreV1Client.getApiClient().getJSON().serialize(record));
        objectMeta.setAnnotations(annotations);
        if (record.getOwnerReference() != null) {
            objectMeta.setOwnerReferences(Collections.singletonList(record.getOwnerReference()));
        }
        configMap.setMetadata(objectMeta);
        V1ConfigMap createdConfigMap = coreV1Client.createNamespacedConfigMap(namespace, configMap, null, null, null, null);
        configMapRefer.set(createdConfigMap);
        return true;
    } catch (ApiException e) {
        if (e.getCode() == HttpURLConnection.HTTP_CONFLICT) {
            log.debug("received {} when creating configmap lock", e.getCode(), e);
        } else {
            log.error("received {} when creating configmap lock", e.getCode(), e);
        }
        return false;
    }
}
Also used : HashMap(java.util.HashMap) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) ApiException(io.kubernetes.client.openapi.ApiException)

Example 8 with V1ObjectMeta

use of io.kubernetes.client.openapi.models.V1ObjectMeta in project java by kubernetes-client.

the class EventLoggerTest method testPatchComputing.

@Test
public void testPatchComputing() {
    CoreV1Event event1 = new CoreV1EventBuilder().withSource(new V1EventSourceBuilder().build()).withMetadata(new V1ObjectMeta()).withInvolvedObject(new V1ObjectReferenceBuilder().build()).withCount(1).withMessage("foo1").build();
    CoreV1Event event2 = new CoreV1EventBuilder().withSource(new V1EventSourceBuilder().build()).withMetadata(new V1ObjectMeta()).withInvolvedObject(new V1ObjectReferenceBuilder().build()).withCount(2).withMessage("foo2").build();
    String aggregatedKey = EventUtils.getAggregatedAndLocalKeyByReason(event1).getRight();
    EventLogger eventLogger = new EventLogger(100, EventUtils::getEventKey);
    MutablePair<CoreV1Event, V1Patch> result1 = eventLogger.observe(event1, aggregatedKey);
    assertEquals(event1, result1.getLeft());
    assertNull(result1.getRight());
    MutablePair<CoreV1Event, V1Patch> result2 = eventLogger.observe(event2, aggregatedKey);
    assertEquals(event2, result2.getLeft());
    assertNotNull(result2.getRight());
}
Also used : V1EventSourceBuilder(io.kubernetes.client.openapi.models.V1EventSourceBuilder) V1ObjectReferenceBuilder(io.kubernetes.client.openapi.models.V1ObjectReferenceBuilder) EventLogger(io.kubernetes.client.extended.event.legacy.EventLogger) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) EventUtils(io.kubernetes.client.extended.event.legacy.EventUtils) V1Patch(io.kubernetes.client.custom.V1Patch) CoreV1EventBuilder(io.kubernetes.client.openapi.models.CoreV1EventBuilder) CoreV1Event(io.kubernetes.client.openapi.models.CoreV1Event) Test(org.junit.Test)

Example 9 with V1ObjectMeta

use of io.kubernetes.client.openapi.models.V1ObjectMeta in project java by kubernetes-client.

the class DeploymentHelper method equalIgnoreHash.

/**
 * EqualIgnoreHash returns true if two given podTemplateSpec are equal, ignoring the diff in value
 * of Labels[pod-template-hash] We ignore pod-template-hash because: 1. The hash result would be
 * different upon podTemplateSpec API changes (e.g. the addition of a new field will cause the
 * hash code to change) 2. The deployment template won't have hash labels
 */
private static boolean equalIgnoreHash(V1PodTemplateSpec template1, V1PodTemplateSpec template2) {
    if (!Objects.equals(template1.getSpec(), template2.getSpec()))
        return false;
    V1ObjectMeta m1Copy = objectMetaDeepCopy(template1.getMetadata());
    V1ObjectMeta m2Copy = objectMetaDeepCopy(template2.getMetadata());
    m1Copy.getLabels().remove(DEFAULT_DEPLOYMENT_UNIQUE_LABEL_KEY);
    m2Copy.getLabels().remove(DEFAULT_DEPLOYMENT_UNIQUE_LABEL_KEY);
    return m1Copy.equals(m2Copy);
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta)

Example 10 with V1ObjectMeta

use of io.kubernetes.client.openapi.models.V1ObjectMeta in project java by kubernetes-client.

the class KubectlExec method execute.

@Override
public Integer execute() throws KubectlException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(name).namespace(namespace));
    Exec exec = new Exec(apiClient);
    try {
        Process proc = exec.exec(pod, command, container, stdin, tty);
        copyAsync(proc.getInputStream(), System.out);
        copyAsync(proc.getErrorStream(), System.err);
        if (stdin) {
            copyAsync(System.in, proc.getOutputStream());
        }
        return proc.waitFor();
    } catch (InterruptedException | ApiException | IOException ex) {
        throw new KubectlException(ex);
    }
}
Also used : Exec(io.kubernetes.client.Exec) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) IOException(java.io.IOException) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) ApiException(io.kubernetes.client.openapi.ApiException)

Aggregations

V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)78 Test (org.junit.Test)49 V1Pod (io.kubernetes.client.openapi.models.V1Pod)37 HashMap (java.util.HashMap)14 ApiException (io.kubernetes.client.openapi.ApiException)11 V1PodList (io.kubernetes.client.openapi.models.V1PodList)11 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)10 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)8 V1Container (io.kubernetes.client.openapi.models.V1Container)7 ApiClient (io.kubernetes.client.openapi.ApiClient)6 JSON (io.kubernetes.client.openapi.JSON)6 V1Job (io.kubernetes.client.openapi.models.V1Job)6 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)6 V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)6 V1Patch (io.kubernetes.client.custom.V1Patch)5 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)5 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)5 V1Service (io.kubernetes.client.openapi.models.V1Service)5 V1ServiceSpec (io.kubernetes.client.openapi.models.V1ServiceSpec)5 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)5