forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_helm_release.go
167 lines (150 loc) · 4.07 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
package cmd
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
defaultChartRepo = "http://jenkins-x-chartmuseum:8080"
)
// 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(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := StepHelmReleaseOptions{
StepHelmOptions: StepHelmOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
},
}
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()
cmdutil.CheckErr(err)
},
}
options.addStepHelmFlags(cmd)
return cmd
}
func (o *StepHelmReleaseOptions) Run() error {
dir := o.Dir
helmBinary, err := o.helmInitDependencyBuild(dir, o.defaultReleaseCharts())
if err != nil {
return err
}
err = o.runCommandVerboseAt(dir, helmBinary, "package", ".")
if err != nil {
return err
}
chartFile := filepath.Join(dir, "Chart.yaml")
name, version, err := helm.LoadChartNameAndVersion(chartFile)
if err != nil {
return err
}
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 err
}
if !exists {
return fmt.Errorf("Generated helm file %s does not exist!", tarball)
}
defer os.Remove(tarball)
chartRepo := o.releaseChartMuseumUrl()
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.KubeClient()
if err != nil {
return err
}
secret, err := client.CoreV1().Secrets(ns).Get(kube.SecretJenkinsChartMuseum, metav1.GetOptions{})
if err != nil {
log.Warnf("Could not load Secret %s in namespace %s: %s\n", kube.SecretJenkinsChartMuseum, 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 enviroment variable $CHARTMUSEUM_CREDS_USR defined")
}
if password == "" {
return fmt.Errorf("No enviroment 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 err
}
log.Infof("Uploading chart file %s to %s\n", util.ColorInfo(tarball), util.ColorInfo(u))
req, err := http.NewRequest(http.MethodPost, u, bufio.NewReader(file))
if err != nil {
return err
}
req.SetBasicAuth(userName, password)
req.Header.Set("Content-Type", "application/gzip")
res, err := client.Do(req)
if err != nil {
return err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
responseMessage := string(body)
statusCode := res.StatusCode
log.Infof("Received %d response: %s\n", statusCode, responseMessage)
if statusCode >= 300 {
return fmt.Errorf("Failed to post chart to %s due to response %d: %s", u, statusCode, responseMessage)
}
return nil
}