Search in sources :

Example 1 with JobTemplateSpec

use of io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec in project yakc by manusa.

the class CronJobs method trigger.

// https://github.com/kubernetes/kubernetes/blob/ff4234720d4ec390ca6f6aac4c19a21ba6661841/staging/src/k8s.io/kubectl/pkg/cmd/create/create_job.go#L263
private int trigger(KubernetesClient kc) throws IOException {
    if (params == null || params.length != 1) {
        error("Missing required param <name>");
        return 1;
    }
    final String name = params[0];
    final String applicableNamespace = namespace(kc);
    info("Triggering CronJob " + applicableNamespace + "/" + name);
    final CronJob cronJob = kc.create(BatchV1beta1Api.class).readNamespacedCronJob(name, applicableNamespace).get();
    final JobTemplateSpec jts = cronJob.getSpec().getJobTemplate();
    final String jobName = String.format("%s-manual-%s", name.length() > 38 ? name.substring(0, 38) : name, new Random().nextInt(999999));
    kc.create(BatchV1Api.class).createNamespacedJob(applicableNamespace, Job.builder().metadata(ObjectMeta.builder().name(jobName).namespace(applicableNamespace).labels(new HashMap<>(Optional.ofNullable(cronJob.getMetadata().getLabels()).orElse(Collections.emptyMap()))).putInAnnotations("cronjob.kubernetes.io/instantiate", "manual").addToOwnerReferences(OwnerReference.builder().kind(cronJob.getKind()).apiVersion(cronJob.getApiVersion()).controller(false).name(cronJob.getMetadata().getName()).uid(cronJob.getMetadata().getUid()).build()).build()).spec(jts.getSpec()).build()).get();
    info(" - Created manual Job: " + jobName);
    return 0;
}
Also used : Random(java.util.Random) CronJob(com.marcnuri.yakc.model.io.k8s.api.batch.v1beta1.CronJob) JobTemplateSpec(com.marcnuri.yakc.model.io.k8s.api.batch.v1beta1.JobTemplateSpec)

Example 2 with JobTemplateSpec

use of io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec in project yakc by manusa.

the class CronJobService method trigger.

public Job trigger(String name, String namespace) throws IOException {
    final CronJob cronJob = kubernetesClient.create(BatchV1beta1Api.class).readNamespacedCronJob(name, namespace).get();
    final JobTemplateSpec jts = cronJob.getSpec().getJobTemplate();
    final String jobName = String.format("%s-manual-%s", name.length() > 38 ? name.substring(0, 38) : name, new Random().nextInt(999999));
    return kubernetesClient.create(BatchV1Api.class).createNamespacedJob(namespace, Job.builder().metadata(ObjectMeta.builder().name(jobName).namespace(namespace).labels(new HashMap<>(Optional.ofNullable(cronJob.getMetadata().getLabels()).orElse(Collections.emptyMap()))).putInAnnotations("cronjob.kubernetes.io/instantiate", "manual").addToOwnerReferences(OwnerReference.builder().kind(cronJob.getKind()).apiVersion(cronJob.getApiVersion()).controller(false).name(cronJob.getMetadata().getName()).uid(cronJob.getMetadata().getUid()).build()).build()).spec(jts.getSpec()).build()).get();
}
Also used : Random(java.util.Random) CronJob(com.marcnuri.yakc.model.io.k8s.api.batch.v1beta1.CronJob) JobTemplateSpec(com.marcnuri.yakc.model.io.k8s.api.batch.v1beta1.JobTemplateSpec)

Example 3 with JobTemplateSpec

use of io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec in project stackgres by ongres.

the class DbOpsAnnotationDecorator method decorateCronJob.

protected void decorateCronJob(@NotNull StackGresDbOpsContext context, @NotNull HasMetadata resource) {
    CronJob cronJob = (CronJob) resource;
    Map<String, String> jobTemplateAnnotations = Optional.ofNullable(cronJob.getSpec()).map(CronJobSpec::getJobTemplate).map(JobTemplateSpec::getMetadata).map(ObjectMeta::getAnnotations).orElse(new HashMap<>());
    jobTemplateAnnotations.putAll(getAllResourcesAnnotations(context));
    Optional.ofNullable(cronJob.getSpec()).map(CronJobSpec::getJobTemplate).ifPresent(template -> {
        final ObjectMeta metadata = Optional.ofNullable(template.getMetadata()).orElse(new ObjectMeta());
        metadata.setAnnotations(jobTemplateAnnotations);
        template.setMetadata(metadata);
        Map<String, String> cronJobPodTemplateAnnotations = Optional.ofNullable(cronJob.getSpec()).map(CronJobSpec::getJobTemplate).map(JobTemplateSpec::getSpec).map(JobSpec::getTemplate).map(PodTemplateSpec::getMetadata).map(ObjectMeta::getAnnotations).orElse(new HashMap<>());
        cronJobPodTemplateAnnotations.putAll(getPodAnnotations(context));
        Optional.ofNullable(template.getSpec()).map(JobSpec::getTemplate).ifPresent(podTemplate -> {
            final ObjectMeta podTemplateMetadata = Optional.ofNullable(podTemplate.getMetadata()).orElse(new ObjectMeta());
            podTemplateMetadata.setAnnotations(cronJobPodTemplateAnnotations);
            podTemplate.setMetadata(podTemplateMetadata);
        });
    });
    decorateResource(cronJob, getAllResourcesAnnotations(context));
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) JobTemplateSpec(io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec)

