From fa646c318d27f132f79abdbb2621f21915ec60bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Mesureux?= Date: Tue, 26 Nov 2024 10:41:27 +0100 Subject: [PATCH] feat(cronJob): allow override timeZone --- .../openpaas/dailyclean/service/KubernetesService.java | 5 ++++- .../fr/axa/openpaas/dailyclean/util/KubernetesUtils.java | 6 ++++-- .../axa/openpaas/dailyclean/util/ScriptPlaceholder.java | 3 ++- api/api/src/main/resources/application.properties | 1 + api/api/src/main/resources/scripts/cronjob.yml | 1 + .../resource/TimeRangesResourceOnStartupTest.java | 8 ++++---- .../dailyclean/resource/TimeRangesResourceTest.java | 6 +++--- .../service/ScheduledTimeSwitchOverServiceTest.java | 4 ++-- 8 files changed, 21 insertions(+), 13 deletions(-) diff --git a/api/api/src/main/java/fr/axa/openpaas/dailyclean/service/KubernetesService.java b/api/api/src/main/java/fr/axa/openpaas/dailyclean/service/KubernetesService.java index 7b04610..9fa67ac 100644 --- a/api/api/src/main/java/fr/axa/openpaas/dailyclean/service/KubernetesService.java +++ b/api/api/src/main/java/fr/axa/openpaas/dailyclean/service/KubernetesService.java @@ -35,6 +35,9 @@ public class KubernetesService { @ConfigProperty(name = "service.job.serviceAccountName") String serviceAccountName; + @ConfigProperty(name = "service.job.timeZone") + String timeZone; + @ConfigProperty(name = "service.deployment.label.dailyclean") String dailycleanLabelName; @@ -193,7 +196,7 @@ private void createCronJob(String cron, KubernetesArgument argument) { logger.info("Creating cron job from object"); kubernetesClient.batch().v1().cronjobs().inNamespace(namespace) - .load(KubernetesUtils.createCronJobAsInputStream(argument, cron, imgName,serviceAccountName)) + .load(KubernetesUtils.createCronJobAsInputStream(argument, cron, imgName, serviceAccountName, timeZone)) .createOrReplace(); logger.info("Successfully created cronjob with name {}", KubernetesUtils.getCronName(argument)); } diff --git a/api/api/src/main/java/fr/axa/openpaas/dailyclean/util/KubernetesUtils.java b/api/api/src/main/java/fr/axa/openpaas/dailyclean/util/KubernetesUtils.java index 36e7be8..6e623e7 100644 --- a/api/api/src/main/java/fr/axa/openpaas/dailyclean/util/KubernetesUtils.java +++ b/api/api/src/main/java/fr/axa/openpaas/dailyclean/util/KubernetesUtils.java @@ -46,13 +46,15 @@ private KubernetesUtils() {} public static InputStream createCronJobAsInputStream(KubernetesArgument argument, String cron, String imgName, - String serviceAccountName) { + String serviceAccountName, + String timeZone) { String text = getFileAsString("scripts/cronjob.yml"); String cronJobAsdString = text.replace(NAME.getPlaceholder(), getCronName(argument)) .replace(ARGUMENT.getPlaceholder(), argument.getValue()) .replace(SCHEDULE.getPlaceholder(), cron) .replace(IMG_NAME.getPlaceholder(), imgName) - .replace(SERVICE_ACCOUNT_NAME.getPlaceholder(), serviceAccountName); + .replace(SERVICE_ACCOUNT_NAME.getPlaceholder(), serviceAccountName) + .replace(TIME_ZONE.getPlaceholder(), timeZone); return new ByteArrayInputStream(cronJobAsdString.getBytes()); } diff --git a/api/api/src/main/java/fr/axa/openpaas/dailyclean/util/ScriptPlaceholder.java b/api/api/src/main/java/fr/axa/openpaas/dailyclean/util/ScriptPlaceholder.java index 4d6f438..110ad8c 100644 --- a/api/api/src/main/java/fr/axa/openpaas/dailyclean/util/ScriptPlaceholder.java +++ b/api/api/src/main/java/fr/axa/openpaas/dailyclean/util/ScriptPlaceholder.java @@ -6,7 +6,8 @@ public enum ScriptPlaceholder { ARGUMENT("{{argument}}"), SCHEDULE("{{schedule}}"), IMG_NAME("{{imgName}}"), - SERVICE_ACCOUNT_NAME("{{serviceAccountName}}"),; + SERVICE_ACCOUNT_NAME("{{serviceAccountName}}"), + TIME_ZONE("{{timeZone}}"),; private String placeholder; ScriptPlaceholder(String placeholder) { diff --git a/api/api/src/main/resources/application.properties b/api/api/src/main/resources/application.properties index add347e..61bfdc2 100644 --- a/api/api/src/main/resources/application.properties +++ b/api/api/src/main/resources/application.properties @@ -3,6 +3,7 @@ service.name=Dailyclean service.version=1.0.0 service.job.imageName=axaguildev/dailyclean-job:latest service.job.serviceAccountName=default +service.job.timeZone=CET service.deployment.label.dailyclean=axa.com/dailyclean quarkus.scheduler.cron-type=unix diff --git a/api/api/src/main/resources/scripts/cronjob.yml b/api/api/src/main/resources/scripts/cronjob.yml index 653046f..2033589 100644 --- a/api/api/src/main/resources/scripts/cronjob.yml +++ b/api/api/src/main/resources/scripts/cronjob.yml @@ -5,6 +5,7 @@ metadata: spec: concurrencyPolicy: Forbid schedule: '{{schedule}}' + timeZone: '{{timeZone}}' jobTemplate: spec: template: diff --git a/api/api/src/test/java/fr/axa/openpaas/dailyclean/resource/TimeRangesResourceOnStartupTest.java b/api/api/src/test/java/fr/axa/openpaas/dailyclean/resource/TimeRangesResourceOnStartupTest.java index 513dde5..203a43d 100644 --- a/api/api/src/test/java/fr/axa/openpaas/dailyclean/resource/TimeRangesResourceOnStartupTest.java +++ b/api/api/src/test/java/fr/axa/openpaas/dailyclean/resource/TimeRangesResourceOnStartupTest.java @@ -31,9 +31,9 @@ public void shouldUpdateTheImageVersionOnStartup() { // Create cron jobs with old versions InputStream cronJobStart = - KubernetesUtils.createCronJobAsInputStream(START, CRON_10_00, OLD_IMAGE_NAME, SERVICE_ACCOUNT_NAME); + KubernetesUtils.createCronJobAsInputStream(START, CRON_10_00, OLD_IMAGE_NAME, SERVICE_ACCOUNT_NAME, TIME_ZONE); InputStream cronJobStop = - KubernetesUtils.createCronJobAsInputStream(STOP, CRON_10_00, OLD_IMAGE_NAME, SERVICE_ACCOUNT_NAME); + KubernetesUtils.createCronJobAsInputStream(STOP, CRON_10_00, OLD_IMAGE_NAME, SERVICE_ACCOUNT_NAME, TIME_ZONE); client.load(cronJobStart).inNamespace(namespace).createOrReplace(); client.load(cronJobStop).inNamespace(namespace).createOrReplace(); @@ -67,7 +67,7 @@ public void shouldUpdateTheImageVersionOnStartupOnlyIfStartExisted() { // Create cron jobs with old versions InputStream cronJobStart = - KubernetesUtils.createCronJobAsInputStream(START, cronStart, OLD_IMAGE_NAME, SERVICE_ACCOUNT_NAME); + KubernetesUtils.createCronJobAsInputStream(START, cronStart, OLD_IMAGE_NAME, SERVICE_ACCOUNT_NAME, TIME_ZONE); client.load(cronJobStart).inNamespace(namespace).createOrReplace(); @@ -102,7 +102,7 @@ public void shouldUpdateTheImageVersionOnStartupOnlyIfStopExisted() { // Create cron jobs with old versions InputStream cronJobStop = - KubernetesUtils.createCronJobAsInputStream(STOP, cronStop, OLD_IMAGE_NAME, SERVICE_ACCOUNT_NAME); + KubernetesUtils.createCronJobAsInputStream(STOP, cronStop, OLD_IMAGE_NAME, SERVICE_ACCOUNT_NAME, TIME_ZONE); client.load(cronJobStop).inNamespace(namespace).createOrReplace(); diff --git a/api/api/src/test/java/fr/axa/openpaas/dailyclean/resource/TimeRangesResourceTest.java b/api/api/src/test/java/fr/axa/openpaas/dailyclean/resource/TimeRangesResourceTest.java index 95a2c2e..42416aa 100644 --- a/api/api/src/test/java/fr/axa/openpaas/dailyclean/resource/TimeRangesResourceTest.java +++ b/api/api/src/test/java/fr/axa/openpaas/dailyclean/resource/TimeRangesResourceTest.java @@ -136,7 +136,7 @@ public void shouldUpsertExistingTimerangesWithNewOnes() { public void shouldUpsertExistingTimerangeWithNewOneAndCreate() { String cronStart = CRON_10_00; - InputStream cronJobStart = KubernetesUtils.createCronJobAsInputStream(START, cronStart, IMG_NAME, SERVICE_ACCOUNT_NAME); + InputStream cronJobStart = KubernetesUtils.createCronJobAsInputStream(START, cronStart, IMG_NAME, SERVICE_ACCOUNT_NAME, TIME_ZONE); KubernetesClient client = mockServer.getClient(); final String namespace = client.getNamespace(); @@ -163,8 +163,8 @@ public void shouldUpsertExistingTimerangeWithNewOneAndCreate() { private void initializeExistingCronJobs(String cronStart, String cronStop) { - InputStream cronJobStart = KubernetesUtils.createCronJobAsInputStream(START, cronStart, IMG_NAME, SERVICE_ACCOUNT_NAME); - InputStream cronJobStop = KubernetesUtils.createCronJobAsInputStream(STOP, cronStop, IMG_NAME, SERVICE_ACCOUNT_NAME); + InputStream cronJobStart = KubernetesUtils.createCronJobAsInputStream(START, cronStart, IMG_NAME, SERVICE_ACCOUNT_NAME, TIME_ZONE); + InputStream cronJobStop = KubernetesUtils.createCronJobAsInputStream(STOP, cronStop, IMG_NAME, SERVICE_ACCOUNT_NAME, TIME_ZONE); KubernetesClient client = mockServer.getClient(); final String namespace = client.getNamespace(); diff --git a/api/api/src/test/java/fr/axa/openpaas/dailyclean/service/ScheduledTimeSwitchOverServiceTest.java b/api/api/src/test/java/fr/axa/openpaas/dailyclean/service/ScheduledTimeSwitchOverServiceTest.java index 804af4f..376a2ad 100644 --- a/api/api/src/test/java/fr/axa/openpaas/dailyclean/service/ScheduledTimeSwitchOverServiceTest.java +++ b/api/api/src/test/java/fr/axa/openpaas/dailyclean/service/ScheduledTimeSwitchOverServiceTest.java @@ -229,11 +229,11 @@ private void initializeExistingCronJobs(String cronStart, String cronStop) { KubernetesClient client = mockServer.getClient(); final String namespace = client.getNamespace(); if(cronStart != null) { - InputStream cronJobStart = KubernetesUtils.createCronJobAsInputStream(START, cronStart, "imgName", "default"); + InputStream cronJobStart = KubernetesUtils.createCronJobAsInputStream(START, cronStart, "imgName", "default", "CET"); client.load(cronJobStart).inNamespace(namespace).createOrReplace(); } if(cronStop != null) { - InputStream cronJobStop = KubernetesUtils.createCronJobAsInputStream(STOP, cronStop, "imgName", "default"); + InputStream cronJobStop = KubernetesUtils.createCronJobAsInputStream(STOP, cronStop, "imgName", "default", "CET"); client.load(cronJobStop).inNamespace(namespace).createOrReplace(); } }