-
Notifications
You must be signed in to change notification settings - Fork 277
fix(instanceset): stabilize rolling update participants #10665
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
Open
leon-ape
wants to merge
7
commits into
main
Choose a base branch
from
bugfix/rolling-update-role-stability
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
769a74b
fix(instanceset): stabilize rolling update participants
leon-ape dd696d4
Merge remote-tracking branch 'origin/main' into bugfix/rolling-update…
leon-ape e54e11b
fix(instanceset): persist rolling update admission window
leon-ape cd09a22
fix(instanceset): preserve rolling update admission lifecycle
leon-ape 4a441c6
Merge remote-tracking branch 'origin/main' into bugfix/rolling-update…
leon-ape 6f282ba
fix(instanceset): bound rolling update window state
leon-ape 11a1688
fix(instanceset): stabilize rollout identity
leon-ape 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| Copyright (C) 2022-2026 ApeCloud Co., Ltd | ||
|
|
||
| This file is part of KubeBlocks project | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU Affero General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU Affero General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Affero General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package rollingupdate | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| ) | ||
|
|
||
| const WindowAnnotationKey = "workloads.kubeblocks.io/rolling-update-window" | ||
|
|
||
| type window struct { | ||
| RolloutID string `json:"rolloutID"` | ||
| Replicas int `json:"replicas"` | ||
| Participants []string `json:"participants"` | ||
| } | ||
|
|
||
| // Participants returns the stable set of instance names admitted to a rolling | ||
| // update. orderedNames must already be sorted according to the update policy. | ||
| func Participants(owner metav1.Object, rolloutID string, replicas int, orderedNames []string) sets.Set[string] { | ||
| if replicas < 0 { | ||
| replicas = 0 | ||
| } | ||
| if replicas >= len(orderedNames) { | ||
| removeWindow(owner) | ||
| return sets.New(orderedNames...) | ||
| } | ||
|
|
||
| if saved, ok := loadWindow(owner, rolloutID, replicas, orderedNames); ok { | ||
| return sets.New(saved.Participants...) | ||
| } | ||
|
|
||
| participants := append([]string(nil), orderedNames[:replicas]...) | ||
| saveWindow(owner, window{ | ||
| RolloutID: rolloutID, | ||
| Replicas: replicas, | ||
| Participants: participants, | ||
| }) | ||
| return sets.New(participants...) | ||
| } | ||
|
|
||
| func loadWindow(owner metav1.Object, rolloutID string, replicas int, orderedNames []string) (window, bool) { | ||
| annotations := owner.GetAnnotations() | ||
| if annotations == nil { | ||
| return window{}, false | ||
| } | ||
| raw, ok := annotations[WindowAnnotationKey] | ||
| if !ok { | ||
| return window{}, false | ||
| } | ||
|
|
||
| var saved window | ||
| if json.Unmarshal([]byte(raw), &saved) != nil || saved.RolloutID != rolloutID || saved.Replicas != replicas || | ||
|
leon-ape marked this conversation as resolved.
Outdated
|
||
| len(saved.Participants) != replicas { | ||
| return window{}, false | ||
| } | ||
|
|
||
| validNames := sets.New(orderedNames...) | ||
| participants := sets.New[string]() | ||
| for _, name := range saved.Participants { | ||
| if !validNames.Has(name) || participants.Has(name) { | ||
| return window{}, false | ||
| } | ||
| participants.Insert(name) | ||
| } | ||
| return saved, true | ||
| } | ||
|
|
||
| func saveWindow(owner metav1.Object, state window) { | ||
| data, err := json.Marshal(state) | ||
| if err != nil { | ||
| return | ||
| } | ||
| annotations := owner.GetAnnotations() | ||
| if annotations == nil { | ||
| annotations = make(map[string]string) | ||
| } | ||
| annotations[WindowAnnotationKey] = string(data) | ||
| owner.SetAnnotations(annotations) | ||
| } | ||
|
|
||
| func removeWindow(owner metav1.Object) { | ||
| annotations := owner.GetAnnotations() | ||
| if annotations == nil { | ||
| return | ||
| } | ||
| if _, ok := annotations[WindowAnnotationKey]; !ok { | ||
| return | ||
| } | ||
| delete(annotations, WindowAnnotationKey) | ||
| owner.SetAnnotations(annotations) | ||
| } | ||
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,69 @@ | ||
| /* | ||
| Copyright (C) 2022-2026 ApeCloud Co., Ltd | ||
|
|
||
| This file is part of KubeBlocks project | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU Affero General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU Affero General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Affero General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package rollingupdate | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| ) | ||
|
|
||
| func TestParticipantsRemainStable(t *testing.T) { | ||
| owner := &metav1.PartialObjectMetadata{} | ||
| got := Participants(owner, "2", 1, []string{"a", "b", "c"}) | ||
| if !got.Equal(sets.New("a")) { | ||
| t.Fatalf("expected initial participant a, got %v", got) | ||
| } | ||
|
|
||
| got = Participants(owner, "2", 1, []string{"b", "a", "c"}) | ||
| if !got.Equal(sets.New("a")) { | ||
| t.Fatalf("expected participant a after reorder, got %v", got) | ||
| } | ||
|
|
||
| got = Participants(owner, "3", 1, []string{"b", "a", "c"}) | ||
| if !got.Equal(sets.New("b")) { | ||
| t.Fatalf("expected new rollout to select b, got %v", got) | ||
| } | ||
| } | ||
|
|
||
| func TestParticipantsResetForReplicaChangeAndInvalidState(t *testing.T) { | ||
| owner := &metav1.PartialObjectMetadata{} | ||
| Participants(owner, "2", 1, []string{"a", "b", "c"}) | ||
|
|
||
| got := Participants(owner, "2", 2, []string{"b", "a", "c"}) | ||
| if !got.Equal(sets.New("b", "a")) { | ||
| t.Fatalf("expected replica change to rebuild window, got %v", got) | ||
| } | ||
|
|
||
| owner.Annotations[WindowAnnotationKey] = "invalid" | ||
| got = Participants(owner, "2", 1, []string{"c", "b", "a"}) | ||
| if !got.Equal(sets.New("c")) { | ||
| t.Fatalf("expected invalid state to be rebuilt, got %v", got) | ||
| } | ||
|
|
||
| got = Participants(owner, "2", 3, []string{"c", "b", "a"}) | ||
| if !got.Equal(sets.New("a", "b", "c")) { | ||
| t.Fatalf("expected all participants, got %v", got) | ||
| } | ||
| if _, ok := owner.Annotations[WindowAnnotationKey]; ok { | ||
| t.Fatal("expected a full window not to retain the annotation") | ||
| } | ||
| } |
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.