...

Source file src/github.com/redhat-developer/odo/pkg/component/validate.go

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

     1  package component
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	devfile "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     9  	"github.com/devfile/library/v2/pkg/devfile/parser"
    10  	devfilefs "github.com/devfile/library/v2/pkg/testingutil/filesystem"
    11  
    12  	"github.com/redhat-developer/odo/pkg/kclient"
    13  	"github.com/redhat-developer/odo/pkg/libdevfile"
    14  )
    15  
    16  // ValidateResourcesExist validates if the Kubernetes inlined components are installed on the cluster
    17  func ValidateResourcesExist(client kclient.ClientInterface, devfileObj parser.DevfileObj, k8sComponents []devfile.Component, context string) error {
    18  	if len(k8sComponents) == 0 {
    19  		return nil
    20  	}
    21  
    22  	var unsupportedResources []string
    23  	for _, c := range k8sComponents {
    24  		kindErr, err := ValidateResourcesExistInK8sComponent(client, devfileObj, c, context)
    25  		if err != nil {
    26  			if kindErr != "" {
    27  				unsupportedResources = append(unsupportedResources, kindErr)
    28  			} else {
    29  				return err
    30  			}
    31  		}
    32  	}
    33  
    34  	if len(unsupportedResources) > 0 {
    35  		// tell the user about all the unsupported resources in one message
    36  		return fmt.Errorf("following resource(s) in the devfile are not supported by your cluster; please install corresponding Operator(s) before doing \"odo dev\": %s", strings.Join(unsupportedResources, ", "))
    37  	}
    38  	return nil
    39  }
    40  
    41  // ValidateResourcesExistInK8sComponent validates if resources defined inside a Kubernetes inlined component are installed on the cluster
    42  func ValidateResourcesExistInK8sComponent(client kclient.ClientInterface, devfileObj parser.DevfileObj, k8sComponent devfile.Component, context string) (kindErr string, err error) {
    43  	// get the string representation of the YAML definition of a CRD
    44  	uList, err := libdevfile.GetK8sComponentAsUnstructuredList(devfileObj, k8sComponent.Name, context, devfilefs.DefaultFs{})
    45  	if err != nil {
    46  		return "", err
    47  	}
    48  	for _, u := range uList {
    49  		_, err = client.GetRestMappingFromUnstructured(u)
    50  		if err != nil && u.GetKind() != "ServiceBinding" {
    51  			// getting a RestMapping would fail if there are no matches for the Kind field on the cluster;
    52  			// but if it's a "ServiceBinding" resource, we don't add it to unsupported list because odo can create links
    53  			// without having SBO installed
    54  			return u.GetKind(), errors.New("resource not supported")
    55  		}
    56  	}
    57  	return "", nil
    58  }
    59  

View as plain text