-
Notifications
You must be signed in to change notification settings - Fork 1
feat(cocoonset): stamp generation onto owned pods for lifecycle bridge #4
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
35cf175
feat(cocoonset): stamp generation onto owned pods for lifecycle bridge
tonicmuroq 1240951
chore: bump cocoon-common to lifecycle-state contract merge commit
tonicmuroq 040cb62
docs: tighten lifecycle generation-stamp comments
CMGS f830797
test: assert build* preserves cocoonset generation annotation
CMGS 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package cocoonset | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "maps" | ||
| "slices" | ||
|
|
||
| cocoonv1 "github.com/cocoonstack/cocoon-common/apis/v1" | ||
| commonk8s "github.com/cocoonstack/cocoon-common/k8s" | ||
| ) | ||
|
|
||
| // syncCocoonSetGeneration writes cs.Generation to each owned pod so | ||
| // vk-cocoon can echo it back as lifecycle-observed-generation, giving | ||
| // clients a counter-based completion signal immune to wallclock skew. | ||
| func (r *Reconciler) syncCocoonSetGeneration(ctx context.Context, cs *cocoonv1.CocoonSet, classified classifiedPods) error { | ||
| for _, name := range slices.Sorted(maps.Keys(classified.allByName)) { | ||
| if ctxErr := ctx.Err(); ctxErr != nil { | ||
| return ctxErr | ||
| } | ||
| pod := classified.allByName[name] | ||
| if err := commonk8s.PatchCocoonSetGeneration(ctx, r.Client, pod, cs.Generation); err != nil { | ||
| return fmt.Errorf("patch cocoonset generation on %s/%s: %w", pod.Namespace, pod.Name, err) | ||
| } | ||
| } | ||
| return nil | ||
| } |
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,71 @@ | ||
| package cocoonset | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| ctrlfake "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
|
|
||
| cocoonv1 "github.com/cocoonstack/cocoon-common/apis/v1" | ||
| "github.com/cocoonstack/cocoon-common/meta" | ||
| ) | ||
|
|
||
| func TestSyncCocoonSetGenerationStampsAllOwnedPods(t *testing.T) { | ||
| scheme := testScheme(t) | ||
| cs := newCocoonSet("demo", func(cs *cocoonv1.CocoonSet) { cs.Generation = 7 }) | ||
|
|
||
| mainPod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "demo-0", Namespace: "ns"}} | ||
| subPod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "demo-1", Namespace: "ns", | ||
| Annotations: map[string]string{meta.AnnotationCocoonSetGeneration: "5"}, | ||
| }} | ||
|
|
||
| cli := ctrlfake.NewClientBuilder().WithScheme(scheme).WithObjects(mainPod, subPod).Build() | ||
| r := &Reconciler{Client: cli, Scheme: scheme} | ||
| classified := classifiedPods{ | ||
| main: mainPod, | ||
| sub: map[int32]*corev1.Pod{1: subPod}, | ||
| allByName: map[string]*corev1.Pod{"demo-0": mainPod, "demo-1": subPod}, | ||
| } | ||
|
|
||
| if err := r.syncCocoonSetGeneration(t.Context(), cs, classified); err != nil { | ||
| t.Fatalf("syncCocoonSetGeneration: %v", err) | ||
| } | ||
|
|
||
| for _, name := range []string{"demo-0", "demo-1"} { | ||
| t.Run(name, func(t *testing.T) { | ||
| var got corev1.Pod | ||
| if err := cli.Get(t.Context(), types.NamespacedName{Namespace: "ns", Name: name}, &got); err != nil { | ||
| t.Fatalf("get %s: %v", name, err) | ||
| } | ||
| if got.Annotations[meta.AnnotationCocoonSetGeneration] != "7" { | ||
| t.Errorf("%s generation annotation = %q, want 7", | ||
| name, got.Annotations[meta.AnnotationCocoonSetGeneration]) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSyncCocoonSetGenerationNoOpWhenAlreadyCurrent(t *testing.T) { | ||
| scheme := testScheme(t) | ||
| cs := newCocoonSet("demo", func(cs *cocoonv1.CocoonSet) { cs.Generation = 3 }) | ||
|
|
||
| pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "demo-0", Namespace: "ns", | ||
| Annotations: map[string]string{meta.AnnotationCocoonSetGeneration: "3"}, | ||
| }} | ||
| cli := ctrlfake.NewClientBuilder().WithScheme(scheme).WithObjects(pod).Build() | ||
| r := &Reconciler{Client: cli, Scheme: scheme} | ||
| classified := classifiedPods{ | ||
| main: pod, | ||
| allByName: map[string]*corev1.Pod{"demo-0": pod}, | ||
| } | ||
|
|
||
| // PatchCocoonSetGeneration must short-circuit on equal — the fake | ||
| // client would error on a Patch with empty body otherwise. | ||
| if err := r.syncCocoonSetGeneration(t.Context(), cs, classified); err != nil { | ||
| t.Fatalf("syncCocoonSetGeneration: %v", err) | ||
| } | ||
| } |
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
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.