Skip to content

Commit 7b49f6e

Browse files
committed
Package charts as tgz file
1 parent 1f7a925 commit 7b49f6e

File tree

1 file changed

+71
-26
lines changed

1 file changed

+71
-26
lines changed

tools/manifests/manifest_builder.go

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
package main
2424

2525
import (
26+
"archive/tar"
2627
"bytes"
28+
"compress/gzip"
2729
"fmt"
2830
"io/ioutil"
2931
"log"
3032
"os"
3133
"os/exec"
34+
"path"
3235
"path/filepath"
3336
"strconv"
3437
"strings"
@@ -148,6 +151,19 @@ Storage:
148151
`
149152
)
150153

154+
var (
155+
chartTemplateGroups = map[string]chartTemplates{
156+
"kube-arangodb": chartTemplates{
157+
"Chart.yaml": kubeArangoDBChartTemplate,
158+
"values.yaml": kubeArangoDBValuesTemplate,
159+
},
160+
"kube-arangodb-storage": chartTemplates{
161+
"Chart.yaml": kubeArangoDBStorageChartTemplate,
162+
"values.yaml": kubeArangoDBStorageValuesTemplate,
163+
},
164+
}
165+
)
166+
151167
func init() {
152168
pflag.StringVar(&options.OutputSuffix, "output-suffix", "", "Suffix of the generated manifest files")
153169
pflag.StringVar(&options.TemplatesDir, "templates-dir", "manifests/templates", "Directory containing manifest templates")
@@ -234,6 +250,15 @@ func main() {
234250
log.Fatalf("Failed to read VERSION file: %v", err)
235251
}
236252

253+
// Prepare chart tars
254+
chartTarBufs := make(map[string]*bytes.Buffer)
255+
chartTars := make(map[string]*tar.Writer)
256+
for groupName := range chartTemplateGroups {
257+
buf := &bytes.Buffer{}
258+
chartTarBufs[groupName] = buf
259+
chartTars[groupName] = tar.NewWriter(buf)
260+
}
261+
237262
// Process templates
238263
templateOptions := TemplateOptions{
239264
Version: strings.TrimSpace(string(version)),
@@ -420,32 +445,24 @@ func main() {
420445

421446
// Save output
422447
if output.Len() > 0 {
423-
outputDir, err := filepath.Abs(filepath.Join("bin", "charts", templateGroup.ChartName, "templates"))
424-
if err != nil {
425-
log.Fatalf("Failed to get absolute output dir: %v\n", err)
448+
tarPath := path.Join(templateGroup.ChartName, "templates", group+".yaml")
449+
hdr := &tar.Header{
450+
Name: tarPath,
451+
Mode: 0644,
452+
Size: int64(output.Len()),
426453
}
427-
outputPath := filepath.Join(outputDir, group+".yaml")
428-
if err := os.MkdirAll(outputDir, 0755); err != nil {
429-
log.Fatalf("Failed to create output directory: %v\n", err)
454+
tw := chartTars[templateGroup.ChartName]
455+
if err := tw.WriteHeader(hdr); err != nil {
456+
log.Fatal(err)
430457
}
431-
if err := ioutil.WriteFile(outputPath, output.Bytes(), 0644); err != nil {
432-
log.Fatalf("Failed to write output file: %v\n", err)
458+
if _, err := tw.Write(output.Bytes()); err != nil {
459+
log.Fatal(err)
433460
}
434461
}
435462
}
436463
}
437464

438465
// Build Chart files
439-
chartTemplateGroups := map[string]chartTemplates{
440-
"kube-arangodb": chartTemplates{
441-
"Chart.yaml": kubeArangoDBChartTemplate,
442-
"values.yaml": kubeArangoDBValuesTemplate,
443-
},
444-
"kube-arangodb-storage": chartTemplates{
445-
"Chart.yaml": kubeArangoDBStorageChartTemplate,
446-
"values.yaml": kubeArangoDBStorageValuesTemplate,
447-
},
448-
}
449466
for groupName, chartTemplates := range chartTemplateGroups {
450467
for name, templateSource := range chartTemplates {
451468
output := &bytes.Buffer{}
@@ -456,17 +473,45 @@ func main() {
456473
t.Execute(output, templateOptions)
457474

458475
// Save output
459-
outputDir, err := filepath.Abs(filepath.Join("bin", "charts", groupName))
460-
if err != nil {
461-
log.Fatalf("Failed to get absolute output dir: %v\n", err)
476+
tarPath := path.Join(groupName, name)
477+
hdr := &tar.Header{
478+
Name: tarPath,
479+
Mode: 0644,
480+
Size: int64(output.Len()),
462481
}
463-
outputPath := filepath.Join(outputDir, name)
464-
if err := os.MkdirAll(outputDir, 0755); err != nil {
465-
log.Fatalf("Failed to create output directory: %v\n", err)
482+
tw := chartTars[groupName]
483+
if err := tw.WriteHeader(hdr); err != nil {
484+
log.Fatal(err)
466485
}
467-
if err := ioutil.WriteFile(outputPath, output.Bytes(), 0644); err != nil {
468-
log.Fatalf("Failed to write output file: %v\n", err)
486+
if _, err := tw.Write(output.Bytes()); err != nil {
487+
log.Fatal(err)
469488
}
470489
}
471490
}
491+
492+
// Save charts
493+
for groupName, tw := range chartTars {
494+
if err := tw.Close(); err != nil {
495+
log.Fatal(err)
496+
}
497+
// Gzip tarball
498+
tarBytes := chartTarBufs[groupName].Bytes()
499+
output := &bytes.Buffer{}
500+
gw := gzip.NewWriter(output)
501+
if _, err := gw.Write(tarBytes); err != nil {
502+
log.Fatal(err)
503+
}
504+
gw.Close()
505+
outputDir, err := filepath.Abs("bin/charts")
506+
if err != nil {
507+
log.Fatalf("Failed to get absolute output dir: %v\n", err)
508+
}
509+
outputPath := filepath.Join(outputDir, groupName+".tgz")
510+
if err := os.MkdirAll(outputDir, 0755); err != nil {
511+
log.Fatalf("Failed to create output directory: %v\n", err)
512+
}
513+
if err := ioutil.WriteFile(outputPath, output.Bytes(), 0644); err != nil {
514+
log.Fatalf("Failed to write output file: %v\n", err)
515+
}
516+
}
472517
}

0 commit comments

Comments
 (0)