...

Source file src/github.com/redhat-developer/odo/pkg/libdevfile/component_kubernetes_utils.go

Documentation: github.com/redhat-developer/odo/pkg/libdevfile

     1  package libdevfile
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  
     7  	"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     8  	"github.com/devfile/library/v2/pkg/devfile/parser"
     9  	"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
    10  	devfilefs "github.com/devfile/library/v2/pkg/testingutil/filesystem"
    11  	yaml3 "gopkg.in/yaml.v3"
    12  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    13  	"sigs.k8s.io/yaml"
    14  )
    15  
    16  // GetK8sComponentAsUnstructuredList parses the Inlined/URI K8s of the Devfile K8s component and returns a list of unstructured.Unstructured objects;
    17  // List is returned here because it is possible to define multiple K8s resources against a single Devfile K8s component
    18  func GetK8sComponentAsUnstructuredList(devfileObj parser.DevfileObj, componentName string,
    19  	context string, fs devfilefs.Filesystem) ([]unstructured.Unstructured, error) {
    20  
    21  	strCRD, err := GetK8sManifestsWithVariablesSubstituted(devfileObj, componentName, context, fs)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	var uList []unstructured.Unstructured
    27  	// Use the decoder to correctly read file with multiple manifests
    28  	decoder := yaml3.NewDecoder(bytes.NewBufferString(strCRD))
    29  	for {
    30  		var decodeU unstructured.Unstructured
    31  		if err = decoder.Decode(&decodeU.Object); err != nil {
    32  			if err == io.EOF {
    33  				break
    34  			}
    35  			return nil, err
    36  		}
    37  
    38  		// Marshal the object's data so that it can be unmarshalled again into unstructured.Unstructured object
    39  		// We do this again because yaml3 "gopkg.in/yaml.v3" pkg is unable to properly unmarshal the data into an unstructured object
    40  		rawData, err := yaml3.Marshal(decodeU.Object)
    41  		if err != nil {
    42  			return nil, err
    43  		}
    44  
    45  		// Use "sigs.k8s.io/yaml" pkg to correctly unmarshal the data into an unstructured object;
    46  		var u unstructured.Unstructured
    47  		if err = yaml.Unmarshal(rawData, &u.Object); err != nil {
    48  			return nil, err
    49  		}
    50  
    51  		uList = append(uList, u)
    52  
    53  	}
    54  	return uList, nil
    55  }
    56  
    57  // ListKubernetesComponents lists all the kubernetes components from the devfile
    58  func ListKubernetesComponents(devfileObj parser.DevfileObj, path string) (list []unstructured.Unstructured, err error) {
    59  	components, err := devfileObj.Data.GetComponents(common.DevfileOptions{
    60  		ComponentOptions: common.ComponentOptions{ComponentType: v1alpha2.KubernetesComponentType},
    61  	})
    62  	if err != nil {
    63  		return
    64  	}
    65  	var u []unstructured.Unstructured
    66  	for _, kComponent := range components {
    67  		if kComponent.Kubernetes != nil {
    68  			u, err = GetK8sComponentAsUnstructuredList(devfileObj, kComponent.Name, path, devfilefs.DefaultFs{})
    69  			if err != nil {
    70  				return
    71  			}
    72  			list = append(list, u...)
    73  		}
    74  	}
    75  	return
    76  }
    77  
    78  // ListOpenShiftComponents lists all the openshift components from the devfile
    79  func ListOpenShiftComponents(devfileObj parser.DevfileObj, path string) (list []unstructured.Unstructured, err error) {
    80  	components, err := devfileObj.Data.GetComponents(common.DevfileOptions{
    81  		ComponentOptions: common.ComponentOptions{ComponentType: v1alpha2.OpenshiftComponentType},
    82  	})
    83  	if err != nil {
    84  		return
    85  	}
    86  	var u []unstructured.Unstructured
    87  	for _, ocComponent := range components {
    88  		if ocComponent.Openshift != nil {
    89  			u, err = GetK8sComponentAsUnstructuredList(devfileObj, ocComponent.Name, path, devfilefs.DefaultFs{})
    90  			if err != nil {
    91  				return
    92  			}
    93  			list = append(list, u...)
    94  		}
    95  	}
    96  	return
    97  }
    98  
    99  // AddKubernetesComponentToDevfile adds a resource definition to devfile object as an inlined Kubernetes component
   100  func AddKubernetesComponentToDevfile(crd, name string, devfileObj parser.DevfileObj) (parser.DevfileObj, error) {
   101  	err := devfileObj.Data.AddComponents([]v1alpha2.Component{{
   102  		Name: name,
   103  		ComponentUnion: v1alpha2.ComponentUnion{
   104  			Kubernetes: &v1alpha2.KubernetesComponent{
   105  				K8sLikeComponent: v1alpha2.K8sLikeComponent{
   106  					BaseComponent: v1alpha2.BaseComponent{},
   107  					K8sLikeComponentLocation: v1alpha2.K8sLikeComponentLocation{
   108  						Inlined: crd,
   109  					},
   110  				},
   111  			},
   112  		},
   113  	}})
   114  	if err != nil {
   115  		return devfileObj, err
   116  	}
   117  
   118  	return devfileObj, nil
   119  }
   120  

View as plain text