diff --git a/api/openapi-spec/swagger.json b/api/openapi-spec/swagger.json index fe48ff6d4ec..b564442bb2a 100644 --- a/api/openapi-spec/swagger.json +++ b/api/openapi-spec/swagger.json @@ -4814,7 +4814,7 @@ "type": "integer" }, "managedBy": { - "description": "ManagedBy field indicates the controller that manages a Job. The k8s Job controller reconciles jobs which don't have this field at all or the field value is the reserved string `kubernetes.io/job-controller`, but skips reconciling Jobs with a custom value for this field. The value must be a valid domain-prefixed path (e.g. acme.io/foo) - all characters before the first \"/\" must be a valid subdomain as defined by RFC 1123. All characters trailing the first \"/\" must be valid HTTP Path characters as defined by RFC 3986. The value cannot exceed 63 characters. This field is immutable.\n\nThis field is alpha-level. The job controller accepts setting the field when the feature gate JobManagedBy is enabled (disabled by default).", + "description": "ManagedBy field indicates the controller that manages a Job. The k8s Job controller reconciles jobs which don't have this field at all or the field value is the reserved string `kubernetes.io/job-controller`, but skips reconciling Jobs with a custom value for this field. The value must be a valid domain-prefixed path (e.g. acme.io/foo) - all characters before the first \"/\" must be a valid subdomain as defined by RFC 1123. All characters trailing the first \"/\" must be valid HTTP Path characters as defined by RFC 3986. The value cannot exceed 63 characters. This field is immutable.\n\nThis field is beta-level. The job controller accepts setting the field when the feature gate JobManagedBy is enabled (enabled by default).", "type": "string" }, "manualSelector": { diff --git a/api/openapi-spec/v3/apis__batch__v1_openapi.json b/api/openapi-spec/v3/apis__batch__v1_openapi.json index 5b1270365ff..a37d481afc7 100644 --- a/api/openapi-spec/v3/apis__batch__v1_openapi.json +++ b/api/openapi-spec/v3/apis__batch__v1_openapi.json @@ -345,7 +345,7 @@ "type": "integer" }, "managedBy": { - "description": "ManagedBy field indicates the controller that manages a Job. The k8s Job controller reconciles jobs which don't have this field at all or the field value is the reserved string `kubernetes.io/job-controller`, but skips reconciling Jobs with a custom value for this field. The value must be a valid domain-prefixed path (e.g. acme.io/foo) - all characters before the first \"/\" must be a valid subdomain as defined by RFC 1123. All characters trailing the first \"/\" must be valid HTTP Path characters as defined by RFC 3986. The value cannot exceed 63 characters. This field is immutable.\n\nThis field is alpha-level. The job controller accepts setting the field when the feature gate JobManagedBy is enabled (disabled by default).", + "description": "ManagedBy field indicates the controller that manages a Job. The k8s Job controller reconciles jobs which don't have this field at all or the field value is the reserved string `kubernetes.io/job-controller`, but skips reconciling Jobs with a custom value for this field. The value must be a valid domain-prefixed path (e.g. acme.io/foo) - all characters before the first \"/\" must be a valid subdomain as defined by RFC 1123. All characters trailing the first \"/\" must be valid HTTP Path characters as defined by RFC 3986. The value cannot exceed 63 characters. This field is immutable.\n\nThis field is beta-level. The job controller accepts setting the field when the feature gate JobManagedBy is enabled (enabled by default).", "type": "string" }, "manualSelector": { diff --git a/pkg/apis/batch/types.go b/pkg/apis/batch/types.go index 19ca116208b..aa9874507db 100644 --- a/pkg/apis/batch/types.go +++ b/pkg/apis/batch/types.go @@ -477,8 +477,8 @@ type JobSpec struct { // characters as defined by RFC 3986. The value cannot exceed 63 characters. // This field is immutable. // - // This field is alpha-level. The job controller accepts setting the field - // when the feature gate JobManagedBy is enabled (disabled by default). + // This field is beta-level. The job controller accepts setting the field + // when the feature gate JobManagedBy is enabled (enabled by default). // +optional ManagedBy *string } diff --git a/pkg/controller/job/job_controller_test.go b/pkg/controller/job/job_controller_test.go index 32e8912f47c..7e7fba1f95d 100644 --- a/pkg/controller/job/job_controller_test.go +++ b/pkg/controller/job/job_controller_test.go @@ -3087,6 +3087,7 @@ func TestSyncJobWithJobPodFailurePolicy(t *testing.T) { testCases := map[string]struct { enableJobPodReplacementPolicy bool + enableJobManagedBy bool job batch.Job pods []v1.Pod wantConditions []batch.JobCondition @@ -3420,6 +3421,56 @@ func TestSyncJobWithJobPodFailurePolicy(t *testing.T) { wantStatusFailed: 2, wantStatusSucceeded: 0, }, + "fail job with multiple pods; JobManagedBy enabled delays setting terminal condition": { + enableJobManagedBy: true, + job: batch.Job{ + TypeMeta: metav1.TypeMeta{Kind: "Job"}, + ObjectMeta: validObjectMeta, + Spec: batch.JobSpec{ + Selector: validSelector, + Template: validTemplate, + Parallelism: ptr.To[int32](2), + Completions: ptr.To[int32](2), + BackoffLimit: ptr.To[int32](6), + PodFailurePolicy: &batch.PodFailurePolicy{ + Rules: onExitCodeRules, + }, + }, + }, + pods: []v1.Pod{ + { + Status: v1.PodStatus{ + Phase: v1.PodRunning, + }, + }, + { + Status: v1.PodStatus{ + Phase: v1.PodFailed, + ContainerStatuses: []v1.ContainerStatus{ + { + Name: "main-container", + State: v1.ContainerState{ + Terminated: &v1.ContainerStateTerminated{ + ExitCode: 5, + }, + }, + }, + }, + }, + }, + }, + wantConditions: []batch.JobCondition{ + { + Type: batch.JobFailureTarget, + Status: v1.ConditionTrue, + Reason: batch.JobReasonPodFailurePolicy, + Message: "Container main-container for pod default/mypod-1 failed with exit code 5 matching FailJob rule at index 1", + }, + }, + wantStatusActive: 0, + wantStatusFailed: 2, + wantStatusSucceeded: 0, + }, "fail indexed job based on OnExitCodes": { job: batch.Job{ TypeMeta: metav1.TypeMeta{Kind: "Job"}, @@ -3759,6 +3810,56 @@ func TestSyncJobWithJobPodFailurePolicy(t *testing.T) { wantStatusFailed: 1, wantStatusSucceeded: 0, }, + "default job based on OnExitCodes; JobManagedBy enabled triggers adding interim condition": { + enableJobManagedBy: true, + job: batch.Job{ + TypeMeta: metav1.TypeMeta{Kind: "Job"}, + ObjectMeta: validObjectMeta, + Spec: batch.JobSpec{ + Selector: validSelector, + Template: validTemplate, + Parallelism: ptr.To[int32](1), + Completions: ptr.To[int32](1), + BackoffLimit: ptr.To[int32](0), + PodFailurePolicy: &batch.PodFailurePolicy{ + Rules: onExitCodeRules, + }, + }, + }, + pods: []v1.Pod{ + { + Status: v1.PodStatus{ + Phase: v1.PodFailed, + ContainerStatuses: []v1.ContainerStatus{ + { + State: v1.ContainerState{ + Terminated: &v1.ContainerStateTerminated{ + ExitCode: 10, + }, + }, + }, + }, + }, + }, + }, + wantConditions: []batch.JobCondition{ + { + Type: batch.JobFailureTarget, + Status: v1.ConditionTrue, + Reason: batch.JobReasonBackoffLimitExceeded, + Message: "Job has reached the specified backoff limit", + }, + { + Type: batch.JobFailed, + Status: v1.ConditionTrue, + Reason: batch.JobReasonBackoffLimitExceeded, + Message: "Job has reached the specified backoff limit", + }, + }, + wantStatusActive: 0, + wantStatusFailed: 1, + wantStatusSucceeded: 0, + }, "count pod failure based on OnExitCodes; both rules are matching, the first is executed only": { job: batch.Job{ TypeMeta: metav1.TypeMeta{Kind: "Job"}, @@ -4057,6 +4158,7 @@ func TestSyncJobWithJobPodFailurePolicy(t *testing.T) { for name, tc := range testCases { t.Run(name, func(t *testing.T) { featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, features.JobPodReplacementPolicy, tc.enableJobPodReplacementPolicy) + featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, features.JobManagedBy, tc.enableJobManagedBy) if tc.job.Spec.PodReplacementPolicy == nil { tc.job.Spec.PodReplacementPolicy = podReplacementPolicy(batch.Failed) diff --git a/pkg/features/kube_features.go b/pkg/features/kube_features.go index d7fd604e1dd..f342c728a74 100644 --- a/pkg/features/kube_features.go +++ b/pkg/features/kube_features.go @@ -264,6 +264,8 @@ const ( // owner: @mimowo // kep: https://kep.k8s.io/4368 + // alpha: v1.30 + // beta: v1.32 // // Allows to delegate reconciliation of a Job object to an external controller. JobManagedBy featuregate.Feature = "JobManagedBy" diff --git a/pkg/features/versioned_kube_features.go b/pkg/features/versioned_kube_features.go index 7db027c4614..fee9fa53829 100644 --- a/pkg/features/versioned_kube_features.go +++ b/pkg/features/versioned_kube_features.go @@ -391,6 +391,7 @@ var defaultVersionedKubernetesFeatureGates = map[featuregate.Feature]featuregate JobManagedBy: { {Version: version.MustParse("1.30"), Default: false, PreRelease: featuregate.Alpha}, + {Version: version.MustParse("1.32"), Default: true, PreRelease: featuregate.Beta}, }, JobPodFailurePolicy: { diff --git a/pkg/generated/openapi/zz_generated.openapi.go b/pkg/generated/openapi/zz_generated.openapi.go index 840c0c1c6af..10e872f0728 100644 --- a/pkg/generated/openapi/zz_generated.openapi.go +++ b/pkg/generated/openapi/zz_generated.openapi.go @@ -17300,7 +17300,7 @@ func schema_k8sio_api_batch_v1_JobSpec(ref common.ReferenceCallback) common.Open }, "managedBy": { SchemaProps: spec.SchemaProps{ - Description: "ManagedBy field indicates the controller that manages a Job. The k8s Job controller reconciles jobs which don't have this field at all or the field value is the reserved string `kubernetes.io/job-controller`, but skips reconciling Jobs with a custom value for this field. The value must be a valid domain-prefixed path (e.g. acme.io/foo) - all characters before the first \"/\" must be a valid subdomain as defined by RFC 1123. All characters trailing the first \"/\" must be valid HTTP Path characters as defined by RFC 3986. The value cannot exceed 63 characters. This field is immutable.\n\nThis field is alpha-level. The job controller accepts setting the field when the feature gate JobManagedBy is enabled (disabled by default).", + Description: "ManagedBy field indicates the controller that manages a Job. The k8s Job controller reconciles jobs which don't have this field at all or the field value is the reserved string `kubernetes.io/job-controller`, but skips reconciling Jobs with a custom value for this field. The value must be a valid domain-prefixed path (e.g. acme.io/foo) - all characters before the first \"/\" must be a valid subdomain as defined by RFC 1123. All characters trailing the first \"/\" must be valid HTTP Path characters as defined by RFC 3986. The value cannot exceed 63 characters. This field is immutable.\n\nThis field is beta-level. The job controller accepts setting the field when the feature gate JobManagedBy is enabled (enabled by default).", Type: []string{"string"}, Format: "", }, diff --git a/staging/src/k8s.io/api/batch/v1/generated.proto b/staging/src/k8s.io/api/batch/v1/generated.proto index f5a9385f5e0..361ebdca124 100644 --- a/staging/src/k8s.io/api/batch/v1/generated.proto +++ b/staging/src/k8s.io/api/batch/v1/generated.proto @@ -350,8 +350,8 @@ message JobSpec { // characters as defined by RFC 3986. The value cannot exceed 63 characters. // This field is immutable. // - // This field is alpha-level. The job controller accepts setting the field - // when the feature gate JobManagedBy is enabled (disabled by default). + // This field is beta-level. The job controller accepts setting the field + // when the feature gate JobManagedBy is enabled (enabled by default). // +optional optional string managedBy = 15; } diff --git a/staging/src/k8s.io/api/batch/v1/types.go b/staging/src/k8s.io/api/batch/v1/types.go index b42ec231e45..30b79ff969a 100644 --- a/staging/src/k8s.io/api/batch/v1/types.go +++ b/staging/src/k8s.io/api/batch/v1/types.go @@ -480,8 +480,8 @@ type JobSpec struct { // characters as defined by RFC 3986. The value cannot exceed 63 characters. // This field is immutable. // - // This field is alpha-level. The job controller accepts setting the field - // when the feature gate JobManagedBy is enabled (disabled by default). + // This field is beta-level. The job controller accepts setting the field + // when the feature gate JobManagedBy is enabled (enabled by default). // +optional ManagedBy *string `json:"managedBy,omitempty" protobuf:"bytes,15,opt,name=managedBy"` } diff --git a/staging/src/k8s.io/api/batch/v1/types_swagger_doc_generated.go b/staging/src/k8s.io/api/batch/v1/types_swagger_doc_generated.go index d504887884a..893f3371f05 100644 --- a/staging/src/k8s.io/api/batch/v1/types_swagger_doc_generated.go +++ b/staging/src/k8s.io/api/batch/v1/types_swagger_doc_generated.go @@ -127,7 +127,7 @@ var map_JobSpec = map[string]string{ "completionMode": "completionMode specifies how Pod completions are tracked. It can be `NonIndexed` (default) or `Indexed`.\n\n`NonIndexed` means that the Job is considered complete when there have been .spec.completions successfully completed Pods. Each Pod completion is homologous to each other.\n\n`Indexed` means that the Pods of a Job get an associated completion index from 0 to (.spec.completions - 1), available in the annotation batch.kubernetes.io/job-completion-index. The Job is considered complete when there is one successfully completed Pod for each index. When value is `Indexed`, .spec.completions must be specified and `.spec.parallelism` must be less than or equal to 10^5. In addition, The Pod name takes the form `$(job-name)-$(index)-$(random-string)`, the Pod hostname takes the form `$(job-name)-$(index)`.\n\nMore completion modes can be added in the future. If the Job controller observes a mode that it doesn't recognize, which is possible during upgrades due to version skew, the controller skips updates for the Job.", "suspend": "suspend specifies whether the Job controller should create Pods or not. If a Job is created with suspend set to true, no Pods are created by the Job controller. If a Job is suspended after creation (i.e. the flag goes from false to true), the Job controller will delete all active Pods associated with this Job. Users must design their workload to gracefully handle this. Suspending a Job will reset the StartTime field of the Job, effectively resetting the ActiveDeadlineSeconds timer too. Defaults to false.", "podReplacementPolicy": "podReplacementPolicy specifies when to create replacement Pods. Possible values are: - TerminatingOrFailed means that we recreate pods\n when they are terminating (has a metadata.deletionTimestamp) or failed.\n- Failed means to wait until a previously created Pod is fully terminated (has phase\n Failed or Succeeded) before creating a replacement Pod.\n\nWhen using podFailurePolicy, Failed is the the only allowed value. TerminatingOrFailed and Failed are allowed values when podFailurePolicy is not in use. This is an beta field. To use this, enable the JobPodReplacementPolicy feature toggle. This is on by default.", - "managedBy": "ManagedBy field indicates the controller that manages a Job. The k8s Job controller reconciles jobs which don't have this field at all or the field value is the reserved string `kubernetes.io/job-controller`, but skips reconciling Jobs with a custom value for this field. The value must be a valid domain-prefixed path (e.g. acme.io/foo) - all characters before the first \"/\" must be a valid subdomain as defined by RFC 1123. All characters trailing the first \"/\" must be valid HTTP Path characters as defined by RFC 3986. The value cannot exceed 63 characters. This field is immutable.\n\nThis field is alpha-level. The job controller accepts setting the field when the feature gate JobManagedBy is enabled (disabled by default).", + "managedBy": "ManagedBy field indicates the controller that manages a Job. The k8s Job controller reconciles jobs which don't have this field at all or the field value is the reserved string `kubernetes.io/job-controller`, but skips reconciling Jobs with a custom value for this field. The value must be a valid domain-prefixed path (e.g. acme.io/foo) - all characters before the first \"/\" must be a valid subdomain as defined by RFC 1123. All characters trailing the first \"/\" must be valid HTTP Path characters as defined by RFC 3986. The value cannot exceed 63 characters. This field is immutable.\n\nThis field is beta-level. The job controller accepts setting the field when the feature gate JobManagedBy is enabled (enabled by default).", } func (JobSpec) SwaggerDoc() map[string]string { diff --git a/test/e2e/apps/job.go b/test/e2e/apps/job.go index 9d90d9d78e5..ebc9b5e9110 100644 --- a/test/e2e/apps/job.go +++ b/test/e2e/apps/job.go @@ -22,6 +22,7 @@ import ( "fmt" "math" "strconv" + "time" batchv1 "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" @@ -1237,6 +1238,32 @@ done`} err = e2ejob.WaitForJobReady(ctx, f.ClientSet, f.Namespace.Name, job.Name, ptr.To[int32](0)) framework.ExpectNoError(err, "failed to ensure job status ready field in namespace: %s", f.Namespace.Name) }) + + /* + Testname: Jobs, managed-by mechanism + Description: This test verifies the built-in Job controller does not + reconcile the Job, allowing to delegate the reconciliation to an + external controller. + */ + ginkgo.It("should allow to delegate reconciliation to external controller", func(ctx context.Context) { + parallelism := int32(2) + completions := int32(4) + backoffLimit := int32(6) + + ginkgo.By("Creating a job with a custom managed-by field") + job := e2ejob.NewTestJob("succeed", "managed-by", v1.RestartPolicyNever, parallelism, completions, nil, backoffLimit) + job.Spec.ManagedBy = ptr.To("example.com/custom-job-controller") + job, err := e2ejob.CreateJob(ctx, f.ClientSet, f.Namespace.Name, job) + framework.ExpectNoError(err, "failed to create job in namespace: %s/%s", job.Namespace, job.Name) + + ginkgo.By(fmt.Sprintf("Verify the Job %s/%s status isn't updated by the built-in controller", job.Namespace, job.Name)) + // Wait a little to give the built-in Job controller time to update the + // status (if it was enabled) + time.Sleep(time.Second) + job, err = e2ejob.GetJob(ctx, f.ClientSet, f.Namespace.Name, job.Name) + framework.ExpectNoError(err, "failed to get the latest object for the Job %s/%s", job.Namespace, job.Name) + gomega.Expect(job.Status).To(gomega.BeEquivalentTo(batchv1.JobStatus{}), "expected status for the Job %s/%s to be empty", job.Namespace, job.Name) + }) }) func updateJobSuspendWithRetries(ctx context.Context, f *framework.Framework, job *batchv1.Job, suspend *bool) error { diff --git a/test/featuregates_linter/test_data/versioned_feature_list.yaml b/test/featuregates_linter/test_data/versioned_feature_list.yaml index c0b8a1d17b0..66be919efac 100644 --- a/test/featuregates_linter/test_data/versioned_feature_list.yaml +++ b/test/featuregates_linter/test_data/versioned_feature_list.yaml @@ -494,6 +494,10 @@ lockToDefault: false preRelease: Alpha version: "1.30" + - default: true + lockToDefault: false + preRelease: Beta + version: "1.32" - name: JobPodFailurePolicy versionedSpecs: - default: false