...

Source file src/sigs.k8s.io/cli-utils/pkg/object/infos.go

Documentation: sigs.k8s.io/cli-utils/pkg/object

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package object
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    10  	"k8s.io/cli-runtime/pkg/resource"
    11  	"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
    12  )
    13  
    14  // InfosToObjMetas returns object metadata (ObjMetadata) for the
    15  // passed objects (infos); returns an error if one occurs.
    16  func InfosToObjMetas(infos []*resource.Info) ([]ObjMetadata, error) {
    17  	objMetas := make([]ObjMetadata, 0, len(infos))
    18  	for _, info := range infos {
    19  		objMeta, err := InfoToObjMeta(info)
    20  		if err != nil {
    21  			return nil, err
    22  		}
    23  		objMetas = append(objMetas, objMeta)
    24  	}
    25  	return objMetas, nil
    26  }
    27  
    28  // InfoToObjMeta takes information from the provided info and
    29  // returns an ObjMetadata that identifies the resource.
    30  func InfoToObjMeta(info *resource.Info) (ObjMetadata, error) {
    31  	if info == nil || info.Object == nil {
    32  		return ObjMetadata{}, fmt.Errorf("attempting to transform info, but it is empty")
    33  	}
    34  	id := ObjMetadata{
    35  		Namespace: info.Namespace,
    36  		Name:      info.Name,
    37  		GroupKind: info.Object.GetObjectKind().GroupVersionKind().GroupKind(),
    38  	}
    39  	return id, nil
    40  }
    41  
    42  // InfoToUnstructured transforms the passed info object into unstructured format.
    43  func InfoToUnstructured(info *resource.Info) *unstructured.Unstructured {
    44  	return info.Object.(*unstructured.Unstructured)
    45  }
    46  
    47  // UnstructuredToInfo transforms the passed Unstructured object into Info format,
    48  // or an error if one occurs.
    49  func UnstructuredToInfo(obj *unstructured.Unstructured) (*resource.Info, error) {
    50  	// make a copy of the input object to avoid modifying the input
    51  	obj = obj.DeepCopy()
    52  
    53  	annos := obj.GetAnnotations()
    54  
    55  	source := "unstructured"
    56  	path, ok := annos[kioutil.PathAnnotation]
    57  	if ok {
    58  		source = path
    59  	}
    60  	StripKyamlAnnotations(obj)
    61  
    62  	return &resource.Info{
    63  		Name:      obj.GetName(),
    64  		Namespace: obj.GetNamespace(),
    65  		Source:    source,
    66  		Object:    obj,
    67  	}, nil
    68  }
    69  
    70  // InfosToUnstructureds transforms the passed objects in Info format to Unstructured.
    71  func InfosToUnstructureds(infos []*resource.Info) []*unstructured.Unstructured {
    72  	var objs []*unstructured.Unstructured
    73  	for _, info := range infos {
    74  		objs = append(objs, InfoToUnstructured(info))
    75  	}
    76  	return objs
    77  }
    78  
    79  // UnstructuredsToInfos transforms the passed Unstructured objects into Info format
    80  // or an error if one occurs.
    81  func UnstructuredsToInfos(objs []*unstructured.Unstructured) ([]*resource.Info, error) {
    82  	var infos []*resource.Info
    83  	for _, obj := range objs {
    84  		inf, err := UnstructuredToInfo(obj)
    85  		if err != nil {
    86  			return infos, err
    87  		}
    88  		infos = append(infos, inf)
    89  	}
    90  	return infos, nil
    91  }
    92  

View as plain text