Skip to content

fix(helm): ensure coder can be deployed in a non-default namespace #16579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helm/coder/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

{{- if .Values.coder.ingress.enable }}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: coder
namespace: {{ .Release.Namespace }}
labels:
{{- include "coder.labels" . | nindent 4 }}
annotations:
Expand Down
1 change: 1 addition & 0 deletions helm/coder/templates/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ apiVersion: v1
kind: Service
metadata:
name: coder
namespace: {{ .Release.Namespace }}
labels:
{{- include "coder.labels" . | nindent 4 }}
annotations:
Expand Down
102 changes: 62 additions & 40 deletions helm/coder/tests/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
// updateGoldenFiles is a flag that can be set to update golden files.
var updateGoldenFiles = flag.Bool("update", false, "Update golden files")

var namespaces = []string{
"default",
"coder",
}

var testCases = []testCase{
{
name: "default_values",
Expand Down Expand Up @@ -116,6 +121,7 @@ var testCases = []testCase{

type testCase struct {
name string // Name of the test case. This is used to control which values and golden file are used.
namespace string // Namespace is the name of the namespace the resources should be generated within
expectedError string // Expected error from running `helm template`.
}

Expand All @@ -124,7 +130,11 @@ func (tc testCase) valuesFilePath() string {
}

func (tc testCase) goldenFilePath() string {
return filepath.Join("./testdata", tc.name+".golden")
if tc.namespace == "default" {
return filepath.Join("./testdata", tc.name+".golden")
}

return filepath.Join("./testdata", tc.name+"_"+tc.namespace+".golden")
}

func TestRenderChart(t *testing.T) {
Expand All @@ -146,35 +156,41 @@ func TestRenderChart(t *testing.T) {

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

// Ensure that the values file exists.
valuesFilePath := tc.valuesFilePath()
if _, err := os.Stat(valuesFilePath); os.IsNotExist(err) {
t.Fatalf("values file %q does not exist", valuesFilePath)
}
for _, ns := range namespaces {
tc := tc
tc.namespace = ns

// Run helm template with the values file.
templateOutput, err := runHelmTemplate(t, helmPath, "..", valuesFilePath)
if tc.expectedError != "" {
require.Error(t, err, "helm template should have failed")
require.Contains(t, templateOutput, tc.expectedError, "helm template output should contain expected error")
} else {
require.NoError(t, err, "helm template should not have failed")
require.NotEmpty(t, templateOutput, "helm template output should not be empty")
goldenFilePath := tc.goldenFilePath()
goldenBytes, err := os.ReadFile(goldenFilePath)
require.NoError(t, err, "failed to read golden file %q", goldenFilePath)

// Remove carriage returns to make tests pass on Windows.
goldenBytes = bytes.Replace(goldenBytes, []byte("\r"), []byte(""), -1)
expected := string(goldenBytes)

require.NoError(t, err, "failed to load golden file %q")
require.Equal(t, expected, templateOutput)
}
})
t.Run(tc.namespace+"/"+tc.name, func(t *testing.T) {
t.Parallel()

// Ensure that the values file exists.
valuesFilePath := tc.valuesFilePath()
if _, err := os.Stat(valuesFilePath); os.IsNotExist(err) {
t.Fatalf("values file %q does not exist", valuesFilePath)
}

// Run helm template with the values file.
templateOutput, err := runHelmTemplate(t, helmPath, "..", valuesFilePath, tc.namespace)
if tc.expectedError != "" {
require.Error(t, err, "helm template should have failed")
require.Contains(t, templateOutput, tc.expectedError, "helm template output should contain expected error")
} else {
require.NoError(t, err, "helm template should not have failed")
require.NotEmpty(t, templateOutput, "helm template output should not be empty")
goldenFilePath := tc.goldenFilePath()
goldenBytes, err := os.ReadFile(goldenFilePath)
require.NoError(t, err, "failed to read golden file %q", goldenFilePath)

// Remove carriage returns to make tests pass on Windows.
goldenBytes = bytes.ReplaceAll(goldenBytes, []byte("\r"), []byte(""))
expected := string(goldenBytes)

require.NoError(t, err, "failed to load golden file %q")
require.Equal(t, expected, templateOutput)
}
})
}
}
}

Expand All @@ -189,22 +205,28 @@ func TestUpdateGoldenFiles(t *testing.T) {
require.NoError(t, err, "failed to build Helm dependencies")

for _, tc := range testCases {
tc := tc
if tc.expectedError != "" {
t.Logf("skipping test case %q with render error", tc.name)
continue
}

valuesPath := tc.valuesFilePath()
templateOutput, err := runHelmTemplate(t, helmPath, "..", valuesPath)
if err != nil {
t.Logf("error running `helm template -f %q`: %v", valuesPath, err)
t.Logf("output: %s", templateOutput)
}
require.NoError(t, err, "failed to run `helm template -f %q`", valuesPath)
for _, ns := range namespaces {
tc := tc
tc.namespace = ns

valuesPath := tc.valuesFilePath()
templateOutput, err := runHelmTemplate(t, helmPath, "..", valuesPath, tc.namespace)
if err != nil {
t.Logf("error running `helm template -f %q`: %v", valuesPath, err)
t.Logf("output: %s", templateOutput)
}
require.NoError(t, err, "failed to run `helm template -f %q`", valuesPath)

goldenFilePath := tc.goldenFilePath()
err = os.WriteFile(goldenFilePath, []byte(templateOutput), 0o644) // nolint:gosec
require.NoError(t, err, "failed to write golden file %q", goldenFilePath)
goldenFilePath := tc.goldenFilePath()
err = os.WriteFile(goldenFilePath, []byte(templateOutput), 0o644) // nolint:gosec
require.NoError(t, err, "failed to write golden file %q", goldenFilePath)
}
}
t.Log("Golden files updated. Please review the changes and commit them.")
}
Expand All @@ -231,13 +253,13 @@ func updateHelmDependencies(t testing.TB, helmPath, chartDir string) error {

// runHelmTemplate runs helm template on the given chart with the given values and
// returns the raw output.
func runHelmTemplate(t testing.TB, helmPath, chartDir, valuesFilePath string) (string, error) {
func runHelmTemplate(t testing.TB, helmPath, chartDir, valuesFilePath, namespace string) (string, error) {
// Ensure that valuesFilePath exists
if _, err := os.Stat(valuesFilePath); err != nil {
return "", xerrors.Errorf("values file %q does not exist: %w", valuesFilePath, err)
}

cmd := exec.Command(helmPath, "template", chartDir, "-f", valuesFilePath, "--namespace", "default")
cmd := exec.Command(helmPath, "template", chartDir, "-f", valuesFilePath, "--namespace", namespace)
t.Logf("exec command: %v", cmd.Args)
out, err := cmd.CombinedOutput()
return string(out), err
Expand Down
5 changes: 5 additions & 0 deletions helm/coder/tests/testdata/auto_access_url_1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ metadata:
app.kubernetes.io/version: 0.1.0
helm.sh/chart: coder-0.1.0
name: coder
namespace: default
---
# Source: coder/templates/rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: coder-workspace-perms
namespace: default
rules:
- apiGroups: [""]
resources: ["pods"]
Expand Down Expand Up @@ -60,6 +62,7 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: "coder"
namespace: default
subjects:
- kind: ServiceAccount
name: "coder"
Expand All @@ -73,6 +76,7 @@ apiVersion: v1
kind: Service
metadata:
name: coder
namespace: default
labels:
helm.sh/chart: coder-0.1.0
app.kubernetes.io/name: coder
Expand Down Expand Up @@ -109,6 +113,7 @@ metadata:
app.kubernetes.io/version: 0.1.0
helm.sh/chart: coder-0.1.0
name: coder
namespace: default
spec:
replicas: 1
selector:
Expand Down
Loading
Loading