From 596b0a6d983657120bacf9b1d9482822a27460e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Wed, 15 May 2024 13:31:31 +0300 Subject: [PATCH] kubectl create cronjobs: Manually set OwnerReferences Kubernetes-commit: 398ab938efc1cf2eaf47f4d8da842892f212ce65 --- pkg/cmd/create/create_job.go | 20 ++++++++++++++++---- pkg/cmd/create/create_job_test.go | 15 ++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/create/create_job.go b/pkg/cmd/create/create_job.go index 93ba136d0..811720d4f 100644 --- a/pkg/cmd/create/create_job.go +++ b/pkg/cmd/create/create_job.go @@ -35,6 +35,7 @@ import ( "k8s.io/kubectl/pkg/util" "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" + "k8s.io/utils/ptr" ) var ( @@ -261,10 +262,21 @@ func (o *CreateJobOptions) createJobFromCronJob(cronJob *batchv1.CronJob) *batch // this is ok because we know exactly how we want to be serialized TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"}, ObjectMeta: metav1.ObjectMeta{ - Name: o.Name, - Annotations: annotations, - Labels: cronJob.Spec.JobTemplate.Labels, - OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cronJob, batchv1.SchemeGroupVersion.WithKind("CronJob"))}, + Name: o.Name, + Annotations: annotations, + Labels: cronJob.Spec.JobTemplate.Labels, + OwnerReferences: []metav1.OwnerReference{ + { + // we are not using metav1.NewControllerRef because it + // sets BlockOwnerDeletion to true which additionally mandates + // cronjobs/finalizer role and not backwards-compatible. + APIVersion: batchv1.SchemeGroupVersion.String(), + Kind: "CronJob", + Name: cronJob.GetName(), + UID: cronJob.GetUID(), + Controller: ptr.To(true), + }, + }, }, Spec: cronJob.Spec.JobTemplate.Spec, } diff --git a/pkg/cmd/create/create_job_test.go b/pkg/cmd/create/create_job_test.go index facab5bd1..4166265c2 100644 --- a/pkg/cmd/create/create_job_test.go +++ b/pkg/cmd/create/create_job_test.go @@ -24,6 +24,7 @@ import ( corev1 "k8s.io/api/core/v1" apiequality "k8s.io/apimachinery/pkg/api/equality" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" ) func TestCreateJobValidation(t *testing.T) { @@ -161,9 +162,17 @@ func TestCreateJobFromCronJob(t *testing.T) { expected: &batchv1.Job{ TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"}, ObjectMeta: metav1.ObjectMeta{ - Name: jobName, - Annotations: map[string]string{"cronjob.kubernetes.io/instantiate": "manual"}, - OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cronJob, batchv1.SchemeGroupVersion.WithKind("CronJob"))}, + Name: jobName, + Annotations: map[string]string{"cronjob.kubernetes.io/instantiate": "manual"}, + OwnerReferences: []metav1.OwnerReference{ + { + APIVersion: batchv1.SchemeGroupVersion.String(), + Kind: "CronJob", + Name: cronJob.GetName(), + UID: cronJob.GetUID(), + Controller: ptr.To(true), + }, + }, }, Spec: batchv1.JobSpec{ Template: corev1.PodTemplateSpec{