Example 4 with JobTemplateSpec

use of io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec in project stackgres by ongres.

the class ClusterAnnotationDecoratorTest method allResourcesAnnotations_shouldBePresentInCronJobsPodTemplate.

@Test
void allResourcesAnnotations_shouldBePresentInCronJobsPodTemplate() {
    String allResourceAnnotationKey = StringUtil.generateRandom(8);
    String allResourceAnnotationValue = StringUtil.generateRandom(8);
    defaultCluster.getSpec().setPod(null);
    defaultCluster.getSpec().setPostgresServices(null);
    defaultCluster.getSpec().getMetadata().setAnnotations(new StackGresClusterSpecAnnotations());
    defaultCluster.getSpec().getMetadata().getAnnotations().setAllResources(ImmutableMap.of(allResourceAnnotationKey, allResourceAnnotationValue));
    annotationDecorator.decorate(context, resources);
    Map<String, String> expected = Map.of(allResourceAnnotationKey, allResourceAnnotationValue);
    resources.stream().filter(CronJob.class::isInstance).map(CronJob.class::cast).forEach(cronJob -> {
        final JobTemplateSpec jobTemplate = cronJob.getSpec().getJobTemplate();
        checkResourceAnnotations(jobTemplate, expected);
        PodTemplateSpec template = jobTemplate.getSpec().getTemplate();
        checkResourceAnnotations(template, expected);
    });
}
Also used : PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) StackGresClusterSpecAnnotations(io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations) CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) JobTemplateSpec(io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec) Test(org.junit.jupiter.api.Test)

Example 5 with JobTemplateSpec

use of io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec in project stackgres by ongres.

the class BackupAnnotationDecorator method decorateCronJob.

protected void decorateCronJob(@NotNull StackGresBackupContext context, @NotNull HasMetadata resource) {
    CronJob cronJob = (CronJob) resource;
    Map<String, String> jobTemplateAnnotations = Optional.ofNullable(cronJob.getSpec()).map(CronJobSpec::getJobTemplate).map(JobTemplateSpec::getMetadata).map(ObjectMeta::getAnnotations).orElse(new HashMap<>());
    jobTemplateAnnotations.putAll(getAllResourcesAnnotations(context));
    Optional.ofNullable(cronJob.getSpec()).map(CronJobSpec::getJobTemplate).ifPresent(template -> {
        final ObjectMeta metadata = Optional.ofNullable(template.getMetadata()).orElse(new ObjectMeta());
        metadata.setAnnotations(jobTemplateAnnotations);
        template.setMetadata(metadata);
        Map<String, String> cronJobPodTemplateAnnotations = Optional.ofNullable(cronJob.getSpec()).map(CronJobSpec::getJobTemplate).map(JobTemplateSpec::getSpec).map(JobSpec::getTemplate).map(PodTemplateSpec::getMetadata).map(ObjectMeta::getAnnotations).orElse(new HashMap<>());
        cronJobPodTemplateAnnotations.putAll(getPodAnnotations(context));
        Optional.ofNullable(template.getSpec()).map(JobSpec::getTemplate).ifPresent(podTemplate -> {
            final ObjectMeta podTemplateMetadata = Optional.ofNullable(podTemplate.getMetadata()).orElse(new ObjectMeta());
            podTemplateMetadata.setAnnotations(cronJobPodTemplateAnnotations);
            podTemplate.setMetadata(podTemplateMetadata);
        });
    });
    decorateResource(cronJob, getAllResourcesAnnotations(context));
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) CronJob(io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob) JobTemplateSpec(io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec)

Aggregations

PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)4 CronJob (io.fabric8.kubernetes.api.model.batch.v1beta1.CronJob)4 JobTemplateSpec (io.fabric8.kubernetes.api.model.batch.v1beta1.JobTemplateSpec)4 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)3 CronJob (com.marcnuri.yakc.model.io.k8s.api.batch.v1beta1.CronJob)2 JobTemplateSpec (com.marcnuri.yakc.model.io.k8s.api.batch.v1beta1.JobTemplateSpec)2 Random (java.util.Random)2 StackGresClusterSpecAnnotations (io.stackgres.common.crd.sgcluster.StackGresClusterSpecAnnotations)1 Test (org.junit.jupiter.api.Test)1