fieldmanager: Copy LastAppliedAnnotation to remove dependency on corev1

This commit is contained in:
Antoine Pelisse 2023-01-19 09:38:04 -08:00
parent 8d40ba73fb
commit 577f3d8c9d
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) return "", fmt.Errorf("no annotations on obj: %v", obj)
} }
lastApplied, ok := annotations[corev1.LastAppliedConfigAnnotation] lastApplied, ok := annotations[internal.LastAppliedConfigAnnotation]
if !ok { if !ok {
return "", fmt.Errorf("expected last applied annotation, but got none for object: %v", obj) return "", fmt.Errorf("expected last applied annotation, but got none for object: %v", obj)
} }

View File

@ -19,12 +19,17 @@ package internal
import ( import (
"fmt" "fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation" apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/runtime" "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 // SetLastApplied sets the last-applied annotation the given value in
// the object. // the object.
func SetLastApplied(obj runtime.Object, value string) error { func SetLastApplied(obj runtime.Object, value string) error {
@ -36,9 +41,9 @@ func SetLastApplied(obj runtime.Object, value string) error {
if annotations == nil { if annotations == nil {
annotations = map[string]string{} annotations = map[string]string{}
} }
annotations[corev1.LastAppliedConfigAnnotation] = value annotations[LastAppliedConfigAnnotation] = value
if err := apimachineryvalidation.ValidateAnnotationsSize(annotations); err != nil { if err := apimachineryvalidation.ValidateAnnotationsSize(annotations); err != nil {
delete(annotations, corev1.LastAppliedConfigAnnotation) delete(annotations, LastAppliedConfigAnnotation)
} }
accessor.SetAnnotations(annotations) accessor.SetAnnotations(annotations)
return nil return nil

View File

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

View File

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

View File

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