Merge pull request #124883 from ardaguclu/create-cronjobs-role-fix
kubectl create cronjobs: Manually set OwnerReferences
This commit is contained in:
		@@ -35,6 +35,7 @@ import (
 | 
				
			|||||||
	"k8s.io/kubectl/pkg/util"
 | 
						"k8s.io/kubectl/pkg/util"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/util/i18n"
 | 
						"k8s.io/kubectl/pkg/util/i18n"
 | 
				
			||||||
	"k8s.io/kubectl/pkg/util/templates"
 | 
						"k8s.io/kubectl/pkg/util/templates"
 | 
				
			||||||
 | 
						"k8s.io/utils/ptr"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
@@ -264,7 +265,18 @@ func (o *CreateJobOptions) createJobFromCronJob(cronJob *batchv1.CronJob) *batch
 | 
				
			|||||||
			Name:        o.Name,
 | 
								Name:        o.Name,
 | 
				
			||||||
			Annotations: annotations,
 | 
								Annotations: annotations,
 | 
				
			||||||
			Labels:      cronJob.Spec.JobTemplate.Labels,
 | 
								Labels:      cronJob.Spec.JobTemplate.Labels,
 | 
				
			||||||
			OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cronJob, batchv1.SchemeGroupVersion.WithKind("CronJob"))},
 | 
								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,
 | 
							Spec: cronJob.Spec.JobTemplate.Spec,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,6 +24,7 @@ import (
 | 
				
			|||||||
	corev1 "k8s.io/api/core/v1"
 | 
						corev1 "k8s.io/api/core/v1"
 | 
				
			||||||
	apiequality "k8s.io/apimachinery/pkg/api/equality"
 | 
						apiequality "k8s.io/apimachinery/pkg/api/equality"
 | 
				
			||||||
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
						metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
				
			||||||
 | 
						"k8s.io/utils/ptr"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestCreateJobValidation(t *testing.T) {
 | 
					func TestCreateJobValidation(t *testing.T) {
 | 
				
			||||||
@@ -163,7 +164,15 @@ func TestCreateJobFromCronJob(t *testing.T) {
 | 
				
			|||||||
				ObjectMeta: metav1.ObjectMeta{
 | 
									ObjectMeta: metav1.ObjectMeta{
 | 
				
			||||||
					Name:        jobName,
 | 
										Name:        jobName,
 | 
				
			||||||
					Annotations: map[string]string{"cronjob.kubernetes.io/instantiate": "manual"},
 | 
										Annotations: map[string]string{"cronjob.kubernetes.io/instantiate": "manual"},
 | 
				
			||||||
					OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cronJob, batchv1.SchemeGroupVersion.WithKind("CronJob"))},
 | 
										OwnerReferences: []metav1.OwnerReference{
 | 
				
			||||||
 | 
											{
 | 
				
			||||||
 | 
												APIVersion: batchv1.SchemeGroupVersion.String(),
 | 
				
			||||||
 | 
												Kind:       "CronJob",
 | 
				
			||||||
 | 
												Name:       cronJob.GetName(),
 | 
				
			||||||
 | 
												UID:        cronJob.GetUID(),
 | 
				
			||||||
 | 
												Controller: ptr.To(true),
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
				Spec: batchv1.JobSpec{
 | 
									Spec: batchv1.JobSpec{
 | 
				
			||||||
					Template: corev1.PodTemplateSpec{
 | 
										Template: corev1.PodTemplateSpec{
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user