github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/clientfake/meta.go (about) 1 package clientfake 2 3 import ( 4 "fmt" 5 6 "k8s.io/apimachinery/pkg/api/meta" 7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 8 "k8s.io/apimachinery/pkg/runtime" 9 "k8s.io/apiserver/pkg/storage/names" 10 ) 11 12 // BuildSelfLink returns a selflink for the given group version, plural, namespace, and name. 13 func BuildSelfLink(groupVersion, plural, namespace, name string) string { 14 if namespace == metav1.NamespaceAll { 15 return fmt.Sprintf("/apis/%s/%s/%s", groupVersion, plural, name) 16 } 17 return fmt.Sprintf("/apis/%s/namespaces/%s/%s/%s", groupVersion, namespace, plural, name) 18 } 19 20 // AddSimpleGeneratedName returns the given object with a simple generated name added to its metadata. 21 // If a name already exists, there is no GenerateName field set, or there is an issue accessing the object's metadata 22 // the object is returned unmodified. 23 func AddSimpleGeneratedName(obj runtime.Object) runtime.Object { 24 accessor, err := meta.Accessor(obj) 25 if err != nil { 26 return obj 27 } 28 if accessor.GetName() == "" && accessor.GetGenerateName() != "" { 29 // TODO: for tests, it would be nice to be able to retrieve this name later 30 accessor.SetName(names.SimpleNameGenerator.GenerateName(accessor.GetGenerateName())) 31 } 32 33 return obj 34 } 35 36 // AddSimpleGeneratedNames returns the list objects with simple generated names added to their metadata. 37 // If a name already exists, there is no GenerateName field set, or there is an issue accessing the object's metadata 38 // the object is returned unmodified. 39 func AddSimpleGeneratedNames(objs ...runtime.Object) []runtime.Object { 40 for i, obj := range objs { 41 objs[i] = AddSimpleGeneratedName(obj) 42 } 43 44 return objs 45 }