diff --git a/pkg/transformer/kubernetes/kubernetes.go b/pkg/transformer/kubernetes/kubernetes.go index faf98bd75..57e8ee18a 100644 --- a/pkg/transformer/kubernetes/kubernetes.go +++ b/pkg/transformer/kubernetes/kubernetes.go @@ -1601,7 +1601,11 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject. // sort all object so Services are first k.SortServicesFirst(&allobjects) k.RemoveDupObjects(&allobjects) - transformer.AssignNamespaceToObjects(&allobjects, komposeObject.Namespace) + + // Only append namespaces if --namespace has been passed in + if komposeObject.Namespace != "" { + transformer.AssignNamespaceToObjects(&allobjects, komposeObject.Namespace) + } // k.FixWorkloadVersion(&allobjects) return allobjects, nil } diff --git a/pkg/transformer/kubernetes/kubernetes_test.go b/pkg/transformer/kubernetes/kubernetes_test.go index dd3fe4481..ef1a0053f 100644 --- a/pkg/transformer/kubernetes/kubernetes_test.go +++ b/pkg/transformer/kubernetes/kubernetes_test.go @@ -1119,3 +1119,29 @@ func TestNamespaceGeneration(t *testing.T) { } } } + +// Test namespace generation with namespace being blank / "" +func TestNamespaceGenerationBlank(t *testing.T) { + ns := "" + komposeObject := kobject.KomposeObject{ + ServiceConfigs: map[string]kobject.ServiceConfig{"app": newServiceConfig()}, + Namespace: ns, + } + k := Kubernetes{} + objs, err := k.Transform(komposeObject, kobject.ConvertOptions{}) + if err != nil { + t.Error(errors.Wrap(err, "k.Transform failed")) + } + for _, obj := range objs { + if namespace, ok := obj.(*api.Namespace); ok { + if strings.ToLower(ns) != strings.ToLower(namespace.ObjectMeta.Name) { + t.Errorf("Expected namespace name %v, got %v", ns, namespace.ObjectMeta.Name) + } + } + if dep, ok := obj.(*appsv1.Deployment); ok { + if dep.ObjectMeta.Namespace != ns { + t.Errorf("Expected deployment namespace %v, got %v", ns, dep.ObjectMeta.Namespace) + } + } + } +}