-
Notifications
You must be signed in to change notification settings - Fork 15
/
push.go
80 lines (69 loc) · 2.49 KB
/
push.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
package registry
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/chartmuseum/helm-push/pkg/chartmuseum"
"github.com/go-logr/logr"
"helm.sh/helm/v3/pkg/chart"
)
// PushChart pushes the chart to the repository specified by the repoKey. The repository setting is fetched from the associated registry config file
func (c *Client) PushChart(l logr.Logger, repoKey, pkgPath string, ch *chart.Chart) error {
chartName := ch.Name()
version := ch.Metadata.Version
l.WithValues("repo-key", repoKey, "chart-name", chartName, "chart-version", version)
l.V(3).Info("pushing chart")
rCfg, err := c.registries.RegistryConfig(repoKey)
if err != nil {
l.Error(err, "failed to find registry with provided key in registries map")
return fmt.Errorf("failed to find registry with repoKey %s Name %s Version %s in registries map : %w", repoKey, chartName, version, err)
}
c.cfg.push, err = chartmuseum.NewClient(
chartmuseum.URL(rCfg.URL),
chartmuseum.Username(rCfg.Username),
chartmuseum.Password(rCfg.Password),
chartmuseum.AccessToken(rCfg.AccessToken),
chartmuseum.AuthHeader(rCfg.AuthHeader),
chartmuseum.CAFile(rCfg.CaFile),
chartmuseum.CertFile(rCfg.CertFile),
chartmuseum.KeyFile(rCfg.KeyFile),
chartmuseum.InsecureSkipVerify(rCfg.InsecureSkipVerify),
)
if err != nil {
l.Error(err, "failed to create new helm push client")
return fmt.Errorf("failed to create new helm push client : %w", err)
}
resp, err := c.cfg.push.UploadChartPackage(pkgPath, true)
if err != nil {
l.Error(err, "failed to upload chart package")
return fmt.Errorf("failed to upload chart package with repoKey %s Name %s Version %s : %w", repoKey, chartName, version, err)
}
err = handlePushResponse(resp)
defer resp.Body.Close()
if err != nil {
l.Error(err, "failed to handle upload/push http response")
return fmt.Errorf("failed to handle upload/push http response for chart package with repoKey %s Name %s Version %s : %w", repoKey, chartName, version, err)
}
return nil
}
func handlePushResponse(resp *http.Response) error {
if resp.StatusCode != 201 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return getChartmuseumError(b, resp.StatusCode)
}
return nil
}
func getChartmuseumError(b []byte, code int) error {
var er struct {
Error string `json:"error"`
}
err := json.Unmarshal(b, &er)
if err != nil || er.Error == "" {
return fmt.Errorf("%d: could not properly parse response JSON: %s", code, string(b))
}
return fmt.Errorf("%d: %s", code, er.Error)
}