From c9b8ee60e781bd88cb8075b6a0faef2a75388dbb Mon Sep 17 00:00:00 2001 From: Sergey Vilgelm Date: Wed, 29 Apr 2026 14:27:15 -0700 Subject: [PATCH 1/2] feat(flytestdlib): add namespaceutils package to v2 - port GetNamespaceName and GetNameWithNamespacedPrefix utility functions from v1 - enables downstream services to migrate from v1 to v2 flytestdlib - GetNamespaceName generates namespace names using org/project/domain placeholders - GetNameWithNamespacedPrefix concatenates strings with namespace prefix Signed-off-by: Sergey Vilgelm --- flytestdlib/namespaceutils/namespaceutils.go | 27 ++++++++++++ .../namespaceutils/namespaceutils_test.go | 42 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 flytestdlib/namespaceutils/namespaceutils.go create mode 100644 flytestdlib/namespaceutils/namespaceutils_test.go diff --git a/flytestdlib/namespaceutils/namespaceutils.go b/flytestdlib/namespaceutils/namespaceutils.go new file mode 100644 index 00000000000..534f3d14e00 --- /dev/null +++ b/flytestdlib/namespaceutils/namespaceutils.go @@ -0,0 +1,27 @@ +package namespaceutils + +import ( + "fmt" + "strings" +) + +const orgTemplate = "{{ org }}" +const projectTemplate = "{{ project }}" +const domainTemplate = "{{ domain }}" + +const replaceAllInstancesOfString = -1 + +// GetNamespaceName returns kubernetes namespace name according to user defined template from config +func GetNamespaceName(template string, org, project, domain string) string { + var namespace = template + namespace = strings.Replace(namespace, orgTemplate, org, replaceAllInstancesOfString) + namespace = strings.Replace(namespace, projectTemplate, project, replaceAllInstancesOfString) + namespace = strings.Replace(namespace, domainTemplate, domain, replaceAllInstancesOfString) + + return namespace +} + +// GetNameWithNamespacedPrefix returns a name with the given prefix prepended +func GetNameWithNamespacedPrefix(prefix, name string) string { + return fmt.Sprintf("%s%s", prefix, name) +} diff --git a/flytestdlib/namespaceutils/namespaceutils_test.go b/flytestdlib/namespaceutils/namespaceutils_test.go new file mode 100644 index 00000000000..b7acd1d63d5 --- /dev/null +++ b/flytestdlib/namespaceutils/namespaceutils_test.go @@ -0,0 +1,42 @@ +package namespaceutils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetNamespaceName(t *testing.T) { + testCases := []struct { + template string + project string + domain string + want string + }{ + {"prefix-{{ project }}-{{ domain }}", "flytesnacks", "production", "prefix-flytesnacks-production"}, + {"{{ domain }}", "flytesnacks", "production", "production"}, + {"{{ project }}", "flytesnacks", "production", "flytesnacks"}, + } + + for _, tc := range testCases { + got := GetNamespaceName(tc.template, "", 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) + } +} From 4af1ffc4bfdc2db1560b9f858a1b1c5dfcaee523 Mon Sep 17 00:00:00 2001 From: Sergey Vilgelm Date: Mon, 4 May 2026 12:49:33 -0700 Subject: [PATCH 2/2] refactor(flytestdlib): address Copilot review feedback in namespaceutils Use strings.NewReplacer for template substitution, simplify prefix concatenation, and exercise the {{ org }} placeholder in tests. Assisted-By: Claude Opus 4.7 (1M context) Signed-off-by: Sergey Vilgelm --- flytestdlib/namespaceutils/namespaceutils.go | 16 ++++++---------- .../namespaceutils/namespaceutils_test.go | 11 +++++++---- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/flytestdlib/namespaceutils/namespaceutils.go b/flytestdlib/namespaceutils/namespaceutils.go index 534f3d14e00..c6dfc7f2a52 100644 --- a/flytestdlib/namespaceutils/namespaceutils.go +++ b/flytestdlib/namespaceutils/namespaceutils.go @@ -1,7 +1,6 @@ package namespaceutils import ( - "fmt" "strings" ) @@ -9,19 +8,16 @@ const orgTemplate = "{{ org }}" const projectTemplate = "{{ project }}" const domainTemplate = "{{ domain }}" -const replaceAllInstancesOfString = -1 - // GetNamespaceName returns kubernetes namespace name according to user defined template from config func GetNamespaceName(template string, org, project, domain string) string { - var namespace = template - namespace = strings.Replace(namespace, orgTemplate, org, replaceAllInstancesOfString) - namespace = strings.Replace(namespace, projectTemplate, project, replaceAllInstancesOfString) - namespace = strings.Replace(namespace, domainTemplate, domain, replaceAllInstancesOfString) - - return namespace + 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 fmt.Sprintf("%s%s", prefix, name) + return prefix + name } diff --git a/flytestdlib/namespaceutils/namespaceutils_test.go b/flytestdlib/namespaceutils/namespaceutils_test.go index b7acd1d63d5..7bf69efbb0f 100644 --- a/flytestdlib/namespaceutils/namespaceutils_test.go +++ b/flytestdlib/namespaceutils/namespaceutils_test.go @@ -9,17 +9,20 @@ import ( 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"}, + {"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.project, tc.domain) + got := GetNamespaceName(tc.template, tc.org, tc.project, tc.domain) assert.Equal(t, tc.want, got) } }