fieldmanager: Copy LastAppliedAnnotation to remove dependency on corev1

Kubernetes-commit: 577f3d8c9da461da93d4a0b66ca16b2a84f6a4d6
This commit is contained in:
Antoine Pelisse 2023-01-19 09:38:04 -08:00 committed by Kubernetes Publisher
parent e19154e9ae
commit 2a6ed798c7
5 changed files with 18 additions and 15 deletions

View File

@ -817,7 +817,7 @@ func getLastApplied(obj runtime.Object) (string, error) {
return "", fmt.Errorf("no annotations on obj: %v", obj)
}
lastApplied, ok := annotations[corev1.LastAppliedConfigAnnotation]
lastApplied, ok := annotations[internal.LastAppliedConfigAnnotation]
if !ok {
return "", fmt.Errorf("expected last applied annotation, but got none for object: %v", obj)
}

View File

@ -19,12 +19,17 @@ package internal
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/runtime"
)
// LastAppliedConfigAnnotation is the annotation used to store the previous
// configuration of a resource for use in a three way diff by UpdateApplyAnnotation.
//
// This is a copy of the corev1 annotation since we don't want to depend on the whole package.
const LastAppliedConfigAnnotation = "kubectl.kubernetes.io/last-applied-configuration"
// SetLastApplied sets the last-applied annotation the given value in
// the object.
func SetLastApplied(obj runtime.Object, value string) error {
@ -36,9 +41,9 @@ func SetLastApplied(obj runtime.Object, value string) error {
if annotations == nil {
annotations = map[string]string{}
}
annotations[corev1.LastAppliedConfigAnnotation] = value
annotations[LastAppliedConfigAnnotation] = value
if err := apimachineryvalidation.ValidateAnnotationsSize(annotations); err != nil {
delete(annotations, corev1.LastAppliedConfigAnnotation)
delete(annotations, LastAppliedConfigAnnotation)
}
accessor.SetAnnotations(annotations)
return nil

View File

@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
@ -100,7 +99,7 @@ func (f *lastAppliedManager) allowedConflictsFromLastApplied(liveObj runtime.Obj
if annotations == nil {
return nil, fmt.Errorf("no last applied annotation")
}
var lastApplied, ok = annotations[corev1.LastAppliedConfigAnnotation]
var lastApplied, ok = annotations[LastAppliedConfigAnnotation]
if !ok || lastApplied == "" {
return nil, fmt.Errorf("no last applied annotation")
}

View File

@ -19,7 +19,6 @@ package internal
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
@ -78,7 +77,7 @@ func hasLastApplied(obj runtime.Object) bool {
if annotations == nil {
return false
}
lastApplied, ok := annotations[corev1.LastAppliedConfigAnnotation]
lastApplied, ok := annotations[LastAppliedConfigAnnotation]
return ok && len(lastApplied) > 0
}
@ -92,7 +91,7 @@ func buildLastApplied(obj runtime.Object) (string, error) {
// Remove the annotation from the object before encoding the object
var annotations = accessor.GetAnnotations()
delete(annotations, corev1.LastAppliedConfigAnnotation)
delete(annotations, LastAppliedConfigAnnotation)
accessor.SetAnnotations(annotations)
lastApplied, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)

View File

@ -114,7 +114,7 @@ func TestLargeLastApplied(t *testing.T) {
Name: "large-update-test-cm",
Namespace: "default",
Annotations: map[string]string{
corev1.LastAppliedConfigAnnotation: "nonempty",
internal.LastAppliedConfigAnnotation: "nonempty",
},
},
Data: map[string]string{"k": "v"},
@ -129,7 +129,7 @@ func TestLargeLastApplied(t *testing.T) {
Name: "large-update-test-cm",
Namespace: "default",
Annotations: map[string]string{
corev1.LastAppliedConfigAnnotation: "nonempty",
internal.LastAppliedConfigAnnotation: "nonempty",
},
},
Data: map[string]string{"k": "v"},
@ -153,7 +153,7 @@ func TestLargeLastApplied(t *testing.T) {
Name: "large-update-test-cm",
Namespace: "default",
Annotations: map[string]string{
corev1.LastAppliedConfigAnnotation: "nonempty",
internal.LastAppliedConfigAnnotation: "nonempty",
},
},
Data: map[string]string{"k": "v"},
@ -174,7 +174,7 @@ func TestLargeLastApplied(t *testing.T) {
Name: "large-update-test-cm",
Namespace: "default",
Annotations: map[string]string{
corev1.LastAppliedConfigAnnotation: "nonempty",
internal.LastAppliedConfigAnnotation: "nonempty",
},
},
Data: map[string]string{"k": "v"},
@ -220,7 +220,7 @@ func TestLargeLastApplied(t *testing.T) {
if annotations == nil {
t.Errorf("No annotations on obj: %v", f.Live())
}
lastApplied, ok := annotations[corev1.LastAppliedConfigAnnotation]
lastApplied, ok := annotations[internal.LastAppliedConfigAnnotation]
if ok || len(lastApplied) > 0 {
t.Errorf("Expected no last applied annotation, but got last applied with length: %d", len(lastApplied))
}
@ -238,7 +238,7 @@ func getLastApplied(obj runtime.Object) (string, error) {
return "", fmt.Errorf("no annotations on obj: %v", obj)
}
lastApplied, ok := annotations[corev1.LastAppliedConfigAnnotation]
lastApplied, ok := annotations[internal.LastAppliedConfigAnnotation]
if !ok {
return "", fmt.Errorf("expected last applied annotation, but got none for object: %v", obj)
}