/
utils.go
99 lines (84 loc) · 2.74 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package kubernetes
import (
"errors"
"fmt"
"io"
"github.com/SAP/jenkins-library/pkg/command"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/piperutils"
"gopkg.in/yaml.v2"
"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
piperhttp.Uploader
piperhttp.Downloader
}
// deployUtilsBundle struct for utils
type deployUtilsBundle struct {
*command.Command
*piperutils.Files
*piperhttp.Client
}
// NewDeployUtilsBundle initialize using deployUtilsBundle struct
func NewDeployUtilsBundle(customTLSCertificateLinks []string) DeployUtils {
httpClientOptions := piperhttp.ClientOptions{}
if len(customTLSCertificateLinks) > 0 {
httpClientOptions.TransportSkipVerification = false
httpClientOptions.TrustedCerts = customTLSCertificateLinks
}
httpClient := piperhttp.Client{}
httpClient.SetOptions(httpClientOptions)
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",
},
},
StepName: "helmExecute",
},
Files: &piperutils.Files{},
Client: &piperhttp.Client{},
}
// reroute stderr output to logging framework, stdout will be used for command interactions
utils.Stderr(log.Writer())
return &utils
}
// 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)
if err != nil {
return "", "", fmt.Errorf("failed unmarshal: %w", err)
}
name, ok := result["name"].(string)
if !ok || len(name) == 0 {
return "", "", errors.New("name not found in chart yaml file (or wrong type)")
}
version, ok := result["version"].(string)
if !ok || len(name) == 0 {
return "", "", errors.New("version not found in chart yaml file (or wrong type)")
}
return name, version, nil
}