mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 18:24:07 +00:00
Merge pull request #120872 from p0lyn0mial/upstream-has-initial-events-annotation
storage/util: introduce HasInitialEventsEndBookmarkAnnotation
This commit is contained in:
commit
9410de78b2
@ -29,6 +29,13 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"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)
|
type SimpleUpdateFunc func(runtime.Object) (runtime.Object, error)
|
||||||
|
|
||||||
// SimpleUpdateFunc converts SimpleUpdateFunc into UpdateFunc
|
// SimpleUpdateFunc converts SimpleUpdateFunc into UpdateFunc
|
||||||
@ -137,7 +144,18 @@ func AnnotateInitialEventsEndBookmark(obj runtime.Object) error {
|
|||||||
if objAnnotations == nil {
|
if objAnnotations == nil {
|
||||||
objAnnotations = map[string]string{}
|
objAnnotations = map[string]string{}
|
||||||
}
|
}
|
||||||
objAnnotations["k8s.io/initial-events-end"] = "true"
|
objAnnotations[initialEventsAnnotationKey] = "true"
|
||||||
objMeta.SetAnnotations(objAnnotations)
|
objMeta.SetAnnotations(objAnnotations)
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
@ -146,3 +146,42 @@ func TestGetCurrentResourceVersionFromStorage(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, currentPodRV, podRV, "didn't expect to see the pod's RV changed")
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user