public KubernetesClientTimeoutException(HasMetadata resource, long amount, TimeUnit timeUnit) { super(String.format(RESOURCE_FORMAT, timeUnit.toMillis(amount), resource.getKind(), resource.getMetadata().getName(), resource.getMetadata().getNamespace())); this.resourcesNotReady = Collections.unmodifiableList(Arrays.asList(resource)); }
"[Assertion failed] - the object argument must be null"); Endpoints endpoints = client.endpoints().withName(serviceId).get(); List<EndpointSubset> subsets = null != endpoints ? endpoints.getSubsets() : new ArrayList<>(); List<ServiceInstance> instances = new ArrayList<>(); if (!subsets.isEmpty()) { final Service service = client.services().withName(serviceId).get(); if(metadataProps.isAddLabels()) { Map<String, String> labelMetadata = getMapWithPrefixedKeys( service.getMetadata().getLabels(), metadataProps.getLabelsPrefix()); if (log.isDebugEnabled()) { log.debug("Adding label metadata: "+labelMetadata); service.getMetadata().getAnnotations(), metadataProps.getAnnotationsPrefix()); if (log.isDebugEnabled()) { log.debug("Adding annotation metadata: "+annotationMetadata); new DefaultIsServicePortSecureResolver.Input( endpointPort.getPort(), service.getMetadata().getName(), service.getMetadata().getLabels(), service.getMetadata().getAnnotations()
public static ObjectMeta getOrCreateMetadata(HasMetadata entity) { ObjectMeta metadata = entity.getMetadata(); if (metadata == null) { metadata = new ObjectMeta(); entity.setMetadata(metadata); } return metadata; }
@Override public void contribute(Builder builder) { try { Pod current = utils.currentPod().get(); Map<String, Object> details = new HashMap<>(); if (current != null) { details.put("inside", true); details.put("namespace", current.getMetadata().getNamespace()); details.put("podName", current.getMetadata().getName()); details.put("podIp", current.getStatus().getPodIP()); details.put("serviceAccount", current.getSpec().getServiceAccountName()); details.put("nodeName", current.getSpec().getNodeName()); details.put("hostIp", current.getStatus().getHostIP()); } else { details.put("inside", false); } builder.withDetail("kubernetes", details); } catch (Exception e) { LOG.warn("Failed to get pod details", e); } } }
public static Map<String, String> getOrCreateAnnotations(HasMetadata entity) { ObjectMeta metadata = getOrCreateMetadata(entity); Map<String, String> answer = metadata.getAnnotations(); if (answer == null) { // use linked so the annotations can be in the FIFO order answer = new LinkedHashMap<>(); metadata.setAnnotations(answer); } return answer; }
client.namespaces().list() ); client.namespaces().withLabel("this", "works").list() ); client.pods().withLabel("this", "works").list() ); client.pods().inNamespace("test").withLabel("this", "works").list() ); client.pods().inNamespace("test").withName("testing").get() ); PodList podList = client.pods().inNamespace("myproject").list(5, null); podList.getItems().forEach((obj) -> { System.out.println(obj.getMetadata().getName()); }); podList = client.pods().inNamespace("myproject").list(5, podList.getMetadata().getContinue()); podList.getItems().forEach((obj) -> { System.out.println(obj.getMetadata().getName()); }); Integer services = client.services().inNamespace("myproject").list(1, null).getItems().size(); System.out.println(services); } catch (KubernetesClientException e) { logger.error(e.getMessage(), e);
log("Created namespace", client.namespaces().createOrReplace(ns)); client.serviceAccounts().inNamespace("thisisatest").createOrReplace(fabric8); for (int i = 0; i < 2; i++) { System.err.println("Iteration:" + (i+1)); deployment = client.apps().deployments().inNamespace("thisisatest").create(deployment); log("Created deployment", deployment); System.err.println("Scaling up:" + deployment.getMetadata().getName()); client.apps().deployments().inNamespace("thisisatest").withName("nginx").scale(2, true); log("Created replica sets:", client.apps().replicaSets().inNamespace("thisisatest").list().getItems()); System.err.println("Deleting:" + deployment.getMetadata().getName()); client.resource(deployment).delete(); client.namespaces().withName("thisisatest").delete(); client.close();
ObjectMeta metadata = crd.getMetadata(); if (metadata != null) { String name = metadata.getName(); System.out.println(" " + name + " => " + metadata.getSelfLink()); if (DUMMY_CRD_NAME.equals(name)) { dummyCRD = crd; System.out.println("Found CRD: " + dummyCRD.getMetadata().getSelfLink()); } else { dummyCRD = new CustomResourceDefinitionBuilder(). build(); client.customResourceDefinitions().create(dummyCRD); System.out.println("Created CRD " + dummyCRD.getMetadata().getName()); dummyClient = ((MixedOperation<Dummy, DummyList, DoneableDummy, Resource<Dummy, DoneableDummy>>) dummyClient).inNamespace(namespace); ObjectMeta metadata = new ObjectMeta(); metadata.setName("foo"); dummy.setMetadata(metadata); DummySpec dummySpec = new DummySpec(); dummyClient.withResourceVersion(created.getMetadata().getResourceVersion()).watch(new Watcher<Dummy>() { @Override public void eventReceived(Action action, Dummy resource) {
@Override Ingress applyResource(Ingress original, Ingress current) { return client .extensions() .ingresses() .inNamespace(getNamespace()) .withName(current.getMetadata().getName()) .edit() .withMetadata(current.getMetadata()) .withSpec(current.getSpec()) .done(); }
@Override public String getURL(Service service, String portName, String namespace, KubernetesClient client) { String serviceName = service.getMetadata().getName(); ServicePort port = URLFromServiceUtil.getServicePortByName(service, portName); if(port != null && port.getName() != null && isOpenShift(client)) { try { String serviceProtocol = port.getProtocol(); OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class); Route route = openShiftClient.routes().inNamespace(namespace).withName(service.getMetadata().getName()).get(); if (route != null) { return (serviceProtocol + "://" + route.getSpec().getHost()).toLowerCase(); } } catch (KubernetesClientException e) { if(e.getCode() == HttpURLConnection.HTTP_FORBIDDEN) { logger.warn("Could not lookup route:" + serviceName + " in namespace:"+ namespace +", due to: " + e.getMessage()); } } } return null; }
String namespace = oldObj.getMetadata().getNamespace(); String oldName = oldObj.getMetadata().getName(); pods().inNamespace(namespace).withName(pod.getMetadata().getName()) .edit() .editMetadata().addToLabels(DEPLOYMENT_KEY, oldDeploymentHash) .and().done(); } catch (KubernetesClientException e) { LOG.warn("Unable to add deployment key to pod: {}", e.getMessage()); String newName = newObj.getMetadata().getName(); if (newName == null || newName.equals(oldName)) { newName = newName + "-" + newDeploymentHash; while (getReplicas(createdObj) < getReplicas(newObj)) { int newReplicas = getReplicas(createdObj) + 1; resources().inNamespace(namespace).withName(createdObj.getMetadata().getName()).scale(newReplicas, true); waitUntilPodsAreReady(createdObj, namespace, newReplicas); createdObj = setReplicas(createdObj, newReplicas); if (Objects.equals(oldName, newObj.getMetadata().getName())) { createdObj.getMetadata().setResourceVersion(null); createdObj.getMetadata().setName(oldName); editable = resources().inNamespace(namespace).withName(createdObj.getMetadata().getName()).cascading(false).edit(); removeDeploymentKey(editable); createdObj = editable.done();
boolean isOpenshift = client.isAdaptable(OpenShiftClient.class); String namespace = session.getNamespace(); String routeDomain = null; if (entity instanceof Pod) { Pod pod = (Pod) entity; log.status("Applying pod:" + getName(pod)); Set<Secret> secrets = generateSecrets(client, session, pod.getMetadata()); String serviceAccountName = pod.getSpec().getServiceAccountName(); log.status("Applying replication controller:" + getName(replicationController)); Set<Secret> secrets = generateSecrets(client, session, replicationController.getSpec().getTemplate().getMetadata()); String serviceAccountName = replicationController.getSpec().getTemplate().getSpec().getServiceAccountName(); String name = metadata.getName(); if (isOpenshift) { OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class); OAuthClient current = openShiftClient.oAuthClients().withName(name).get(); boolean create = false; if (current == null) { openShiftClient.oAuthClients().withName(name).delete(); current.getMetadata().setResourceVersion(null);
public static void main(String args[]) { try (final KubernetesClient client = new DefaultKubernetesClient()) { Service service = new ServiceBuilder() .withNewMetadata() .withName("my-service") .endMetadata() .withNewSpec() .withSelector(Collections.singletonMap("app", "MyApp")) .addNewPort() .withName("test-port") .withProtocol("TCP") .withPort(80) .withTargetPort(new IntOrString(9376)) .endPort() .withType("LoadBalancer") .endSpec() .withNewStatus() .withNewLoadBalancer() .addNewIngress() .withIp("146.148.47.155") .endIngress() .endLoadBalancer() .endStatus() .build(); service = client.services().inNamespace(client.getNamespace()).create(service); log("Created service with name ", service.getMetadata().getName()); String serviceURL = client.services().inNamespace(client.getNamespace()).withName(service.getMetadata().getName()).getURL("test-port"); log("Service URL", serviceURL); } }
log("Created namespace", client.namespaces().create(ns)); client.serviceAccounts().inNamespace("thisisatest").create(fabric8); log("Created image stream", client.imageStreams().inNamespace("thisisatest").createNew() .withNewMetadata() .withName("example-camel-cdi") .done()); log("Created image stream", client.imageStreams().inNamespace("thisisatest").createNew() .withNewMetadata() .withName("java-sti") .withNewMetadata().withName("custom-build-config").endMetadata() .build()); log("Build:", build.getMetadata().getName()); client.buildConfigs().inNamespace("thisisatest").withName("custom-build-config") for (Build b: client.builds().inNamespace("thisisatest").list().getItems()) { log("\t\t\t"+b.getMetadata().getName()); log("\t\t\t\t\t Log:");client.builds().inNamespace("thisisatest").withName(b.getMetadata().getName()).watchLog(System.out); client.namespaces().withName("thisisatest").delete(); client.close();
Config config = new ConfigBuilder().withMasterUrl(master).build(); try(final KubernetesClient client = new DefaultKubernetesClient(config)) { final String namespace = client.getNamespace(); cronJob1 = client.batch().cronjobs().inNamespace(namespace).create(cronJob1); log("Successfully created cronjob with name ", cronJob1.getMetadata().getName()); try (Watch watch = client.pods().inNamespace(namespace).withLabel("job-name").watch(new Watcher<Pod>() { @Override public void eventReceived(Action action, Pod aPod) { log("An error occured while processing cronjobs:", exception.getMessage());
try (final KubernetesClient client = new DefaultKubernetesClient(config)) { if (namespace == null) { namespace = client.getNamespace(); List<HasMetadata> resources = client.load(new FileInputStream(fileName)).get(); if (resources.isEmpty()) { System.err.println("No resources loaded from file: " +fileName); Pod pod = (Pod) resource; System.out.println("Creating pod in namespace " + namespace); NonNamespaceOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods = client.pods().inNamespace(namespace); Pod result = pods.create(pod); System.out.println("Created pod " + result.getMetadata().getName()); } else { System.err.println("Loaded resource is not a Pod! " + resource); logger.error(e.getMessage(), e); } catch (Exception e) { logger.error(e.getMessage(), e);
String namespace = "default"; log("namespace", namespace); Pod pod = client.pods().inNamespace(namespace).load(PodPresetExamples.class.getResourceAsStream("/pod-preset-example.yml")).get(); log("Pod created"); client.pods().inNamespace(namespace).create(pod); .build(); log("Creating Pod Preset : " + podPreset.getMetadata().getName()); client.settings().podPresets().inNamespace(namespace).create(podPreset); pod = client.pods().inNamespace(namespace).withName(pod.getMetadata().getName()).get(); log("Updated pod: "); log(SerializationUtils.dumpAsYaml(pod));
private void updateConfigMap(ConfigMap oldConfigMap, ConfigMap newConfigMap) { kubernetesClient.configMaps() .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace())) .withName(leaderProperties.getConfigMapName()) .lockResourceVersion(oldConfigMap.getMetadata().getResourceVersion()) .replace(newConfigMap); } }
@Override public void eventReceived(Action action, Pod aPod) { log(aPod.getMetadata().getName(), aPod.getStatus().getPhase()); if(aPod.getStatus().getPhase().equals("Succeeded")) { log("Logs -> ", client.pods().inNamespace(namespace).withName(aPod.getMetadata().getName()).getLog()); watchLatch.countDown(); } }
public KubernetesList processLocally(Map<String, String> valuesMap) { String namespace = getItem() != null ? getItem().getMetadata().getNamespace() : getNamespace(); if (namespace == null) { namespace = getConfig().getNamespace(); String name = getItem() != null ? getItem().getMetadata().getName() : getName(); .inNamespace(namespace) .withName(name) .get(); throw KubernetesClientException.launderThrowable(e);