github.com/xgoffin/jenkins-library@v1.154.0/cmd/helmExecute.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/SAP/jenkins-library/pkg/kubernetes"
     7  	"github.com/SAP/jenkins-library/pkg/log"
     8  	"github.com/SAP/jenkins-library/pkg/telemetry"
     9  )
    10  
    11  func helmExecute(config helmExecuteOptions, telemetryData *telemetry.CustomData) {
    12  	helmConfig := kubernetes.HelmExecuteOptions{
    13  		ChartPath:                 config.ChartPath,
    14  		Image:                     config.Image,
    15  		Namespace:                 config.Namespace,
    16  		KubeContext:               config.KubeContext,
    17  		KubeConfig:                config.KubeConfig,
    18  		HelmDeployWaitSeconds:     config.HelmDeployWaitSeconds,
    19  		AppVersion:                config.AppVersion,
    20  		DependencyUpdate:          config.DependencyUpdate,
    21  		HelmValues:                config.HelmValues,
    22  		FilterTest:                config.FilterTest,
    23  		DumpLogs:                  config.DumpLogs,
    24  		TargetRepositoryURL:       config.TargetRepositoryURL,
    25  		TargetRepositoryName:      config.TargetRepositoryName,
    26  		TargetRepositoryUser:      config.TargetRepositoryUser,
    27  		TargetRepositoryPassword:  config.TargetRepositoryPassword,
    28  		HelmCommand:               config.HelmCommand,
    29  		CustomTLSCertificateLinks: config.CustomTLSCertificateLinks,
    30  	}
    31  
    32  	utils := kubernetes.NewDeployUtilsBundle(helmConfig.CustomTLSCertificateLinks)
    33  
    34  	helmChart := config.ChartPath + "Chart.yaml"
    35  	nameChart, packageVersion, err := kubernetes.GetChartInfo(helmChart, utils)
    36  	if err != nil {
    37  		log.Entry().WithError(err).Fatalf("failed to get version in Chart.yaml: %v", err)
    38  	}
    39  	helmConfig.DeploymentName = nameChart
    40  	helmConfig.PackageVersion = packageVersion
    41  
    42  	helmExecutor := kubernetes.NewHelmExecutor(helmConfig, utils, GeneralConfig.Verbose, log.Writer())
    43  
    44  	// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
    45  	if err := runHelmExecute(config, helmExecutor); err != nil {
    46  		log.Entry().WithError(err).Fatalf("step execution failed: %v", err)
    47  	}
    48  }
    49  
    50  func runHelmExecute(config helmExecuteOptions, helmExecutor kubernetes.HelmExecutor) error {
    51  	switch config.HelmCommand {
    52  	case "upgrade":
    53  		if err := helmExecutor.RunHelmUpgrade(); err != nil {
    54  			return fmt.Errorf("failed to execute upgrade: %v", err)
    55  		}
    56  	case "lint":
    57  		if err := helmExecutor.RunHelmLint(); err != nil {
    58  			return fmt.Errorf("failed to execute helm lint: %v", err)
    59  		}
    60  	case "install":
    61  		if err := helmExecutor.RunHelmInstall(); err != nil {
    62  			return fmt.Errorf("failed to execute helm install: %v", err)
    63  		}
    64  	case "test":
    65  		if err := helmExecutor.RunHelmTest(); err != nil {
    66  			return fmt.Errorf("failed to execute helm test: %v", err)
    67  		}
    68  	case "uninstall":
    69  		if err := helmExecutor.RunHelmUninstall(); err != nil {
    70  			return fmt.Errorf("failed to execute helm uninstall: %v", err)
    71  		}
    72  	case "package":
    73  		if err := helmExecutor.RunHelmPackage(); err != nil {
    74  			return fmt.Errorf("failed to execute helm package: %v", err)
    75  		}
    76  	case "publish":
    77  		if err := helmExecutor.RunHelmPublish(); err != nil {
    78  			return fmt.Errorf("failed to execute helm publish: %v", err)
    79  		}
    80  	default:
    81  		if err := runHelmExecuteDefault(config, helmExecutor); err != nil {
    82  			return err
    83  		}
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func runHelmExecuteDefault(config helmExecuteOptions, helmExecutor kubernetes.HelmExecutor) error {
    90  	if err := helmExecutor.RunHelmLint(); err != nil {
    91  		return fmt.Errorf("failed to execute helm lint: %v", err)
    92  	}
    93  
    94  	if err := helmExecutor.RunHelmPackage(); err != nil {
    95  		return fmt.Errorf("failed to execute helm package: %v", err)
    96  	}
    97  
    98  	if config.Publish {
    99  		if err := helmExecutor.RunHelmPublish(); err != nil {
   100  			return fmt.Errorf("failed to execute helm publish: %v", err)
   101  		}
   102  	}
   103  
   104  	return nil
   105  }