Skip to content

Commit

Permalink
Add management tools cron job
Browse files Browse the repository at this point in the history
With the cron job component, you can run mangement tools commands on a schedule, for example to empty the recycle bin or expire old versions.
  • Loading branch information
Stefan Bethke authored and stefanbethke committed Jun 4, 2024
1 parent 25dc01a commit e3ecafd
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 82 deletions.
229 changes: 150 additions & 79 deletions docs/custom-resource.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.tsystemsmms.cmcc.cmccoperator.components.corba.*;
import com.tsystemsmms.cmcc.cmccoperator.components.generic.*;
import com.tsystemsmms.cmcc.cmccoperator.components.job.MgmtToolsCronJobComponent;
import com.tsystemsmms.cmcc.cmccoperator.components.job.MgmtToolsJobComponent;
import com.tsystemsmms.cmcc.cmccoperator.crds.ComponentSpec;
import com.tsystemsmms.cmcc.cmccoperator.targetstate.TargetState;
Expand Down Expand Up @@ -67,6 +68,12 @@ public MgmtToolsJobComponent mgmtToolsJobComponent(KubernetesClient kubernetesCl
return new MgmtToolsJobComponent(kubernetesClient, targetState, cs);
}

@Bean("component:management-tools-cron")
@Scope(SCOPE_PROTOTYPE)
public MgmtToolsCronJobComponent mgmtToolsCronJobComponent(KubernetesClient kubernetesClient, TargetState targetState, ComponentSpec cs) {
return new MgmtToolsCronJobComponent(kubernetesClient, targetState, cs);
}

