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;
}
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();
}
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));
}
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);
});
}
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));
}
Aggregations