diff --git a/flytestdlib/namespaceutils/namespaceutils.go b/flytestdlib/namespaceutils/namespaceutils.go new file mode 100644 index 0000000000..c6dfc7f2a5 --- /dev/null +++ b/flytestdlib/namespaceutils/namespaceutils.go @@ -0,0 +1,23 @@ +package namespaceutils + +import ( + "strings" +) + +const orgTemplate = "{{ org }}" +const projectTemplate = "{{ project }}" +const domainTemplate = "{{ domain }}" + +// GetNamespaceName returns kubernetes namespace name according to user defined template from config +func GetNamespaceName(template string, org, project, domain string) string { + return strings.NewReplacer( + orgTemplate, org, + projectTemplate, project, + domainTemplate, domain, + ).Replace(template) +} + +// GetNameWithNamespacedPrefix returns a name with the given prefix prepended +func GetNameWithNamespacedPrefix(prefix, name string) string { + return prefix + name +} diff --git a/flytestdlib/namespaceutils/namespaceutils_test.go b/flytestdlib/namespaceutils/namespaceutils_test.go new file mode 100644 index 0000000000..7bf69efbb0 --- /dev/null +++ b/flytestdlib/namespaceutils/namespaceutils_test.go @@ -0,0 +1,45 @@ +package namespaceutils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetNamespaceName(t *testing.T) { + testCases := []struct { + template string + org string + project string + domain string + want string + }{ + {"prefix-{{ project }}-{{ domain }}", "", "flytesnacks", "production", "prefix-flytesnacks-production"}, + {"{{ domain }}", "", "flytesnacks", "production", "production"}, + {"{{ project }}", "", "flytesnacks", "production", "flytesnacks"}, + {"{{ org }}-{{ project }}-{{ domain }}", "acme", "flytesnacks", "production", "acme-flytesnacks-production"}, + {"{{ org }}", "acme", "flytesnacks", "production", "acme"}, + } + + for _, tc := range testCases { + got := GetNamespaceName(tc.template, tc.org, tc.project, tc.domain) + assert.Equal(t, tc.want, got) + } +} + +func TestGetNameWithNamespacedPrefix(t *testing.T) { + testCases := []struct { + prefix string + name string + want string + }{ + {"project-domain-", "app", "project-domain-app"}, + {"", "app", "app"}, + {"prefix-", "", "prefix-"}, + } + + for _, tc := range testCases { + got := GetNameWithNamespacedPrefix(tc.prefix, tc.name) + assert.Equal(t, tc.want, got) + } +}