-
Notifications
You must be signed in to change notification settings - Fork 2
feat(planner): add replace-pod task to NodeUpdate plan (closes #211) #216
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1222cf3
feat(planner): add replace-pod task to NodeUpdate plan (closes #211)
bdchatham a89f364
review: address k8s expert findings
bdchatham 7a18b56
review: trim comments + extract test consts to satisfy lint
bdchatham 4baabfb
review: switch SeiNode StatefulSets to OnDelete update strategy
bdchatham e6eaa34
fix(test): import appsv1 for OnDelete strategy assertion; trim comments
bdchatham 26b3ee8
review: MaxRetries=3 on replace-pod for transient pod-delete conflicts
bdchatham 6ed60e5
fix(executor): use APIReader for read-after-write on StatefulSet (clo…
bdchatham aac07fa
test: wire APIReader into controller-side test fixtures
bdchatham 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,131 @@ | ||
| package task | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| appsv1 "k8s.io/api/apps/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| seiv1alpha1 "github.com/sei-protocol/sei-k8s-controller/api/v1alpha1" | ||
| ) | ||
|
|
||
| const TaskTypeReplacePod = "replace-pod" | ||
|
|
||
| type ReplacePodParams struct { | ||
| NodeName string `json:"nodeName"` | ||
| Namespace string `json:"namespace"` | ||
| } | ||
|
|
||
| type replacePodExecution struct { | ||
| taskBase | ||
| params ReplacePodParams | ||
| cfg ExecutionConfig | ||
| } | ||
|
|
||
| func deserializeReplacePod(id string, params json.RawMessage, cfg ExecutionConfig) (TaskExecution, error) { | ||
| var p ReplacePodParams | ||
| if len(params) > 0 { | ||
| if err := json.Unmarshal(params, &p); err != nil { | ||
| return nil, fmt.Errorf("deserializing replace-pod params: %w", err) | ||
| } | ||
| } | ||
| return &replacePodExecution{ | ||
| taskBase: taskBase{id: id, status: ExecutionRunning}, | ||
| params: p, | ||
| cfg: cfg, | ||
| }, nil | ||
| } | ||
|
|
||
| // Execute deletes pods at the StatefulSet's old revision. Pairs with | ||
| // the StatefulSet's OnDelete update strategy: pod lifecycle is the | ||
| // SeiNode controller's responsibility, not the StatefulSet controller's. | ||
| func (e *replacePodExecution) Execute(ctx context.Context) error { | ||
| node, err := ResourceAs[*seiv1alpha1.SeiNode](e.cfg) | ||
| if err != nil { | ||
| return Terminal(err) | ||
| } | ||
|
|
||
| sts := &appsv1.StatefulSet{} | ||
| key := types.NamespacedName{Name: node.Name, Namespace: node.Namespace} | ||
| // Bypass cache: apply-statefulset just wrote this in the same reconcile. | ||
| if err := e.cfg.APIReader.Get(ctx, key, sts); err != nil { | ||
| if apierrors.IsNotFound(err) { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("getting statefulset: %w", err) | ||
| } | ||
|
|
||
| if sts.Status.ObservedGeneration < sts.Generation { | ||
| return nil | ||
| } | ||
| if sts.Status.UpdateRevision == "" { | ||
| return nil | ||
| } | ||
| if sts.Status.CurrentRevision == sts.Status.UpdateRevision { | ||
| e.complete() | ||
| return nil | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| if sts.Spec.Selector == nil || len(sts.Spec.Selector.MatchLabels) == 0 { | ||
| return Terminal(fmt.Errorf("statefulset %q has no selector; cannot identify owned pods", node.Name)) | ||
| } | ||
|
|
||
| // Multi-replica needs ordinal-aware deletion (reverse-ordinal). Fail | ||
| // loud rather than silently violate StatefulSet rolling-update semantics. | ||
| if sts.Spec.Replicas != nil && *sts.Spec.Replicas > 1 { | ||
| return Terminal(fmt.Errorf( | ||
| "replace-pod does not support multi-replica StatefulSets (got replicas=%d); "+ | ||
| "add ordinal-aware deletion before scaling up", *sts.Spec.Replicas)) | ||
| } | ||
|
|
||
| pods := &corev1.PodList{} | ||
| if err := e.cfg.KubeClient.List(ctx, pods, | ||
| client.InNamespace(node.Namespace), | ||
| client.MatchingLabels(sts.Spec.Selector.MatchLabels), | ||
| ); err != nil { | ||
| return fmt.Errorf("listing pods for statefulset %q: %w", node.Name, err) | ||
| } | ||
|
|
||
| updateRev := sts.Status.UpdateRevision | ||
| for i := range pods.Items { | ||
| pod := &pods.Items[i] | ||
| if !ownedByStatefulSet(pod, sts) { | ||
| continue | ||
| } | ||
| hash, hasHash := pod.Labels[appsv1.ControllerRevisionHashLabelKey] | ||
| if !hasHash { | ||
| continue | ||
| } | ||
| if hash == updateRev { | ||
| continue | ||
| } | ||
| if pod.DeletionTimestamp != nil { | ||
| continue | ||
| } | ||
| if err := e.cfg.KubeClient.Delete(ctx, pod); err != nil && !apierrors.IsNotFound(err) { | ||
| return fmt.Errorf("deleting pod %q: %w", pod.Name, err) | ||
| } | ||
| } | ||
|
|
||
| e.complete() | ||
| return nil | ||
| } | ||
|
|
||
| func ownedByStatefulSet(pod *corev1.Pod, sts *appsv1.StatefulSet) bool { | ||
| for _, ref := range pod.OwnerReferences { | ||
| if ref.Kind == "StatefulSet" && ref.UID == sts.UID { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // Status returns the cached execution status. | ||
| func (e *replacePodExecution) Status(_ context.Context) ExecutionStatus { | ||
| return e.DefaultStatus() | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.