Java Code Examples for io.fabric8.kubernetes.api.model.PodTemplateSpec#getSpec()

The following examples show how to use io.fabric8.kubernetes.api.model.PodTemplateSpec#getSpec() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DockerImageWatcher.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private boolean updateImageName(HasMetadata entity, PodTemplateSpec template, String imagePrefix, String imageName) {
    boolean answer = false;
    PodSpec spec = template.getSpec();
    if (spec != null) {
        List<Container> containers = spec.getContainers();
        if (containers != null) {
            for (Container container : containers) {
                String image = container.getImage();
                if (image != null && image.startsWith(imagePrefix)) {
                    container.setImage(imageName);
                    log.info("Updating " + KubernetesHelper.getKind(entity) + " " + KubernetesHelper.getName(entity) + " to use image: " + imageName);
                    answer = true;
                }
            }
        }
    }
    return answer;
}
 
Example 2
Source File: KubernetesEnvironment.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
public PodData(Deployment deployment) {
  PodTemplateSpec podTemplate = deployment.getSpec().getTemplate();

  // it is not required for PodTemplate to have name specified
  // but many of Che Server components rely that PodData has name
  // so, provision name from deployment if it is missing
  ObjectMeta podTemplateMeta = podTemplate.getMetadata();
  if (podTemplateMeta == null) {
    podTemplate.setMetadata(
        new ObjectMetaBuilder().withName(deployment.getMetadata().getName()).build());
  } else {
    if (podTemplateMeta.getName() == null) {
      podTemplateMeta.setName(deployment.getMetadata().getName());
    }
  }

  this.podSpec = podTemplate.getSpec();
  this.podMeta = podTemplate.getMetadata();
  this.role = PodRole.DEPLOYMENT;
}
 
