@Scheduled(fixedDelayString = "${spring.cloud.kubernetes.discovery.catalogServicesWatchDelay:30000}") public void catalogServicesWatch() { try { List<String> previousState = catalogEndpointsState.get(); //not all pods participate in the service discovery. only those that have endpoints. List<Endpoints> endpoints = kubernetesClient.endpoints().list().getItems(); List<String> endpointsPodNames = endpoints.stream() .map(Endpoints::getSubsets) .filter(Objects::nonNull) .flatMap(Collection::stream) .map(EndpointSubset::getAddresses) .filter(Objects::nonNull) .flatMap(Collection::stream) .map(EndpointAddress::getTargetRef) .filter(Objects::nonNull) .map(ObjectReference::getName) // pod name unique in namespace .sorted(String::compareTo).collect(Collectors.toList()); catalogEndpointsState.set(endpointsPodNames); if (!endpointsPodNames.equals(previousState)) { logger.trace("Received endpoints update from kubernetesClient: {}", endpointsPodNames); publisher.publishEvent(new HeartbeatEvent(this, endpointsPodNames)); } } catch (Exception e) { logger.error("Error watching Kubernetes Services", e); } }
@Override public List<String> getServices() { return client.services().list() .getItems() .stream().map(s -> s.getMetadata().getName()) .collect(Collectors.toList()); } }
public static void main(String[] args) { try { OpenShiftClient client = new DefaultOpenShiftClient(); if (!client.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE)) { System.out.println("WARNING this cluster does not support the API Group " + OpenShiftAPIGroups.IMAGE); return; } ImageStreamList list = client.imageStreams().list(); if (list == null) { System.out.println("ERROR no list returned!"); return; } List<ImageStream> items = list.getItems(); for (ImageStream item : items) { System.out.println("ImageStream " + item.getMetadata().getName() + " has version: " + item.getApiVersion()); } System.out.println("Found " + items.size() + " ImageStream(s)"); } catch (KubernetesClientException e) { System.out.println("Failed: " + e); e.printStackTrace(); } }
public static void main(String[] args) { try { OpenShiftClient client = new DefaultOpenShiftClient(); if (!client.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.BUILD)) { System.out.println("WARNING this cluster does not support the API Group " + OpenShiftAPIGroups.BUILD); return; } BuildConfigList list = client.buildConfigs().list(); if (list == null) { System.out.println("ERROR no list returned!"); return; } List<BuildConfig> items = list.getItems(); for (BuildConfig item : items) { System.out.println("BuildConfig " + item.getMetadata().getName() + " has version: " + item.getApiVersion()); } } catch (KubernetesClientException e) { System.out.println("Failed: " + e); e.printStackTrace(); } }
public static void main(String[] args) { String master = "http://localhost:8080/"; if (args.length == 1) { master = args[0]; } io.fabric8.kubernetes.client.Config config = new io.fabric8.kubernetes.client.ConfigBuilder().withMasterUrl(master).build(); try (final KubernetesClient client = new DefaultKubernetesClient(config)) { StorageClassList storageClassList = client.storage().storageClasses().list(); logger.info(storageClassList.toString()); } catch (KubernetesClientException e) { logger.error(e.getMessage(), e); } } }
public static void main(String[] args) throws InterruptedException { String master = "https://localhost:8443/"; if (args.length == 1) { master = args[0]; } Config config = new ConfigBuilder().withMasterUrl(master) .withTrustCerts(true) .withUsername("admin") .withPassword("admin") .withNamespace("default") .build(); try (final KubernetesClient client = new AutoAdaptableKubernetesClient(config)) { log("Received pods", client.pods().list()); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage(), e); Throwable[] suppressed = e.getSuppressed(); if (suppressed != null) { for (Throwable t : suppressed) { logger.error(t.getMessage(), t); } } } }
public static void main(String[] args) { try { OpenShiftClient client = new DefaultOpenShiftClient(); if (!client.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.APPS)) { System.out.println("WARNING this cluster does not support the API Group " + OpenShiftAPIGroups.APPS); return; } DeploymentConfigList list = client.deploymentConfigs().list(); if (list == null) { System.out.println("ERROR no list returned!"); return; } List<DeploymentConfig> items = list.getItems(); for (DeploymentConfig item : items) { System.out.println("DeploymentConfig " + item.getMetadata().getName() + " has version: " + item.getApiVersion()); } if (items.size() > 0) { // lets check .get() too DeploymentConfig deploymentConfig = items.get(0); String name = deploymentConfig.getMetadata().getName(); deploymentConfig = client.deploymentConfigs().withName(name).get(); assertNotNull("No DeploymentConfig found for name " + name, deploymentConfig); System.out.println("get() DeploymentConfig " + name + " has version: " + deploymentConfig.getApiVersion()); } } catch (KubernetesClientException e) { System.out.println("Failed: " + e); e.printStackTrace(); } }
StorageClassList storageClassList = client.storage().storageClasses().list(); logger.info("List of storage classes: {}", storageClassList.toString()); storageClassList = client.storage().storageClasses().list(); logger.info("List of storage classes: {}", storageClassList.toString()); storageClassList = client.storage().storageClasses().list(); logger.info("List of storage classes: {}", storageClassList.toString());
DeploymentConfigList list = client.deploymentConfigs().list(); assertNotNull("No DeploymentConfigList returned", list); List<DeploymentConfig> items = list.getItems();
public static void main(String[] args) throws InterruptedException { String namespace = "myproject"; String master = "CLUSTER_URL"; Config config = new ConfigBuilder().withMasterUrl(master).build(); OpenShiftClient client = new DefaultOpenShiftClient(config); try { ImageStreamTag istag = new ImageStreamTagBuilder().withNewMetadata().withName("bar1:1.0.12").endMetadata() .withNewTag().withNewFrom().withKind("DockerImage").withName("openshift/wildfly-81-centos7:latest").endFrom().endTag() .build(); log("Created istag", client.imageStreamTags().inNamespace(namespace).create(istag)); Thread.sleep(30000); }finally { log("ImageStreamTags are :"); log(client.imageStreamTags().inNamespace(namespace).withName("bar1:1.0.12").get().toString()); log("ImageStreamTags using list are :"); log(client.imageStreamTags().list().getItems().get(0).toString()); log("Deleted istag",client.imageStreamTags().withName("bar1:1.0.12").delete()); client.close(); } }
public Optional<Reader> streamLogs(String label, String integrationName) { return client.pods().list().getItems().stream() .filter(p -> integrationName.equals(p.getMetadata().getLabels().get(label))).findAny(). flatMap(p -> //Optional.of(client.pods().inNamespace(config.getNamespace()).withName(p.getMetadata().getName()).getLogReader()) Optional.of(new StringReader("REQUIRES_LIBRARY_UPDATE!!!!")) ); }
@Override public List<String> getServices() { return client.services().list() .getItems() .stream().map(s -> s.getMetadata().getName()) .collect(Collectors.toList()); } }
public Collection<String> getIntegrationPods() { return client.pods().list().getItems().stream() .filter(pod -> pod.getMetadata().getLabels().containsKey("integration")) .map(pod -> pod.getMetadata().getLabels().get("integration")) .collect(Collectors.toList()); }
protected void doList(Exchange exchange, String operation) throws Exception { PersistentVolumeClaimList persistentVolumeClaimList = getEndpoint() .getKubernetesClient().persistentVolumeClaims().list(); MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); exchange.getOut().setBody(persistentVolumeClaimList.getItems()); }
protected void doList(Exchange exchange, String operation) throws Exception { HorizontalPodAutoscalerList hpaList = getEndpoint().getKubernetesClient().autoscaling().horizontalPodAutoscalers().list(); MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); exchange.getOut().setBody(hpaList.getItems()); }
protected void doList(Exchange exchange, String operation) throws Exception { JobList jobList = getEndpoint().getKubernetesClient().batch().jobs().list(); MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); exchange.getOut().setBody(jobList.getItems()); }
protected void doList(Exchange exchange, String operation) throws Exception { DeploymentList deploymentsList = getEndpoint().getKubernetesClient().apps().deployments().list(); MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); exchange.getOut().setBody(deploymentsList.getItems()); }
/** * Returns all the clients for the first working pod for the given service */ public List<J4pClient> clientsForService(String serviceName) { List<Pod> pods = KubernetesHelper.getPodsForService(requireService(serviceName), kubernetes.pods().list().getItems()); return clientsForPod(pods); }
/** * Returns a client for the first working pod for the given service */ public J4pClient clientForService(String serviceName) { List<Pod> pods = KubernetesHelper.getPodsForService(requireService(serviceName), kubernetes.pods().list().getItems()); return clientForPod(pods); }
public static void assumeKubernetes() throws Exception { try (DefaultKubernetesClient client = new DefaultKubernetesClient( new ConfigBuilder(Config.autoConfigure(null)).build())) { client.pods().list(); } catch (Exception e) { assumeNoException(e); } }