diff --git a/docs/custom-resource.md b/docs/custom-resource.md index 583c7fb..43ff258 100644 --- a/docs/custom-resource.md +++ b/docs/custom-resource.md @@ -456,18 +456,18 @@ components: env: - name: CLEANRECYCLEBIN_BEFORE_DATE value: 1 hour ago - - name: CLEANVERIONS_KEEP_VERSIONS_DAYS + - name: CLEANVERSIONS_KEEP_VERSIONS_DAYS value: "1" - - name: CLEANVERIONS_KEEP_VERSIONS_NUMBER + - name: CLEANVERSIONS_KEEP_VERSIONS_NUMBER value: "1" - - name: CLEANVERIONS_TARGET_PATH + - name: CLEANVERSIONS_TARGET_PATH value: / - name: TZ value: Europe/Berlin extra: - config: | - cron: "*/5 * * * *" - timezone: "Europe/Berlin" + activeDeadlineSeconds: "3600" + cron: "*/5 * * * *" + timezone: "Europe/Berlin" milestone: ManagementReady ``` @@ -798,11 +798,12 @@ If you want to run different commands on separate schedules, add them as separat Note that you might need to add your own scripts to the management tools image to fully make use of the cron job facility. -| Property | Type | Default | Description | -|-------------------------|-----------------|------------|-------------------------------------------------------------------------------------------------------------------------| -| `args` | list of Strings | "" | List of the entrypoint scripts to run. No default. | -| `extra.config.cron` | String | "0 0 0 * +" | When to execute the job. Default is every day at midnight local time of the k8s cluster. | -| `extra.config.timezone` | String | "" | The time zone of the time specification. By default, this is the local time of the cluster. Use "Europe/Berlin" format. | +| Property | Type | Default | Description | +|-------------------------------|-----------------|-------------|-------------------------------------------------------------------------------------------------------------------------| +| `args` | list of Strings | "" | List of the entrypoint scripts to run. No default. | +| `extra.activeDeadlineSeconds` | String | "1800" | How long to run the job before k8s considers it stuck and kills it. | +| `extra.cron` | String | "0 0 * * *" | When to execute the job. Default is every day at midnight local time of the k8s cluster. | +| `extra.timezone` | String | "" | The time zone of the time specification. By default, this is the local time of the cluster. Use "Europe/Berlin" format. | ### Component `overview` diff --git a/src/main/java/com/tsystemsmms/cmcc/cmccoperator/components/job/MgmtToolsCronJobComponent.java b/src/main/java/com/tsystemsmms/cmcc/cmccoperator/components/job/MgmtToolsCronJobComponent.java index 30dad35..60de203 100644 --- a/src/main/java/com/tsystemsmms/cmcc/cmccoperator/components/job/MgmtToolsCronJobComponent.java +++ b/src/main/java/com/tsystemsmms/cmcc/cmccoperator/components/job/MgmtToolsCronJobComponent.java @@ -38,10 +38,12 @@ public class MgmtToolsCronJobComponent extends SpringBootComponent implements Ha public static final String EXTRA_CONFIG = "config"; long activeDeadlineSeconds = 30 * 60L; - private MgmtCronJobConfig mgmtCronJobConfig = null; + String cron = "0 0 * * *"; + String timezone = ""; public MgmtToolsCronJobComponent(KubernetesClient kubernetesClient, TargetState targetState, ComponentSpec componentSpec) { super(kubernetesClient, targetState, componentSpec, "management-tools"); + updateConfig(); } @Override @@ -52,18 +54,32 @@ public List buildResources() { @Override public Component updateComponentSpec(ComponentSpec newCs) { super.updateComponentSpec(newCs); - if (mgmtCronJobConfig != null) - mgmtCronJobConfig = getMgmtCronJobConfigFromExtra(); + updateConfig(); return this; } + private void updateConfig() { + var extraConfig = getComponentSpec().getExtra(EXTRA_CONFIG); + if (extraConfig.isPresent()) { + Yaml yaml = new Yaml(new Constructor(MgmtCronJobConfig.class)); + MgmtCronJobConfig config = yaml.load(extraConfig.get()); + cron = config.getCron(); + timezone = config.getTimezone(); + } + getComponentSpec().getExtra("activeDeadlineSeconds") + .ifPresent(v -> activeDeadlineSeconds = Long.parseLong(v)); + getComponentSpec().getExtra("cron") + .ifPresent(v -> cron = v); + getComponentSpec().getExtra("timezone") + .ifPresent(v -> timezone = v); + } + private CronJob buildJob() { - getMgmtCronJobConfig(); return new CronJobBuilder() .withMetadata(getResourceMetadata()) .withSpec(new CronJobSpecBuilder() - .withSchedule(mgmtCronJobConfig.cron) - .withTimeZone(mgmtCronJobConfig.timezone) + .withSchedule(cron) + .withTimeZone(timezone) .withFailedJobsHistoryLimit(3) .withSuccessfulJobsHistoryLimit(1) .withJobTemplate(new JobTemplateSpecBuilder() @@ -154,18 +170,13 @@ public Optional isReady() { return Optional.of(Boolean.TRUE); } - private MgmtCronJobConfig getMgmtCronJobConfigFromExtra() { + private Optional getMgmtCronJobConfigFromExtra() { Yaml yaml = new Yaml(new Constructor(MgmtCronJobConfig.class)); if (getComponentSpec().getExtra() == null || !getComponentSpec().getExtra().containsKey(EXTRA_CONFIG)) - throw new CustomResourceConfigError("Must specify " + EXTRA_CONFIG + " with job parameters for job \"" + getSpecName() + "\""); - return yaml.load(getComponentSpec().getExtra().get(EXTRA_CONFIG)); + return Optional.empty(); + return Optional.of(yaml.load(getComponentSpec().getExtra().get(EXTRA_CONFIG))); } - private MgmtCronJobConfig getMgmtCronJobConfig() { - if (mgmtCronJobConfig == null) - mgmtCronJobConfig = getMgmtCronJobConfigFromExtra(); - return mgmtCronJobConfig; - } @Override public String getUapiClientDefaultUsername() { diff --git a/src/main/java/com/tsystemsmms/cmcc/cmccoperator/crds/ComponentSpec.java b/src/main/java/com/tsystemsmms/cmcc/cmccoperator/crds/ComponentSpec.java index b1a6154..d2ae5d5 100644 --- a/src/main/java/com/tsystemsmms/cmcc/cmccoperator/crds/ComponentSpec.java +++ b/src/main/java/com/tsystemsmms/cmcc/cmccoperator/crds/ComponentSpec.java @@ -18,10 +18,7 @@ import lombok.AllArgsConstructor; import lombok.Data; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; +import java.util.*; @Data public class ComponentSpec { @@ -83,6 +80,18 @@ public ComponentSpec(ComponentSpec that) { this.update(that); } + /** + * Get the value of an entry in the extra hash. + * + * @param key name of the entry + * @return value of the entry, or Optional.empty + */ + public Optional getExtra(String key) { + if (extra == null) + return Optional.empty(); + return Optional.ofNullable(extra.get(key)); + } + public void update(ComponentSpec that) { this.setAnnotations(that.getAnnotations()); this.setArgs(that.getArgs());