Merge pull request #120872 from p0lyn0mial/upstream-has-initial-events-annotation

storage/util: introduce HasInitialEventsEndBookmarkAnnotation
This commit is contained in:
Kubernetes Prow Robot 2023-09-25 07:47:25 -07:00 committed by GitHub
commit 9410de78b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 1 deletions

View File

@ -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
}

View File

@ -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)
})
}
}