Skip to content

Commit 58f666e

Browse files
authored
feat(api): Namespace template function (#3069)
1 parent 716e711 commit 58f666e

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

api/pkg/template/app.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package template
2+
3+
import (
4+
"github.com/replicatedhq/embedded-cluster/pkg-new/constants"
5+
)
6+
7+
// namespace returns the namespace for the app
8+
func (e *Engine) namespace() string {
9+
return constants.KotsadmNamespace
10+
}

api/pkg/template/app_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package template
2+
3+
import (
4+
"testing"
5+
6+
"github.com/replicatedhq/embedded-cluster/pkg-new/constants"
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestEngine_namespace(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
expectedResult string
15+
}{
16+
{
17+
name: "returns kotsadm namespace",
18+
expectedResult: constants.KotsadmNamespace,
19+
},
20+
}
21+
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
engine := NewEngine(nil, WithMode(ModeGeneric))
25+
err := engine.Parse("{{repl Namespace }}")
26+
require.NoError(t, err)
27+
28+
result, err := engine.Execute(nil)
29+
require.NoError(t, err)
30+
assert.Equal(t, tt.expectedResult, result)
31+
})
32+
}
33+
}
34+
35+
func TestEngine_namespaceIntegrated(t *testing.T) {
36+
tests := []struct {
37+
name string
38+
template string
39+
expectedResult string
40+
}{
41+
{
42+
name: "namespace in string concatenation",
43+
template: "{{repl Namespace }}-suffix",
44+
expectedResult: constants.KotsadmNamespace + "-suffix",
45+
},
46+
{
47+
name: "namespace in conditional logic",
48+
template: `{{repl if eq Namespace "kotsadm" }}correct{{repl else }}incorrect{{repl end }}`,
49+
expectedResult: "correct",
50+
},
51+
{
52+
name: "namespace value check",
53+
template: "prefix-{{repl Namespace }}",
54+
expectedResult: "prefix-" + constants.KotsadmNamespace,
55+
},
56+
}
57+
58+
for _, tt := range tests {
59+
t.Run(tt.name, func(t *testing.T) {
60+
engine := NewEngine(nil, WithMode(ModeGeneric))
61+
err := engine.Parse(tt.template)
62+
require.NoError(t, err)
63+
64+
result, err := engine.Execute(nil)
65+
require.NoError(t, err)
66+
assert.Equal(t, tt.expectedResult, result)
67+
})
68+
}
69+
}

api/pkg/template/execute.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ func (e *Engine) getFuncMap() template.FuncMap {
188188

189189
// Airgap template functions
190190
"IsAirgap": e.isAirgap,
191+
192+
// App template functions
193+
"Namespace": e.namespace,
191194
}
192195
}
193196

e2e/kots-release-install-v3/config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,3 +649,11 @@ spec:
649649
---
650650
651651
{{repl VersionLabel }}
652+
653+
- name: template_function_namespace
654+
type: label
655+
title: |
656+
## Namespace
657+
---
658+
659+
{{repl Namespace }}

0 commit comments

Comments
 (0)