You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	| @@ -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)) | ||||
| 	} | ||||
|   | ||||
| @@ -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{}, | ||||
|   | ||||
| @@ -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", | ||||
|   | ||||
| @@ -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. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user