From 8cc119201d66322649ea955d151f74c31c04cb54 Mon Sep 17 00:00:00 2001 From: Pierangelo Di Pilato Date: Wed, 12 Jan 2022 09:26:45 +0100 Subject: [PATCH] Adapt unstructured objects to apis.HasSpec Signed-off-by: Pierangelo Di Pilato --- .../defaulting/defaulting.go | 2 +- .../resourcesemantics/defaulting/user_info.go | 39 +++++++++++-------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/webhook/resourcesemantics/defaulting/defaulting.go b/webhook/resourcesemantics/defaulting/defaulting.go index a1ea57fa62..a8a30fae13 100644 --- a/webhook/resourcesemantics/defaulting/defaulting.go +++ b/webhook/resourcesemantics/defaulting/defaulting.go @@ -433,7 +433,7 @@ func (ac *reconciler) callback(ctx context.Context, gvk schema.GroupVersionKind, } if shouldSetUserInfo { - setUserInfoAnnotationsUnstructured(ctx, after, before, req) + setUserInfoAnnotations(adaptUnstructuredHasSpecCtx(ctx, req), unstructuredHasSpec{after}, req.Resource.Group) } // Create patches. diff --git a/webhook/resourcesemantics/defaulting/user_info.go b/webhook/resourcesemantics/defaulting/user_info.go index 477e6678e7..fcd97a4561 100644 --- a/webhook/resourcesemantics/defaulting/user_info.go +++ b/webhook/resourcesemantics/defaulting/user_info.go @@ -51,30 +51,35 @@ func setUserInfoAnnotations(ctx context.Context, resource apis.HasSpec, groupNam annotations[groupName+apis.CreatorAnnotationSuffix] = ui.Username annotations[groupName+apis.UpdaterAnnotationSuffix] = ui.Username } + objectMetaAccessor.GetObjectMeta().SetAnnotations(annotations) } } -// setUserInfoAnnotationsUnstructured sets creator and updater annotations on a resource. -func setUserInfoAnnotationsUnstructured(ctx context.Context, after *unstructured.Unstructured, before *unstructured.Unstructured, req *admissionv1.AdmissionRequest) { - if v, ok := after.Object["metadata"]; ok { - if metadata, ok := v.(map[string]interface{}); ok { - if v, ok := metadata["annotations"]; ok { - if annotations, ok := v.(map[string]interface{}); ok { - if apis.IsInUpdate(ctx) { +type unstructuredHasSpec struct { + *unstructured.Unstructured +} - if equality.Semantic.DeepEqual(before.UnstructuredContent(), after.UnstructuredContent()) { - return - } +func (us unstructuredHasSpec) GetObjectMeta() metav1.Object { + return us.Unstructured +} - annotations[req.Resource.Group+apis.UpdaterAnnotationSuffix] = req.UserInfo.Username - return - } +var _ metav1.ObjectMetaAccessor = unstructuredHasSpec{} - annotations[req.Resource.Group+apis.CreatorAnnotationSuffix] = req.UserInfo.Username - annotations[req.Resource.Group+apis.UpdaterAnnotationSuffix] = req.UserInfo.Username - } - } +func (us unstructuredHasSpec) GetUntypedSpec() interface{} { + if s, ok := us.Unstructured.Object["spec"]; ok { + return s + } + return nil +} +func adaptUnstructuredHasSpecCtx(ctx context.Context, req *admissionv1.AdmissionRequest) context.Context { + if apis.IsInUpdate(ctx) { + b := apis.GetBaseline(ctx) + if apis.IsInStatusUpdate(ctx) { + ctx = apis.WithinSubResourceUpdate(ctx, unstructuredHasSpec{b.(*unstructured.Unstructured)}, req.SubResource) + } else { + ctx = apis.WithinUpdate(ctx, unstructuredHasSpec{b.(*unstructured.Unstructured)}) } } + return ctx }