...

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

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

     1  package component
     2  
     3  import (
     4  	"fmt"
     5  
     6  	devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     7  	"github.com/devfile/library/v2/pkg/devfile/parser"
     8  	devfilefs "github.com/devfile/library/v2/pkg/testingutil/filesystem"
     9  	"k8s.io/klog"
    10  
    11  	"github.com/redhat-developer/odo/pkg/kclient"
    12  	odolabels "github.com/redhat-developer/odo/pkg/labels"
    13  	"github.com/redhat-developer/odo/pkg/libdevfile"
    14  	"github.com/redhat-developer/odo/pkg/log"
    15  	"github.com/redhat-developer/odo/pkg/service"
    16  )
    17  
    18  // ApplyKubernetes contains the logic to create the k8s resources defined by the `apply` command
    19  // mode(Dev, Deploy): the mode in which the resources are deployed
    20  // appName: application name
    21  // devfile: the devfile object
    22  // kubernetes: the kubernetes devfile component to be deployed
    23  // kubeClient: Kubernetes client to be used to deploy the resource
    24  // path: path to the context directory
    25  func ApplyKubernetes(
    26  	mode string,
    27  	appName string,
    28  	componentName string,
    29  	devfile parser.DevfileObj,
    30  	kubernetes devfilev1.Component,
    31  	kubeClient kclient.ClientInterface,
    32  	path string,
    33  ) error {
    34  	// TODO: Use GetK8sComponentAsUnstructured here and pass it to ValidateResourcesExistInK8sComponent
    35  	// Validate if the GVRs represented by Kubernetes inlined components are supported by the underlying cluster
    36  	kind, err := ValidateResourcesExistInK8sComponent(kubeClient, devfile, kubernetes, path)
    37  	if err != nil {
    38  		return fmt.Errorf("%s: %w", kind, err)
    39  	}
    40  
    41  	// Get the most common labels that's applicable to all resources being deployed.
    42  	// Set the mode. Regardless of what Kubernetes resource we are deploying.
    43  	runtime := GetComponentRuntimeFromDevfileMetadata(devfile.Data.GetMetadata())
    44  	labels := odolabels.GetLabels(componentName, appName, runtime, mode, false)
    45  
    46  	klog.V(4).Infof("Injecting labels: %+v into k8s artifact", labels)
    47  
    48  	// Create the annotations
    49  	// Retrieve the component type from the devfile and also inject it into the list of annotations
    50  	annotations := make(map[string]string)
    51  	odolabels.SetProjectType(annotations, GetComponentTypeFromDevfileMetadata(devfile.Data.GetMetadata()))
    52  
    53  	// Get the Kubernetes component
    54  	uList, err := libdevfile.GetK8sComponentAsUnstructuredList(devfile, kubernetes.Name, path, devfilefs.DefaultFs{})
    55  	if err != nil {
    56  		return err
    57  	}
    58  	for _, u := range uList {
    59  		// Deploy the actual Kubernetes component and error out if there's an issue.
    60  		log.Sectionf("Deploying Kubernetes Component: %s", u.GetName())
    61  		err = service.PushKubernetesResource(kubeClient, u, labels, annotations, mode)
    62  		if err != nil {
    63  			return fmt.Errorf("failed to create service(s) associated with the component: %w", err)
    64  		}
    65  	}
    66  	return nil
    67  }
    68  

View as plain text