Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions flytestdlib/namespaceutils/namespaceutils.go
Original file line number Diff line number Diff line change
@@ -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)
Comment thread
SVilgelm marked this conversation as resolved.
Outdated
Comment thread
SVilgelm marked this conversation as resolved.
Outdated

return namespace
}

// GetNameWithNamespacedPrefix returns a name with the given prefix prepended
func GetNameWithNamespacedPrefix(prefix, name string) string {
return fmt.Sprintf("%s%s", prefix, name)
Comment thread
SVilgelm marked this conversation as resolved.
Outdated
}
Comment thread
SVilgelm marked this conversation as resolved.
42 changes: 42 additions & 0 deletions flytestdlib/namespaceutils/namespaceutils_test.go
Original file line number Diff line number Diff line change
@@ -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)
Comment thread
SVilgelm marked this conversation as resolved.
Outdated
Comment thread
SVilgelm marked this conversation as resolved.
Outdated
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)
}
}
Loading