Example 3
Source File: DebugEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private boolean enableDebugging(HasMetadata entity, PodTemplateSpec template) {
    if (template != null) {
        PodSpec podSpec = template.getSpec();
        if (podSpec != null) {
            List<Container> containers = podSpec.getContainers();
            if (!containers.isEmpty()) {
                Container container = containers.get(0);
                List<EnvVar> env = container.getEnv();
                if (env == null) {
                    env = new ArrayList<>();
                }
                String remoteDebugPort =
                    KubernetesHelper.getEnvVar(env, ENV_VAR_JAVA_DEBUG_PORT, ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
                boolean enabled = false;
                if (KubernetesHelper.setEnvVar(env, ENV_VAR_JAVA_DEBUG, "true")) {
                    container.setEnv(env);
                    enabled = true;
                }
                List<ContainerPort> ports = container.getPorts();
                if (ports == null) {
                    ports = new ArrayList<>();
                }
                if (KubernetesResourceUtil.addPort(ports, remoteDebugPort, "debug", log)) {
                    container.setPorts(ports);
                    enabled = true;
                }
                if (enabled) {
                    log.info("Enabling debug on " + KubernetesHelper.getKind(entity) + " " + KubernetesHelper.getName(
                        entity) + " due to the property: " + ENABLE_DEBUG_MAVEN_PROPERTY);
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 4
Source File: DeploymentConfigEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private DeploymentConfigSpec getDeploymentConfigSpec(Integer replicas, Integer revisionHistoryLimit, LabelSelector selector, PodTemplateSpec podTemplateSpec, String strategyType) {
    DeploymentConfigSpecBuilder specBuilder = new DeploymentConfigSpecBuilder();
    if (replicas != null) {
        specBuilder.withReplicas(replicas);
    }
    if (revisionHistoryLimit != null) {
        specBuilder.withRevisionHistoryLimit(revisionHistoryLimit);
    }

    if (selector  != null) {
        Map<String, String> matchLabels = selector.getMatchLabels();
        if (matchLabels != null && !matchLabels.isEmpty()) {
            specBuilder.withSelector(matchLabels);
        }
    }
    if (podTemplateSpec != null) {
        specBuilder.withTemplate(podTemplateSpec);
        PodSpec podSpec = podTemplateSpec.getSpec();
        Objects.requireNonNull(podSpec, "No PodSpec for PodTemplate:" + podTemplateSpec);
        Objects.requireNonNull(podSpec, "No containers for PodTemplate.spec: " + podTemplateSpec);
    }
    DeploymentStrategy deploymentStrategy = getDeploymentStrategy(strategyType);
    if (deploymentStrategy != null) {
        specBuilder.withStrategy(deploymentStrategy);
    }

    if(enableAutomaticTrigger.equals(Boolean.TRUE)) {
        specBuilder.addNewTrigger().withType("ConfigChange").endTrigger();
    }

    return specBuilder.build();
}
 
Example 5
Source File: DebugMojo.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
private boolean enableDebugging(HasMetadata entity, PodTemplateSpec template) {
    if (template != null) {
        PodSpec podSpec = template.getSpec();
        if (podSpec != null) {
            List<Container> containers = podSpec.getContainers();
            boolean enabled = false;
            for (int i = 0; i < containers.size(); i++) {
                Container container = containers.get(i);
                List<EnvVar> env = container.getEnv();
                if (env == null) {
                    env = new ArrayList<>();
                }
                remoteDebugPort = KubernetesHelper.getEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_PORT, DebugConstants.ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
                if (KubernetesHelper.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG, "true")) {
                    container.setEnv(env);
                    enabled = true;
                }
                if (KubernetesHelper.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SUSPEND, String.valueOf(debugSuspend))) {
                    container.setEnv(env);
                    enabled = true;
                }
                List<ContainerPort> ports = container.getPorts();
                if (ports == null) {
                    ports = new ArrayList<>();
                }
                if (KubernetesResourceUtil.addPort(ports, remoteDebugPort, "debug", log)) {
                    container.setPorts(ports);
                    enabled = true;
                }
                if (debugSuspend) {
                    // Setting a random session value to force pod restart
                    this.debugSuspendValue = String.valueOf(new Random().nextLong());
                    KubernetesHelper.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION, this.debugSuspendValue);
                    container.setEnv(env);
                    if (container.getReadinessProbe() != null) {
                        log.info("Readiness probe will be disabled on " + KubernetesHelper.getKind(entity) + " " + getName(entity) + " to allow attaching a remote debugger during suspension");
                        container.setReadinessProbe(null);
                    }
                    enabled = true;
                } else {
                    if (KubernetesHelper.removeEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION)) {
                        container.setEnv(env);
                        enabled = true;
                    }
                }
            }
            if (enabled) {
                log.info("Enabling debug on " + KubernetesHelper.getKind(entity) + " " + getName(entity));
                return true;
            }
        }
    }
    return false;
}
 
Example 6
Source File: KubernetesResourceUtil.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
protected static HasMetadata mergeDeployments(Deployment resource1, Deployment resource2, KitLogger log, boolean switchOnLocalCustomisation) {
    Deployment resource1OrCopy = resource1;
    if (!switchOnLocalCustomisation) {
        // lets copy the original to avoid modifying it
        resource1OrCopy = new DeploymentBuilder(resource1OrCopy).build();
    }
    HasMetadata answer = resource1OrCopy;
    DeploymentSpec spec1 = resource1OrCopy.getSpec();
    DeploymentSpec spec2 = resource2.getSpec();
    if (spec1 == null) {
        resource1OrCopy.setSpec(spec2);
    } else {
        PodTemplateSpec template1 = spec1.getTemplate();
        PodTemplateSpec template2 = null;
        if (spec2 != null) {
            template2 = spec2.getTemplate();
        }
        if (template1 != null && template2 != null) {
            mergeMetadata(template1, template2);
        }
        if (template1 == null) {
            spec1.setTemplate(template2);
        } else {
            PodSpec podSpec1 = template1.getSpec();
            PodSpec podSpec2 = null;
            if (template2 != null) {
                podSpec2 = template2.getSpec();
            }
            if (podSpec1 == null) {
                template1.setSpec(podSpec2);
            } else {
                String defaultName = null;
                PodTemplateSpec updateTemplate = template1;
                if (switchOnLocalCustomisation) {
                    HasMetadata override = resource2;
                    if (isLocalCustomisation(podSpec1)) {
                        updateTemplate = template2;
                        PodSpec tmp = podSpec1;
                        podSpec1 = podSpec2;
                        podSpec2 = tmp;
                    } else {
                        answer = resource2;
                        override = resource1OrCopy;
                    }
                    mergeMetadata(answer, override);
                } else {
                    mergeMetadata(resource1OrCopy, resource2);
                }
                if (updateTemplate != null) {
                    if (podSpec2 == null) {
                        updateTemplate.setSpec(podSpec1);
                    } else {
                        PodSpecBuilder podSpecBuilder = new PodSpecBuilder(podSpec1);
                        mergePodSpec(podSpecBuilder, podSpec2, defaultName);
                        updateTemplate.setSpec(podSpecBuilder.build());
                    }
                }
                return answer;
            }
        }
    }
    log.info("Merging 2 resources for " + KubernetesHelper.getKind(resource1OrCopy) + " " + KubernetesHelper.getName(resource1OrCopy) + " from " + getSourceUrlAnnotation(resource1OrCopy) + " and " + getSourceUrlAnnotation(resource2) + " and removing " + getSourceUrlAnnotation(resource1OrCopy));
    return resource1OrCopy;
}