sigs.k8s.io/cluster-api@v1.7.1/internal/contract/references.go (about) 1 /* 2 Copyright 2021 The Kubernetes 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 contract 18 19 import ( 20 "strings" 21 22 "github.com/pkg/errors" 23 corev1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 25 "k8s.io/klog/v2" 26 ) 27 28 // Ref provide a helper struct for working with references in Unstructured objects. 29 type Ref struct { 30 path Path 31 } 32 33 // Path returns the path of the reference. 34 func (r *Ref) Path() Path { 35 return r.path 36 } 37 38 // Get gets the reference value from the Unstructured object. 39 func (r *Ref) Get(obj *unstructured.Unstructured) (*corev1.ObjectReference, error) { 40 return GetNestedRef(obj, r.path...) 41 } 42 43 // Set sets the reference value in the Unstructured object. 44 func (r *Ref) Set(obj, refObj *unstructured.Unstructured) error { 45 return SetNestedRef(obj, refObj, r.path...) 46 } 47 48 // GetNestedRef returns the ref value from a nested field in an Unstructured object. 49 func GetNestedRef(obj *unstructured.Unstructured, fields ...string) (*corev1.ObjectReference, error) { 50 ref := &corev1.ObjectReference{} 51 if v, ok, err := unstructured.NestedString(obj.UnstructuredContent(), append(fields, "apiVersion")...); ok && err == nil { 52 ref.APIVersion = v 53 } else { 54 return nil, errors.Errorf("failed to get %s.apiVersion from %s", strings.Join(fields, "."), obj.GetKind()) 55 } 56 if v, ok, err := unstructured.NestedString(obj.UnstructuredContent(), append(fields, "kind")...); ok && err == nil { 57 ref.Kind = v 58 } else { 59 return nil, errors.Errorf("failed to get %s.kind from %s", strings.Join(fields, "."), obj.GetKind()) 60 } 61 if v, ok, err := unstructured.NestedString(obj.UnstructuredContent(), append(fields, "name")...); ok && err == nil { 62 ref.Name = v 63 } else { 64 return nil, errors.Errorf("failed to get %s.name from %s", strings.Join(fields, "."), obj.GetKind()) 65 } 66 if v, ok, err := unstructured.NestedString(obj.UnstructuredContent(), append(fields, "namespace")...); ok && err == nil { 67 ref.Namespace = v 68 } else { 69 return nil, errors.Errorf("failed to get %s.namespace from %s", strings.Join(fields, "."), obj.GetKind()) 70 } 71 return ref, nil 72 } 73 74 // SetNestedRef sets the value of a nested field in an Unstructured to a reference to the refObj provided. 75 func SetNestedRef(obj, refObj *unstructured.Unstructured, fields ...string) error { 76 ref := map[string]interface{}{ 77 "kind": refObj.GetKind(), 78 "namespace": refObj.GetNamespace(), 79 "name": refObj.GetName(), 80 "apiVersion": refObj.GetAPIVersion(), 81 } 82 if err := unstructured.SetNestedField(obj.UnstructuredContent(), ref, fields...); err != nil { 83 return errors.Wrapf(err, "failed to set object reference on object %v %s", 84 obj.GroupVersionKind(), klog.KObj(obj)) 85 } 86 return nil 87 } 88 89 // ObjToRef returns a reference to the given object. 90 // Note: This function only operates on Unstructured instead of client.Object 91 // because it is only safe to assume for Unstructured that the GVK is set. 92 func ObjToRef(obj *unstructured.Unstructured) *corev1.ObjectReference { 93 gvk := obj.GetObjectKind().GroupVersionKind() 94 return &corev1.ObjectReference{ 95 Kind: gvk.Kind, 96 APIVersion: gvk.GroupVersion().String(), 97 Namespace: obj.GetNamespace(), 98 Name: obj.GetName(), 99 } 100 }