-
Notifications
You must be signed in to change notification settings - Fork 785
/
step_helm_release.go
175 lines (157 loc) · 5.11 KB
/
step_helm_release.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package helm
import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// StepHelmReleaseOptions contains the command line flags
type StepHelmReleaseOptions struct {
StepHelmOptions
}
var (
StepHelmReleaseLong = templates.LongDesc(`
This pipeline step releases the Helm chart in the current directory
`)
StepHelmReleaseExample = templates.Examples(`
jx step helm release
`)
)
func NewCmdStepHelmRelease(commonOpts *opts.CommonOptions) *cobra.Command {
options := StepHelmReleaseOptions{
StepHelmOptions: StepHelmOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
},
}
cmd := &cobra.Command{
Use: "release",
Short: "Releases the helm chart in the current directory",
Aliases: []string{""},
Long: StepHelmReleaseLong,
Example: StepHelmReleaseExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
options.addStepHelmFlags(cmd)
return cmd
}
func (o *StepHelmReleaseOptions) Run() error {
dir := o.Dir
valuesFiles, err := o.discoverValuesFiles(dir)
if err != nil {
return err
}
_, err = o.HelmInitDependencyBuild(dir, o.DefaultReleaseCharts(), valuesFiles)
if err != nil {
return errors.Wrapf(err, "failed to build dependencies for chart from directory '%s'", dir)
}
o.Helm().SetCWD(dir)
err = o.Helm().PackageChart()
if err != nil {
return errors.Wrapf(err, "failed to package the chart from directory '%s'", dir)
}
chartFile := filepath.Join(dir, "Chart.yaml")
name, version, err := helm.LoadChartNameAndVersion(chartFile)
if err != nil {
return errors.Wrap(err, "failed to load chart name and version")
}
if name == "" {
return fmt.Errorf("Could not find name in chart %s", chartFile)
}
if version == "" {
return fmt.Errorf("Could not find version in chart %s", chartFile)
}
tarball := fmt.Sprintf("%s-%s.tgz", name, version)
exists, err := util.FileExists(tarball)
if err != nil {
return errors.Wrapf(err, "don't find the chart archive '%s'", tarball)
}
if !exists {
return fmt.Errorf("Generated helm file %s does not exist!", tarball)
}
defer os.Remove(tarball)
chartRepo := o.ReleaseChartRepositoryURL()
userName := os.Getenv("CHARTMUSEUM_CREDS_USR")
password := os.Getenv("CHARTMUSEUM_CREDS_PSW")
if userName == "" || password == "" {
// lets try load them from the secret directly
client, ns, err := o.KubeClientAndNamespace()
if err != nil {
return errors.Wrap(err, "failed to create the kube client")
}
secret, err := client.CoreV1().Secrets(ns).Get(kube.SecretJenkinsChartMuseum, metav1.GetOptions{})
if err != nil {
secret, err = client.CoreV1().Secrets(ns).Get(kube.SecretBucketRepo, metav1.GetOptions{})
}
if err != nil {
log.Logger().Warnf("Could not load Secret %s or %s in namespace %s: %s", kube.SecretJenkinsChartMuseum, kube.SecretBucketRepo, ns, err)
} else {
if secret != nil && secret.Data != nil {
if userName == "" {
userName = string(secret.Data["BASIC_AUTH_USER"])
}
if password == "" {
password = string(secret.Data["BASIC_AUTH_PASS"])
}
}
}
}
if userName == "" {
return fmt.Errorf("No environment variable $CHARTMUSEUM_CREDS_USR defined")
}
if password == "" {
return fmt.Errorf("No environment variable CHARTMUSEUM_CREDS_PSW defined")
}
// post the tarball to the chart repository
client := http.Client{}
u := util.UrlJoin(chartRepo, "/api/charts")
file, err := os.Open(tarball)
if err != nil {
return errors.Wrapf(err, "failed to open the chart archive '%s'", tarball)
}
log.Logger().Infof("Uploading chart file %s to %s", util.ColorInfo(tarball), util.ColorInfo(u))
req, err := http.NewRequest(http.MethodPost, u, bufio.NewReader(file))
if err != nil {
return errors.Wrapf(err, "failed to build the chart upload request for endpoint '%s'", u)
}
req.SetBasicAuth(userName, password)
req.Header.Set("Content-Type", "application/gzip")
res, err := client.Do(req)
if err != nil {
if res == nil {
return errors.Wrapf(err, "failed to execute the chart upload HTTP request, url: '%s', error: '%v'", u, err)
}
errRes, _ := ioutil.ReadAll(res.Body)
return errors.Wrapf(err, "failed to execute the chart upload HTTP request, url: '%s', status: '%s', response: '%s'", u, res.Status, string(errRes))
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return errors.Wrap(err, "failed to read the response body of chart upload request")
}
responseMessage := string(body)
statusCode := res.StatusCode
log.Logger().Infof("Received %d response: %s", statusCode, responseMessage)
if statusCode >= 300 {
return fmt.Errorf("Failed to post chart to %s due to response %d: %s", u, statusCode, responseMessage)
}
return nil
}