2020-01-24 15:30:27 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
2020-02-04 11:46:43 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
2020-01-24 15:30:27 +02:00
|
|
|
)
|
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
func kubernetesDeploy(config kubernetesDeployOptions, telemetryData *telemetry.CustomData) {
|
2020-06-30 10:48:30 +02:00
|
|
|
c := command.Command{
|
|
|
|
ErrorCategoryMapping: map[string][]string{
|
2020-10-05 17:46:44 +02:00
|
|
|
log.ErrorConfiguration.String(): {
|
|
|
|
"Error: Get * no such host",
|
2020-06-30 10:48:30 +02:00
|
|
|
"Error: path * not found",
|
2020-10-05 17:46:44 +02:00
|
|
|
"Error: rendered manifests contain a resource that already exists.",
|
|
|
|
"Error: unknown flag",
|
|
|
|
"Error: UPGRADE FAILED: * failed to replace object: * is invalid",
|
|
|
|
"Error: UPGRADE FAILED: * failed to create resource: * is invalid",
|
|
|
|
"Error: UPGRADE FAILED: an error occurred * not found",
|
2020-06-30 10:48:30 +02:00
|
|
|
"Error: UPGRADE FAILED: query: failed to query with labels:",
|
2020-10-05 17:46:44 +02:00
|
|
|
"Invalid value: \"\": field is immutable",
|
|
|
|
},
|
|
|
|
log.ErrorCustom.String(): {
|
|
|
|
"Error: release * failed, * timed out waiting for the condition",
|
2020-06-30 10:48:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-01-24 15:30:27 +02:00
|
|
|
// reroute stderr output to logging framework, stdout will be used for command interactions
|
2020-05-06 13:35:40 +02:00
|
|
|
c.Stderr(log.Writer())
|
|
|
|
runKubernetesDeploy(config, &c, log.Writer())
|
2020-01-24 15:30:27 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 11:28:16 +02:00
|
|
|
func runKubernetesDeploy(config kubernetesDeployOptions, command command.ExecRunner, stdout io.Writer) {
|
2020-04-24 09:37:11 +02:00
|
|
|
if config.DeployTool == "helm" || config.DeployTool == "helm3" {
|
2020-01-24 15:30:27 +02:00
|
|
|
runHelmDeploy(config, command, stdout)
|
|
|
|
} else {
|
|
|
|
runKubectlDeploy(config, command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 11:28:16 +02:00
|
|
|
func runHelmDeploy(config kubernetesDeployOptions, command command.ExecRunner, stdout io.Writer) {
|
2020-01-24 15:30:27 +02:00
|
|
|
_, containerRegistry, err := splitRegistryURL(config.ContainerRegistryURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatalf("Container registry url '%v' incorrect", config.ContainerRegistryURL)
|
|
|
|
}
|
|
|
|
containerImageName, containerImageTag, err := splitFullImageName(config.Image)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatalf("Container image '%v' incorrect", config.Image)
|
|
|
|
}
|
|
|
|
helmLogFields := map[string]interface{}{}
|
|
|
|
helmLogFields["Chart Path"] = config.ChartPath
|
|
|
|
helmLogFields["Namespace"] = config.Namespace
|
|
|
|
helmLogFields["Deployment Name"] = config.DeploymentName
|
|
|
|
helmLogFields["Context"] = config.KubeContext
|
|
|
|
helmLogFields["Kubeconfig"] = config.KubeConfig
|
|
|
|
log.Entry().WithFields(helmLogFields).Debug("Calling Helm")
|
|
|
|
|
|
|
|
helmEnv := []string{fmt.Sprintf("KUBECONFIG=%v", config.KubeConfig)}
|
2020-04-24 09:37:11 +02:00
|
|
|
if config.DeployTool == "helm" && len(config.TillerNamespace) > 0 {
|
2020-01-24 15:30:27 +02:00
|
|
|
helmEnv = append(helmEnv, fmt.Sprintf("TILLER_NAMESPACE=%v", config.TillerNamespace))
|
|
|
|
}
|
2020-02-27 13:11:22 +02:00
|
|
|
log.Entry().Debugf("Helm SetEnv: %v", helmEnv)
|
|
|
|
command.SetEnv(helmEnv)
|
2020-01-24 15:30:27 +02:00
|
|
|
command.Stdout(stdout)
|
|
|
|
|
2020-04-24 09:37:11 +02:00
|
|
|
if config.DeployTool == "helm" {
|
|
|
|
initParams := []string{"init", "--client-only"}
|
|
|
|
if err := command.RunExecutable("helm", initParams...); err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("Helm init call failed")
|
|
|
|
}
|
2020-01-24 15:30:27 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:16:25 +02:00
|
|
|
var secretsData string
|
|
|
|
if len(config.ContainerRegistryUser) == 0 || len(config.ContainerRegistryPassword) == 0 {
|
|
|
|
log.Entry().Info("No container registry credentials provided or credentials incomplete: skipping secret creation")
|
2020-08-18 07:32:36 +02:00
|
|
|
if len(config.ContainerRegistrySecret) > 0 {
|
|
|
|
secretsData = fmt.Sprintf(",imagePullSecrets[0].name=%v", config.ContainerRegistrySecret)
|
|
|
|
}
|
2020-08-14 17:16:25 +02:00
|
|
|
} else {
|
|
|
|
var dockerRegistrySecret bytes.Buffer
|
|
|
|
command.Stdout(&dockerRegistrySecret)
|
|
|
|
kubeParams := []string{
|
|
|
|
"--insecure-skip-tls-verify=true",
|
|
|
|
"create",
|
|
|
|
"secret",
|
|
|
|
"docker-registry",
|
|
|
|
config.ContainerRegistrySecret,
|
|
|
|
fmt.Sprintf("--docker-server=%v", containerRegistry),
|
|
|
|
fmt.Sprintf("--docker-username=%v", config.ContainerRegistryUser),
|
|
|
|
fmt.Sprintf("--docker-password=%v", config.ContainerRegistryPassword),
|
|
|
|
"--dry-run=true",
|
|
|
|
"--output=json",
|
|
|
|
}
|
|
|
|
log.Entry().Infof("Calling kubectl create secret --dry-run=true ...")
|
|
|
|
log.Entry().Debugf("kubectl parameters %v", kubeParams)
|
|
|
|
if err := command.RunExecutable("kubectl", kubeParams...); err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("Retrieving Docker config via kubectl failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var dockerRegistrySecretData struct {
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
Data struct {
|
|
|
|
DockerConfJSON string `json:".dockerconfigjson"`
|
|
|
|
} `json:"data"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(dockerRegistrySecret.Bytes(), &dockerRegistrySecretData); err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("Reading docker registry secret json failed")
|
|
|
|
}
|
|
|
|
// make sure that secret is hidden in log output
|
|
|
|
log.RegisterSecret(dockerRegistrySecretData.Data.DockerConfJSON)
|
2020-01-24 15:30:27 +02:00
|
|
|
|
2020-08-18 07:32:36 +02:00
|
|
|
log.Entry().Debugf("Secret created: %v", string(dockerRegistrySecret.Bytes()))
|
|
|
|
|
2020-08-14 17:16:25 +02:00
|
|
|
// pass secret in helm default template way and in Piper backward compatible way
|
|
|
|
secretsData = fmt.Sprintf(",secret.name=%v,secret.dockerconfigjson=%v,imagePullSecrets[0].name=%v", config.ContainerRegistrySecret, dockerRegistrySecretData.Data.DockerConfJSON, config.ContainerRegistrySecret)
|
|
|
|
}
|
2020-08-06 11:23:36 +02:00
|
|
|
|
2020-08-18 07:32:36 +02:00
|
|
|
// Deprecated functionality
|
|
|
|
// only for backward compatible handling of ingress.hosts
|
|
|
|
// this requires an adoption of the default ingress.yaml template
|
|
|
|
// Due to the way helm is implemented it is currently not possible to overwrite a part of a list:
|
|
|
|
// see: https://github.com/helm/helm/issues/5711#issuecomment-636177594
|
|
|
|
// Recommended way is to use a custom values file which contains the appropriate data
|
2020-01-24 15:30:27 +02:00
|
|
|
ingressHosts := ""
|
|
|
|
for i, h := range config.IngressHosts {
|
|
|
|
ingressHosts += fmt.Sprintf(",ingress.hosts[%v]=%v", i, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
upgradeParams := []string{
|
|
|
|
"upgrade",
|
|
|
|
config.DeploymentName,
|
|
|
|
config.ChartPath,
|
2020-08-18 07:32:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range config.HelmValues {
|
|
|
|
upgradeParams = append(upgradeParams, "--values", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
upgradeParams = append(
|
|
|
|
upgradeParams,
|
2020-01-24 15:30:27 +02:00
|
|
|
"--install",
|
|
|
|
"--force",
|
2020-04-24 09:37:11 +02:00
|
|
|
"--namespace", config.Namespace,
|
2020-01-24 15:30:27 +02:00
|
|
|
"--set",
|
2020-08-14 17:16:25 +02:00
|
|
|
fmt.Sprintf("image.repository=%v/%v,image.tag=%v%v%v", containerRegistry, containerImageName, containerImageTag, secretsData, ingressHosts),
|
2020-08-18 07:32:36 +02:00
|
|
|
)
|
2020-01-24 15:30:27 +02:00
|
|
|
|
2020-04-24 09:37:11 +02:00
|
|
|
if config.DeployTool == "helm" {
|
|
|
|
upgradeParams = append(upgradeParams, "--wait", "--timeout", strconv.Itoa(config.HelmDeployWaitSeconds))
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.DeployTool == "helm3" {
|
2020-11-10 16:43:19 +02:00
|
|
|
upgradeParams = append(upgradeParams, "--wait", "--timeout", fmt.Sprintf("%vs", config.HelmDeployWaitSeconds))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !config.KeepFailedDeployments {
|
|
|
|
upgradeParams = append(upgradeParams, "--atomic")
|
2020-04-24 09:37:11 +02:00
|
|
|
}
|
|
|
|
|
2020-01-24 15:30:27 +02:00
|
|
|
if len(config.KubeContext) > 0 {
|
|
|
|
upgradeParams = append(upgradeParams, "--kube-context", config.KubeContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.AdditionalParameters) > 0 {
|
|
|
|
upgradeParams = append(upgradeParams, config.AdditionalParameters...)
|
|
|
|
}
|
|
|
|
|
|
|
|
command.Stdout(stdout)
|
|
|
|
log.Entry().Info("Calling helm upgrade ...")
|
|
|
|
log.Entry().Debugf("Helm parameters %v", upgradeParams)
|
|
|
|
if err := command.RunExecutable("helm", upgradeParams...); err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("Helm upgrade call failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-01 11:28:16 +02:00
|
|
|
func runKubectlDeploy(config kubernetesDeployOptions, command command.ExecRunner) {
|
2020-01-24 15:30:27 +02:00
|
|
|
_, containerRegistry, err := splitRegistryURL(config.ContainerRegistryURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatalf("Container registry url '%v' incorrect", config.ContainerRegistryURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeParams := []string{
|
|
|
|
"--insecure-skip-tls-verify=true",
|
|
|
|
fmt.Sprintf("--namespace=%v", config.Namespace),
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.KubeConfig) > 0 {
|
|
|
|
log.Entry().Info("Using KUBECONFIG environment for authentication.")
|
|
|
|
kubeEnv := []string{fmt.Sprintf("KUBECONFIG=%v", config.KubeConfig)}
|
2020-02-27 13:11:22 +02:00
|
|
|
command.SetEnv(kubeEnv)
|
2020-01-24 15:30:27 +02:00
|
|
|
if len(config.KubeContext) > 0 {
|
|
|
|
kubeParams = append(kubeParams, fmt.Sprintf("--context=%v", config.KubeContext))
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
log.Entry().Info("Using --token parameter for authentication.")
|
|
|
|
kubeParams = append(kubeParams, fmt.Sprintf("--server=%v", config.APIServer))
|
|
|
|
kubeParams = append(kubeParams, fmt.Sprintf("--token=%v", config.KubeToken))
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.CreateDockerRegistrySecret {
|
|
|
|
if len(config.ContainerRegistryUser)+len(config.ContainerRegistryPassword) == 0 {
|
|
|
|
log.Entry().Fatal("Cannot create Container registry secret without proper registry username/password")
|
|
|
|
}
|
|
|
|
// first check if secret already exists
|
|
|
|
kubeCheckParams := append(kubeParams, "get", "secret", config.ContainerRegistrySecret)
|
|
|
|
if err := command.RunExecutable("kubectl", kubeCheckParams...); err != nil {
|
|
|
|
log.Entry().Infof("Registry secret '%v' does not exist, let's create it ...", config.ContainerRegistrySecret)
|
|
|
|
kubeSecretParams := append(
|
|
|
|
kubeParams,
|
|
|
|
"create",
|
|
|
|
"secret",
|
|
|
|
"docker-registry",
|
|
|
|
config.ContainerRegistrySecret,
|
|
|
|
fmt.Sprintf("--docker-server=%v", containerRegistry),
|
|
|
|
fmt.Sprintf("--docker-username=%v", config.ContainerRegistryUser),
|
|
|
|
fmt.Sprintf("--docker-password=%v", config.ContainerRegistryPassword),
|
|
|
|
)
|
|
|
|
log.Entry().Infof("Creating container registry secret '%v'", config.ContainerRegistrySecret)
|
|
|
|
log.Entry().Debugf("Running kubectl with following parameters: %v", kubeSecretParams)
|
|
|
|
if err := command.RunExecutable("kubectl", kubeSecretParams...); err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("Creating container registry secret failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
appTemplate, err := ioutil.ReadFile(config.AppTemplate)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatalf("Error when reading appTemplate '%v'", config.AppTemplate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update image name in deployment yaml, expects placeholder like 'image: <image-name>'
|
|
|
|
re := regexp.MustCompile(`image:[ ]*<image-name>`)
|
|
|
|
appTemplate = []byte(re.ReplaceAllString(string(appTemplate), fmt.Sprintf("image: %v/%v", containerRegistry, config.Image)))
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(config.AppTemplate, appTemplate, 0700)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatalf("Error when updating appTemplate '%v'", config.AppTemplate)
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeApplyParams := append(kubeParams, "apply", "--filename", config.AppTemplate)
|
|
|
|
if len(config.AdditionalParameters) > 0 {
|
|
|
|
kubeApplyParams = append(kubeApplyParams, config.AdditionalParameters...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := command.RunExecutable("kubectl", kubeApplyParams...); err != nil {
|
|
|
|
log.Entry().Debugf("Running kubectl with following parameters: %v", kubeApplyParams)
|
|
|
|
log.Entry().WithError(err).Fatal("Deployment with kubectl failed.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitRegistryURL(registryURL string) (protocol, registry string, err error) {
|
|
|
|
parts := strings.Split(registryURL, "://")
|
|
|
|
if len(parts) != 2 || len(parts[1]) == 0 {
|
|
|
|
return "", "", fmt.Errorf("Failed to split registry url '%v'", registryURL)
|
|
|
|
}
|
|
|
|
return parts[0], parts[1], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitFullImageName(image string) (imageName, tag string, err error) {
|
|
|
|
parts := strings.Split(image, ":")
|
|
|
|
switch len(parts) {
|
|
|
|
case 0:
|
|
|
|
return "", "", fmt.Errorf("Failed to split image name '%v'", image)
|
|
|
|
case 1:
|
|
|
|
if len(parts[0]) > 0 {
|
|
|
|
return parts[0], "", nil
|
|
|
|
}
|
|
|
|
return "", "", fmt.Errorf("Failed to split image name '%v'", image)
|
|
|
|
case 2:
|
|
|
|
return parts[0], parts[1], nil
|
|
|
|
}
|
|
|
|
return "", "", fmt.Errorf("Failed to split image name '%v'", image)
|
|
|
|
}
|