knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/webhook/resourcesemantics/defaulting/user_info.go (about) 1 /* 2 Copyright 2019 The Knative Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package defaulting 18 19 import ( 20 "context" 21 22 admissionv1 "k8s.io/api/admission/v1" 23 "k8s.io/apimachinery/pkg/api/equality" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 26 27 "knative.dev/pkg/apis" 28 ) 29 30 var ( 31 emptyGroupUpdaterAnnotation = apis.UpdaterAnnotationSuffix[1:] 32 emptyGroupCreatorAnnotation = apis.CreatorAnnotationSuffix[1:] 33 ) 34 35 // setUserInfoAnnotations sets creator and updater annotations on a resource. 36 func setUserInfoAnnotations(ctx context.Context, resource apis.HasSpec, groupName string) { 37 if ui := apis.GetUserInfo(ctx); ui != nil { 38 objectMetaAccessor, ok := resource.(metav1.ObjectMetaAccessor) 39 if !ok { 40 return 41 } 42 43 annotations := objectMetaAccessor.GetObjectMeta().GetAnnotations() 44 if annotations == nil { 45 annotations = map[string]string{} 46 objectMetaAccessor.GetObjectMeta().SetAnnotations(annotations) 47 } 48 49 updaterAnnotation := emptyGroupUpdaterAnnotation 50 creatorAnnotation := emptyGroupCreatorAnnotation 51 if groupName != "" { 52 updaterAnnotation = groupName + apis.UpdaterAnnotationSuffix 53 creatorAnnotation = groupName + apis.CreatorAnnotationSuffix 54 } 55 56 if apis.IsInUpdate(ctx) { 57 old := apis.GetBaseline(ctx).(apis.HasSpec) 58 if equality.Semantic.DeepEqual(old.GetUntypedSpec(), resource.GetUntypedSpec()) { 59 return 60 } 61 annotations[updaterAnnotation] = ui.Username 62 } else { 63 annotations[creatorAnnotation] = ui.Username 64 annotations[updaterAnnotation] = ui.Username 65 } 66 objectMetaAccessor.GetObjectMeta().SetAnnotations(annotations) 67 } 68 } 69 70 type unstructuredHasSpec struct { 71 *unstructured.Unstructured 72 } 73 74 func (us unstructuredHasSpec) GetObjectMeta() metav1.Object { 75 return us.Unstructured 76 } 77 78 var _ metav1.ObjectMetaAccessor = unstructuredHasSpec{} 79 80 func (us unstructuredHasSpec) GetUntypedSpec() interface{} { 81 if s, ok := us.Unstructured.Object["spec"]; ok { 82 return s 83 } 84 return nil 85 } 86 87 func adaptUnstructuredHasSpecCtx(ctx context.Context, req *admissionv1.AdmissionRequest) context.Context { 88 if apis.IsInUpdate(ctx) { 89 b := apis.GetBaseline(ctx) 90 if apis.IsInStatusUpdate(ctx) { 91 ctx = apis.WithinSubResourceUpdate(ctx, unstructuredHasSpec{b.(*unstructured.Unstructured)}, req.SubResource) 92 } else { 93 ctx = apis.WithinUpdate(ctx, unstructuredHasSpec{b.(*unstructured.Unstructured)}) 94 } 95 } 96 return ctx 97 }