Skip to content

Commit

Permalink
Add job annotations into cron job builder
Browse files Browse the repository at this point in the history
 - Add jobAnnotations by resolving against all the property sources including the Scheduler Properties from the server config
 - Add integration tests to verify the property overriding at the server/deployment levels

Resolves #389
  • Loading branch information
LifeSweeteneryasit committed Sep 21, 2023
1 parent 8af0567 commit 9e62681
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ protected CronJob createCronJob(ScheduleRequest scheduleRequest) {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,58 @@ public void testImagePullPolicyOverride() {
kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}

@Test
public void testJobAnnotationsFromSchedulerProperties() {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
kubernetesSchedulerProperties.setJobAnnotations("test1:value1");
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);

assertEquals("Job annotation is not set", "value1", cronJob.getMetadata().getAnnotations().get("test1"));

kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}

@Test
public void testJobAnnotationsOverride() {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
kubernetesSchedulerProperties.setJobAnnotations("test1:value1");
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());
String prefix = KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX;
Map<String, String> schedulerProperties = new HashMap<>(getSchedulerProperties());
schedulerProperties.put(prefix + ".jobAnnotations", "test1:value2");

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

CronJob cronJob = kubernetesScheduler.createCronJob(scheduleRequest);

assertEquals("Job annotation is not set", "value2", cronJob.getMetadata().getAnnotations().get("test1"));

kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}

@Test
public void testImagePullPolicyDefault() {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
Expand Down Expand Up @@ -592,6 +644,35 @@ public void testImagePullSecretDefault() {
kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}

@Test
public void testImagePullSecretFromSchedulerProperties() {
KubernetesSchedulerProperties kubernetesSchedulerProperties = new KubernetesSchedulerProperties();
if (kubernetesSchedulerProperties.getNamespace() == null) {
kubernetesSchedulerProperties.setNamespace("default");
}

String secretName = "image-secret";
kubernetesSchedulerProperties.setImagePullSecret(secretName);
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();
assertEquals("Unexpected image pull secret", secretName, secrets.get(0).getName());

kubernetesScheduler.unschedule(cronJob.getMetadata().getName());
}

@Test
public void testCustomEnvironmentVariables() {
String prefix = KubernetesSchedulerProperties.KUBERNETES_SCHEDULER_PROPERTIES_PREFIX;
Expand Down

0 comments on commit 9e62681

Please sign in to comment.