Skip to content

Commit

Permalink
Merge pull request #107 from projectsyn/feat/ns-labels
Browse files Browse the repository at this point in the history
Allow users to add custom labels to the Keycloak namespace
  • Loading branch information
simu authored Jan 4, 2022
2 parents 736b751 + fd4e22a commit 9b441b8
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions class/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ parameters:
keycloak: "10.3.1"
# FQDN should be overwritten on the cluster level
fqdn: keycloak.example.com
# Namespace labels
namespaceLabels: {}
# Keycloak Admin
admin:
secretname: keycloak-admin-user
Expand Down
3 changes: 2 additions & 1 deletion component/main.jsonnet
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// main template for keycloak
local k8up = import 'lib/backup-k8up.libjsonnet';
local com = import 'lib/commodore.libjsonnet';
local kap = import 'lib/kapitan.libjsonnet';
local kube = import 'lib/kube.libjsonnet';
local rl = import 'lib/resource-locker.libjsonnet';
Expand All @@ -11,7 +12,7 @@ local namespace = kube.Namespace(params.namespace) {
metadata+: {
labels+: {
SYNMonitoring: 'main',
},
} + com.makeMergeable(params.namespaceLabels),
},
};

Expand Down
15 changes: 15 additions & 0 deletions docs/modules/ROOT/pages/references/parameters.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ When using multiple instances for this component, each instance needs its own na
You can't deploy multiple instances into the same namespace.
====

== `namespaceLabels`

[horizontal]
type:: dict
default:: `{}`

Additional labels to add to the component's namespace.
Key value pairs in the dict are directly added to the generated namespace manifest.

[IMPORTANT]
====
Users must ensure that the provided key value pairs are valid Kubernetes label keys and label values.
In particular, make sure to quote boolean or string label values as otherwise the namespace may get created without any labels.
====

== `name`

[horizontal]
Expand Down
2 changes: 2 additions & 0 deletions tests/builtin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
parameters:
keycloak:
namespaceLabels:
test: testing
extraEnv:
FOO:
value: "bar"
Expand Down
23 changes: 23 additions & 0 deletions tests/builtin/namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package builtin

import (
"testing"

"github.com/projectsyn/component-keycloak/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
expectedNsLabels = map[string]string{
"SYNMonitoring": "main",
"name": "syn-builtin",
"test": "testing",
}
)

func Test_NamespaceLabels(t *testing.T) {
ns := common.DecodeNamespace(t, testPath+"/00_namespace.yaml")
require.NotEmpty(t, ns.Labels)
assert.Equal(t, ns.Labels, expectedNsLabels)
}
7 changes: 7 additions & 0 deletions tests/common/boilerplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func DecodeSecret(t *testing.T, path string) *corev1.Secret {
return DecodeWithSchema(t, path, subject, scheme).(*corev1.Secret)
}

func DecodeNamespace(t *testing.T, path string) *corev1.Namespace {
subject := &corev1.Namespace{}
scheme := NewSchemeWithDefault(t)
require.NoError(t, corev1.AddToScheme(scheme))
return DecodeWithSchema(t, path, subject, scheme).(*corev1.Namespace)
}

func DecodeWithSchema(t *testing.T, path string, into runtime.Object, schema *runtime.Scheme) runtime.Object {
data, err := ioutil.ReadFile(path)
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions tests/golden/builtin/builtin/builtin/00_namespace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ metadata:
labels:
SYNMonitoring: main
name: syn-builtin
test: testing
name: syn-builtin

0 comments on commit 9b441b8

Please sign in to comment.