forked from eclipse-jkube/jkube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jkube-kit): Initial Support for CronJob (eclipse-jkube#1954)
Signed-off-by: Anurag Rajawat <[email protected]>
- Loading branch information
1 parent
48ebbeb
commit d049be9
Showing
10 changed files
with
270 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...kit/enricher/api/src/main/java/org/eclipse/jkube/kit/enricher/handler/CronJobHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.enricher.handler; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.jkube.kit.common.util.KubernetesHelper; | ||
import org.eclipse.jkube.kit.config.image.ImageConfiguration; | ||
import org.eclipse.jkube.kit.config.resource.ControllerResourceConfig; | ||
|
||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.PodTemplateSpec; | ||
import io.fabric8.kubernetes.api.model.batch.v1.CronJob; | ||
import io.fabric8.kubernetes.api.model.batch.v1.CronJobBuilder; | ||
import io.fabric8.kubernetes.api.model.batch.v1.CronJobSpec; | ||
import io.fabric8.kubernetes.api.model.batch.v1.CronJobSpecBuilder; | ||
import io.fabric8.kubernetes.api.model.batch.v1.JobSpec; | ||
import io.fabric8.kubernetes.api.model.batch.v1.JobSpecBuilder; | ||
import io.fabric8.kubernetes.api.model.batch.v1.JobTemplateSpec; | ||
import io.fabric8.kubernetes.api.model.batch.v1.JobTemplateSpecBuilder; | ||
|
||
public class CronJobHandler implements ControllerHandler<CronJob> { | ||
private static final String DEFAULT_JOB_RESTART_POLICY = "OnFailure"; | ||
private final PodTemplateHandler podTemplateHandler; | ||
|
||
public CronJobHandler(PodTemplateHandler podTemplateHandler) { | ||
this.podTemplateHandler = podTemplateHandler; | ||
} | ||
|
||
@Override | ||
public CronJob get(ControllerResourceConfig config, List<ImageConfiguration> images) { | ||
return new CronJobBuilder() | ||
.withMetadata(createCronJobMetadata(config)) | ||
.withSpec(createCronJobSpec(config, images)) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public PodTemplateSpec getPodTemplateSpec(ControllerResourceConfig config, List<ImageConfiguration> images) { | ||
return get(config, images).getSpec().getJobTemplate().getSpec().getTemplate(); | ||
} | ||
|
||
@Override | ||
public PodTemplateSpec getPodTemplate(CronJob controller) { | ||
return controller.getSpec().getJobTemplate().getSpec().getTemplate(); | ||
} | ||
|
||
@Override | ||
public void overrideReplicas(KubernetesListBuilder resources, int replicas) { | ||
// NOOP | ||
} | ||
|
||
private ObjectMeta createCronJobMetadata(ControllerResourceConfig config) { | ||
return new ObjectMetaBuilder() | ||
.withName(KubernetesHelper.validateKubernetesId(config.getControllerName(), "controller name")) | ||
.build(); | ||
} | ||
|
||
private CronJobSpec createCronJobSpec(ControllerResourceConfig config, List<ImageConfiguration> images) { | ||
return new CronJobSpecBuilder() | ||
.withSchedule(KubernetesHelper.validateCronJobSchedule(config.getSchedule())) | ||
.withJobTemplate(createJobTemplateSpec(config, images)) | ||
.build(); | ||
} | ||
|
||
private JobTemplateSpec createJobTemplateSpec(ControllerResourceConfig config, List<ImageConfiguration> images) { | ||
return new JobTemplateSpecBuilder() | ||
.withSpec(createJobSpec(config, images)) | ||
.build(); | ||
} | ||
|
||
private JobSpec createJobSpec(ControllerResourceConfig config, List<ImageConfiguration> images) { | ||
return new JobSpecBuilder() | ||
.withTemplate(podTemplateHandler.getPodTemplate(config, | ||
Optional.ofNullable(config.getRestartPolicy()).orElse(DEFAULT_JOB_RESTART_POLICY), images)) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,4 @@ private List<Volume> getVolumes(ControllerResourceConfig config) { | |
return ret; | ||
} | ||
|
||
|
||
|
||
} |
151 changes: 151 additions & 0 deletions
151
...enricher/api/src/test/java/org/eclipse/jkube/kit/enricher/handler/CronJobHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.enricher.handler; | ||
|
||
import io.fabric8.kubernetes.api.model.PodSpec; | ||
import io.fabric8.kubernetes.api.model.PodTemplateSpec; | ||
import io.fabric8.kubernetes.api.model.batch.v1.CronJob; | ||
import io.fabric8.kubernetes.api.model.batch.v1.JobSpec; | ||
import org.eclipse.jkube.kit.config.image.ImageConfiguration; | ||
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; | ||
import org.eclipse.jkube.kit.config.resource.ControllerResourceConfig; | ||
import org.eclipse.jkube.kit.config.resource.GroupArtifactVersion; | ||
import org.eclipse.jkube.kit.config.resource.VolumeConfig; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
|
||
class CronJobHandlerTest { | ||
private List<VolumeConfig> volumes; | ||
private List<ImageConfiguration> images; | ||
private CronJobHandler cronJobHandler; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
volumes = new ArrayList<>(); | ||
images = new ArrayList<>(); | ||
List<String> mounts = new ArrayList<>(); | ||
List<String> ports = new ArrayList<>(); | ||
List<String> tags = new ArrayList<>(); | ||
|
||
//volume config with name and multiple mount | ||
mounts.add("/path/system"); | ||
mounts.add("/path/sys"); | ||
|
||
ports.add("8080"); | ||
ports.add("9090"); | ||
|
||
tags.add("latest"); | ||
tags.add("test"); | ||
|
||
VolumeConfig volumeConfig = VolumeConfig.builder() | ||
.name("test").mounts(mounts).type("hostPath").path("/test/path").build(); | ||
volumes.add(volumeConfig); | ||
|
||
//container name with alias | ||
final BuildConfiguration buildImageConfiguration = BuildConfiguration.builder() | ||
.ports(ports).from("fabric8/maven:latest").cleanup("try") | ||
.tags(tags).compressionString("gzip").build(); | ||
|
||
ImageConfiguration imageConfiguration = ImageConfiguration.builder() | ||
.name("test").alias("test-app").build(buildImageConfiguration) | ||
.registry("docker.io").build(); | ||
images.add(imageConfiguration); | ||
|
||
cronJobHandler = new CronJobHandler(new PodTemplateHandler(new ContainerHandler(new Properties(), | ||
new GroupArtifactVersion("g", "a", "v"), new ProbeHandler()))); | ||
} | ||
|
||
@Test | ||
void get_withValidConfigs_shouldReturnConfigWithContainers() { | ||
// Given | ||
ControllerResourceConfig config = ControllerResourceConfig.builder() | ||
.schedule("* * * * *") | ||
.controllerName("testing") | ||
.restartPolicy("Never") | ||
.volumes(volumes) | ||
.build(); | ||
|
||
// When | ||
CronJob cronJob = cronJobHandler.get(config, images); | ||
|
||
// Then | ||
assertThat(cronJob.getSpec().getJobTemplate().getSpec().getTemplate().getSpec().getContainers()) | ||
.isNotNull(); | ||
assertThat(cronJob) | ||
.hasFieldOrPropertyWithValue("metadata.name", "testing") | ||
.hasFieldOrPropertyWithValue("spec.schedule", "* * * * *") | ||
.satisfies(cj -> assertThat(cj.getSpec().getJobTemplate().getSpec()) | ||
.isNotNull() | ||
.extracting(JobSpec::getTemplate) | ||
.extracting(PodTemplateSpec::getSpec) | ||
.hasFieldOrPropertyWithValue("restartPolicy", "Never") | ||
.extracting(PodSpec::getVolumes).asList().first() | ||
.hasFieldOrPropertyWithValue("name", "test") | ||
.hasFieldOrPropertyWithValue("hostPath.path", "/test/path") | ||
); | ||
} | ||
|
||
@Test | ||
void get_withoutSchedule_shouldThrowException() { | ||
// Given | ||
ControllerResourceConfig config = ControllerResourceConfig.builder() | ||
.controllerName("testing") | ||
.restartPolicy("Never") | ||
.volumes(volumes) | ||
.build(); | ||
|
||
// When & Then | ||
assertThatIllegalArgumentException() | ||
.isThrownBy(() -> cronJobHandler.get(config, images)) | ||
.withMessage("No schedule is specified!"); | ||
} | ||
|
||
@Test | ||
void get_withInvalidControllerName_shouldThrowException() { | ||
// Given | ||
ControllerResourceConfig config = ControllerResourceConfig.builder() | ||
.schedule("* * * * *") | ||
.imagePullPolicy("IfNotPresent") | ||
.controllerName("TesTing") | ||
.volumes(volumes) | ||
.build(); | ||
// When & Then | ||
assertThatIllegalArgumentException() | ||
.isThrownBy(() -> cronJobHandler.get(config, images)) | ||
.withMessageStartingWith("Invalid upper case letter 'T'") | ||
.withMessageEndingWith("controller name value: TesTing"); | ||
} | ||
|
||
@Test | ||
void get_withoutControllerName_shouldThrowException() { | ||
// Given | ||
ControllerResourceConfig config = ControllerResourceConfig.builder() | ||
.schedule("* * * * *") | ||
.imagePullPolicy("IfNotPresent") | ||
.volumes(volumes) | ||
.build(); | ||
// When & Then | ||
assertThatIllegalArgumentException() | ||
.isThrownBy(() -> cronJobHandler.get(config, images)) | ||
.withMessage("No controller name is specified!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters