generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
managedJobsNamespaceSelector for Deployment, StatefulSets, and Pods (#…
…3765) * managedJobsNamespaceSelector for deployment and statefulset * normalize manageJobsWithoutQueueName logic in pod webhook * refactor to use common helper function * unit tests for WorkloadShouldBeSuspended * linter fixes * add test for job owned by kueue managed parent * bug fix in deployment/statefulset webhooks
- Loading branch information
1 parent
6df4a22
commit e5dd67a
Showing
6 changed files
with
224 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package jobframework | ||
|
||
import ( | ||
"testing" | ||
|
||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"sigs.k8s.io/kueue/pkg/features" | ||
utiltesting "sigs.k8s.io/kueue/pkg/util/testing" | ||
utiltestingjob "sigs.k8s.io/kueue/pkg/util/testingjobs/job" | ||
) | ||
|
||
func TestWorkloadShouldBeSuspended(t *testing.T) { | ||
t.Cleanup(EnableIntegrationsForTest(t, "batch/job")) | ||
managedNamespace := &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "managed-ns", | ||
Labels: map[string]string{"kubernetes.io/metadata.name": "managed-ns"}, | ||
}, | ||
} | ||
unmanagedNamespace := &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "unmanaged-ns", | ||
Labels: map[string]string{"kubernetes.io/metadata.name": "unmanaged-ns"}, | ||
}, | ||
} | ||
parent := utiltestingjob.MakeJob("parent", managedNamespace.Name).Queue("default").Obj() | ||
ls := &metav1.LabelSelector{ | ||
MatchExpressions: []metav1.LabelSelectorRequirement{ | ||
{ | ||
Key: "kubernetes.io/metadata.name", | ||
Operator: metav1.LabelSelectorOpNotIn, | ||
Values: []string{unmanagedNamespace.Name}, | ||
}, | ||
}, | ||
} | ||
namespaceSelector, _ := metav1.LabelSelectorAsSelector(ls) | ||
|
||
cases := map[string]struct { | ||
obj client.Object | ||
manageJobsWithoutQueueName bool | ||
featureGateEnabled bool | ||
wantSuspend bool | ||
}{ | ||
"job with queue name ": { | ||
obj: utiltestingjob.MakeJob("test-job", managedNamespace.Name).Queue("default").Obj(), | ||
manageJobsWithoutQueueName: false, | ||
featureGateEnabled: true, | ||
wantSuspend: true, | ||
}, | ||
"job with queue name manageJobs": { | ||
obj: utiltestingjob.MakeJob("test-job", managedNamespace.Name).Queue("default").Obj(), | ||
manageJobsWithoutQueueName: true, | ||
featureGateEnabled: true, | ||
wantSuspend: true, | ||
}, | ||
"job without queue name": { | ||
obj: utiltestingjob.MakeJob("test-job", managedNamespace.Name).Obj(), | ||
manageJobsWithoutQueueName: false, | ||
featureGateEnabled: true, | ||
wantSuspend: false, | ||
}, | ||
"job without queue name with manageJobs": { | ||
obj: utiltestingjob.MakeJob("test-job", managedNamespace.Name).Obj(), | ||
manageJobsWithoutQueueName: true, | ||
featureGateEnabled: true, | ||
wantSuspend: true, | ||
}, | ||
"job without queue name but with managed parent with manageJobs": { | ||
obj: utiltestingjob.MakeJob("test-job", managedNamespace.Name). | ||
OwnerReference(parent.Name, batchv1.SchemeGroupVersion.WithKind("Job")). | ||
Obj(), | ||
manageJobsWithoutQueueName: true, | ||
featureGateEnabled: true, | ||
wantSuspend: false, | ||
}, | ||
"job without queue name with manageJobs with feature disabled": { | ||
obj: utiltestingjob.MakeJob("test-job", managedNamespace.Name).Obj(), | ||
manageJobsWithoutQueueName: true, | ||
featureGateEnabled: false, | ||
wantSuspend: true, | ||
}, | ||
"job without queue name with manageJobs in unmanaged ns": { | ||
obj: utiltestingjob.MakeJob("test-job", unmanagedNamespace.Name).Obj(), | ||
manageJobsWithoutQueueName: true, | ||
featureGateEnabled: true, | ||
wantSuspend: false, | ||
}, | ||
"job without queue name with manageJobs in unmanaged ns with feature disabled": { | ||
obj: utiltestingjob.MakeJob("test-job", unmanagedNamespace.Name).Obj(), | ||
manageJobsWithoutQueueName: true, | ||
featureGateEnabled: false, | ||
wantSuspend: true, | ||
}, | ||
} | ||
|
||
for tcName, tc := range cases { | ||
t.Run(tcName, func(t *testing.T) { | ||
builder := utiltesting.NewClientBuilder() | ||
builder.WithObjects(managedNamespace, unmanagedNamespace, tc.obj, parent) | ||
client := builder.Build() | ||
ctx, _ := utiltesting.ContextWithLog(t) | ||
|
||
features.SetFeatureGateDuringTest(t, features.ManagedJobsNamespaceSelector, tc.featureGateEnabled) | ||
suspend, err := WorkloadShouldBeSuspended(ctx, tc.obj, client, tc.manageJobsWithoutQueueName, namespaceSelector) | ||
if err != nil { | ||
t.Errorf("Got error: %v", err) | ||
} | ||
if suspend != tc.wantSuspend { | ||
t.Errorf("Unexpected result: got %v wanted %v", suspend, tc.wantSuspend) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters