Skip to content

Commit db0cb9b

Browse files
authored
Fix CRDs when packaging (#233)
## What? CRDs were missing when packaged due to them being copied to the wrong output folder.
1 parent b46bf13 commit db0cb9b

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

helm/private/packager/packager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ func installHelmContent(workingDir string, packagePath string, stampedChartConte
735735
return "", fmt.Errorf("Error unmarshalling crds manifest %s: %w", crdsManifest, err)
736736
}
737737

738-
crdsDir := filepath.Join(workingDir, "crds")
738+
crdsDir := filepath.Join(workingDir, packagePath, "crds")
739739
crdsRoot := ""
740740

741741
// Copy all templates

tests/with_crds/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@rules_go//go:def.bzl", "go_test")
12
load("//helm:defs.bzl", "helm_chart", "helm_lint_test", "helm_template_test")
23

34
helm_chart(
@@ -14,3 +15,14 @@ helm_template_test(
1415
name = "with_crds_template_test",
1516
chart = ":with_crds",
1617
)
18+
19+
go_test(
20+
name = "with_crds_test",
21+
srcs = ["with_crds_test.go"],
22+
data = [":with_crds"],
23+
env = {"HELM_CHART": "$(rlocationpath :with_crds)"},
24+
deps = [
25+
"@in_gopkg_yaml_v3//:yaml_v3",
26+
"@rules_go//go/runfiles",
27+
],
28+
)

tests/with_crds/with_crds_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package main
2+
3+
import (
4+
"archive/tar"
5+
"compress/gzip"
6+
"fmt"
7+
"io"
8+
"os"
9+
"testing"
10+
11+
"github.com/bazelbuild/rules_go/go/runfiles"
12+
"gopkg.in/yaml.v3"
13+
)
14+
15+
type HelmChartDependency struct {
16+
Name string
17+
Repository string
18+
Version string
19+
}
20+
21+
type HelmChart struct {
22+
Dependencies []HelmChartDependency
23+
}
24+
25+
func loadChart(content string) (HelmChart, error) {
26+
var chart HelmChart
27+
err := yaml.Unmarshal([]byte(content), &chart)
28+
if err != nil {
29+
return chart, fmt.Errorf("Error unmarshalling chart content: %w", err)
30+
}
31+
32+
return chart, nil
33+
}
34+
35+
func TestWithCrdsTest(t *testing.T) {
36+
// Retrieve the Helm chart location from the environment variable
37+
helmChartPath := os.Getenv("HELM_CHART")
38+
if helmChartPath == "" {
39+
t.Fatal("HELM_CHART environment variable is not set")
40+
}
41+
42+
// Locate the runfile
43+
path, err := runfiles.Rlocation(helmChartPath)
44+
if err != nil {
45+
t.Fatalf("Failed to find runfile with: %v", err)
46+
}
47+
48+
// Open the .tgz file
49+
file, err := os.Open(path)
50+
if err != nil {
51+
t.Fatalf("Failed to open the Helm chart file: %v", err)
52+
}
53+
defer file.Close()
54+
55+
// Wrap the file in a Gzip reader
56+
gzr, err := gzip.NewReader(file)
57+
if err != nil {
58+
t.Fatalf("Failed to create Gzip reader: %v", err)
59+
}
60+
defer gzr.Close()
61+
62+
// Create a tar reader from the Gzip reader
63+
tarReader := tar.NewReader(gzr)
64+
65+
// Initialize flags to check for the two files
66+
var crdsFound bool
67+
68+
// Iterate through the tar archive
69+
for {
70+
header, err := tarReader.Next()
71+
if err == io.EOF {
72+
break // End of archive
73+
}
74+
if err != nil {
75+
t.Fatalf("Error reading tar archive: %v", err)
76+
}
77+
78+
if header.Name == "with-crds/crds/test.crd.yaml" {
79+
crdsFound = true
80+
if err != nil {
81+
t.Fatalf("Failed to read with-crds/crds/test.crd.yaml: %v", err)
82+
}
83+
}
84+
}
85+
86+
if !crdsFound {
87+
t.Error("crds/test.crd.yaml was not found in the Helm chart")
88+
}
89+
}

0 commit comments

Comments
 (0)