Java Code Examples for io.fabric8.kubernetes.api.model.PodSpec#setRestartPolicy()

The following examples show how to use io.fabric8.kubernetes.api.model.PodSpec#setRestartPolicy() . 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: RestartPolicyRewriter.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private void rewriteRestartPolicy(PodSpec podSpec, String podName, KubernetesEnvironment env) {
  final String restartPolicy = podSpec.getRestartPolicy();

  if (restartPolicy != null && !DEFAULT_RESTART_POLICY.equalsIgnoreCase(restartPolicy)) {
    final String warnMsg =
        format(
            Warnings.RESTART_POLICY_SET_TO_NEVER_WARNING_MESSAGE_FMT,
            restartPolicy,
            podName,
            DEFAULT_RESTART_POLICY);
    env.addWarning(new WarningImpl(Warnings.RESTART_POLICY_SET_TO_NEVER_WARNING_CODE, warnMsg));
  }
  podSpec.setRestartPolicy(DEFAULT_RESTART_POLICY);
}
 
Example 2
Source File: KubernetesDeployments.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Starts the specified Pod via a Deployment.
 *
 * @param pod pod to deploy
 * @return created pod
 * @throws InfrastructureException when any exception occurs
 */
public Pod deploy(Pod pod) throws InfrastructureException {
  putLabel(pod, CHE_WORKSPACE_ID_LABEL, workspaceId);
  // Since we use the pod's metadata as the deployment's metadata
  // This is used to identify the pod in CreateWatcher.
  String originalName = pod.getMetadata().getName();
  putLabel(pod, CHE_DEPLOYMENT_NAME_LABEL, originalName);

  ObjectMeta metadata = pod.getMetadata();
  PodSpec podSpec = pod.getSpec();
  podSpec.setRestartPolicy("Always"); // Only allowable value

  Deployment deployment =
      new DeploymentBuilder()
          .withMetadata(metadata)
          .withNewSpec()
          .withNewSelector()
          .withMatchLabels(metadata.getLabels())
          .endSelector()
          .withReplicas(1)
          .withNewTemplate()
          .withMetadata(metadata)
          .withSpec(podSpec)
          .endTemplate()
          .endSpec()
          .build();
  return createDeployment(deployment, workspaceId);
}
 
Example 3
Source File: KubernetesDeployments.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
public Pod deploy(Deployment deployment) throws InfrastructureException {
  ObjectMeta podMeta = deployment.getSpec().getTemplate().getMetadata();
  putLabel(podMeta, CHE_WORKSPACE_ID_LABEL, workspaceId);
  putLabel(podMeta, CHE_DEPLOYMENT_NAME_LABEL, deployment.getMetadata().getName());
  putLabel(deployment.getMetadata(), CHE_WORKSPACE_ID_LABEL, workspaceId);
  // Match condition for a deployment is an AND of all labels
  setSelector(deployment, podMeta.getLabels());
  // Avoid accidental setting of multiple replicas
  deployment.getSpec().setReplicas(1);

  PodSpec podSpec = deployment.getSpec().getTemplate().getSpec();
  podSpec.setRestartPolicy("Always"); // Only allowable value

  return createDeployment(deployment, workspaceId);
}
 
