Skip to content

Commit 6df1a12

Browse files
authored
Merge pull request #64 from jiaqiluo/compress-crd-files
2 parents 579e62f + f4d0865 commit 6df1a12

File tree

8 files changed

+77
-1
lines changed

8 files changed

+77
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
/dist
55
*.swp
66
.idea
7-
charts-build-scripts
7+
charts-build-scripts
8+
.DS_Store
9+
pkg/.DS_Store
10+
templates/.DS_Store

pkg/charts/additionalchart.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ func (c *AdditionalChart) ApplyMainChanges(pkgFs billy.Filesystem) error {
4444
if err != nil {
4545
return fmt.Errorf("Encountered error while trying to get the main chart's working directory: %s", err)
4646
}
47+
if c.CRDChartOptions.UseTarArchive {
48+
if err := helm.ArchiveCRDs(pkgFs, mainChartWorkingDir, path.ChartCRDDir, c.WorkingDir, path.ChartExtraFileDir); err != nil {
49+
return fmt.Errorf("encountered error while trying to bundle and compress CRD files from the main chart: %s", err)
50+
}
51+
}
4752
if err := helm.CopyCRDsFromChart(pkgFs, mainChartWorkingDir, path.ChartCRDDir, c.WorkingDir, c.CRDChartOptions.CRDDirectory); err != nil {
4853
return fmt.Errorf("Encountered error while trying to copy CRDs from %s to %s: %s", mainChartWorkingDir, c.WorkingDir, err)
4954
}
@@ -66,12 +71,14 @@ func (c *AdditionalChart) RevertMainChanges(pkgFs billy.Filesystem) error {
6671
return fmt.Errorf("Working directory %s has not been prepared yet", c.WorkingDir)
6772
}
6873
if c.CRDChartOptions == nil {
74+
// return if the additional chart is not a CRD chart
6975
return nil
7076
}
7177
mainChartWorkingDir, err := c.getMainChartWorkingDir(pkgFs)
7278
if err != nil {
7379
return fmt.Errorf("Encountered error while trying to get the main chart's working directory: %s", err)
7480
}
81+
// copy CRD files from packages/<package>/charts-crd/crd-manifest/ back to packages/<package>/charts/crds/
7582
if err := helm.CopyCRDsFromChart(pkgFs, c.WorkingDir, c.CRDChartOptions.CRDDirectory, mainChartWorkingDir, path.ChartCRDDir); err != nil {
7683
return fmt.Errorf("Encountered error while trying to copy CRDs from %s to %s: %s", c.WorkingDir, mainChartWorkingDir, err)
7784
}

pkg/charts/package.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func (p *Package) Prepare() error {
3939
return fmt.Errorf("Encountered error while preparing main chart: %s", err)
4040
}
4141
if p.Chart.Upstream.IsWithinPackage() {
42+
// in the case of local chart
4243
for _, additionalChart := range p.AdditionalCharts {
4344
exists, err := filesystem.PathExists(p.fs, additionalChart.WorkingDir)
4445
if err != nil {

pkg/charts/parse.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func GetAdditionalChartFromOptions(opt options.AdditionalChartOptions) (Addition
175175
TemplateDirectory: templateDirectory,
176176
CRDDirectory: crdDirectory,
177177
AddCRDValidationToMainChart: opt.CRDChartOptions.AddCRDValidationToMainChart,
178+
UseTarArchive: opt.CRDChartOptions.UseTarArchive,
178179
}
179180
}
180181
return a, nil

pkg/filesystem/filesystem.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,52 @@ func UnarchiveTgz(fs billy.Filesystem, tgzPath, tgzSubdirectory, destPath string
249249
return nil
250250
}
251251

252+
// ArchiveDir archives a directory or a file into a tgz file and put it at destTgzPath which should end with .tgz
253+
func ArchiveDir(fs billy.Filesystem, srcPath, destTgzPath string) error {
254+
if !strings.HasSuffix(destTgzPath, ".tgz") {
255+
return fmt.Errorf("cannot archive %s to %s since the archive path does not end with '.tgz'", srcPath, destTgzPath)
256+
}
257+
tgzFile, err := fs.Create(destTgzPath)
258+
if err != nil {
259+
return err
260+
}
261+
defer tgzFile.Close()
262+
263+
gz := gzip.NewWriter(tgzFile)
264+
defer gz.Close()
265+
266+
tarWriter := tar.NewWriter(gz)
267+
defer tarWriter.Close()
268+
269+
return WalkDir(fs, srcPath, func(fs billy.Filesystem, path string, isDir bool) error {
270+
info, err := fs.Stat(path)
271+
if err != nil {
272+
return err
273+
}
274+
header, err := tar.FileInfoHeader(info, info.Name())
275+
if err != nil {
276+
return err
277+
}
278+
// overwrite the name to be the full path to the file
279+
header.Name = path
280+
if err := tarWriter.WriteHeader(header); err != nil {
281+
return err
282+
}
283+
// The directory structure is preserved, but there is no data to read from a directory
284+
if isDir {
285+
return nil
286+
}
287+
file, err := fs.Open(path)
288+
if err != nil {
289+
return err
290+
}
291+
defer file.Close()
292+
293+
_, err = io.Copy(tarWriter, file)
294+
return err
295+
})
296+
}
297+
252298
// RelativePathFunc is a function that is applied on a relative path within the given filesystem
253299
type RelativePathFunc func(fs billy.Filesystem, path string, isDir bool) error
254300

pkg/helm/crds.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,17 @@ func DeleteCRDsFromChart(fs billy.Filesystem, helmChartPath string) error {
4949
}
5050
return nil
5151
}
52+
53+
// ArchiveCRDs bundles, compresses and saves the CRD files from the source to the destination
54+
func ArchiveCRDs(fs billy.Filesystem, srcHelmChartPath, srcCRDsDir, dstHelmChartPath, destCRDsDir string) error {
55+
if err := filesystem.RemoveAll(fs, filepath.Join(dstHelmChartPath, destCRDsDir)); err != nil {
56+
return err
57+
}
58+
if err := fs.MkdirAll(filepath.Join(dstHelmChartPath, destCRDsDir), os.ModePerm); err != nil {
59+
return err
60+
}
61+
srcCRDsDirPath := filepath.Join(srcHelmChartPath, srcCRDsDir)
62+
dstFilePath := filepath.Join(dstHelmChartPath, destCRDsDir, fmt.Sprintf("%s.tgz", "crd-manifest"))
63+
logrus.Infof("Compressing CRDs from %s to %s", srcCRDsDirPath, dstFilePath)
64+
return filesystem.ArchiveDir(fs, srcCRDsDirPath, dstFilePath)
65+
}

pkg/options/additionalchart.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ type CRDChartOptions struct {
1818
CRDDirectory string `yaml:"crdDirectory" default:"templates"`
1919
// Whether to add a validation file to your main chart to check that CRDs exist
2020
AddCRDValidationToMainChart bool `yaml:"addCRDValidationToMainChart"`
21+
// UseTarArchive indicates whether to bundle and compress CRD files into a tgz file
22+
UseTarArchive bool `yaml:"useTarArchive"`
2123
}

pkg/path/path.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const (
3737

3838
// ChartCRDDir represents the directory that we expect to contain CRDs within the chart
3939
ChartCRDDir = "crds"
40+
// ChartExtraFileDir represents the directory that contains non-YAML files
41+
ChartExtraFileDir = "files"
4042
// ChartValidateInstallCRDFile is the path to the file pushed to upstream that validates the existence of CRDs in the chart
4143
ChartValidateInstallCRDFile = "templates/validate-install-crd.yaml"
4244
)

0 commit comments

Comments
 (0)