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

The following examples show how to use io.fabric8.kubernetes.api.model.PodTemplateSpec#setSpec() . 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: TillerInstaller.java    From microbean-helm with Apache License 2.0 5 votes vote down vote up
protected DeploymentSpec createDeploymentSpec(final int replicas,
                                              final Map<String, String> labels,
                                              final Map<String, String> nodeSelector,
                                              String serviceAccountName,
                                              final String imageName,
                                              final ImagePullPolicy imagePullPolicy,
                                              final int maxHistory,
                                              final String namespace,
                                              final boolean hostNetwork,
                                              final boolean tls,
                                              final boolean verifyTls) {    
  final DeploymentSpec deploymentSpec = new DeploymentSpec();
  deploymentSpec.setReplicas(Math.max(1, replicas));
  final PodTemplateSpec podTemplateSpec = new PodTemplateSpec();
  final ObjectMeta metadata = new ObjectMeta();
  metadata.setLabels(normalizeLabels(labels));
  podTemplateSpec.setMetadata(metadata);
  final PodSpec podSpec = new PodSpec();
  serviceAccountName = normalizeServiceAccountName(serviceAccountName);    
  podSpec.setServiceAccountName(serviceAccountName);
  podSpec.setContainers(Arrays.asList(this.createContainer(imageName, imagePullPolicy, maxHistory, namespace, tls, verifyTls)));
  podSpec.setHostNetwork(Boolean.valueOf(hostNetwork));
  if (nodeSelector != null && !nodeSelector.isEmpty()) {
    podSpec.setNodeSelector(nodeSelector);
  }
  if (tls) {
    final Volume volume = new Volume();
    volume.setName(DEFAULT_NAME + "-certs");
    final SecretVolumeSource secretVolumeSource = new SecretVolumeSource();
    secretVolumeSource.setSecretName(SECRET_NAME);
    volume.setSecret(secretVolumeSource);
    podSpec.setVolumes(Arrays.asList(volume));
  }
  podTemplateSpec.setSpec(podSpec);
  deploymentSpec.setTemplate(podTemplateSpec);
  final LabelSelector selector = new LabelSelector();
  selector.setMatchLabels(labels);
  deploymentSpec.setSelector(selector);
  return deploymentSpec;
}
 
Example 2
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;
}
 
Example 3
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"));
}