Example 4
Source File: KubernetesTaskLauncher.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
private void launch(String appId, AppDeploymentRequest request) {
	Map<String, String> idMap = createIdMap(appId, request);
	Map<String, String> podLabelMap = new HashMap<>();
	podLabelMap.put("task-name", request.getDefinition().getName());
	podLabelMap.put(SPRING_MARKER_KEY, SPRING_MARKER_VALUE);

	Map<String, String> deploymentProperties = request.getDeploymentProperties();
	Map<String, String> deploymentLabels = this.deploymentPropertiesResolver.getDeploymentLabels(deploymentProperties);
	if (!CollectionUtils.isEmpty(deploymentLabels)) {
		logger.debug(String.format("Adding deploymentLabels: %s", deploymentLabels));
	}
	PodSpec podSpec = createPodSpec(request);

	podSpec.setRestartPolicy(getRestartPolicy(request).name());
	if (this.properties.isCreateJob()) {
		logger.debug(String.format("Launching Job for task: %s", appId));
		ObjectMeta objectMeta = new ObjectMetaBuilder()
				.withLabels(podLabelMap)
				.addToLabels(idMap)
				.addToLabels(deploymentLabels)
				.withAnnotations(this.deploymentPropertiesResolver.getJobAnnotations(deploymentProperties))
				.build();
		PodTemplateSpec podTemplateSpec = new PodTemplateSpec(objectMeta, podSpec);

		JobSpec jobSpec = new JobSpecBuilder()
				.withTemplate(podTemplateSpec)
				.withBackoffLimit(getBackoffLimit(request))
				.build();

		this.client.batch().jobs()
				.createNew()
				.withNewMetadata()
				.withName(appId)
				.withLabels(Collections.singletonMap("task-name", podLabelMap.get("task-name")))
				.addToLabels(idMap)
				.withAnnotations(this.deploymentPropertiesResolver.getJobAnnotations(deploymentProperties))
				.endMetadata()
				.withSpec(jobSpec)
				.done();
	}
	else {
		logger.debug(String.format("Launching Pod for task: %s", appId));
		this.client.pods()
				.createNew()
				.withNewMetadata()
				.withName(appId)
				.withLabels(podLabelMap)
				.addToLabels(deploymentLabels)
				.withAnnotations(this.deploymentPropertiesResolver.getJobAnnotations(deploymentProperties))
				.addToLabels(idMap)
				.endMetadata()
				.withSpec(podSpec)
				.done();
	}
}
 
Example 5
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void listScheduleWithExternalCronJobs() {
	CronJobList cronJobList = new CronJobList();
	CronJobSpec cronJobSpec = new CronJobSpec();
	JobTemplateSpec jobTemplateSpec = new JobTemplateSpec();
	JobSpec jobSpec = new JobSpec();
	PodTemplateSpec podTemplateSpec = new PodTemplateSpec();
	PodSpec podSpec = new PodSpec();
	Container container = new Container();
	container.setName("test");
	container.setImage("busybox");
	podSpec.setContainers(Arrays.asList(container));
	podSpec.setRestartPolicy("OnFailure");
	podTemplateSpec.setSpec(podSpec);
	jobSpec.setTemplate(podTemplateSpec);
	jobTemplateSpec.setSpec(jobSpec);
	cronJobSpec.setJobTemplate(jobTemplateSpec);
	cronJobSpec.setSchedule("0/10 * * * *");

	CronJob cronJob1 = new CronJob();
	ObjectMeta objectMeta1 = new ObjectMeta();
	Map<String, String> labels = new HashMap<>();
	labels.put("spring-cronjob-id", "test");
	objectMeta1.setLabels(labels);
	objectMeta1.setName("job1");
	cronJob1.setMetadata(objectMeta1);
	cronJob1.setSpec(cronJobSpec);
	ObjectMeta objectMeta2 = new ObjectMeta();
	objectMeta2.setName("job2");
	CronJob cronJob2 = new CronJob();
	cronJob2.setSpec(cronJobSpec);
	cronJob2.setMetadata(objectMeta2);
	ObjectMeta objectMeta3 = new ObjectMeta();
	objectMeta3.setName("job3");
	CronJob cronJob3 = new CronJob();
	cronJob3.setSpec(cronJobSpec);
	cronJob3.setMetadata(objectMeta3);
	cronJobList.setItems(Arrays.asList(cronJob1, cronJob2, cronJob3));
	this.kubernetesClient.batch().cronjobs().create(cronJob1);
	this.kubernetesClient.batch().cronjobs().create(cronJob2);
	this.kubernetesClient.batch().cronjobs().create(cronJob3);
	List<ScheduleInfo> scheduleInfos = this.scheduler.list();
	assertThat(scheduleInfos.size() == 1);
	assertThat(scheduleInfos.get(0).getScheduleName().equals("job1"));
}