protected boolean isExposeService(Service service) { String expose = KubernetesHelper.getLabels(service).get("expose"); return expose != null && expose.toLowerCase().equals("true"); }
public static Template combineTemplates(Template firstTemplate, Template template) { List<HasMetadata> objects = template.getObjects(); if (objects != null) { for (HasMetadata object : objects) { addTemplateObject(firstTemplate, object); } } List<Parameter> parameters = firstTemplate.getParameters(); if (parameters == null) { parameters = new ArrayList<>(); firstTemplate.setParameters(parameters); } combineParameters(parameters, template.getParameters()); String name = KubernetesHelper.getName(template); if (StringUtils.isNotBlank(name)) { // lets merge all the fabric8 annotations using the template id qualifier as a postfix Map<String, String> annotations = KubernetesHelper.getOrCreateAnnotations(firstTemplate); Map<String, String> otherAnnotations = KubernetesHelper.getOrCreateAnnotations(template); Set<Map.Entry<String, String>> entries = otherAnnotations.entrySet(); for (Map.Entry<String, String> entry : entries) { String key = entry.getKey(); String value = entry.getValue(); if (!annotations.containsKey(key)) { annotations.put(key, value); } } } return firstTemplate; }
private Pod getNewestPod(List<Pod> items) { Pod targetPod = null; if (items != null) { for (Pod pod : items) { if (KubernetesHelper.isPodWaiting(pod) || KubernetesHelper.isPodRunning(pod)) { if (targetPod == null || (KubernetesHelper.isPodReady(pod) && KubernetesResourceUtil.isNewerResource(pod, targetPod))) { targetPod = pod; } } } } return targetPod; }
@Override public void eventReceived(Watcher.Action action, Pod pod) { podWaitLog.info(getName(pod) + " status: " + getPodStatusDescription(pod) + getPodStatusMessagePostfix(action)); if (isAddOrModified(action) && KubernetesHelper.isPodRunning(pod) && KubernetesHelper.isPodReady(pod) && podHasEnvVars(pod, envVars)) { foundPod = pod; terminateLatch.countDown(); } }
public void applyBuildConfig(BuildConfig entity, String sourceName) { OpenShiftClient openShiftClient = getOpenShiftClient(); if (openShiftClient != null) { String id = getName(entity); String namespace = KubernetesHelper.getNamespace(entity); if (StringUtils.isBlank(namespace)) { namespace = getNamespace(); log.info("Updating BuildConfig from " + sourceName); try { String resourceVersion = KubernetesHelper.getResourceVersion(old); ObjectMeta metadata = getOrCreateMetadata(entity); metadata.setNamespace(namespace); metadata.setResourceVersion(resourceVersion); log.warn("Creation disabled so not creating BuildConfig from " + sourceName + " namespace " + namespace + " name " + getName(entity)); } else { doCreateBuildConfig(entity, namespace, sourceName);
/** * Returns true if the namespace is created */ public boolean applyNamespace(Namespace entity) { String namespace = getOrCreateMetadata(entity).getName(); log.info("Using namespace: " + namespace); String name = getName(entity); Objects.requireNonNull(name, "No name for " + entity ); Namespace old = kubernetesClient.namespaces().withName(name).get(); if (!isRunning(old)) { try { Object answer = kubernetesClient.namespaces().create(entity); logGeneratedEntity("Created namespace: ", namespace, entity, answer); return true; } catch (Exception e) { onApplyError("Failed to create namespace: " + name + " due " + e.getMessage(), e); } } return false; }
public static String getSourceUrlAnnotation(HasMetadata item) { return KubernetesHelper.getOrCreateAnnotations(item).get(RESOURCE_SOURCE_URL_ANNOTATION); }
private void enrichService(Service service) { if (hasWebPort(service)) { ObjectMeta metadata = service.getMetadata(); if (metadata == null) { metadata = new ObjectMeta(); service.setMetadata(metadata); } Map<String, String> labels = KubernetesHelper.getOrCreateLabels(service); if (!labels.containsKey(EXPOSE_LABEL)) { labels.put(EXPOSE_LABEL, "true"); log.verbose("Adding Service label '%s:true' on service %s" + " so that it is exposed by the exposecontroller microservice." + " To disable use the maven argument: '-Dfabric8.profile=internal-microservice'", EXPOSE_LABEL, KubernetesHelper.getName(service)); } } }
private void onPod(Watcher.Action action, Pod pod, KubernetesClient kubernetes, String namespace, String ctrlCMessage, boolean followLog) { String name = KubernetesHelper.getName(pod); if (action.equals(Watcher.Action.DELETED)) { addedPods.remove(name); if (Objects.equals(watchingPodName, name)) { watchingPodName = null; addedPods.remove(name); } } else { if (action.equals(Watcher.Action.ADDED) || action.equals(Watcher.Action.MODIFIED)) { addedPods.put(name, pod); } } Pod watchPod = KubernetesResourceUtil.getNewestPod(addedPods.values()); newestPodName = KubernetesHelper.getName(watchPod); Logger statusLog = Objects.equals(name, newestPodName) ? context.getNewPodLog() : context.getOldPodLog(); if (!action.equals(Watcher.Action.MODIFIED) || watchingPodName == null || !watchingPodName.equals(name)) { statusLog.info("%s status: %s%s", name, getPodStatusDescription(pod), getPodStatusMessagePostfix(action)); } if (watchPod != null && KubernetesHelper.isPodRunning(watchPod)) { watchLogOfPodName(kubernetes, namespace, ctrlCMessage, followLog, watchPod, KubernetesHelper.getName(watchPod)); } }
public void applyRoute(Route entity, String sourceName) { OpenShiftClient openShiftClient = getOpenShiftClient(); if (openShiftClient != null) { String id = getName(entity); Objects.requireNonNull(id, "No name for " + entity + " " + sourceName); String namespace = KubernetesHelper.getNamespace(entity); if (StringUtils.isBlank(namespace)) { namespace = getNamespace(); } Route route = openShiftClient.routes().inNamespace(namespace).withName(id).get(); if (route == null) { try { log.info("Creating Route " + namespace + ":" + id + " " + (entity.getSpec() != null ? "host: " + entity.getSpec().getHost() : "No Spec !")); openShiftClient.routes().inNamespace(namespace).create(entity); } catch (Exception e) { onApplyError("Failed to create Route from " + sourceName + ". " + e + ". " + entity, e); } } } }
private Ingress createIngressForService(String routeDomainPostfix, String namespace, Service service) { Ingress ingress = null; String serviceName = KubernetesHelper.getName(service); ServiceSpec serviceSpec = service.getSpec(); if (serviceSpec != null && StringUtils.isNotBlank(serviceName) && shouldCreateExternalURLForService(service, serviceName)) { .withNewBackend() .withServiceName(serviceName) .withServicePort(KubernetesHelper.createIntOrString(portNumber)) .endBackend() .build();
private Map<String, ImageStream> readAlreadyExtractedImageStreams(File target) throws IOException { // If it already exists, read in the file and use it for update Map<String, ImageStream> imageStreams = new HashMap<>(); if (target.length() > 0) { for (HasMetadata entity : KubernetesResourceUtil.loadResources(target)) { if ("ImageStream".equals(KubernetesHelper.getKind(entity))) { imageStreams.put(entity.getMetadata().getName(), (ImageStream) entity); } // Ignore all other kind of entities. There shouldn't be any included anyway } } return imageStreams; }
if (openshiftClient != null) { ProjectRequest entity = new ProjectRequest(); ObjectMeta metadata = getOrCreateMetadata(entity); metadata.setName(namespaceName); String namespace = kubernetesClient.getNamespace(); if (StringUtils.isNotBlank(namespace)) { Map<String, String> entityLabels = getOrCreateLabels(entity); if (labels != null) { entityLabels.putAll(labels); ObjectMeta metadata = getOrCreateMetadata(entity); metadata.setName(namespaceName); String namespace = kubernetesClient.getNamespace(); if (StringUtils.isNotBlank(namespace)) { Map<String, String> entityLabels = getOrCreateLabels(entity); if (labels != null) { entityLabels.putAll(labels);
String answer = KubernetesHelper.getOrCreateAnnotations(srv).get(Fabric8Annotations.SERVICE_EXPOSE_URL.toString()); if (StringUtils.isNotBlank(answer)) { return answer; if (items != null) { for (Ingress item : items) { String ns = KubernetesHelper.getNamespace(item); if (Objects.equal(serviceNamespace, ns)) { IngressSpec spec = item.getSpec();
public String getNamespace() { if (StringUtils.isBlank(this.namespace)) { this.namespace = KubernetesHelper.getDefaultNamespace(); } return namespace; }
if (items != null) { for (Pod pod : items) { if (KubernetesHelper.isPodRunning(pod) || KubernetesHelper.isPodWaiting(pod)) { if (latestPod == null || KubernetesResourceUtil.isNewerResource(pod, latestPod)) { if (ignorePodsOlderThan != null) {
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; }
@Override public void eventReceived(Action action, Pod aPod) { if(KubernetesHelper.isPodReady(aPod)) { readyLatch.countDown(); } }