Skip to content

Commit

Permalink
feat(cronJob): allow override timeZone
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-mesureux committed Nov 26, 2024
1 parent c661096 commit 41c0410
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions api/api/src/main/resources/scripts/cronjob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ metadata:
spec:
concurrencyPolicy: Forbid
schedule: '{{schedule}}'
timeZone: '{{timeZone}}'
jobTemplate:
spec:
template:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 41c0410

Please sign in to comment.