Skip to content

Commit

Permalink
Fix namespace resolution with DEFAULT_NAMESPACE and other parameters …
Browse files Browse the repository at this point in the history
…set (#2659)
  • Loading branch information
shubham-bansal96 committed Dec 7, 2022
1 parent 9ccd2a4 commit 4dde3c9
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 21 deletions.
36 changes: 21 additions & 15 deletions pkg/utils/namespace.go
Expand Up @@ -42,7 +42,7 @@ func init() {
FunctionNamespace: os.Getenv(ENV_FUNCTION_NAMESPACE),
BuiderNamespace: os.Getenv(ENV_BUILDER_NAMESPACE),
DefaultNamespace: os.Getenv(ENV_DEFAULT_NAMESPACE),
FissionResourceNS: getNamespaces(),
FissionResourceNS: GetNamespaces(),
Logger: loggerfactory.GetLogger(),
}

Expand Down Expand Up @@ -106,27 +106,33 @@ func (nsr *NamespaceResolver) FissionNSWithOptions(option ...option) map[string]
return fissionResourceNS
}

func getNamespaces() map[string]string {
envValue := os.Getenv(ENV_ADDITIONAL_NAMESPACE)
if len(envValue) == 0 {
return map[string]string{
metav1.NamespaceDefault: metav1.NamespaceDefault,
}
func GetNamespaces() map[string]string {
namespaces := make(map[string]string)

envValue := os.Getenv(ENV_DEFAULT_NAMESPACE)
if len(envValue) > 0 {
namespaces[envValue] = envValue
}

lstNamespaces := strings.Split(envValue, ",")
namespaces := make(map[string]string, len(lstNamespaces))
for _, namespace := range lstNamespaces {
//check to handle string with additional comma at the end of string. eg- ns1,ns2,
if namespace != "" {
namespaces[namespace] = namespace
envValue = os.Getenv(ENV_ADDITIONAL_NAMESPACE)
if len(envValue) > 0 {
lstNamespaces := strings.Split(envValue, ",")
for _, namespace := range lstNamespaces {
//check to handle string with additional comma at the end of string. eg- ns1,ns2,
if namespace != "" {
namespaces[namespace] = namespace
}
}
}

if len(namespaces) == 0 {
namespaces[metav1.NamespaceDefault] = metav1.NamespaceDefault
}
return namespaces
}

func (nsr *NamespaceResolver) GetBuilderNS(namespace string) string {
if nsr.FunctionNamespace == "" || nsr.BuiderNamespace == "" {
if nsr.BuiderNamespace == "" {
return namespace
}

Expand All @@ -137,7 +143,7 @@ func (nsr *NamespaceResolver) GetBuilderNS(namespace string) string {
}

func (nsr *NamespaceResolver) GetFunctionNS(namespace string) string {
if nsr.FunctionNamespace == "" || nsr.BuiderNamespace == "" {
if nsr.FunctionNamespace == "" {
return namespace
}

Expand Down
130 changes: 124 additions & 6 deletions pkg/utils/namespace_test.go
@@ -1,6 +1,7 @@
package utils

import (
"os"
"testing"
)

Expand All @@ -20,22 +21,52 @@ func TestNamespaceResolver(t *testing.T) {
},
{
name: "should return testns2 namespace",
namespaceResolver: getFissionNamespaces("fission-builder", "fission-function", "testns"),
namespaceResolver: getFissionNamespaces("fission-builder", "fission-function", "default"),
namespace: "testns2",
expected: "testns2",
},
{
name: "should return fission-builder namespace",
namespaceResolver: getFissionNamespaces("fission-builder", "", "default"),
namespace: "default",
expected: "fission-builder",
},
{
name: "should return testns3 namespace",
namespaceResolver: getFissionNamespaces("", "", "testns"),
namespaceResolver: getFissionNamespaces("fission-builder", "", "testns"),
namespace: "testns3",
expected: "testns3",
},
{
name: "should return testns4 namespace",
namespaceResolver: getFissionNamespaces("", "fission-function", "default"),
namespace: "testns4",
expected: "testns4",
},
{
name: "should return testns5 namespace",
namespaceResolver: getFissionNamespaces("", "fission-function", "testns"),
namespace: "testns5",
expected: "testns5",
},
{
name: "should return default namespace",
namespaceResolver: getFissionNamespaces("fission-builder", "", "default"),
namespaceResolver: getFissionNamespaces("", "", "default"),
namespace: "default",
expected: "default",
},
{
name: "should return testns6 namespace",
namespaceResolver: getFissionNamespaces("", "", "default"),
namespace: "testns6",
expected: "testns6",
},
{
name: "should return testns7 namespace",
namespaceResolver: getFissionNamespaces("", "", ""),
namespace: "testns7",
expected: "testns7",
},
} {
t.Run(test.name, func(t *testing.T) {
ns := test.namespaceResolver.GetBuilderNS(test.namespace)
Expand All @@ -61,22 +92,52 @@ func TestNamespaceResolver(t *testing.T) {
},
{
name: "should return testns2 namespace",
namespaceResolver: getFissionNamespaces("fission-builder", "fission-function", "testns"),
namespaceResolver: getFissionNamespaces("fission-builder", "fission-function", "default"),
namespace: "testns2",
expected: "testns2",
},
{
name: "should return fission-function namespace",
namespaceResolver: getFissionNamespaces("", "fission-function", "default"),
namespace: "default",
expected: "fission-function",
},
{
name: "should return testns3 namespace",
namespaceResolver: getFissionNamespaces("", "", "testns"),
namespaceResolver: getFissionNamespaces("", "fission-function", "testns"),
namespace: "testns3",
expected: "testns3",
},
{
name: "should return testns4 namespace",
namespaceResolver: getFissionNamespaces("fission-builder", "", "default"),
namespace: "testns4",
expected: "testns4",
},
{
name: "should return testns5 namespace",
namespaceResolver: getFissionNamespaces("fission-builder", "", "testns"),
namespace: "testns5",
expected: "testns5",
},
{
name: "should return default namespace",
namespaceResolver: getFissionNamespaces("", "fission-function", "default"),
namespaceResolver: getFissionNamespaces("", "", "default"),
namespace: "default",
expected: "default",
},
{
name: "should return testns6 namespace",
namespaceResolver: getFissionNamespaces("", "", "default"),
namespace: "testns6",
expected: "testns6",
},
{
name: "should return testns7 namespace",
namespaceResolver: getFissionNamespaces("", "", ""),
namespace: "testns7",
expected: "testns7",
},
} {
t.Run(test.name, func(t *testing.T) {
ns := test.namespaceResolver.GetFunctionNS(test.namespace)
Expand Down Expand Up @@ -115,6 +176,51 @@ func TestNamespaceResolver(t *testing.T) {
})
}
})

t.Run("getNamespace", func(t *testing.T) {
for _, test := range []struct {
name string
defaultNamespace string
additionalNamespace string
expected int
}{
{
name: "length of namespaces should be 1",
defaultNamespace: "",
additionalNamespace: "",
expected: 1,
},
{
name: "length of namespaces should be 1",
defaultNamespace: "default",
additionalNamespace: "",
expected: 1,
},
{
name: "length of namespaces should be 1",
defaultNamespace: "default",
additionalNamespace: "default",
expected: 1,
},
{
name: "length of namespaces should be 3",
defaultNamespace: "default",
additionalNamespace: "testns1,testns2",
expected: 3,
},
} {
t.Run(test.name, func(t *testing.T) {
err := setNamespace(test.defaultNamespace, test.additionalNamespace)
if err != nil {
t.Fatalf("error setting environment Variable %s", err.Error())
}
ns := GetNamespaces()
if test.expected != len(ns) {
t.Errorf("expected length of namespace %d, got %d", test.expected, len(ns))
}
})
}
})
}

func getFissionNamespaces(builderNS, functionNS, defaultNS string) *NamespaceResolver {
Expand All @@ -124,3 +230,15 @@ func getFissionNamespaces(builderNS, functionNS, defaultNS string) *NamespaceRes
DefaultNamespace: defaultNS,
}
}

func setNamespace(defaultNamespace string, additionalNamespace string) error {
err := os.Setenv(ENV_DEFAULT_NAMESPACE, defaultNamespace)
if err != nil {
return err
}
err = os.Setenv(ENV_ADDITIONAL_NAMESPACE, additionalNamespace)
if err != nil {
return err
}
return nil
}

0 comments on commit 4dde3c9

Please sign in to comment.