diff --git a/staging/src/k8s.io/apiserver/pkg/storage/util.go b/staging/src/k8s.io/apiserver/pkg/storage/util.go index 7b23c7a7af8..6d5fb36d24e 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/util.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/util.go @@ -29,6 +29,13 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) +const ( + // initialEventsAnnotationKey the name of the key + // under which an annotation marking the end of list stream + // is kept. + initialEventsAnnotationKey = "k8s.io/initial-events-end" +) + type SimpleUpdateFunc func(runtime.Object) (runtime.Object, error) // SimpleUpdateFunc converts SimpleUpdateFunc into UpdateFunc @@ -137,7 +144,18 @@ func AnnotateInitialEventsEndBookmark(obj runtime.Object) error { if objAnnotations == nil { objAnnotations = map[string]string{} } - objAnnotations["k8s.io/initial-events-end"] = "true" + objAnnotations[initialEventsAnnotationKey] = "true" objMeta.SetAnnotations(objAnnotations) return nil } + +// HasInitialEventsEndBookmarkAnnotation checks the presence of the +// special annotation which marks that the initial events have been sent. +func HasInitialEventsEndBookmarkAnnotation(obj runtime.Object) (bool, error) { + objMeta, err := meta.Accessor(obj) + if err != nil { + return false, err + } + objAnnotations := objMeta.GetAnnotations() + return objAnnotations[initialEventsAnnotationKey] == "true", nil +} diff --git a/staging/src/k8s.io/apiserver/pkg/storage/util_test.go b/staging/src/k8s.io/apiserver/pkg/storage/util_test.go index 2a1b737b816..ef13dc3c52d 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/util_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/util_test.go @@ -146,3 +146,42 @@ func TestGetCurrentResourceVersionFromStorage(t *testing.T) { require.NoError(t, err) require.Equal(t, currentPodRV, podRV, "didn't expect to see the pod's RV changed") } + +func TestHasInitialEventsEndBookmarkAnnotation(t *testing.T) { + createPod := func(name string) *example.Pod { + return &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: name}} + } + createAnnotatedPod := func(name, value string) *example.Pod { + p := createPod(name) + p.Annotations = map[string]string{} + p.Annotations["k8s.io/initial-events-end"] = value + return p + } + scenarios := []struct { + name string + object runtime.Object + expectAnnotation bool + }{ + { + name: "a standard obj with the initial-events-end annotation set to true", + object: createAnnotatedPod("p1", "true"), + expectAnnotation: true, + }, + { + name: "a standard obj with the initial-events-end annotation set to false", + object: createAnnotatedPod("p1", "false"), + }, + { + name: "a standard obj without the annotation", + object: createPod("p1"), + }, + } + + for _, scenario := range scenarios { + t.Run(scenario.name, func(t *testing.T) { + hasAnnotation, err := storage.HasInitialEventsEndBookmarkAnnotation(scenario.object) + require.NoError(t, err) + require.Equal(t, scenario.expectAnnotation, hasAnnotation) + }) + } +}