1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

feat(kubernetesDeploy): allow disabling --force (#2495)

fixes #1731
This commit is contained in:
Oliver Nocon 2021-01-07 16:52:16 +01:00 committed by GitHub
parent 5f5450449e
commit ab3362849a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 5 deletions

View File

@ -149,12 +149,15 @@ func runHelmDeploy(config kubernetesDeployOptions, command command.ExecRunner, s
upgradeParams = append(
upgradeParams,
"--install",
"--force",
"--namespace", config.Namespace,
"--set",
fmt.Sprintf("image.repository=%v/%v,image.tag=%v%v%v", containerRegistry, containerImageName, containerImageTag, secretsData, ingressHosts),
)
if config.ForceUpdates {
upgradeParams = append(upgradeParams, "--force")
}
if config.DeployTool == "helm" {
upgradeParams = append(upgradeParams, "--wait", "--timeout", strconv.Itoa(config.HelmDeployWaitSeconds))
}

View File

@ -25,6 +25,7 @@ type kubernetesDeployOptions struct {
CreateDockerRegistrySecret bool `json:"createDockerRegistrySecret,omitempty"`
DeploymentName string `json:"deploymentName,omitempty"`
DeployTool string `json:"deployTool,omitempty"`
ForceUpdates bool `json:"forceUpdates,omitempty"`
HelmDeployWaitSeconds int `json:"helmDeployWaitSeconds,omitempty"`
HelmValues []string `json:"helmValues,omitempty"`
Image string `json:"image,omitempty"`
@ -126,6 +127,7 @@ func addKubernetesDeployFlags(cmd *cobra.Command, stepConfig *kubernetesDeployOp
cmd.Flags().BoolVar(&stepConfig.CreateDockerRegistrySecret, "createDockerRegistrySecret", false, "Only for `deployTool:kubectl`: Toggle to turn on `containerRegistrySecret` creation.")
cmd.Flags().StringVar(&stepConfig.DeploymentName, "deploymentName", os.Getenv("PIPER_deploymentName"), "Defines the name of the deployment.")
cmd.Flags().StringVar(&stepConfig.DeployTool, "deployTool", `kubectl`, "Defines the tool which should be used for deployment.")
cmd.Flags().BoolVar(&stepConfig.ForceUpdates, "forceUpdates", true, "Helm only: force resource updates with helm parameter `--force`")
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.")
@ -260,6 +262,14 @@ func kubernetesDeployMetadata() config.StepData {
Mandatory: true,
Aliases: []config.Alias{},
},
{
Name: "forceUpdates",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "bool",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "helmDeployWaitSeconds",
ResourceRef: []config.ResourceReference{},

View File

@ -24,6 +24,7 @@ func TestRunKubernetesDeploy(t *testing.T) {
ChartPath: "path/to/chart",
DeploymentName: "deploymentName",
DeployTool: "helm",
ForceUpdates: true,
HelmDeployWaitSeconds: 400,
IngressHosts: []string{"ingress.host1", "ingress.host2"},
Image: "path/to/Image:latest",
@ -56,11 +57,11 @@ func TestRunKubernetesDeploy(t *testing.T) {
"deploymentName",
"path/to/chart",
"--install",
"--force",
"--namespace",
"deploymentNamespace",
"--set",
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret,ingress.hosts[0]=ingress.host1,ingress.hosts[1]=ingress.host2",
"--force",
"--wait",
"--timeout",
"400",
@ -81,6 +82,7 @@ func TestRunKubernetesDeploy(t *testing.T) {
ChartPath: "path/to/chart",
DeploymentName: "deploymentName",
DeployTool: "helm",
ForceUpdates: true,
HelmDeployWaitSeconds: 400,
IngressHosts: []string{"ingress.host1", "ingress.host2"},
Image: "path/to/Image:latest",
@ -114,11 +116,11 @@ func TestRunKubernetesDeploy(t *testing.T) {
"deploymentName",
"path/to/chart",
"--install",
"--force",
"--namespace",
"deploymentNamespace",
"--set",
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret,ingress.hosts[0]=ingress.host1,ingress.hosts[1]=ingress.host2",
"--force",
"--wait",
"--timeout",
"400",
@ -138,6 +140,7 @@ func TestRunKubernetesDeploy(t *testing.T) {
ChartPath: "path/to/chart",
DeploymentName: "deploymentName",
DeployTool: "helm3",
ForceUpdates: true,
HelmDeployWaitSeconds: 400,
HelmValues: []string{"values1.yaml", "values2.yaml"},
Image: "path/to/Image:latest",
@ -171,11 +174,11 @@ func TestRunKubernetesDeploy(t *testing.T) {
"--values",
"values2.yaml",
"--install",
"--force",
"--namespace",
"deploymentNamespace",
"--set",
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret",
"--force",
"--wait",
"--timeout",
"400s",
@ -196,6 +199,7 @@ func TestRunKubernetesDeploy(t *testing.T) {
ChartPath: "path/to/chart",
DeploymentName: "deploymentName",
DeployTool: "helm3",
ForceUpdates: true,
HelmDeployWaitSeconds: 400,
HelmValues: []string{"values1.yaml", "values2.yaml"},
Image: "path/to/Image:latest",
@ -230,11 +234,11 @@ func TestRunKubernetesDeploy(t *testing.T) {
"--values",
"values2.yaml",
"--install",
"--force",
"--namespace",
"deploymentNamespace",
"--set",
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret",
"--force",
"--wait",
"--timeout",
"400s",
@ -252,6 +256,7 @@ func TestRunKubernetesDeploy(t *testing.T) {
ContainerRegistrySecret: "testSecret",
DeploymentName: "deploymentName",
DeployTool: "helm3",
ForceUpdates: true,
HelmDeployWaitSeconds: 400,
IngressHosts: []string{},
Image: "path/to/Image:latest",
@ -272,7 +277,46 @@ func TestRunKubernetesDeploy(t *testing.T) {
"deploymentName",
"path/to/chart",
"--install",
"--namespace",
"deploymentNamespace",
"--set",
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,imagePullSecrets[0].name=testSecret",
"--force",
"--wait",
"--timeout",
"400s",
"--atomic",
"--kube-context",
"testCluster",
"--testParam",
"testValue",
}, e.Calls[0].Params, "Wrong upgrade parameters")
})
t.Run("test helm v3 - no force", func(t *testing.T) {
opts := kubernetesDeployOptions{
ContainerRegistryURL: "https://my.registry:55555",
ChartPath: "path/to/chart",
ContainerRegistrySecret: "testSecret",
DeploymentName: "deploymentName",
DeployTool: "helm3",
HelmDeployWaitSeconds: 400,
IngressHosts: []string{},
Image: "path/to/Image:latest",
AdditionalParameters: []string{"--testParam", "testValue"},
KubeContext: "testCluster",
Namespace: "deploymentNamespace",
}
e := mock.ExecMockRunner{}
var stdout bytes.Buffer
runKubernetesDeploy(opts, &e, &stdout)
assert.Equal(t, []string{
"upgrade",
"deploymentName",
"path/to/chart",
"--install",
"--namespace",
"deploymentNamespace",
"--set",

View File

@ -170,6 +170,15 @@ spec:
- kubectl
- helm
- helm3
- name: forceUpdates
type: bool
description: "Helm only: force resource updates with helm parameter `--force`"
mandatory: false
scope:
- PARAMETERS
- STAGES
- STEPS
default: true
- name: helmDeployWaitSeconds
type: int
description: Number of seconds before helm deploy returns.