generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow mutating queue name in StatefulSet Webhook. #3520
Open
mbobrovskyi
wants to merge
1
commit into
kubernetes-sigs:main
Choose a base branch
from
epam:fix/manageJobsWithoutQueueName
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,7 +83,6 @@ rules: | |
- apps | ||
resources: | ||
- replicasets | ||
- statefulsets | ||
verbs: | ||
- get | ||
- list | ||
|
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 |
---|---|---|
|
@@ -82,7 +82,6 @@ rules: | |
- apps | ||
resources: | ||
- replicasets | ||
- statefulsets | ||
verbs: | ||
- get | ||
- list | ||
|
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
110 changes: 110 additions & 0 deletions
110
pkg/controller/jobs/statefulset/statefulset_pod_reconciler.go
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,110 @@ | ||
/* | ||
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 statefulset | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/tools/record" | ||
"k8s.io/klog/v2" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"sigs.k8s.io/kueue/pkg/controller/jobframework" | ||
podcontroller "sigs.k8s.io/kueue/pkg/controller/jobs/pod" | ||
clientutil "sigs.k8s.io/kueue/pkg/util/client" | ||
) | ||
|
||
type PodReconciler struct { | ||
client client.Client | ||
} | ||
|
||
func NewPodReconciler(client client.Client, _ record.EventRecorder, _ ...jobframework.Option) jobframework.JobReconcilerInterface { | ||
return &PodReconciler{client: client} | ||
} | ||
|
||
var _ jobframework.JobReconcilerInterface = (*PodReconciler)(nil) | ||
|
||
func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
ctrl.Log.V(3).Info("Setting up Pod reconciler for StatefulSet") | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&corev1.Pod{}). | ||
Named("statefulset-pod"). | ||
WithEventFilter(r). | ||
Complete(r) | ||
} | ||
|
||
func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { | ||
pod := &corev1.Pod{} | ||
err := r.client.Get(ctx, req.NamespacedName, pod) | ||
if err != nil { | ||
// we'll ignore not-found errors, since there is nothing to do. | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
log := ctrl.LoggerFrom(ctx).WithValues("pod", klog.KObj(pod)) | ||
ctx = ctrl.LoggerInto(ctx, log) | ||
log.V(2).Info("Reconciling StatefulSet Pod") | ||
|
||
if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodFailed { | ||
err = client.IgnoreNotFound(clientutil.Patch(ctx, r.client, pod, true, func() (bool, error) { | ||
removed := controllerutil.RemoveFinalizer(pod, podcontroller.PodFinalizer) | ||
if removed { | ||
log.V(3).Info( | ||
"Finalizing statefulset pod in group", | ||
"pod", klog.KObj(pod), | ||
"group", pod.Labels[podcontroller.GroupNameLabel], | ||
) | ||
} | ||
return removed, nil | ||
})) | ||
} | ||
|
||
return ctrl.Result{}, err | ||
} | ||
|
||
var _ predicate.Predicate = (*PodReconciler)(nil) | ||
|
||
func (r *PodReconciler) Generic(event.GenericEvent) bool { | ||
return false | ||
} | ||
|
||
func (r *PodReconciler) Create(e event.CreateEvent) bool { | ||
return r.handle(e.Object) | ||
} | ||
|
||
func (r *PodReconciler) Update(e event.UpdateEvent) bool { | ||
return r.handle(e.ObjectNew) | ||
} | ||
|
||
func (r *PodReconciler) Delete(event.DeleteEvent) bool { | ||
return false | ||
} | ||
|
||
func (r *PodReconciler) handle(obj client.Object) bool { | ||
pod, isPod := obj.(*corev1.Pod) | ||
if !isPod { | ||
return false | ||
} | ||
// Handle only statefulset pods. | ||
return pod.Annotations[podcontroller.SuspendedByParentAnnotation] == FrameworkName | ||
} |
95 changes: 95 additions & 0 deletions
95
pkg/controller/jobs/statefulset/statefulset_pod_reconciler_test.go
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,95 @@ | ||
/* | ||
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 statefulset | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
appsv1 "k8s.io/api/apps/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/controller-runtime/pkg/reconcile" | ||
|
||
utiltesting "sigs.k8s.io/kueue/pkg/util/testing" | ||
testingjobspod "sigs.k8s.io/kueue/pkg/util/testingjobs/pod" | ||
"sigs.k8s.io/kueue/pkg/util/testingjobs/statefulset" | ||
) | ||
|
||
var ( | ||
baseCmpOpts = []cmp.Option{ | ||
cmpopts.EquateEmpty(), | ||
cmpopts.IgnoreFields(metav1.ObjectMeta{}, "ResourceVersion"), | ||
} | ||
) | ||
|
||
func TestPodReconciler(t *testing.T) { | ||
cases := map[string]struct { | ||
lws *appsv1.StatefulSet | ||
pod *corev1.Pod | ||
wantPod *corev1.Pod | ||
wantErr error | ||
}{ | ||
"should finalize succeeded pod": { | ||
lws: statefulset.MakeStatefulSet("sts", "ns").Obj(), | ||
pod: testingjobspod.MakePod("pod", "ns"). | ||
StatusPhase(corev1.PodSucceeded). | ||
KueueFinalizer(). | ||
Obj(), | ||
wantPod: testingjobspod.MakePod("pod", "ns"). | ||
StatusPhase(corev1.PodSucceeded). | ||
Obj(), | ||
}, | ||
"should finalize failed pod": { | ||
lws: statefulset.MakeStatefulSet("sts", "ns").Obj(), | ||
pod: testingjobspod.MakePod("pod", "ns"). | ||
StatusPhase(corev1.PodFailed). | ||
KueueFinalizer(). | ||
Obj(), | ||
wantPod: testingjobspod.MakePod("pod", "ns"). | ||
StatusPhase(corev1.PodFailed). | ||
Obj(), | ||
}, | ||
} | ||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
ctx, _ := utiltesting.ContextWithLog(t) | ||
clientBuilder := utiltesting.NewClientBuilder() | ||
|
||
kClient := clientBuilder.WithObjects(tc.lws, tc.pod).Build() | ||
|
||
reconciler := NewPodReconciler(kClient, nil) | ||
|
||
podKey := client.ObjectKeyFromObject(tc.pod) | ||
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: podKey}) | ||
if diff := cmp.Diff(tc.wantErr, err, cmpopts.EquateErrors()); diff != "" { | ||
t.Errorf("Reconcile returned error (-want,+got):\n%s", diff) | ||
} | ||
|
||
gotPod := &corev1.Pod{} | ||
if err := kClient.Get(ctx, podKey, gotPod); err != nil { | ||
t.Fatalf("Could not get Pod after reconcile: %v", err) | ||
} | ||
|
||
if diff := cmp.Diff(tc.wantPod, gotPod, baseCmpOpts...); diff != "" { | ||
t.Errorf("Pod after reconcile (-want,+got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
102 changes: 0 additions & 102 deletions
102
pkg/controller/jobs/statefulset/statefulset_reconciler.go
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this changed? Please don't if not necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes, a Pod is updated, but the StatefulSet isn't aware of it, causing the reconcile process to not work as expected (e.g., the finalizer isn't removed). Thats why it is better to reconcile Pod instead of StatefulSet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this case - can you elaborate? For example in the core k8s Job we also have finalizers on Job pods, but the reconciler is at the Job level, reference. I would like to first document such problematic scenarios in some form of tests and change the implementation in a dedicated PR (if needed).