Skip to content

Commit 596b0a6

Browse files
ardagucluk8s-publishing-bot
authored andcommitted
kubectl create cronjobs: Manually set OwnerReferences
Kubernetes-commit: 398ab938efc1cf2eaf47f4d8da842892f212ce65
1 parent 0fe334a commit 596b0a6

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

pkg/cmd/create/create_job.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/kubectl/pkg/util"
3636
"k8s.io/kubectl/pkg/util/i18n"
3737
"k8s.io/kubectl/pkg/util/templates"
38+
"k8s.io/utils/ptr"
3839
)
3940

4041
var (
@@ -261,10 +262,21 @@ func (o *CreateJobOptions) createJobFromCronJob(cronJob *batchv1.CronJob) *batch
261262
// this is ok because we know exactly how we want to be serialized
262263
TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"},
263264
ObjectMeta: metav1.ObjectMeta{
264-
Name: o.Name,
265-
Annotations: annotations,
266-
Labels: cronJob.Spec.JobTemplate.Labels,
267-
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cronJob, batchv1.SchemeGroupVersion.WithKind("CronJob"))},
265+
Name: o.Name,
266+
Annotations: annotations,
267+
Labels: cronJob.Spec.JobTemplate.Labels,
268+
OwnerReferences: []metav1.OwnerReference{
269+
{
270+
// we are not using metav1.NewControllerRef because it
271+
// sets BlockOwnerDeletion to true which additionally mandates
272+
// cronjobs/finalizer role and not backwards-compatible.
273+
APIVersion: batchv1.SchemeGroupVersion.String(),
274+
Kind: "CronJob",
275+
Name: cronJob.GetName(),
276+
UID: cronJob.GetUID(),
277+
Controller: ptr.To(true),
278+
},
279+
},
268280
},
269281
Spec: cronJob.Spec.JobTemplate.Spec,
270282
}

pkg/cmd/create/create_job_test.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
corev1 "k8s.io/api/core/v1"
2525
apiequality "k8s.io/apimachinery/pkg/api/equality"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/utils/ptr"
2728
)
2829

2930
func TestCreateJobValidation(t *testing.T) {
@@ -161,9 +162,17 @@ func TestCreateJobFromCronJob(t *testing.T) {
161162
expected: &batchv1.Job{
162163
TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"},
163164
ObjectMeta: metav1.ObjectMeta{
164-
Name: jobName,
165-
Annotations: map[string]string{"cronjob.kubernetes.io/instantiate": "manual"},
166-
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cronJob, batchv1.SchemeGroupVersion.WithKind("CronJob"))},
165+
Name: jobName,
166+
Annotations: map[string]string{"cronjob.kubernetes.io/instantiate": "manual"},
167+
OwnerReferences: []metav1.OwnerReference{
168+
{
169+
APIVersion: batchv1.SchemeGroupVersion.String(),
170+
Kind: "CronJob",
171+
Name: cronJob.GetName(),
172+
UID: cronJob.GetUID(),
173+
Controller: ptr.To(true),
174+
},
175+
},
167176
},
168177
Spec: batchv1.JobSpec{
169178
Template: corev1.PodTemplateSpec{

0 commit comments

Comments
 (0)