1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/helmExecute.go
Vitalii Sidorov 63cdfc0e68
Fix helm execute command (#3701)
* Add small fix

* fix unit-tests

* Add deploymentName and packageVersion as flags

* small fix

* Change getting version of helm chart

* small fix

Co-authored-by: “Vitalii <“vitalii.sidorov@sap.com”>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-04-14 15:43:47 +02:00

120 lines
3.9 KiB
Go

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"
"github.com/SAP/jenkins-library/pkg/versioning"
)
func helmExecute(config helmExecuteOptions, telemetryData *telemetry.CustomData) {
helmConfig := kubernetes.HelmExecuteOptions{
ChartPath: config.ChartPath,
Image: config.Image,
Namespace: config.Namespace,
KubeContext: config.KubeContext,
KubeConfig: config.KubeConfig,
HelmDeployWaitSeconds: config.HelmDeployWaitSeconds,
AppVersion: config.AppVersion,
Dependency: config.Dependency,
PackageDependencyUpdate: config.PackageDependencyUpdate,
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,
// ArtifactVersion: config.Version,
Version: config.Version,
}
utils := kubernetes.NewDeployUtilsBundle(helmConfig.CustomTLSCertificateLinks)
artifactOpts := versioning.Options{
VersioningScheme: "library",
}
artifact, err := versioning.GetArtifact("helm", "", &artifactOpts, utils)
if err != nil {
log.Entry().WithError(err).Fatalf("getting artifact information failed: %v", err)
}
artifactInfo, err := artifact.GetCoordinates()
helmConfig.DeploymentName = artifactInfo.ArtifactID
if len(config.Version) == 0 {
helmConfig.Version = artifactInfo.Version
}
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
if err := runHelmExecute(config, helmExecutor); err != nil {
log.Entry().WithError(err).Fatalf("step execution failed: %v", err)
}
}
func runHelmExecute(config helmExecuteOptions, helmExecutor kubernetes.HelmExecutor) error {
switch config.HelmCommand {
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 "dependency":
if err := helmExecutor.RunHelmDependency(); err != nil {
return fmt.Errorf("failed to execute helm dependency: %v", err)
}
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 len(config.Dependency) > 0 {
if err := helmExecutor.RunHelmDependency(); err != nil {
return fmt.Errorf("failed to execute helm dependency: %v", err)
}
}
if config.Publish {
if err := helmExecutor.RunHelmPublish(); err != nil {
return fmt.Errorf("failed to execute helm publish: %v", err)
}
}
return nil
}