1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/kubernetes/utils.go
Vitalii Sidorov 63cdfc0e68
Fix helm execute command (#3701)
* Add small fix

* fix unit-tests

* Add deploymentName and packageVersion as flags

* small fix

* Change getting version of helm chart

* small fix

Co-authored-by: “Vitalii <“vitalii.sidorov@sap.com”>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-04-14 15:43:47 +02:00

104 lines
2.9 KiB
Go

package kubernetes
import (
"errors"
"fmt"
"io"
"net/http"
"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
DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error
piperutils.FileUtils
piperhttp.Uploader
}
// deployUtilsBundle struct for utils
type deployUtilsBundle struct {
*command.Command
*piperutils.Files
piperhttp.Uploader
}
// 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",
},
},
},
Files: &piperutils.Files{},
Uploader: &httpClient,
}
// 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
}
func (d *deployUtilsBundle) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
return fmt.Errorf("not implemented")
}