1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-10-30 23:57:50 +02:00

Add source repo for helmExecute (#4082)

Co-authored-by: Ralf Pannemans <ralf.pannemans@sap.com>

Co-authored-by: Ralf Pannemans <ralf.pannemans@sap.com>
This commit is contained in:
Johannes Dillmann
2022-11-07 16:35:00 +01:00
committed by GitHub
parent c802aa512d
commit 5f643826f5
5 changed files with 172 additions and 15 deletions

View File

@@ -33,6 +33,10 @@ func helmExecute(config helmExecuteOptions, telemetryData *telemetry.CustomData,
TargetRepositoryName: config.TargetRepositoryName,
TargetRepositoryUser: config.TargetRepositoryUser,
TargetRepositoryPassword: config.TargetRepositoryPassword,
SourceRepositoryName: config.SourceRepositoryName,
SourceRepositoryURL: config.SourceRepositoryURL,
SourceRepositoryUser: config.SourceRepositoryUser,
SourceRepositoryPassword: config.SourceRepositoryPassword,
HelmCommand: config.HelmCommand,
CustomTLSCertificateLinks: config.CustomTLSCertificateLinks,
Version: config.Version,

View File

@@ -24,6 +24,10 @@ type helmExecuteOptions struct {
TargetRepositoryName string `json:"targetRepositoryName,omitempty"`
TargetRepositoryUser string `json:"targetRepositoryUser,omitempty"`
TargetRepositoryPassword string `json:"targetRepositoryPassword,omitempty"`
SourceRepositoryURL string `json:"sourceRepositoryURL,omitempty"`
SourceRepositoryName string `json:"sourceRepositoryName,omitempty"`
SourceRepositoryUser string `json:"sourceRepositoryUser,omitempty"`
SourceRepositoryPassword string `json:"sourceRepositoryPassword,omitempty"`
HelmDeployWaitSeconds int `json:"helmDeployWaitSeconds,omitempty"`
HelmValues []string `json:"helmValues,omitempty"`
Image string `json:"image,omitempty"`
@@ -126,6 +130,8 @@ Note: piper supports only helm3 version, since helm2 is deprecated.`,
}
log.RegisterSecret(stepConfig.TargetRepositoryUser)
log.RegisterSecret(stepConfig.TargetRepositoryPassword)
log.RegisterSecret(stepConfig.SourceRepositoryUser)
log.RegisterSecret(stepConfig.SourceRepositoryPassword)
log.RegisterSecret(stepConfig.KubeConfig)
log.RegisterSecret(stepConfig.DockerConfigJSON)
@@ -197,6 +203,10 @@ func addHelmExecuteFlags(cmd *cobra.Command, stepConfig *helmExecuteOptions) {
cmd.Flags().StringVar(&stepConfig.TargetRepositoryName, "targetRepositoryName", os.Getenv("PIPER_targetRepositoryName"), "set the chart repository. The value is required for install/upgrade/uninstall commands.")
cmd.Flags().StringVar(&stepConfig.TargetRepositoryUser, "targetRepositoryUser", os.Getenv("PIPER_targetRepositoryUser"), "Username for the chart repository where the compiled helm .tgz archive shall be uploaded - typically provided by the CI/CD environment.")
cmd.Flags().StringVar(&stepConfig.TargetRepositoryPassword, "targetRepositoryPassword", os.Getenv("PIPER_targetRepositoryPassword"), "Password for the target repository where the compiled helm .tgz archive shall be uploaded - typically provided by the CI/CD environment.")
cmd.Flags().StringVar(&stepConfig.SourceRepositoryURL, "sourceRepositoryURL", os.Getenv("PIPER_sourceRepositoryURL"), "URL of the source repository where the dependencies can be downloaded.")
cmd.Flags().StringVar(&stepConfig.SourceRepositoryName, "sourceRepositoryName", os.Getenv("PIPER_sourceRepositoryName"), "Set the name of the chart repository. The value might be required for fetching dependencies.")
cmd.Flags().StringVar(&stepConfig.SourceRepositoryUser, "sourceRepositoryUser", os.Getenv("PIPER_sourceRepositoryUser"), "Username for the chart repository for fetching the dependencies.")
cmd.Flags().StringVar(&stepConfig.SourceRepositoryPassword, "sourceRepositoryPassword", os.Getenv("PIPER_sourceRepositoryPassword"), "Password for the chart repository for fetching the dependencies.")
cmd.Flags().IntVar(&stepConfig.HelmDeployWaitSeconds, "helmDeployWaitSeconds", 300, "Number of seconds before helm deploy returns.")
cmd.Flags().StringSliceVar(&stepConfig.HelmValues, "helmValues", []string{}, "List of helm values as YAML file reference or URL (as per helm parameter description for `-f` / `--values`)")
cmd.Flags().StringVar(&stepConfig.Image, "image", os.Getenv("PIPER_image"), "Full name of the image to be deployed.")
@@ -345,6 +355,66 @@ func helmExecuteMetadata() config.StepData {
Aliases: []config.Alias{{Name: "helmRepositoryPassword"}},
Default: os.Getenv("PIPER_targetRepositoryPassword"),
},
{
Name: "sourceRepositoryURL",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_sourceRepositoryURL"),
},
{
Name: "sourceRepositoryName",
ResourceRef: []config.ResourceReference{},
Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_sourceRepositoryName"),
},
{
Name: "sourceRepositoryUser",
ResourceRef: []config.ResourceReference{
{
Name: "sourceRepositoryCredentialsId",
Param: "username",
Type: "secret",
},
{
Name: "sourceRepositoryUserSecretName",
Type: "vaultSecret",
Default: "dependencies",
},
},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_sourceRepositoryUser"),
},
{
Name: "sourceRepositoryPassword",
ResourceRef: []config.ResourceReference{
{
Name: "sourceRepositoryCredentialsId",
Param: "password",
Type: "secret",
},
{
Name: "sourceRepositoryPasswordSecret",
Type: "vaultSecret",
Default: "dependencies",
},
},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_sourceRepositoryPassword"),
},
{
Name: "helmDeployWaitSeconds",
ResourceRef: []config.ResourceReference{},

View File

@@ -55,6 +55,10 @@ type HelmExecuteOptions struct {
TargetRepositoryName string `json:"targetRepositoryName,omitempty"`
TargetRepositoryUser string `json:"targetRepositoryUser,omitempty"`
TargetRepositoryPassword string `json:"targetRepositoryPassword,omitempty"`
SourceRepositoryURL string `json:"sourceRepositoryURL,omitempty"`
SourceRepositoryName string `json:"sourceRepositoryName,omitempty"`
SourceRepositoryUser string `json:"sourceRepositoryUser,omitempty"`
SourceRepositoryPassword string `json:"sourceRepositoryPassword,omitempty"`
HelmCommand string `json:"helmCommand,omitempty"`
CustomTLSCertificateLinks []string `json:"customTlsCertificateLinks,omitempty"`
}
@@ -89,25 +93,25 @@ func (h *HelmExecute) runHelmInit() error {
}
// runHelmAdd is used to add a chart repository
func (h *HelmExecute) runHelmAdd() error {
func (h *HelmExecute) runHelmAdd(name, url, user, password string) error {
helmParams := []string{
"repo",
"add",
}
if len(h.config.TargetRepositoryName) == 0 {
return fmt.Errorf("there is no TargetRepositoryName value. 'helm repo add' command requires 2 arguments")
if len(name) == 0 {
return fmt.Errorf("there is no RepositoryName value. 'helm repo add' command requires 2 arguments")
}
if len(h.config.TargetRepositoryURL) == 0 {
return fmt.Errorf("there is no TargetRepositoryURL value. 'helm repo add' command requires 2 arguments")
if len(url) == 0 {
return fmt.Errorf("there is no RepositoryURL value. 'helm repo add' command requires 2 arguments")
}
if len(h.config.TargetRepositoryUser) != 0 {
helmParams = append(helmParams, "--username", h.config.TargetRepositoryUser)
if len(user) != 0 {
helmParams = append(helmParams, "--username", user)
}
if len(h.config.TargetRepositoryPassword) != 0 {
helmParams = append(helmParams, "--password", h.config.TargetRepositoryPassword)
if len(password) != 0 {
helmParams = append(helmParams, "--password", password)
}
helmParams = append(helmParams, h.config.TargetRepositoryName)
helmParams = append(helmParams, h.config.TargetRepositoryURL)
helmParams = append(helmParams, name)
helmParams = append(helmParams, url)
if h.verbose {
helmParams = append(helmParams, "--debug")
}
@@ -132,7 +136,7 @@ func (h *HelmExecute) RunHelmUpgrade() error {
}
if len(h.config.ChartPath) == 0 {
if err := h.runHelmAdd(); err != nil {
if err := h.runHelmAdd(h.config.TargetRepositoryName, h.config.TargetRepositoryURL, h.config.TargetRepositoryUser, h.config.TargetRepositoryPassword); err != nil {
return fmt.Errorf("failed to add a chart repository: %v", err)
}
helmParams = append(helmParams, h.config.TargetRepositoryName)
@@ -217,7 +221,7 @@ func (h *HelmExecute) RunHelmInstall() error {
}
if len(h.config.ChartPath) == 0 {
if err := h.runHelmAdd(); err != nil {
if err := h.runHelmAdd(h.config.TargetRepositoryName, h.config.TargetRepositoryURL, h.config.TargetRepositoryUser, h.config.TargetRepositoryPassword); err != nil {
return fmt.Errorf("failed to add a chart repository: %v", err)
}
helmParams = append(helmParams, h.config.TargetRepositoryName)
@@ -361,6 +365,12 @@ func (h *HelmExecute) RunHelmDependency() error {
return fmt.Errorf("there is no dependency value. Possible values are build, list, update")
}
if len(h.config.SourceRepositoryName) > 0 && len(h.config.SourceRepositoryURL) > 0 {
if err := h.runHelmAdd(h.config.SourceRepositoryName, h.config.SourceRepositoryURL, h.config.SourceRepositoryUser, h.config.SourceRepositoryPassword); err != nil {
log.Entry().WithError(err).Fatal("Helm repo call failed")
}
}
helmParams := []string{
"dependency",
}
@@ -392,7 +402,7 @@ func (h *HelmExecute) RunHelmDependency() error {
return nil
}
//RunHelmPublish is used to upload a chart to a registry
// RunHelmPublish is used to upload a chart to a registry
func (h *HelmExecute) RunHelmPublish() (string, error) {
err := h.runHelmInit()
if err != nil {

View File

@@ -100,7 +100,7 @@ func TestRunHelmAdd(t *testing.T) {
verbose: testCase.generalVerbose,
stdout: log.Writer(),
}
err := helmExecute.runHelmAdd()
err := helmExecute.runHelmAdd(testCase.config.TargetRepositoryName, testCase.config.TargetRepositoryURL, testCase.config.TargetRepositoryUser, testCase.config.TargetRepositoryPassword)
if testCase.expectedError != nil {
assert.Error(t, err)
} else {
@@ -460,6 +460,34 @@ func TestRunHelmDependency(t *testing.T) {
{Exec: "helm", Params: []string{"dependency", "update", "."}},
},
},
{
config: HelmExecuteOptions{
ChartPath: ".",
Dependency: "update",
SourceRepositoryName: "foo",
SourceRepositoryURL: "bar",
},
expectedError: nil,
expectedExecCalls: []mock.ExecCall{
{Exec: "helm", Params: []string{"repo", "add", "foo", "bar"}},
{Exec: "helm", Params: []string{"dependency", "update", "."}},
},
},
{
config: HelmExecuteOptions{
ChartPath: ".",
Dependency: "update",
SourceRepositoryName: "foo",
SourceRepositoryURL: "bar",
SourceRepositoryUser: "username",
SourceRepositoryPassword: "password",
},
expectedError: nil,
expectedExecCalls: []mock.ExecCall{
{Exec: "helm", Params: []string{"repo", "add", "--username", "username", "--password", "password", "foo", "bar"}},
{Exec: "helm", Params: []string{"dependency", "update", "."}},
},
},
}
for i, testCase := range testTable {

View File

@@ -123,6 +123,51 @@ spec:
param: custom/helmRepositoryPassword
- name: commonPipelineEnvironment
param: custom/repositoryPassword
- name: sourceRepositoryURL
description: "URL of the source repository where the dependencies can be downloaded."
type: string
scope:
- PARAMETERS
- STAGES
- STEPS
- name: sourceRepositoryName
type: string
description: Set the name of the chart repository. The value might be required for fetching dependencies.
scope:
- GENERAL
- PARAMETERS
- STAGES
- STEPS
- name: sourceRepositoryUser
description: "Username for the chart repository for fetching the dependencies."
type: string
scope:
- PARAMETERS
- STAGES
- STEPS
secret: true
resourceRef:
- name: sourceRepositoryCredentialsId
type: secret
param: username
- type: vaultSecret
name: sourceRepositoryUserSecretName
default: dependencies
- name: sourceRepositoryPassword
description: "Password for the chart repository for fetching the dependencies."
type: string
scope:
- PARAMETERS
- STAGES
- STEPS
secret: true
resourceRef:
- name: sourceRepositoryCredentialsId
type: secret
param: password
- type: vaultSecret
name: sourceRepositoryPasswordSecret
default: dependencies
- name: helmDeployWaitSeconds
type: int
description: Number of seconds before helm deploy returns.