/
package.go
166 lines (140 loc) · 3.7 KB
/
package.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
package helm
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
ctrl "sigs.k8s.io/controller-runtime"
)
var tempChartsDir = "/tmp/vcs-charts/"
var log = ctrl.Log.WithName("helm")
// uploadCharts upload charts from local dir to chartmuseum, repoName is the repo name
// as well as local sub-dir name
func uploadCharts(repoName string) error {
url := fmt.Sprintf("%s/api/%s/charts?force=true", "http://captain-chartmuseum:8080", repoName)
dir := tempChartsDir + repoName
// if dir not exist, but no error found before, consider this VCS does not
// contains valid helm charts. So we ignore this step, chartmusuem will provide
// a default index
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Info("vcs charts dir not exist, consider no charts found, skip upload", "name", repoName)
return nil
}
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Error(err, "read local charts dir error")
return err
}
for _, f := range files {
if !f.IsDir() {
if err := postChart(url, filepath.Join(dir, f.Name())); err != nil {
log.Error(err, "upload chart error", "filename", f.Name())
return err
}
log.Info("upload chart done", "filename", f.Name())
}
}
return nil
}
// SouceToChartRepo load vcs source to charts and upload them to chart repo
func SouceToChartRepo(name, dir, path string) error {
if err := createCharts(name, dir, path); err != nil {
return err
}
if err := uploadCharts(name); err != nil {
return err
}
return nil
}
// createCharts create chart from source dir. We should support two scenario:
// 1. the path is a helm chart dir
// 2. the patch contains multiple chart
func createCharts(vcs, dir, path string) error {
target := dir + "/" + path
if path == "/" {
target = dir
}
if strings.HasPrefix(path, "/") {
target = dir + path
}
log.Info("vcs source dir is", "dir", target)
files, err := ioutil.ReadDir(target)
if err != nil {
log.Error(err, "read vcs source dir error")
return err
}
dest := tempChartsDir + vcs
p := action.Package{
Destination: dest,
}
TOP:
for _, f := range files {
if !f.IsDir() && f.Name() == "Chart.yaml" {
log.Info("vsc source dir is a helm package", "dir", target)
_, err := p.Run(target, nil)
if err != nil {
log.Error(err, "package chart error")
return err
}
return nil
}
if f.Name() == ".git" || f.Name() == ".svn" {
continue
}
if f.IsDir() {
sp := filepath.Join(target, f.Name())
files, err := ioutil.ReadDir(sp)
if err != nil {
log.Error(err, "read vcs source sub dir error", "name", sp)
return err
}
for _, f := range files {
if !f.IsDir() && f.Name() == "Chart.yaml" {
result, err := packageChart(sp, dest)
if err != nil {
log.Error(err, "package chart error")
return err
}
log.Info("package chart done", "path", sp, "result", result)
continue TOP
}
}
}
}
return nil
}
// postChart post chart to repo
func postChart(url string, filename string) error {
client := &http.Client{}
data, err := os.Open(filename)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, data)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode >= 300 {
return errors.New("upload chart error, http status code is: " + resp.Status)
}
return nil
}
// a simple version of the helm package impl.
func packageChart(path, target string) (string, error) {
ch, err := loader.LoadDir(path)
if err != nil {
return "", err
}
log.Info("load chart from local dir", "dir", path, "chart", ch.Metadata.Name)
return chartutil.Save(ch, target)
}