Skip to content

Commit

Permalink
MgmtToolsCronJobComponent: make activeDeadlineSeconds configurable
Browse files Browse the repository at this point in the history
While here, simplify the config parameters for this component. The old "config" entries are still understood for backwards compatibility.
  • Loading branch information
Stefan Bethke committed Jun 12, 2024
1 parent f127066 commit 6ab2e1b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
23 changes: 12 additions & 11 deletions docs/custom-resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,18 +54,32 @@ public List<HasMetadata> 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()
Expand Down Expand Up @@ -154,18 +170,13 @@ public Optional<Boolean> isReady() {
return Optional.of(Boolean.TRUE);
}

private MgmtCronJobConfig getMgmtCronJobConfigFromExtra() {
private Optional<MgmtCronJobConfig> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String> 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());
Expand Down

0 comments on commit 6ab2e1b

Please sign in to comment.