Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cocoonset/lifecycle.go
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
}
71 changes: 71 additions & 0 deletions cocoonset/lifecycle_test.go
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)
}
}
3 changes: 3 additions & 0 deletions cocoonset/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ func newManagedPod(cs *cocoonv1.CocoonSet, podName, role, slotLabel string, sche
meta.LabelSlot: slotLabel,
"app.kubernetes.io/name": cs.Name,
},
Annotations: map[string]string{
meta.AnnotationCocoonSetGeneration: strconv.FormatInt(cs.Generation, 10),
},
Comment thread
CMGS marked this conversation as resolved.
},
Spec: corev1.PodSpec{
TerminationGracePeriodSeconds: &one,
Expand Down
10 changes: 10 additions & 0 deletions cocoonset/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ func TestNewManagedPodCarriesCocoonToleration(t *testing.T) {
}
}

func TestNewManagedPodStampsCocoonSetGeneration(t *testing.T) {
cs := newCocoonSet("demo", func(cs *cocoonv1.CocoonSet) {
cs.Generation = 42
})
pod := mustNewManagedPod(t, cs, "demo-0", meta.RoleMain, "0", testScheme(t))
if got := pod.Annotations[meta.AnnotationCocoonSetGeneration]; got != "42" {
t.Errorf("cocoonset generation annotation = %q, want 42", got)
}
}

func TestPodSpecMatchesAgentIdenticalSpec(t *testing.T) {
cs := newCocoonSet("demo")
scheme := testScheme(t)
Expand Down
6 changes: 6 additions & 0 deletions cocoonset/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
owned := filterOwnedPods(podList.Items, &cs)
classified := classifyPods(owned)

// Stamp before any spec-driven patch so observed-generation reflects
// the spec revision that produced the resulting state.
if err := r.syncCocoonSetGeneration(ctx, &cs, classified); err != nil {
return ctrl.Result{}, err
}

// Stop reconciling if main agent is in a terminal phase (e.g. Failed).
if classified.main != nil && meta.IsPodTerminal(classified.main) {
return ctrl.Result{}, r.patchStatus(ctx, &cs,
Expand Down
19 changes: 1 addition & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/cocoonstack/cocoon-operator
go 1.25.6

require (
github.com/cocoonstack/cocoon-common v0.2.1-0.20260506143712-eaa6e94cf830
github.com/cocoonstack/cocoon-common v0.2.1-0.20260510051935-9af438194381
github.com/cocoonstack/epoch v0.2.3-0.20260506150956-5d672b90749f
github.com/go-logr/logr v1.4.3
github.com/projecteru2/core v0.0.0-20241016125006-ff909eefe04c
Expand All @@ -15,66 +15,49 @@ require (
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/alphadose/haxmap v1.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cockroachdb/errors v1.9.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
github.com/getsentry/sentry-go v0.20.0 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-sql-driver/mysql v1.9.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/gnostic-models v0.7.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.18.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.11 // indirect
github.com/klauspost/crc32 v1.3.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/crc64nvme v1.1.1 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/minio-go/v7 v7.0.99 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/philhofer/fwd v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.23.2 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.66.1 // indirect
github.com/prometheus/procfs v0.16.1 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/rs/zerolog v1.29.1 // indirect
github.com/spf13/cobra v1.10.2 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/tinylib/msgp v1.6.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.46.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.48.0 // indirect
golang.org/x/oauth2 v0.30.0 // indirect
Expand Down
Loading