Skip to content
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
10 changes: 10 additions & 0 deletions cmd/clusters-service/pkg/server/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ func TestCreatePullRequest(t *testing.T) {
viper.SetDefault("capi-repository-path", "clusters/my-cluster/clusters")
viper.SetDefault("capi-repository-clusters-path", "clusters")
viper.SetDefault("add-bases-kustomization", "enabled")
viper.SetDefault("capi-templates-namespace", "default")
testCases := []struct {
name string
clusterState []runtime.Object
Expand Down Expand Up @@ -497,6 +498,9 @@ metadata:
templates.weave.works/create-request: '{"repository_url":"https://github.com/org/repo.git","head_branch":"feature-01","base_branch":"main","title":"New
Cluster","description":"Creates a cluster through a CAPI template","template_name":"cluster-template-1","parameter_values":{"CLUSTER_NAME":"dev","NAMESPACE":"default"},"commit_message":"Add
cluster manifest","values":[{"name":"demo-profile","version":"0.0.1"}]}'
labels:
templates.weave.works/template-name: cluster-template-1
templates.weave.works/template-namespace: default
name: dev
namespace: default
`,
Expand Down Expand Up @@ -600,6 +604,9 @@ metadata:
templates.weave.works/create-request: '{"repository_url":"https://github.com/org/repo.git","head_branch":"feature-01","base_branch":"main","title":"New
Cluster","description":"Creates a cluster through a CAPI template","template_name":"cluster-template-1","parameter_values":{"CLUSTER_NAME":"dev","NAMESPACE":"clusters-namespace"},"commit_message":"Add
cluster manifest","values":[{"name":"demo-profile","version":"0.0.1","namespace":"test-system"}]}'
labels:
templates.weave.works/template-name: cluster-template-1
templates.weave.works/template-namespace: default
name: dev
namespace: clusters-namespace
`,
Expand Down Expand Up @@ -714,6 +721,9 @@ metadata:
templates.weave.works/create-request: '{"repository_url":"https://github.com/org/repo.git","head_branch":"feature-01","base_branch":"main","title":"New
Cluster","description":"Creates a cluster through a CAPI template","template_name":"cluster-template-1","parameter_values":{"CLUSTER_NAME":"dev","NAMESPACE":"clusters-namespace"},"commit_message":"Add
cluster manifest","kustomizations":[{"metadata":{"name":"apps-capi","namespace":"flux-system"},"spec":{"path":"./apps/capi","source_ref":{"name":"flux-system","namespace":"flux-system"},"target_namespace":"foo-ns"}},{"metadata":{"name":"apps-billing","namespace":"flux-system"},"spec":{"path":"./apps/billing","source_ref":{"name":"flux-system","namespace":"flux-system"}}}]}'
labels:
templates.weave.works/template-name: cluster-template-1
templates.weave.works/template-namespace: default
name: dev
namespace: clusters-namespace
`,
Expand Down
4 changes: 4 additions & 0 deletions cmd/clusters-service/pkg/server/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
func renderTemplateWithValues(t apitemplates.Template, name, namespace string, values map[string]string) ([][]byte, error) {
opts := []templates.RenderOptFunc{
templates.InNamespace(namespace),
templates.InjectLabels(map[string]string{
"templates.weave.works/template-name": name,
"templates.weave.works/template-namespace": viper.GetString("capi-templates-namespace"),
}),
}
if viper.GetString("inject-prune-annotation") != "disabled" {
opts = append(opts, templates.InjectPruneAnnotation)
Expand Down
18 changes: 10 additions & 8 deletions cmd/clusters-service/pkg/server/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ func TestRenderTemplate(t *testing.T) {
clusterState: []runtime.Object{
makeCAPITemplate(t),
},
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n name: test-cluster\n namespace: test-ns\n",
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: default\n name: test-cluster\n namespace: test-ns\n",
},
{
name: "render template with optional value",
Expand Down Expand Up @@ -642,7 +642,7 @@ func TestRenderTemplate(t *testing.T) {
}
}),
},
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n name: test-cluster\n namespace: test-ns\n",
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: default\n name: test-cluster\n namespace: test-ns\n",
},
{
name: "render template with default value",
Expand Down Expand Up @@ -672,7 +672,7 @@ func TestRenderTemplate(t *testing.T) {
}
}),
},
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterNamefoo\n name: test-cluster\n namespace: test-ns\n",
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterNamefoo\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: default\n name: test-cluster\n namespace: test-ns\n",
},
{
// some client might send empty credentials objects
Expand All @@ -689,7 +689,7 @@ func TestRenderTemplate(t *testing.T) {
Name: "",
Namespace: "",
},
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n name: test-cluster\n namespace: test-ns\n",
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: default\n name: test-cluster\n namespace: test-ns\n",
},
{
name: "render template with credentials",
Expand Down Expand Up @@ -718,7 +718,7 @@ func TestRenderTemplate(t *testing.T) {
Name: "cred-name",
Namespace: "cred-namespace",
},
expected: "apiVersion: infrastructure.cluster.x-k8s.io/v1alpha4\nkind: AWSCluster\nmetadata:\n name: boop\n namespace: test-ns\nspec:\n identityRef:\n kind: AWSClusterStaticIdentity\n name: cred-name\n",
expected: "apiVersion: infrastructure.cluster.x-k8s.io/v1alpha4\nkind: AWSCluster\nmetadata:\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: default\n name: boop\n namespace: test-ns\nspec:\n identityRef:\n kind: AWSClusterStaticIdentity\n name: cred-name\n",
},
{
name: "enable prune injections",
Expand All @@ -727,7 +727,7 @@ func TestRenderTemplate(t *testing.T) {
clusterState: []runtime.Object{
makeCAPITemplate(t),
},
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n kustomize.toolkit.fluxcd.io/prune: disabled\n name: test-cluster\n namespace: test-ns\n",
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n kustomize.toolkit.fluxcd.io/prune: disabled\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: default\n name: test-cluster\n namespace: test-ns\n",
},
{
name: "render template with renderType: templating",
Expand Down Expand Up @@ -757,7 +757,7 @@ func TestRenderTemplate(t *testing.T) {
}
}),
},
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n name: test-cluster\n namespace: test-ns\n",
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: default\n name: test-cluster\n namespace: test-ns\n",
},
}

Expand All @@ -766,6 +766,7 @@ func TestRenderTemplate(t *testing.T) {
viper.Reset()
viper.SetDefault("inject-prune-annotation", tt.pruneEnvVar)
viper.SetDefault("capi-clusters-namespace", tt.clusterNamespace)
viper.SetDefault("capi-templates-namespace", "default")

s := createServer(t, serverOptions{
clusterState: tt.clusterState,
Expand Down Expand Up @@ -839,7 +840,7 @@ func TestRenderTemplate_ValidateVariables(t *testing.T) {
makeCAPITemplate(t),
},
clusterName: "test-cluster",
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n kustomize.toolkit.fluxcd.io/prune: disabled\n name: test-cluster\n namespace: default\n",
expected: "apiVersion: fooversion\nkind: fookind\nmetadata:\n annotations:\n capi.weave.works/display-name: ClusterName\n kustomize.toolkit.fluxcd.io/prune: disabled\n labels:\n templates.weave.works/template-name: cluster-template-1\n templates.weave.works/template-namespace: default\n name: test-cluster\n namespace: default\n",
},
{
name: "value contains non alphanumeric",
Expand Down Expand Up @@ -870,6 +871,7 @@ func TestRenderTemplate_ValidateVariables(t *testing.T) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
viper.Reset()
viper.SetDefault("capi-templates-namespace", "default")

s := createServer(t, serverOptions{
clusterState: tt.clusterState,
Expand Down
8 changes: 8 additions & 0 deletions cmd/clusters-service/pkg/templates/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func InNamespace(ns string) RenderOptFunc {
}
}

// InjectLabels is a render option that updates the object metadata with the desired labels
func InjectLabels(labels map[string]string) RenderOptFunc {
return func(uns *unstructured.Unstructured) error {
uns.SetLabels(labels)
return nil
}
}

func processUnstructured(b []byte, opts ...RenderOptFunc) ([]byte, error) {
dec := serializer.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
uns := &unstructured.Unstructured{}
Expand Down