2022-02-10 11:25:03 +02:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2022-03-17 18:13:34 +02:00
|
|
|
"errors"
|
2022-02-10 11:25:03 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
2022-03-17 18:13:34 +02:00
|
|
|
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
2022-02-10 11:25:03 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
2022-03-17 18:13:34 +02:00
|
|
|
"gopkg.in/yaml.v2"
|
2022-02-10 11:25:03 +02:00
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeployUtils interface
|
|
|
|
type DeployUtils interface {
|
|
|
|
SetEnv(env []string)
|
|
|
|
Stdout(out io.Writer)
|
|
|
|
Stderr(err io.Writer)
|
|
|
|
RunExecutable(e string, p ...string) error
|
|
|
|
|
|
|
|
piperutils.FileUtils
|
2022-03-17 18:13:34 +02:00
|
|
|
piperhttp.Uploader
|
2022-06-29 12:00:37 +02:00
|
|
|
piperhttp.Downloader
|
2022-02-10 11:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// deployUtilsBundle struct for utils
|
|
|
|
type deployUtilsBundle struct {
|
|
|
|
*command.Command
|
|
|
|
*piperutils.Files
|
2022-06-29 12:00:37 +02:00
|
|
|
*piperhttp.Client
|
2022-02-10 11:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDeployUtilsBundle initialize using deployUtilsBundle struct
|
2022-03-17 18:13:34 +02:00
|
|
|
func NewDeployUtilsBundle(customTLSCertificateLinks []string) DeployUtils {
|
|
|
|
httpClientOptions := piperhttp.ClientOptions{}
|
|
|
|
|
|
|
|
if len(customTLSCertificateLinks) > 0 {
|
|
|
|
httpClientOptions.TransportSkipVerification = false
|
|
|
|
httpClientOptions.TrustedCerts = customTLSCertificateLinks
|
|
|
|
}
|
|
|
|
|
|
|
|
httpClient := piperhttp.Client{}
|
|
|
|
httpClient.SetOptions(httpClientOptions)
|
|
|
|
|
2022-02-10 11:25:03 +02:00
|
|
|
utils := deployUtilsBundle{
|
|
|
|
Command: &command.Command{
|
|
|
|
ErrorCategoryMapping: map[string][]string{
|
|
|
|
log.ErrorConfiguration.String(): {
|
|
|
|
"Error: Get * no such host",
|
|
|
|
"Error: path * not found",
|
|
|
|
"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",
|
|
|
|
"Error: UPGRADE FAILED: query: failed to query with labels:",
|
|
|
|
"Invalid value: \"\": field is immutable",
|
|
|
|
},
|
|
|
|
log.ErrorCustom.String(): {
|
|
|
|
"Error: release * failed, * timed out waiting for the condition",
|
|
|
|
},
|
|
|
|
},
|
2022-08-05 13:08:19 +02:00
|
|
|
StepName: "helmExecute",
|
2022-02-10 11:25:03 +02:00
|
|
|
},
|
2022-06-29 12:00:37 +02:00
|
|
|
Files: &piperutils.Files{},
|
|
|
|
Client: &piperhttp.Client{},
|
2022-02-10 11:25:03 +02:00
|
|
|
}
|
|
|
|
// reroute stderr output to logging framework, stdout will be used for command interactions
|
|
|
|
utils.Stderr(log.Writer())
|
|
|
|
return &utils
|
|
|
|
}
|
|
|
|
|
2022-03-17 18:13:34 +02:00
|
|
|
// GetChartInfo is used to get name and version of helm chart
|
|
|
|
func GetChartInfo(chartYamlFile string, utils DeployUtils) (string, string, error) {
|
|
|
|
|
|
|
|
var result map[string]interface{}
|
|
|
|
p, err := utils.FileRead(chartYamlFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("file couldn't read: %w", err)
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(p, &result)
|
2022-02-10 11:25:03 +02:00
|
|
|
if err != nil {
|
2022-03-17 18:13:34 +02:00
|
|
|
return "", "", fmt.Errorf("failed unmarshal: %w", err)
|
2022-02-10 11:25:03 +02:00
|
|
|
}
|
|
|
|
|
2022-03-17 18:13:34 +02:00
|
|
|
name, ok := result["name"].(string)
|
|
|
|
if !ok || len(name) == 0 {
|
|
|
|
return "", "", errors.New("name not found in chart yaml file (or wrong type)")
|
2022-02-10 11:25:03 +02:00
|
|
|
}
|
|
|
|
|
2022-03-17 18:13:34 +02:00
|
|
|
version, ok := result["version"].(string)
|
|
|
|
if !ok || len(name) == 0 {
|
|
|
|
return "", "", errors.New("version not found in chart yaml file (or wrong type)")
|
2022-02-10 11:25:03 +02:00
|
|
|
}
|
|
|
|
|
2022-03-17 18:13:34 +02:00
|
|
|
return name, version, nil
|
2022-02-10 11:25:03 +02:00
|
|
|
}
|