sigs.k8s.io/cluster-api@v1.7.1/internal/controllers/topology/cluster/util.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 cluster 18 19 import ( 20 "context" 21 22 "github.com/pkg/errors" 23 corev1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 25 26 "sigs.k8s.io/cluster-api/controllers/external" 27 ) 28 29 // getReference gets the object referenced in ref. 30 func (r *Reconciler) getReference(ctx context.Context, ref *corev1.ObjectReference) (*unstructured.Unstructured, error) { 31 if ref == nil { 32 return nil, errors.New("reference is not set") 33 } 34 35 obj, err := external.Get(ctx, r.UnstructuredCachingClient, ref, ref.Namespace) 36 if err != nil { 37 return nil, errors.Wrapf(err, "failed to retrieve %s %q in namespace %q", ref.Kind, ref.Name, ref.Namespace) 38 } 39 return obj, nil 40 } 41 42 // refToUnstructured returns an unstructured object with details from an ObjectReference. 43 func refToUnstructured(ref *corev1.ObjectReference) *unstructured.Unstructured { 44 uns := &unstructured.Unstructured{} 45 uns.SetAPIVersion(ref.APIVersion) 46 uns.SetKind(ref.Kind) 47 uns.SetNamespace(ref.Namespace) 48 uns.SetName(ref.Name) 49 return uns 50 }