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..");
}
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;
}
}
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());
}
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);
}
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);
}
}
Aggregations