-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
341 lines (314 loc) · 7.35 KB
/
env.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package helmtest
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"text/template"
"time"
"github.com/kharf/declcd/internal/gittest"
"github.com/kharf/declcd/internal/ocitest"
"github.com/kharf/declcd/pkg/kube"
"github.com/kharf/declcd/pkg/project"
"gotest.tools/v3/assert"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
helmKube "helm.sh/helm/v3/pkg/kube"
helmRegistry "helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/repo"
"k8s.io/client-go/rest"
"sigs.k8s.io/yaml"
)
type projectOption struct {
repo *gittest.LocalGitRepository
testProject string
testRoot string
}
var _ Option = (*projectOption)(nil)
func (opt projectOption) apply(opts *options) {
opts.project = opt
}
type oci bool
var _ Option = (*oci)(nil)
func (opt oci) apply(opts *options) {
opts.oci = bool(opt)
}
type options struct {
oci bool
project projectOption
}
type Option interface {
apply(*options)
}
func WithOCI(enabled bool) oci {
return oci(enabled)
}
func WithProject(
repo *gittest.LocalGitRepository,
testProject string,
testRoot string,
) projectOption {
return projectOption{
repo: repo,
testProject: testProject,
testRoot: testRoot,
}
}
type Server interface {
// base URL of form http://ipaddr:port with no trailing slash
URL() string
Close()
}
type ociRegistry struct {
server *ocitest.Registry
}
var _ Server = (*ociRegistry)(nil)
func (r *ociRegistry) Close() {
r.server.Close()
}
func (r *ociRegistry) URL() string {
return r.server.URL()
}
type yamlBasedRepository struct {
server *httptest.Server
}
var _ Server = (*yamlBasedRepository)(nil)
func (r *yamlBasedRepository) Close() {
r.server.Close()
}
func (r *yamlBasedRepository) URL() string {
return r.server.URL
}
type Environment struct {
HelmConfig action.Configuration
ChartServer Server
chartArchives []*os.File
}
func (env Environment) Close() {
if env.ChartServer != nil {
env.ChartServer.Close()
}
for _, f := range env.chartArchives {
os.Remove(f.Name())
}
}
func StartHelmEnv(t testing.TB, cfg *rest.Config, opts ...Option) Environment {
options := &options{
oci: false,
}
for _, o := range opts {
o.apply(options)
}
helmCfg := action.Configuration{}
var helmEnv Environment
helmKube.ManagedFieldsManager = project.ControllerName
k8sClient, err := kube.NewDynamicClient(cfg)
if err != nil {
t.Fatal(err)
}
getter := &kube.InMemoryRESTClientGetter{
Cfg: cfg,
RestMapper: k8sClient.RESTMapper(),
}
err = helmCfg.Init(getter, "default", "secret", log.Printf)
if err != nil {
t.Fatal(err)
}
helmCfg.KubeClient = &kube.HelmClient{
Client: helmCfg.KubeClient.(*helmKube.Client),
DynamicClient: k8sClient,
FieldManager: "controller",
}
helmEnv = startHelmServer(t, options)
ReplaceTemplate(t, options.project.testProject, options.project.repo, helmEnv.ChartServer.URL())
// need to be always set, even though we dont test helm releases
helmEnv.HelmConfig = helmCfg
return helmEnv
}
func startHelmServer(t testing.TB, options *options) Environment {
v1Archive := createChartArchive(t, "test", "1.0.0")
v2Archive := createChartArchive(t, "testv2", "2.0.0")
var chartServer Server
if options.oci {
ociServer, err := ocitest.NewTLSRegistry()
assert.NilError(t, err)
helmOpts := []helmRegistry.ClientOption{
helmRegistry.ClientOptDebug(true),
helmRegistry.ClientOptWriter(os.Stderr),
helmRegistry.ClientOptHTTPClient(ociServer.Client()),
helmRegistry.ClientOptResolver(nil),
}
helmRegistryClient, err := helmRegistry.NewClient(helmOpts...)
assert.NilError(t, err)
v1Bytes, err := os.ReadFile(v1Archive.Name())
assert.NilError(t, err)
_, err = helmRegistryClient.Push(
v1Bytes,
fmt.Sprintf("%s/%s:%s", ociServer.Addr(), "test", "1.0.0"),
)
assert.NilError(t, err)
chartServer = &ociRegistry{
server: ociServer,
}
} else {
httpsServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "index.yaml") {
index := &repo.IndexFile{
APIVersion: "v1",
Generated: time.Now(),
Entries: map[string]repo.ChartVersions{
"test": {
&repo.ChartVersion{
Metadata: &chart.Metadata{
APIVersion: "v1",
Version: "1.0.0",
Name: "test",
},
URLs: []string{chartServer.URL() + "/test-1.0.0.tgz"},
},
&repo.ChartVersion{
Metadata: &chart.Metadata{
APIVersion: "v1",
Version: "2.0.0",
Name: "test",
},
URLs: []string{chartServer.URL() + "/test-2.0.0.tgz"},
},
},
},
}
indexBytes, err := yaml.Marshal(index)
if err != nil {
t.Fatal(err)
}
w.Write(indexBytes)
return
}
archive := v1Archive
if strings.Contains(r.URL.Path, "2.0.0") {
archive = v2Archive
}
w.Header().Set("Content-Type", "application/gzip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filepath.Base(archive.Name())))
file, err := os.Open(archive.Name())
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(w, file); err != nil {
t.Fatal(err)
}
}))
chartServer = &yamlBasedRepository{
server: httpsServer,
}
}
return Environment{
ChartServer: chartServer,
chartArchives: []*os.File{v1Archive, v2Archive},
}
}
func createChartArchive(t testing.TB, chart string, version string) *os.File {
archive, err := os.CreateTemp("", fmt.Sprintf("*-test-%s.tgz", version))
if err != nil {
t.Fatal(err)
}
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
gzWriter := gzip.NewWriter(archive)
tarWriter := tar.NewWriter(gzWriter)
chartDir := filepath.Join(dir, "test", "testdata", "charts")
walkDirErr := fs.WalkDir(
os.DirFS(chartDir),
chart,
func(path string, d fs.DirEntry, err error) error {
if d.IsDir() || path == ".helmignore" {
return nil
}
info, err := d.Info()
if err != nil {
return err
}
header := &tar.Header{
Name: path,
Mode: int64(info.Mode()),
Size: info.Size(),
}
if err := tarWriter.WriteHeader(header); err != nil {
return err
}
file, err := os.Open(filepath.Join(chartDir, path))
if err != nil {
return err
}
defer file.Close()
if _, err := io.Copy(tarWriter, file); err != nil {
return err
}
return nil
},
)
err = tarWriter.Close()
if err != nil {
t.Fatal(err)
}
err = gzWriter.Close()
if err != nil {
t.Fatal(err)
}
if walkDirErr != nil {
t.Fatal(err)
}
return archive
}
func ReplaceTemplate(
t testing.TB,
testProject string,
repo *gittest.LocalGitRepository,
repoURL string,
) {
releasesFilePath := filepath.Join(
testProject,
"infra",
"prometheus",
"releases.cue",
)
releasesContent, err := os.ReadFile(releasesFilePath)
if err != nil {
t.Fatal(err)
}
tmpl, err := template.New("releases").Parse(string(releasesContent))
if err != nil {
t.Fatal(err)
}
releasesFile, err := os.Create(releasesFilePath)
if err != nil {
t.Fatal(err)
}
defer releasesFile.Close()
err = tmpl.Execute(releasesFile, struct {
Name string
RepoUrl string
Version string
}{
Name: "test",
RepoUrl: repoURL,
Version: "1.0.0",
})
if err != nil {
t.Fatal(err)
}
err = repo.CommitFile("infra/prometheus/releases.cue", "overwrite template")
if err != nil {
t.Fatal(err)
}
}