Skip to content

Commit

Permalink
only add namespace if specified (#1731)
Browse files Browse the repository at this point in the history
#### What type of PR is this?

<!--
Add one of the following kinds:
/kind cleanup
/kind documentation
/kind feature
-->

/kind bug

#### What this PR does / why we need it:

When we generate the YAML, we should NOT add namespace by default,
namespace should only be added if it has been specified via the command
line.

#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->

Fixes #1729

#### Special notes for your reviewer:

Signed-off-by: Charlie Drage <[email protected]>
  • Loading branch information
cdrage authored Oct 11, 2023
1 parent 46dcb91 commit 09dc978
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/transformer/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/transformer/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

0 comments on commit 09dc978

Please sign in to comment.