github.com/verrazzano/verrazzano@v1.7.0/application-operator/controllers/navigation/names.go (about)

     1  // Copyright (c) 2020, 2021, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package navigation
     5  
     6  import (
     7  	"fmt"
     8  	"github.com/gertd/go-pluralize"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    11  	"k8s.io/apimachinery/pkg/types"
    12  	"strings"
    13  )
    14  
    15  // GetDefinitionOfResource converts APIVersion and Kind of CR to a CRD namespaced name.
    16  // For example CR APIVersion.Kind core.oam.dev/v1alpha2.ContainerizedWorkload would be converted
    17  // to containerizedworkloads.core.oam.dev in the default (i.e. "") namespace.
    18  // resourceAPIVersion - The CR APIVersion
    19  // resourceKind - The CR Kind
    20  func GetDefinitionOfResource(resourceAPIVersion string, resourceKind string) types.NamespacedName {
    21  	grp, ver := ParseGroupAndVersionFromAPIVersion(resourceAPIVersion)
    22  	res := pluralize.NewClient().Plural(strings.ToLower(resourceKind))
    23  	grpVerRes := metav1.GroupVersionResource{
    24  		Group:    grp,
    25  		Version:  ver,
    26  		Resource: res,
    27  	}
    28  	var name string
    29  	if grp == "" {
    30  		name = grpVerRes.Resource
    31  	} else {
    32  		name = grpVerRes.Resource + "." + grpVerRes.Group
    33  	}
    34  	return types.NamespacedName{Namespace: "", Name: name}
    35  }
    36  
    37  // ParseGroupAndVersionFromAPIVersion splits APIVersion into API and version parts.
    38  // An APIVersion takes the form api/version (e.g. networking.k8s.io/v1)
    39  // If the input does not contain a / the group is defaulted to the empty string.
    40  // apiVersion - The combined api and version to split
    41  func ParseGroupAndVersionFromAPIVersion(apiVersion string) (string, string) {
    42  	parts := strings.SplitN(apiVersion, "/", 2)
    43  	if len(parts) < 2 {
    44  		// Use empty group for core types.
    45  		return "", parts[0]
    46  	}
    47  	return parts[0], parts[1]
    48  }
    49  
    50  // GetNamespacedNameFromObjectMeta creates a namespaced name from the values in an object meta.
    51  func GetNamespacedNameFromObjectMeta(objectMeta metav1.ObjectMeta) types.NamespacedName {
    52  	return types.NamespacedName{
    53  		Namespace: objectMeta.Namespace,
    54  		Name:      objectMeta.Name,
    55  	}
    56  }
    57  
    58  // GetNamespacedNameFromUnstructured creates a namespaced name from the values in a unstructured.
    59  func GetNamespacedNameFromUnstructured(u *unstructured.Unstructured) types.NamespacedName {
    60  	return types.NamespacedName{
    61  		Namespace: u.GetNamespace(),
    62  		Name:      u.GetName(),
    63  	}
    64  }
    65  
    66  // ParseNamespacedNameFromQualifiedName parses a string representation of a namespace qualified name.
    67  func ParseNamespacedNameFromQualifiedName(qualifiedName string) (*types.NamespacedName, error) {
    68  	parts := strings.SplitN(qualifiedName, "/", 2)
    69  	if len(parts) < 2 {
    70  		return nil, fmt.Errorf("failed to parse namespaced name %s", qualifiedName)
    71  	}
    72  	namespace := strings.TrimSpace(parts[0])
    73  	name := strings.TrimSpace(parts[1])
    74  	if len(name) == 0 {
    75  		return nil, fmt.Errorf("failed to parse namespaced name %s", qualifiedName)
    76  	}
    77  	return &types.NamespacedName{Namespace: namespace, Name: name}, nil
    78  }