Skip to content

Commit

Permalink
Adapt unstructured objects to apis.HasSpec
Browse files Browse the repository at this point in the history
Signed-off-by: Pierangelo Di Pilato <[email protected]>
  • Loading branch information
pierDipi committed Jan 12, 2022
1 parent 4162f86 commit 8cc1192
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion webhook/resourcesemantics/defaulting/defaulting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 22 additions & 17 deletions webhook/resourcesemantics/defaulting/user_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 8cc1192

Please sign in to comment.