private static Map<String, String> getData(KubernetesClient client, String name, String namespace, String[] profiles) { try { ConfigMap map = StringUtils.isEmpty(namespace) ? client.configMaps().withName(name).get() : client.configMaps().inNamespace(namespace).withName(name).get(); if (map != null) { return processAllEntries(map.getData(), profiles); } } catch (Exception e) { LOG.warn("Can't read configMap with name: [" + name + "] in namespace:[" + namespace + "]. Ignoring"); } return new HashMap<>(); }
private ConfigMap getConfigMap() { return kubernetesClient.configMaps() .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace())) .withName(leaderProperties.getConfigMapName()) .get(); }
"[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<>();
@Override public List<ServiceInstance> getInstances(String serviceId) { Assert.notNull(serviceId, "[Assertion failed] - the object argument must be null"); return Optional.ofNullable(client.endpoints().withName(serviceId).get()).orElse(new Endpoints()) .getSubsets() .stream() .flatMap(s -> s.getAddresses().stream().map(a -> (ServiceInstance) new KubernetesServiceInstance(serviceId, a ,s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false))) .collect(Collectors.toList()); }
private static List<ServiceInstance> getInstances(KubernetesClient client, String name, String namespace) { Assert.notNull(name, "[Assertion failed] - the service name must not be null"); return Optional.ofNullable(client.endpoints().inNamespace(namespace).withName(name).get()) .orElse(new Endpoints()) .getSubsets() .stream() .flatMap(s -> s.getAddresses().stream().map(a -> (ServiceInstance) new KubernetesServiceInstance(name, a ,s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false))) .collect(Collectors.toList()); }
@Override public User currentUser() { return users().withName("~").get(); }
private static Map<String, String> getData(KubernetesClient client, String name, String namespace) { Map<String, String> result = new HashMap<>(); try { ConfigMap map = namespace == null || namespace.isEmpty() ? client.configMaps().withName(name).get() : client.configMaps().inNamespace(namespace).withName(name).get(); if (map != null) { for (Map.Entry<String, String> entry : map.getData().entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.equals(APPLICATION_YAML) || key.equals(APPLICATION_YML)) { result.putAll(YAML_TO_PROPETIES.andThen(PROPERTIES_TO_MAP).apply(value)); } else if (key.equals(APPLICATION_PROPERTIES)) { result.putAll(KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP).apply(value)); } else { result.put(key, value); } } } } catch (Exception e) { LOGGER.warn("Can't read configMap with name: [" + name + "] in namespace:[" + namespace + "]. Ignoring"); } return result; }
@Override public ServiceInstance getLocalServiceInstance() { String serviceName = properties.getServiceName(); String podName = System.getenv(HOSTNAME); ServiceInstance defaultInstance = new DefaultServiceInstance(serviceName, "localhost", 8080, false); Endpoints endpoints = client.endpoints().withName(serviceName).get(); if (Utils.isNullOrEmpty(podName) || endpoints == null) { return defaultInstance; } try { return endpoints.getSubsets() .stream() .filter(s -> s.getAddresses().get(0).getTargetRef().getName().equals(podName)) .map(s -> (ServiceInstance) new KubernetesServiceInstance(serviceName, s.getAddresses().stream().findFirst().orElseThrow(IllegalStateException::new), s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false)) .findFirst().orElse(defaultInstance); } catch (Throwable t) { return defaultInstance; } }
public void start() { ConfigMap map = Strings.isNullOrEmpty(namespace) ? client.configMaps().withName(name).get() : client.configMaps().inNamespace(namespace).withName(name).get(); if (map != null) { currentData.set(asObjectMap(map.getData())); } watch = Strings.isNullOrEmpty(namespace) ? client.configMaps().withName(name).watch(watcher) : client.configMaps().inNamespace(namespace).withName(namespace).watch(watcher); started.set(true); }
@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; }
PodSecurityPolicy podSecurityPolicy = client.extensions().podSecurityPolicies().load(new FileInputStream(sample)).get(); client.extensions().podSecurityPolicies().create(podSecurityPolicy); logger.log(Level.INFO, "PodSecurityPolicy created with Name : "
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(); } }
private static void maybeLogS2iOnKubeWarning(Vertx vertx, KubernetesClient client) { try { // Check the KafkaConnectS2I isn't installed and whinge if it is CustomResourceDefinition crd = client.customResourceDefinitions().withName(KafkaConnectS2I.CRD_NAME).get(); if (crd != null) { log.warn("The KafkaConnectS2I custom resource definition can only be installed on OpenShift, because plain Kubernetes doesn't support S2I. " + "Execute 'kubectl delete crd " + KafkaConnectS2I.CRD_NAME + "' to remove this warning."); } } catch (KubernetesClientException e) { } }
/** * Synchronously gets the resource with the given {@code name} in the given {@code namespace}. * @param namespace The namespace. * @param name The name. * @return The resource, or null if it doesn't exist. */ public T get(String namespace, String name) { return operation().inNamespace(namespace).withName(name).get(); }
log("Get namespace by name", client.namespaces().withName("thisisatest").get());