@Bean("component:mongodb")
@Scope(SCOPE_PROTOTYPE)
public MongoDBComponent mongoDBComponent(KubernetesClient kubernetesClient, TargetState targetState, ComponentSpec cs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022. T-Systems Multimedia Solutions GmbH
* Copyright (c) 2022-2024. T-Systems Multimedia Solutions GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
*
Expand All @@ -8,9 +8,10 @@
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

package com.tsystemsmms.cmcc.cmccoperator.crds;
package com.tsystemsmms.cmcc.cmccoperator.components.job;

import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.tsystemsmms.cmcc.cmccoperator.crds.UsernamePasswordSecretRef;
import io.fabric8.kubernetes.api.model.EnvVar;
import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024. T-Systems Multimedia Solutions GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

package com.tsystemsmms.cmcc.cmccoperator.components.job;

import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import lombok.Data;

@Data
public class MgmtCronJobConfig {
@JsonPropertyDescription("k8s cron expression determining when the job will be run. Default every day at midnight")
String cron = "0 0 0 * *";

@JsonPropertyDescription("Time zone for the time specification, default local timezone of the cluster")
String timezone = "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Copyright (c) 2024. T-Systems Multimedia Solutions GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

package com.tsystemsmms.cmcc.cmccoperator.components.job;

import com.tsystemsmms.cmcc.cmccoperator.components.Component;
import com.tsystemsmms.cmcc.cmccoperator.components.HasUapiClient;
import com.tsystemsmms.cmcc.cmccoperator.components.SpringBootComponent;
import com.tsystemsmms.cmcc.cmccoperator.crds.ComponentSpec;
import com.tsystemsmms.cmcc.cmccoperator.targetstate.CustomResourceConfigError;
import com.tsystemsmms.cmcc.cmccoperator.targetstate.TargetState;
import com.tsystemsmms.cmcc.cmccoperator.utils.EnvVarSet;
import com.tsystemsmms.cmcc.cmccoperator.utils.Utils;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.batch.v1.*;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.tsystemsmms.cmcc.cmccoperator.utils.Utils.EnvVarSimple;

/**
* Run a Management Tools command regularly using an k8s CronJob.
*/
public class MgmtToolsCronJobComponent extends SpringBootComponent implements HasUapiClient {
public static final String EXTRA_CONFIG = "config";

long activeDeadlineSeconds = 30 * 60L;
private MgmtCronJobConfig mgmtCronJobConfig = null;

public MgmtToolsCronJobComponent(KubernetesClient kubernetesClient, TargetState targetState, ComponentSpec componentSpec) {
super(kubernetesClient, targetState, componentSpec, "management-tools");
}

@Override
public List<HasMetadata> buildResources() {
return List.of(buildJob());
}

@Override
public Component updateComponentSpec(ComponentSpec newCs) {
super.updateComponentSpec(newCs);
if (mgmtCronJobConfig != null)
mgmtCronJobConfig = getMgmtCronJobConfigFromExtra();
return this;
}

private CronJob buildJob() {
getMgmtCronJobConfig();
return new CronJobBuilder()
.withMetadata(getResourceMetadata())
.withSpec(new CronJobSpecBuilder()
.withSchedule(mgmtCronJobConfig.cron)
.withTimeZone(mgmtCronJobConfig.timezone)
.withFailedJobsHistoryLimit(3)
.withSuccessfulJobsHistoryLimit(1)
.withJobTemplate(new JobTemplateSpecBuilder()
.withSpec(new JobSpecBuilder()
.withActiveDeadlineSeconds(activeDeadlineSeconds)
.withBackoffLimit(3)
.withCompletions(1)
.withParallelism(1)
.withTemplate(new PodTemplateSpecBuilder()
.withMetadata(new ObjectMetaBuilder()
.withAnnotations(getAnnotations())
.withLabels(getSelectorLabels())
.build())
.withSpec(new PodSpecBuilder()
.withRestartPolicy("Never")
.withContainers(buildContainers())
.withInitContainers(getInitContainers())
.withVolumes(getVolumes())
.build())
.build())
.build())
.build())
.build())
.build();
}

@Override
public SecurityContext getSecurityContext() {
return Utils.mergeObjects(SecurityContext.class,
new SecurityContextBuilder()
.withReadOnlyRootFilesystem(false) // properties location is a mix of dynamically created files and files from the image
.build(),
getCmcc().getSpec().getDefaults().getSecurityContext(),
getComponentSpec().getSecurityContext());
}

@Override
public Probe getLivenessProbe() {
return null;
}

@Override
public Probe getReadinessProbe() {
return null;
}

@Override
public Probe getStartupProbe() {
return null;
}

@Override
public EnvVarSet getEnvVars() {
EnvVarSet env = super.getEnvVars();

env.add(EnvVarSimple("JAVA_HEAP", ""));
env.add(EnvVarSimple("JAVA_OPTS", getCmcc().getSpec().getDefaults().getJavaOpts()));

env.add(EnvVarSimple("CAP_CLIENT_SERVER_IOR_URL", getTargetState().getServiceUrlFor("content-server", "cms")));
env.add(EnvVarSimple("DEV_MASTER_CAP_CLIENT_SERVER_IOR_URL", getTargetState().getServiceUrlFor("content-server", "mls")));
env.add(EnvVarSimple("DEV_MANAGEMENT_CAP_CLIENT_SERVER_IOR_URL", getTargetState().getServiceUrlFor("content-server", "cms")));

env.addAll(getUapiClientEnvVars("TOOLS"));
env.add(EnvVarSimple("DEBUG_ENTRYPOINT", "true"));
env.add(EnvVarSimple("IMPORT_DIR", "/coremedia/import"));

env.addAll(getTargetState().getComponentCollection().getHasJdbcClientComponent("content-server", "cms")
.getJdbcClientEnvVars("DEV_MANAGEMENT_JDBC"));
env.addAll(getTargetState().getComponentCollection().getHasJdbcClientComponent("content-server", "mls")
.getJdbcClientEnvVars("DEV_MASTER_JDBC"));

return env;
}

@Override
public HashMap<String, String> getSelectorLabels() {
HashMap<String, String> labels = super.getSelectorLabels();
labels.putAll(getJobLabels());
return labels;
}

public static Map<String, String> getJobLabels() {
return Map.of("cmcc.tsystemsmms.com/cronjob", MgmtToolsCronJobComponent.class.getSimpleName().replaceAll("Component$", ""));
}

public Optional<Boolean> isReady() {
// Once the object has been created, it is ready. Any execution failures are not tracked by the operator.
return Optional.of(Boolean.TRUE);
}

private 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));
}

private MgmtCronJobConfig getMgmtCronJobConfig() {
if (mgmtCronJobConfig == null)
mgmtCronJobConfig = getMgmtCronJobConfigFromExtra();
return mgmtCronJobConfig;
}

@Override
public String getUapiClientDefaultUsername() {
return UAPI_ADMIN_USERNAME;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.tsystemsmms.cmcc.cmccoperator.components.Component;
import com.tsystemsmms.cmcc.cmccoperator.components.HasUapiClient;
import com.tsystemsmms.cmcc.cmccoperator.crds.ComponentSpec;
import com.tsystemsmms.cmcc.cmccoperator.crds.ImportJob;
import com.tsystemsmms.cmcc.cmccoperator.targetstate.CustomResourceConfigError;
import com.tsystemsmms.cmcc.cmccoperator.targetstate.TargetState;
import com.tsystemsmms.cmcc.cmccoperator.utils.EnvVarSet;
Expand Down

0 comments on commit e3ecafd

Please sign in to comment.