Skip to content

Commit 1e38240

Browse files
Implements labels and annotations as subcommands of edit
1 parent 94dab9d commit 1e38240

File tree

5 files changed

+275
-0
lines changed

5 files changed

+275
-0
lines changed

pkg/commands/addannotation.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
"regexp"
24+
25+
"github.com/spf13/cobra"
26+
27+
"github.com/kubernetes-sigs/kustomize/pkg/constants"
28+
"github.com/kubernetes-sigs/kustomize/pkg/fs"
29+
)
30+
31+
type addAnnotationOptions struct {
32+
annotations string
33+
}
34+
35+
// newCmdAddAnnotation adds one or more commonAnnotations to the kustomization file.
36+
func newCmdAddAnnotation(fsys fs.FileSystem) *cobra.Command {
37+
var o addAnnotationOptions
38+
39+
cmd := &cobra.Command{
40+
Use: "annotation",
41+
Short: "Adds one or more commonAnnotations to the kustomization.yaml in current directory",
42+
Example: `
43+
add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`,
44+
RunE: func(cmd *cobra.Command, args []string) error {
45+
err := o.Validate(args)
46+
if err != nil {
47+
return err
48+
}
49+
err = o.Complete(cmd, args)
50+
if err != nil {
51+
return err
52+
}
53+
return o.RunAddAnnotation(fsys)
54+
},
55+
}
56+
return cmd
57+
}
58+
59+
// Validate validates addAnnotation command.
60+
func (o *addAnnotationOptions) Validate(args []string) error {
61+
if len(args) < 1 {
62+
return errors.New("must specify an annotation")
63+
}
64+
if len(args) > 1 {
65+
return errors.New("annotations must be comma-separated, with no spaces. See help text for example.")
66+
}
67+
inputs := strings.Split(args[0],",")
68+
for _, input := range inputs {
69+
ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input)
70+
if err != nil {
71+
return err
72+
}
73+
if !ok {
74+
return fmt.Errorf("invalid annotation format: %s", input)
75+
}
76+
}
77+
o.annotations = args[0]
78+
return nil
79+
}
80+
81+
// Complete completes addAnnotation command.
82+
func (o *addAnnotationOptions) Complete(cmd *cobra.Command, args []string) error {
83+
return nil
84+
}
85+
86+
// RunAddAnnotation runs addAnnotation command (do real work).
87+
func (o *addAnnotationOptions) RunAddAnnotation(fsys fs.FileSystem) error {
88+
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
89+
if err != nil {
90+
return err
91+
}
92+
93+
m, err := mf.read()
94+
if err != nil {
95+
return err
96+
}
97+
98+
if m.CommonAnnotations == nil{
99+
m.CommonAnnotations = make(map[string]string)
100+
}
101+
annotations := strings.Split(o.annotations, ",")
102+
for _, ann := range annotations {
103+
kv := strings.Split(ann, ":")
104+
if _, ok := m.CommonAnnotations[kv[0]]; ok {
105+
return fmt.Errorf("annotation %s already in kustomization file", kv[0])
106+
}
107+
m.CommonAnnotations[kv[0]] = kv[1]
108+
}
109+
110+
return mf.write(m)
111+
}

pkg/commands/addannotation_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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

pkg/commands/addlabel.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
"regexp"
21+
"errors"
22+
"fmt"
23+
"strings"
24+
25+
"github.com/spf13/cobra"
26+
27+
"github.com/kubernetes-sigs/kustomize/pkg/constants"
28+
"github.com/kubernetes-sigs/kustomize/pkg/fs"
29+
)
30+
31+
type addLabelOptions struct {
32+
labels string
33+
}
34+
35+
// newCmdAddLabel adds one or more commonLabels to the kustomization file.
36+
func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command {
37+
var o addLabelOptions
38+
39+
cmd := &cobra.Command{
40+
Use: "label",
41+
Short: "Adds one or more commonLabels to the kustomization.yaml in current directory",
42+
Example: `
43+
add label {labelKey1:labelValue1},{labelKey2:labelValue2}`,
44+
RunE: func(cmd *cobra.Command, args []string) error {
45+
err := o.Validate(args)
46+
if err != nil {
47+
return err
48+
}
49+
err = o.Complete(cmd, args)
50+
if err != nil {
51+
return err
52+
}
53+
return o.RunAddLabel(fsys)
54+
},
55+
}
56+
return cmd
57+
}
58+
59+
// Validate validates addLabel command.
60+
// TODO: make sure label is of correct format key:value
61+
func (o *addLabelOptions) Validate(args []string) error {
62+
for _, arg := range args {
63+
fmt.Println(arg + "****")
64+
}
65+
66+
if len(args) < 1 {
67+
return errors.New("must specify a label")
68+
}
69+
if len(args) > 1 {
70+
return errors.New("labels must be comma-separated, with no spaces. See help text for example.")
71+
}
72+
inputs := strings.Split(args[0],",")
73+
for _, input := range inputs {
74+
ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input)
75+
if err != nil {
76+
return err
77+
}
78+
if !ok {
79+
return fmt.Errorf("invalid label format: %s", input)
80+
}
81+
82+
}
83+
84+
o.labels = args[0]
85+
return nil
86+
}
87+
88+
// Complete completes addLabel command.
89+
func (o *addLabelOptions) Complete(cmd *cobra.Command, args []string) error {
90+
return nil
91+
}
92+
93+
// RunAddLabel runs addLabel command (do real work).
94+
func (o *addLabelOptions) RunAddLabel(fsys fs.FileSystem) error {
95+
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
96+
if err != nil {
97+
return err
98+
}
99+
100+
m, err := mf.read()
101+
if err != nil {
102+
return err
103+
}
104+
105+
if m.CommonLabels == nil{
106+
m.CommonLabels = make(map[string]string)
107+
}
108+
109+
labels := strings.Split(o.labels, ",")
110+
for _, label := range labels {
111+
kv := strings.Split(label, ":")
112+
if _, ok := m.CommonLabels[kv[0]]; ok {
113+
return fmt.Errorf("label %s already in kustomization file", kv[0])
114+
}
115+
m.CommonLabels[kv[0]] = kv[1]
116+
}
117+
118+
return mf.write(m)
119+
}

pkg/commands/addlabel_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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

pkg/commands/commands.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ func newCmdAdd(fsys fs.FileSystem) *cobra.Command {
9696
# Adds one or more base directories to the kustomization
9797
kustomize edit add base <filepath>
9898
kustomize edit add base <filepath1>,<filepath2>,<filepath3>
99+
<<<<<<< HEAD
100+
=======
101+
102+
# Adds one or more commonLabels to the kustomization
103+
kustomize edit add label {labelKey1:labelValue1},{labelKey2:labelValue2}
104+
105+
# Adds one or more commonAnnotations to the kustomization
106+
kustomize edit add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}
107+
>>>>>>> Implements labels and annotations as subcommands of edit
99108
`,
100109
Args: cobra.MinimumNArgs(1),
101110
}
@@ -104,6 +113,8 @@ func newCmdAdd(fsys fs.FileSystem) *cobra.Command {
104113
newCmdAddPatch(fsys),
105114
newCmdAddConfigMap(fsys),
106115
newCmdAddBase(fsys),
116+
newCmdAddLabel(fsys),
117+
newCmdAddAnnotation(fsys),
107118
)
108119
return c
109120
}

0 commit comments

Comments
 (0)