io.fabric8.kubernetes.api.model.batch.CronJob Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.batch.CronJob. 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: KubernetesScheduler.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Override
public List<ScheduleInfo> list() {
	CronJobList cronJobList = this.client.batch().cronjobs().list();

	List<CronJob> cronJobs = cronJobList.getItems();
	List<ScheduleInfo> scheduleInfos = new ArrayList<>();

	for (CronJob cronJob : cronJobs) {
		if (cronJob.getMetadata() != null && cronJob.getMetadata().getLabels() != null &&
				StringUtils.hasText(cronJob.getMetadata().getLabels().get(SPRING_CRONJOB_ID_KEY))) {
			Map<String, String> properties = new HashMap<>();
			properties.put(SchedulerPropertyKeys.CRON_EXPRESSION, cronJob.getSpec().getSchedule());

			ScheduleInfo scheduleInfo = new ScheduleInfo();
			scheduleInfo.setScheduleName(cronJob.getMetadata().getName());
			scheduleInfo.setTaskDefinitionName(cronJob.getMetadata().getLabels().get(SPRING_CRONJOB_ID_KEY));
			scheduleInfo.setScheduleProperties(properties);

			scheduleInfos.add(scheduleInfo);
		}
	}

	return scheduleInfos;
}
 
Example #2
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testTaskServiceAccountNameDefault() {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(),
			getDeploymentProperties(), getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	String serviceAccountName = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec()
			.getServiceAccountName();
	assertEquals("Unexpected service account name", KubernetesSchedulerProperties.DEFAULT_TASK_SERVICE_ACCOUNT_NAME,
			serviceAccountName);

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #3
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
private void testEnvironmentVariables(KubernetesSchedulerProperties kubernetesSchedulerProperties,
		Map<String, String> schedulerProperties, EnvVar[] expectedVars) {
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, schedulerProperties,
			null, getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);

	assertTrue("Environment variables should not be empty", !container.getEnv().isEmpty());

	assertThat(container.getEnv()).contains(expectedVars);

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #4
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testImagePullSecretDefault() {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(),
			getDeploymentProperties(), getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	List<LocalObjectReference> secrets = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec()
			.getImagePullSecrets();
	assertTrue("There should be no secrets", secrets.isEmpty());

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #5
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testImagePullPolicyDefault() {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(),
			getDeploymentProperties(), getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);

	assertEquals("Unexpected default image pull policy", ImagePullPolicy.IfNotPresent,
			ImagePullPolicy.relaxedValueOf(container.getImagePullPolicy()));

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #6
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testEntryPointStyleDefault() throws Exception {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(),
			getDeploymentProperties(), getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);

	assertTrue("Environment variables should only have SPRING_CLOUD_APPLICATION_GUID", container.getEnv().size() == 1);
	assertTrue("Command line arguments should not be empty", !container.getArgs().isEmpty());

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #7
Source File: KubernetesScheduler.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
protected CronJob createCronJob(ScheduleRequest scheduleRequest) {
	Map<String, String> labels = Collections.singletonMap(SPRING_CRONJOB_ID_KEY,
			scheduleRequest.getDefinition().getName());

	Map<String, String> schedulerProperties = scheduleRequest.getSchedulerProperties();
	String schedule = schedulerProperties.get(SchedulerPropertyKeys.CRON_EXPRESSION);
	Assert.hasText(schedule, "The property: " + SchedulerPropertyKeys.CRON_EXPRESSION + " must be defined");

	PodSpec podSpec = createPodSpec(scheduleRequest);
	String taskServiceAccountName = this.deploymentPropertiesResolver.getTaskServiceAccountName(scheduleRequest.getSchedulerProperties());
	if (StringUtils.hasText(taskServiceAccountName)) {
		podSpec.setServiceAccountName(taskServiceAccountName);
	}

	CronJob cronJob = new CronJobBuilder().withNewMetadata().withName(scheduleRequest.getScheduleName())
			.withLabels(labels).endMetadata().withNewSpec().withSchedule(schedule).withNewJobTemplate()
			.withNewSpec().withNewTemplate().withSpec(podSpec).endTemplate().endSpec()
			.endJobTemplate().endSpec().build();

	setImagePullSecret(scheduleRequest, cronJob);

	return this.client.batch().cronjobs().create(cronJob);
}
 
Example #8
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithBootEntryPoint() throws IOException {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	kubernetesSchedulerProperties.setEntryPointStyle(EntryPointStyle.boot);
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(), null,
			getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);

	assertNotNull("Command line arguments should not be null", container.getArgs());
	assertEquals("Invalid number of command line arguments", 2, container.getArgs().size());

	assertNotNull("Environment variables should not be null", container.getEnv());
	assertTrue("Invalid number of environment variables", container.getEnv().size() > 1);

	String springApplicationJson = container.getEnv().get(0).getValue();

	Map<String, String> springApplicationJsonValues = new ObjectMapper().readValue(springApplicationJson,
			new TypeReference<HashMap<String, String>>() {
			});

	assertNotNull("SPRING_APPLICATION_JSON should not be null", springApplicationJsonValues);
	assertEquals("Invalid number of SPRING_APPLICATION_JSON entries", 2, springApplicationJsonValues.size());

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #9
Source File: CronJobOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<CronJob, DoneableCronJob> load(InputStream is) {
  try {
    CronJob item = unmarshal(is, CronJob.class);
    return new CronJobOperationsImpl(context.withItem(item));
  } catch (Throwable t) {
    throw KubernetesClientException.launderThrowable(t);
  }
}
 
Example #10
Source File: CronJobOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public CronJobOperationsImpl(OperationContext context) {
  super(context.withApiGroupName("batch")
    .withApiGroupVersion("v1beta1")
    .withCascading(true)
    .withPlural("cronjobs"));
  this.type = CronJob.class;
  this.listType = CronJobList.class;
  this.doneableType = DoneableCronJob.class;
}
 
Example #11
Source File: ContainerSearch.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private Stream<Container> findContainers(HasMetadata o) {
  // hopefully, this covers all types of objects that can contain a container
  if (o instanceof Pod) {
    return ((Pod) o).getSpec().getContainers().stream();
  } else if (o instanceof PodTemplate) {
    return ((PodTemplate) o).getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof DaemonSet) {
    return ((DaemonSet) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof Deployment) {
    return ((Deployment) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof Job) {
    return ((Job) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof ReplicaSet) {
    return ((ReplicaSet) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof ReplicationController) {
    return ((ReplicationController) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof StatefulSet) {
    return ((StatefulSet) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof CronJob) {
    return ((CronJob) o)
        .getSpec()
        .getJobTemplate()
        .getSpec()
        .getTemplate()
        .getSpec()
        .getContainers()
        .stream();
  } else if (o instanceof DeploymentConfig) {
    return ((DeploymentConfig) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof Template) {
    return ((Template) o).getObjects().stream().flatMap(this::findContainers);
  } else {
    return Stream.empty();
  }
}
 
Example #12
Source File: JobHandler.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
private CronJob getCronJob(JobModel jobModel) {
    return new CronJobBuilder()
            .withNewMetadata()
            .withName(jobModel.getName())
            .endMetadata()
            .withNewSpec()
            .withNewJobTemplate()
            .withNewSpec()
            .withActiveDeadlineSeconds((long) jobModel.getActiveDeadlineSeconds())
            .endSpec()
            .endJobTemplate()
            .withSchedule(jobModel.getSchedule())
            .endSpec()
            .build();
}
 
Example #13
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskServiceAccountNameOverride() {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	String taskServiceAccountName = "mysa";
	String prefix = KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX;

	Map<String, String> schedulerProperties = new HashMap<>(getSchedulerProperties());
	schedulerProperties.put(prefix + ".taskServiceAccountName", taskServiceAccountName);

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, schedulerProperties,
			null, getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	String serviceAccountName = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec()
			.getServiceAccountName();
	assertEquals("Unexpected service account name", taskServiceAccountName, serviceAccountName);

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #14
Source File: KubernetesScheduler.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
private void setImagePullSecret(ScheduleRequest scheduleRequest, CronJob cronJob) {

		String imagePullSecret = this.deploymentPropertiesResolver.getImagePullSecret(scheduleRequest.getSchedulerProperties());

		if (StringUtils.hasText(imagePullSecret)) {
			LocalObjectReference localObjectReference = new LocalObjectReference();
			localObjectReference.setName(imagePullSecret);

			cronJob.getSpec().getJobTemplate().getSpec().getTemplate().getSpec().getImagePullSecrets()
					.add(localObjectReference);
		}
	}
 
Example #15
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testImagePullSecret() {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	String secretName = "mysecret";
	String prefix = KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX;

	Map<String, String> schedulerProperties = new HashMap<>(getSchedulerProperties());
	schedulerProperties.put(prefix + ".imagePullSecret", secretName);

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, schedulerProperties,
			null, getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	List<LocalObjectReference> secrets = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec()
			.getImagePullSecrets();
	assertEquals("Unexpected image pull secret", secretName, secrets.get(0).getName());

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #16
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testImagePullPolicyOverride() {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	String prefix = KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX;

	Map<String, String> schedulerProperties = new HashMap<>(getSchedulerProperties());
	schedulerProperties.put(prefix + ".imagePullPolicy", "Always");

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, schedulerProperties,
			null, getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);

	assertEquals("Unexpected image pull policy", "Always", container.getImagePullPolicy());

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #17
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithExecEntryPoint() {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	kubernetesSchedulerProperties.setEntryPointStyle(EntryPointStyle.exec);

	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(), null,
			getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);

	assertNotNull("Command line arguments should not be null", container.getArgs());
	assertNotNull("Environment variables should not be null", container.getEnv());
	assertTrue("Environment variables should only have SPRING_CLOUD_APPLICATION_GUID", container.getEnv().size() == 1);

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #18
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testEntryPointStyleOverride() throws Exception {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	String prefix = KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX;

	Map<String, String> schedulerProperties = new HashMap<>(getSchedulerProperties());
	schedulerProperties.put(prefix + ".entryPointStyle", "boot");

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, schedulerProperties,
			null, getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);

	assertTrue("Environment variables should not be empty", !container.getEnv().isEmpty());
	assertTrue("Invalid number of environment variables", container.getEnv().size() > 1);

	String springApplicationJson = container.getEnv().get(0).getValue();

	Map<String, String> springApplicationJsonValues = new ObjectMapper().readValue(springApplicationJson,
			new TypeReference<HashMap<String, String>>() {
			});

	assertNotNull("SPRING_APPLICATION_JSON should not be null", springApplicationJsonValues);
	assertEquals("Invalid number of SPRING_APPLICATION_JSON entries", 2, springApplicationJsonValues.size());

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #19
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithShellEntryPoint() {
	KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
	if (kubernetesSchedulerProperties.getNamespace() == null) {
		kubernetesSchedulerProperties.setNamespace("default");
	}
	kubernetesSchedulerProperties.setEntryPointStyle(EntryPointStyle.shell);

	KubernetesClient kubernetesClient = new DefaultKubernetesClient()
			.inNamespace(kubernetesSchedulerProperties.getNamespace());

	KubernetesScheduler kubernetesScheduler = new KubernetesScheduler(kubernetesClient,
			kubernetesSchedulerProperties);

	AppDefinition appDefinition = new AppDefinition(randomName(), getAppProperties());
	ScheduleRequest scheduleRequest = new ScheduleRequest(appDefinition, getSchedulerProperties(), null,
			getCommandLineArgs(), randomName(), testApplication());

	CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);
	CronJobSpec cronJobSpec = cronJob.getSpec();

	Container container = cronJobSpec.getJobTemplate().getSpec().getTemplate().getSpec().getContainers().get(0);

	assertNotNull("Command line arguments should not be null", container.getArgs());
	assertEquals("Invalid number of command line arguments", 0, container.getArgs().size());

	assertNotNull("Environment variables should not be null", container.getEnv());
	assertTrue("Invalid number of environment variables", container.getEnv().size() > 1);

	kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}
 
Example #20
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"));
}
 
Example #21
Source File: CronJobCrudTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testCrud() {
  KubernetesClient client = server.getClient();

  CronJob cronJob1 = new CronJobBuilder().withNewMetadata()
    .withName("cronJob1")
    .withResourceVersion("1")
    .addToLabels("foo", "bar")
    .endMetadata()
    .withNewSpec()
    .withSchedule("1 2-14 * * 0-1,5-6")
    .withNewJobTemplate()
    .withNewSpec()
    .withNewTemplate()
    .withNewSpec()
    .addNewImagePullSecret()
    .withName("gcr-secret")
    .endImagePullSecret()
    .addNewContainer()
    .withName("devopsish-netlify-cronjob")
    .withImage("gcr.io/chrisshort-net/devopsish-netlify-cron:latest")
    .addNewEnv()
    .withName("URL")
    .withNewValueFrom()
    .withNewSecretKeyRef()
    .withName("devops-build-hook")
    .withKey("url")
    .endSecretKeyRef()
    .endValueFrom()
    .endEnv()
    .endContainer()
    .endSpec()
    .endTemplate()
    .endSpec()
    .endJobTemplate()
    .endSpec()
    .build();

  CronJob cronJob2 = new CronJobBuilder().withNewMetadata()
    .withName("cronJob2")
    .withResourceVersion("1")
    .endMetadata()
    .withNewSpec()
    .withSchedule("*/1 * * * *")
    .withNewJobTemplate()
    .withNewSpec()
    .withNewTemplate()
    .withNewSpec()
    .addNewContainer()
    .withName("app")
    .withImage("bitnami/kubecfg:0.5.0")
    .addNewEnv()
    .withName("TOKEN")
    .withNewValueFrom()
    .withNewSecretKeyRef()
    .withName("default-token-rtw2m")
    .withKey("token")
    .endSecretKeyRef()
    .endValueFrom()
    .endEnv()
    .endContainer()
    .endSpec()
    .endTemplate()
    .endSpec()
    .endJobTemplate()
    .endSpec()
    .build();

  client.batch().cronjobs().inNamespace("ns1").create(cronJob1);
  client.batch().cronjobs().inNamespace("ns2").create(cronJob2);

  CronJobList cronJobList = client.batch().cronjobs().list();
  assertNotNull(cronJobList);
  assertEquals(0, cronJobList.getItems().size());

  cronJobList = client.batch().cronjobs().inAnyNamespace().list();
  assertNotNull(cronJobList);
  assertEquals(2, cronJobList.getItems().size());

  cronJobList = client.batch().cronjobs().inNamespace("ns1").list();
  assertNotNull(cronJobList);
  assertEquals(1, cronJobList.getItems().size());

  cronJobList = client.batch().cronjobs().inNamespace("ns2").list();
  assertNotNull(cronJobList);
  assertEquals(1, cronJobList.getItems().size());

  cronJobList = client.batch().cronjobs().inNamespace("ns1").withLabels(Collections.singletonMap("foo", "bar")).list();
  assertNotNull(cronJobList);
  assertEquals(1, cronJobList.getItems().size());

  boolean bDeleted = client.batch().cronjobs().inNamespace("ns1").withName("cronJob1").delete();
  cronJobList = client.batch().cronjobs().inNamespace("ns1").list();
  assertTrue(bDeleted);
  assertEquals(0, cronJobList.getItems().size());

  cronJob2 = client.batch().cronjobs().inNamespace("ns2").withName("cronJob2").edit().editSpec().withSchedule("*/1 * * * *").and().done();
  assertNotNull(cronJob2);
  assertEquals("*/1 * * * *", cronJob2.getSpec().getSchedule());
}
 
Example #22
Source File: BatchAPIGroupClient.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public MixedOperation<CronJob, CronJobList, DoneableCronJob, Resource<CronJob, DoneableCronJob>> cronjobs() {
  return new CronJobOperationsImpl(httpClient, getConfiguration());
}
 
Example #23
Source File: UtilsTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
void testGetPluralFromKind() {
  // Given
  Map<String, Class> pluralToKubernetesResourceMap = new HashMap<>();
  pluralToKubernetesResourceMap.put("bindings", Binding.class);
  pluralToKubernetesResourceMap.put("componentstatuses", ComponentStatus.class);
  pluralToKubernetesResourceMap.put("configmaps", ConfigMap.class);
  pluralToKubernetesResourceMap.put("endpoints", Endpoints.class);
  pluralToKubernetesResourceMap.put("events", Event.class);
  pluralToKubernetesResourceMap.put("limitranges", LimitRange.class);
  pluralToKubernetesResourceMap.put("namespaces", Namespace.class);
  pluralToKubernetesResourceMap.put("nodes", Node.class);
  pluralToKubernetesResourceMap.put("persistentvolumeclaims", PersistentVolumeClaim.class);
  pluralToKubernetesResourceMap.put("persistentvolumes", PersistentVolume.class);
  pluralToKubernetesResourceMap.put("pods", Pod.class);
  pluralToKubernetesResourceMap.put("podtemplates", PodTemplate.class);
  pluralToKubernetesResourceMap.put("replicationcontrollers", ReplicationController.class);
  pluralToKubernetesResourceMap.put("resourcequotas", ResourceQuota.class);
  pluralToKubernetesResourceMap.put("secrets", Secret.class);
  pluralToKubernetesResourceMap.put("serviceaccounts", ServiceAccount.class);
  pluralToKubernetesResourceMap.put("services", Service.class);
  pluralToKubernetesResourceMap.put("mutatingwebhookconfigurations", MutatingWebhookConfiguration.class);
  pluralToKubernetesResourceMap.put("validatingwebhookconfigurations", ValidatingWebhookConfiguration.class);
  pluralToKubernetesResourceMap.put("customresourcedefinitions", CustomResourceDefinition.class);
  pluralToKubernetesResourceMap.put("controllerrevisions", ControllerRevision.class);
  pluralToKubernetesResourceMap.put("daemonsets", DaemonSet.class);
  pluralToKubernetesResourceMap.put("deployments", Deployment.class);
  pluralToKubernetesResourceMap.put("replicasets", ReplicaSet.class);
  pluralToKubernetesResourceMap.put("statefulsets", StatefulSet.class);
  pluralToKubernetesResourceMap.put("tokenreviews", TokenReview.class);
  pluralToKubernetesResourceMap.put("localsubjectaccessreviews", LocalSubjectAccessReview.class);
  pluralToKubernetesResourceMap.put("selfsubjectaccessreviews", SelfSubjectAccessReview.class);
  pluralToKubernetesResourceMap.put("selfsubjectrulesreviews", SelfSubjectRulesReview.class);
  pluralToKubernetesResourceMap.put("subjectaccessreviews", SubjectAccessReview.class);
  pluralToKubernetesResourceMap.put("horizontalpodautoscalers", HorizontalPodAutoscaler.class);
  pluralToKubernetesResourceMap.put("cronjobs", CronJob.class);
  pluralToKubernetesResourceMap.put("jobs", Job.class);
  pluralToKubernetesResourceMap.put("certificatesigningrequests", CertificateSigningRequest.class);
  pluralToKubernetesResourceMap.put("leases", Lease.class);
  pluralToKubernetesResourceMap.put("endpointslices", EndpointSlice.class);
  pluralToKubernetesResourceMap.put("ingresses", Ingress.class);
  pluralToKubernetesResourceMap.put("networkpolicies", NetworkPolicy.class);
  pluralToKubernetesResourceMap.put("poddisruptionbudgets", PodDisruptionBudget.class);
  pluralToKubernetesResourceMap.put("podsecuritypolicies", PodSecurityPolicy.class);
  pluralToKubernetesResourceMap.put("clusterrolebindings", ClusterRoleBinding.class);
  pluralToKubernetesResourceMap.put("clusterroles", ClusterRole.class);
  pluralToKubernetesResourceMap.put("rolebindings", RoleBinding.class);
  pluralToKubernetesResourceMap.put("roles", Role.class);
  pluralToKubernetesResourceMap.put("priorityclasses", PriorityClass.class);
  pluralToKubernetesResourceMap.put("csidrivers", CSIDriver.class);
  pluralToKubernetesResourceMap.put("csinodes", CSINode.class);
  pluralToKubernetesResourceMap.put("storageclasses", StorageClass.class);
  pluralToKubernetesResourceMap.put("volumeattachments", VolumeAttachment.class);

  // When & Then
  pluralToKubernetesResourceMap.forEach((plural, kubernetesResource)
    -> assertEquals(plural, Utils.getPluralFromKind(kubernetesResource.getSimpleName())));
}
 
Example #24
Source File: UtilsTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
@DisplayName("Should test whether resource is namespaced or not")
void testWhetherNamespacedOrNot() {
  assertTrue(Utils.isResourceNamespaced(Binding.class));
  assertFalse(Utils.isResourceNamespaced(ComponentStatus.class));
  assertTrue(Utils.isResourceNamespaced(ConfigMap.class));
  assertTrue(Utils.isResourceNamespaced(Endpoints.class));
  assertTrue(Utils.isResourceNamespaced(Event.class));
  assertTrue(Utils.isResourceNamespaced(LimitRange.class));
  assertFalse(Utils.isResourceNamespaced(Namespace.class));
  assertFalse(Utils.isResourceNamespaced(Node.class));
  assertTrue(Utils.isResourceNamespaced(PersistentVolumeClaim.class));
  assertFalse(Utils.isResourceNamespaced(PersistentVolume.class));
  assertTrue(Utils.isResourceNamespaced(Pod.class));
  assertTrue(Utils.isResourceNamespaced(PodTemplate.class));
  assertTrue(Utils.isResourceNamespaced(ReplicationController.class));
  assertTrue(Utils.isResourceNamespaced(ResourceQuota.class));
  assertTrue(Utils.isResourceNamespaced(Secret.class));
  assertTrue(Utils.isResourceNamespaced(ServiceAccount.class));
  assertTrue(Utils.isResourceNamespaced(Service.class));
  assertFalse(Utils.isResourceNamespaced(MutatingWebhookConfiguration.class));
  assertFalse(Utils.isResourceNamespaced(ValidatingWebhookConfiguration.class));
  assertFalse(Utils.isResourceNamespaced(CustomResourceDefinition.class));
  assertTrue(Utils.isResourceNamespaced(ControllerRevision.class));
  assertTrue(Utils.isResourceNamespaced(DaemonSet.class));
  assertTrue(Utils.isResourceNamespaced(Deployment.class));
  assertTrue(Utils.isResourceNamespaced(ReplicaSet.class));
  assertTrue(Utils.isResourceNamespaced(StatefulSet.class));
  assertTrue(Utils.isResourceNamespaced(TokenReview.class));
  assertTrue(Utils.isResourceNamespaced(LocalSubjectAccessReview.class));
  assertTrue(Utils.isResourceNamespaced(SelfSubjectAccessReview.class));
  assertTrue(Utils.isResourceNamespaced(SelfSubjectRulesReview.class));
  assertTrue(Utils.isResourceNamespaced(SubjectAccessReview.class));
  assertTrue(Utils.isResourceNamespaced(HorizontalPodAutoscaler.class));
  assertTrue(Utils.isResourceNamespaced(CronJob.class));
  assertTrue(Utils.isResourceNamespaced(Job.class));
  assertTrue(Utils.isResourceNamespaced(CertificateSigningRequest.class));
  assertTrue(Utils.isResourceNamespaced(Lease.class));
  assertTrue(Utils.isResourceNamespaced(EndpointSlice.class));
  assertTrue(Utils.isResourceNamespaced(Ingress.class));
  assertTrue(Utils.isResourceNamespaced(NetworkPolicy.class));
  assertTrue(Utils.isResourceNamespaced(PodDisruptionBudget.class));
  assertFalse(Utils.isResourceNamespaced(PodSecurityPolicy.class));
  assertFalse(Utils.isResourceNamespaced(ClusterRoleBinding.class));
  assertFalse(Utils.isResourceNamespaced(ClusterRole.class));
  assertTrue(Utils.isResourceNamespaced(RoleBinding.class));
  assertTrue(Utils.isResourceNamespaced(Role.class));
  assertFalse(Utils.isResourceNamespaced(PriorityClass.class));
  assertTrue(Utils.isResourceNamespaced(CSIDriver.class));
  assertTrue(Utils.isResourceNamespaced(CSINode.class));
  assertFalse(Utils.isResourceNamespaced(StorageClass.class));
  assertTrue(Utils.isResourceNamespaced(VolumeAttachment.class));
}
 
Example #25
Source File: CronJobIT.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
public void load() {
  CronJob aCronJob = client.batch().cronjobs().load(getClass().getResourceAsStream("/test-cronjob.yml")).get();
  assertNotNull(aCronJob);
  assertEquals("hello", aCronJob.getMetadata().getName());
}
 
Example #26
Source File: BatchAPIGroupDSL.java    From kubernetes-client with Apache License 2.0 votes vote down vote up
MixedOperation<CronJob, CronJobList, DoneableCronJob, Resource<CronJob, DoneableCronJob>> cronjobs();