Skip to content

Commit f9577ab

Browse files
authored
Merge pull request #247 from ryane/setnamespace
add support for `kustomize edit set namespace`
2 parents 017c4ae + e2cd44f commit f9577ab

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

pkg/commands/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func newCmdSet(fsys fs.FileSystem) *cobra.Command {
123123

124124
c.AddCommand(
125125
newCmdSetNamePrefix(fsys),
126+
newCmdSetNamespace(fsys),
126127
newCmdSetImageTag(fsys),
127128
)
128129
return c

pkg/commands/setnamespace.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package commands
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"strings"
23+
24+
"github.com/kubernetes-sigs/kustomize/pkg/constants"
25+
"github.com/kubernetes-sigs/kustomize/pkg/fs"
26+
"github.com/spf13/cobra"
27+
"k8s.io/apimachinery/pkg/util/validation"
28+
)
29+
30+
type setNamespaceOptions struct {
31+
namespace string
32+
}
33+
34+
// newCmdSetNamespace sets the value of the namespace field in the kustomization.
35+
func newCmdSetNamespace(fsys fs.FileSystem) *cobra.Command {
36+
var o setNamespaceOptions
37+
38+
cmd := &cobra.Command{
39+
Use: "namespace",
40+
Short: "Sets the value of the namespace field in the kustomization file",
41+
Example: `
42+
The command
43+
set namespace staging
44+
will add the field "namespace: staging" to the kustomization file if it doesn't exist,
45+
and overwrite the value with "staging" if the field does exist.
46+
`,
47+
RunE: func(cmd *cobra.Command, args []string) error {
48+
err := o.Validate(args)
49+
if err != nil {
50+
return err
51+
}
52+
return o.RunSetNamespace(fsys)
53+
},
54+
}
55+
return cmd
56+
}
57+
58+
// Validate validates setNamespace command.
59+
func (o *setNamespaceOptions) Validate(args []string) error {
60+
if len(args) != 1 {
61+
return errors.New("must specify exactly one namespace value")
62+
}
63+
ns := args[0]
64+
if errs := validation.IsDNS1123Label(ns); len(errs) != 0 {
65+
return fmt.Errorf("%q is not a valid namespace name: %s", ns, strings.Join(errs, ";"))
66+
}
67+
o.namespace = ns
68+
return nil
69+
}
70+
71+
// RunSetNamespace runs setNamespace command (does real work).
72+
func (o *setNamespaceOptions) RunSetNamespace(fsys fs.FileSystem) error {
73+
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
74+
if err != nil {
75+
return err
76+
}
77+
m, err := mf.read()
78+
if err != nil {
79+
return err
80+
}
81+
m.Namespace = o.namespace
82+
return mf.write(m)
83+
}

pkg/commands/setnamespace_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package commands
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
"testing"
23+
24+
"github.com/kubernetes-sigs/kustomize/pkg/constants"
25+
"github.com/kubernetes-sigs/kustomize/pkg/fs"
26+
)
27+
28+
const (
29+
goodNamespaceValue = "staging"
30+
)
31+
32+
func TestSetNamespaceHappyPath(t *testing.T) {
33+
fakeFS := fs.MakeFakeFS()
34+
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
35+
36+
cmd := newCmdSetNamespace(fakeFS)
37+
args := []string{goodNamespaceValue}
38+
err := cmd.RunE(cmd, args)
39+
if err != nil {
40+
t.Errorf("unexpected cmd error: %v", err)
41+
}
42+
content, err := fakeFS.ReadFile(constants.KustomizationFileName)
43+
if err != nil {
44+
t.Errorf("unexpected read error: %v", err)
45+
}
46+
expected := []byte(fmt.Sprintf("namespace: %s", goodNamespaceValue))
47+
if !strings.Contains(string(content), string(expected)) {
48+
t.Errorf("expected namespace in kustomization file")
49+
}
50+
}
51+
52+
func TestSetNamespaceOverride(t *testing.T) {
53+
fakeFS := fs.MakeFakeFS()
54+
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
55+
56+
cmd := newCmdSetNamespace(fakeFS)
57+
args := []string{goodNamespaceValue}
58+
err := cmd.RunE(cmd, args)
59+
if err != nil {
60+
t.Errorf("unexpected cmd error: %v", err)
61+
}
62+
args = []string{"newnamespace"}
63+
err = cmd.RunE(cmd, args)
64+
if err != nil {
65+
t.Errorf("unexpected cmd error: %v", err)
66+
}
67+
content, err := fakeFS.ReadFile(constants.KustomizationFileName)
68+
if err != nil {
69+
t.Errorf("unexpected read error: %v", err)
70+
}
71+
expected := []byte("namespace: newnamespace")
72+
if !strings.Contains(string(content), string(expected)) {
73+
t.Errorf("expected namespace in kustomization file %s", string(content))
74+
}
75+
}
76+
77+
func TestSetNamespaceNoArgs(t *testing.T) {
78+
fakeFS := fs.MakeFakeFS()
79+
80+
cmd := newCmdSetNamespace(fakeFS)
81+
err := cmd.Execute()
82+
if err == nil {
83+
t.Errorf("expected error: %v", err)
84+
}
85+
if err.Error() != "must specify exactly one namespace value" {
86+
t.Errorf("incorrect error: %v", err.Error())
87+
}
88+
}
89+
90+
func TestSetNamespaceInvalid(t *testing.T) {
91+
fakeFS := fs.MakeFakeFS()
92+
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
93+
94+
cmd := newCmdSetNamespace(fakeFS)
95+
args := []string{"/badnamespace/"}
96+
err := cmd.RunE(cmd, args)
97+
if err == nil {
98+
t.Errorf("expected error: %v", err)
99+
}
100+
if !strings.Contains(err.Error(), "is not a valid namespace name") {
101+
t.Errorf("unexpected error: %v", err.Error())
102+
}
103+
}

0 commit comments

Comments
 (0)