github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/testobj/runtime.go (about)

     1  package testobj
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"k8s.io/apimachinery/pkg/api/meta"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    10  	"k8s.io/apimachinery/pkg/runtime"
    11  	"k8s.io/apimachinery/pkg/types"
    12  )
    13  
    14  // WithName sets the name of the given object and panics if it can't access the object's meta.
    15  func WithName(name string, obj runtime.Object) runtime.Object {
    16  	out := obj.DeepCopyObject()
    17  	m, err := meta.Accessor(out)
    18  	if err != nil {
    19  		panic(fmt.Sprintf("error setting name: %v", err))
    20  	}
    21  
    22  	m.SetName(name)
    23  
    24  	return out
    25  }
    26  
    27  // WithNamespace sets the namespace of the given object and panics if it can't access the object's meta.
    28  func WithNamespace(namespace string, obj runtime.Object) RuntimeMetaObject {
    29  	out := obj.DeepCopyObject().(RuntimeMetaObject)
    30  	out.SetNamespace(namespace)
    31  
    32  	return out
    33  }
    34  
    35  // WithNamespacedName sets the namespace and name of the given object.
    36  func WithNamespacedName(name *types.NamespacedName, obj runtime.Object) RuntimeMetaObject {
    37  	out := obj.DeepCopyObject().(RuntimeMetaObject)
    38  	out.SetNamespace(name.Namespace)
    39  	out.SetName(name.Name)
    40  
    41  	return out
    42  }
    43  
    44  // NamespacedName returns the namespaced name of the given object and panics if it can't access the object's meta.
    45  func NamespacedName(obj runtime.Object) types.NamespacedName {
    46  	m, err := meta.Accessor(obj)
    47  	if err != nil {
    48  		panic(fmt.Sprintf("error setting namespaced name: %v", err))
    49  	}
    50  
    51  	return types.NamespacedName{Namespace: m.GetNamespace(), Name: m.GetName()}
    52  }
    53  
    54  // WithLabel sets the given key/value pair on the labels of each object given and panics if it can't access an object's meta.
    55  func WithLabel(key, value string, objs ...runtime.Object) (labelled []runtime.Object) {
    56  	for _, obj := range objs {
    57  		out := obj.DeepCopyObject()
    58  		m, err := meta.Accessor(out)
    59  		if err != nil {
    60  			panic(fmt.Sprintf("error setting label: %v", err))
    61  		}
    62  
    63  		if len(m.GetLabels()) < 1 {
    64  			m.SetLabels(map[string]string{})
    65  		}
    66  		m.GetLabels()[key] = value
    67  
    68  		labelled = append(labelled, out)
    69  	}
    70  
    71  	return
    72  }
    73  
    74  // WithLabels sets the labels of an object and returns the updated result.
    75  func WithLabels(labels map[string]string, obj RuntimeMetaObject) RuntimeMetaObject {
    76  	out := obj.DeepCopyObject().(RuntimeMetaObject)
    77  	out.SetLabels(labels)
    78  	return out
    79  }
    80  
    81  // StripLabel removes the label with the given key from each object given and panics if it can't access an object's meta.
    82  func StripLabel(key string, objs ...runtime.Object) (stripped []runtime.Object) {
    83  	for _, obj := range objs {
    84  		out := obj.DeepCopyObject()
    85  		m, err := meta.Accessor(out)
    86  		if err != nil {
    87  			panic(fmt.Sprintf("error setting label: %v", err))
    88  		}
    89  
    90  		delete(m.GetLabels(), key)
    91  
    92  		stripped = append(stripped, out)
    93  	}
    94  
    95  	return
    96  }
    97  
    98  // GetUnstructured gets an Unstructured for the given object and panics if it fails.
    99  func GetUnstructured(scheme *runtime.Scheme, obj runtime.Object) *unstructured.Unstructured {
   100  	u := &unstructured.Unstructured{}
   101  	if err := scheme.Convert(obj, u, nil); err != nil {
   102  		panic(fmt.Errorf("error creating unstructured: %v", err))
   103  	}
   104  
   105  	return u
   106  }
   107  
   108  // WithItems sets the items of the list given and panics if it fails.
   109  func WithItems(list runtime.Object, items ...runtime.Object) runtime.Object {
   110  	out := list.DeepCopyObject()
   111  	if err := meta.SetList(out, items); err != nil {
   112  		panic(fmt.Sprintf("error setting list elements: %v", err))
   113  	}
   114  
   115  	return out
   116  }
   117  
   118  // MarshalJSON marshals an object to JSON and panics if it can't.
   119  func MarshalJSON(obj runtime.Object) (marshaled []byte) {
   120  	var err error
   121  	if marshaled, err = json.Marshal(obj); err != nil {
   122  		panic(fmt.Sprintf("failed to marshal obj to json: %s", err))
   123  	}
   124  
   125  	return
   126  }
   127  
   128  var (
   129  	notController          = false
   130  	dontBlockOwnerDeletion = false
   131  )
   132  
   133  // WithOwner appends the an owner to an object and returns a panic if it fails.
   134  func WithOwner(owner, obj RuntimeMetaObject) RuntimeMetaObject {
   135  	out := obj.DeepCopyObject().(RuntimeMetaObject)
   136  	gvk := owner.GetObjectKind().GroupVersionKind()
   137  	apiVersion, kind := gvk.ToAPIVersionAndKind()
   138  
   139  	nonBlockingOwner := metav1.OwnerReference{
   140  		APIVersion:         apiVersion,
   141  		Kind:               kind,
   142  		Name:               owner.GetName(),
   143  		UID:                owner.GetUID(),
   144  		BlockOwnerDeletion: &dontBlockOwnerDeletion,
   145  		Controller:         &notController,
   146  	}
   147  	for _, item := range out.GetOwnerReferences() {
   148  		if item.Kind == nonBlockingOwner.Kind && item.Name == nonBlockingOwner.Name && item.UID == nonBlockingOwner.UID {
   149  			return out
   150  		}
   151  	}
   152  	refs := append(out.GetOwnerReferences(), nonBlockingOwner)
   153  	out.SetOwnerReferences(refs)
   154  
   155  	return out
   156  }