2022-02-10 11:25:03 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/kubernetes"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
|
|
)
|
|
|
|
|
|
|
|
func helmExecute(config helmExecuteOptions, telemetryData *telemetry.CustomData) {
|
|
|
|
helmConfig := kubernetes.HelmExecuteOptions{
|
2022-03-17 18:13:34 +02:00
|
|
|
ChartPath: config.ChartPath,
|
|
|
|
Image: config.Image,
|
|
|
|
Namespace: config.Namespace,
|
|
|
|
KubeContext: config.KubeContext,
|
|
|
|
KubeConfig: config.KubeConfig,
|
|
|
|
HelmDeployWaitSeconds: config.HelmDeployWaitSeconds,
|
|
|
|
AppVersion: config.AppVersion,
|
|
|
|
DependencyUpdate: config.DependencyUpdate,
|
|
|
|
HelmValues: config.HelmValues,
|
|
|
|
FilterTest: config.FilterTest,
|
|
|
|
DumpLogs: config.DumpLogs,
|
|
|
|
TargetRepositoryURL: config.TargetRepositoryURL,
|
|
|
|
TargetRepositoryName: config.TargetRepositoryName,
|
|
|
|
TargetRepositoryUser: config.TargetRepositoryUser,
|
|
|
|
TargetRepositoryPassword: config.TargetRepositoryPassword,
|
|
|
|
HelmCommand: config.HelmCommand,
|
|
|
|
CustomTLSCertificateLinks: config.CustomTLSCertificateLinks,
|
|
|
|
}
|
|
|
|
|
|
|
|
utils := kubernetes.NewDeployUtilsBundle(helmConfig.CustomTLSCertificateLinks)
|
|
|
|
|
|
|
|
helmChart := config.ChartPath + "Chart.yaml"
|
|
|
|
nameChart, packageVersion, err := kubernetes.GetChartInfo(helmChart, utils)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatalf("failed to get version in Chart.yaml: %v", err)
|
2022-02-10 11:25:03 +02:00
|
|
|
}
|
2022-03-17 18:13:34 +02:00
|
|
|
helmConfig.DeploymentName = nameChart
|
|
|
|
helmConfig.PackageVersion = packageVersion
|
2022-02-10 11:25:03 +02:00
|
|
|
|
|
|
|
helmExecutor := kubernetes.NewHelmExecutor(helmConfig, utils, GeneralConfig.Verbose, log.Writer())
|
|
|
|
|
|
|
|
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
|
2022-03-17 18:13:34 +02:00
|
|
|
if err := runHelmExecute(config, helmExecutor); err != nil {
|
2022-02-10 11:25:03 +02:00
|
|
|
log.Entry().WithError(err).Fatalf("step execution failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 18:13:34 +02:00
|
|
|
func runHelmExecute(config helmExecuteOptions, helmExecutor kubernetes.HelmExecutor) error {
|
|
|
|
switch config.HelmCommand {
|
2022-02-10 11:25:03 +02:00
|
|
|
case "upgrade":
|
|
|
|
if err := helmExecutor.RunHelmUpgrade(); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute upgrade: %v", err)
|
|
|
|
}
|
|
|
|
case "lint":
|
|
|
|
if err := helmExecutor.RunHelmLint(); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute helm lint: %v", err)
|
|
|
|
}
|
|
|
|
case "install":
|
|
|
|
if err := helmExecutor.RunHelmInstall(); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute helm install: %v", err)
|
|
|
|
}
|
|
|
|
case "test":
|
|
|
|
if err := helmExecutor.RunHelmTest(); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute helm test: %v", err)
|
|
|
|
}
|
|
|
|
case "uninstall":
|
|
|
|
if err := helmExecutor.RunHelmUninstall(); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute helm uninstall: %v", err)
|
|
|
|
}
|
|
|
|
case "package":
|
|
|
|
if err := helmExecutor.RunHelmPackage(); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute helm package: %v", err)
|
|
|
|
}
|
2022-03-17 18:13:34 +02:00
|
|
|
case "publish":
|
|
|
|
if err := helmExecutor.RunHelmPublish(); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute helm publish: %v", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if err := runHelmExecuteDefault(config, helmExecutor); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runHelmExecuteDefault(config helmExecuteOptions, helmExecutor kubernetes.HelmExecutor) error {
|
|
|
|
if err := helmExecutor.RunHelmLint(); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute helm lint: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := helmExecutor.RunHelmPackage(); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute helm package: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Publish {
|
|
|
|
if err := helmExecutor.RunHelmPublish(); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute helm publish: %v", err)
|
2022-02-10 11:25:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|