Skip to content

Commit 6457162

Browse files
use apimachinery for annotation/label validation
1 parent 4a565ff commit 6457162

File tree

5 files changed

+111
-218
lines changed

5 files changed

+111
-218
lines changed

Gopkg.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/commands/addmetadata.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ import (
2222

2323
"github.com/kubernetes-sigs/kustomize/pkg/constants"
2424
"github.com/kubernetes-sigs/kustomize/pkg/fs"
25-
"github.com/kubernetes-sigs/kustomize/pkg/validate"
2625
"github.com/spf13/cobra"
26+
"k8s.io/apimachinery/pkg/api/validation"
27+
v1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
28+
"k8s.io/apimachinery/pkg/util/validation/field"
2729
)
2830

2931
// KindOfAdd is the kind of metadata being added: label or annotation
@@ -46,12 +48,17 @@ func (k KindOfAdd) String() string {
4648
}
4749

4850
type addMetadataOptions struct {
49-
metadata map[string]string
51+
metadata map[string]string
52+
validateAnnotations func(map[string]string) field.ErrorList
53+
validateLabels func(map[string]string) field.ErrorList
5054
}
5155

5256
// newCmdAddAnnotation adds one or more commonAnnotations to the kustomization file.
5357
func newCmdAddAnnotation(fsys fs.FileSystem) *cobra.Command {
5458
var o addMetadataOptions
59+
o.validateAnnotations = func(x map[string]string) field.ErrorList {
60+
return validation.ValidateAnnotations(x, field.NewPath("field"))
61+
}
5562

5663
cmd := &cobra.Command{
5764
Use: "annotation",
@@ -72,6 +79,9 @@ func newCmdAddAnnotation(fsys fs.FileSystem) *cobra.Command {
7279
// newCmdAddLabel adds one or more commonLabels to the kustomization file.
7380
func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command {
7481
var o addMetadataOptions
82+
o.validateLabels = func(x map[string]string) field.ErrorList {
83+
return v1validation.ValidateLabels(x, field.NewPath("field"))
84+
}
7585

7686
cmd := &cobra.Command{
7787
Use: "label",
@@ -100,23 +110,30 @@ func (o *addMetadataOptions) ValidateAndParse(args []string, k KindOfAdd) error
100110
}
101111
inputs := strings.Split(args[0], ",")
102112
for _, input := range inputs {
113+
metadata := make(map[string]string)
114+
//parse annotation keys and values into metadata
115+
kv := strings.Split(input, ":")
116+
if len(kv[0]) < 1 {
117+
return fmt.Errorf("invalid %s format: %s", k, input)
118+
}
119+
if len(kv) > 1 {
120+
metadata[kv[0]] = kv[1]
121+
} else {
122+
metadata[kv[0]] = ""
123+
}
103124
switch k {
104125
case label:
105-
valid, err := validate.IsValidLabel(input)
106-
if !valid {
107-
return err
126+
if errs := o.validateLabels(metadata); len(errs) != 0 {
127+
return fmt.Errorf("invalid %s format: %s", k, input)
108128
}
109129
case annotation:
110-
valid, err := validate.IsValidAnnotation(input)
111-
if !valid {
112-
return err
130+
if errs := o.validateAnnotations(metadata); len(errs) != 0 {
131+
return fmt.Errorf("invalid %s format: %s", k, input)
113132
}
114133
default:
115134
return fmt.Errorf("unknown metadata kind %s", k)
116135
}
117-
//parse annotation keys and values into metadata
118-
kv := strings.Split(input, ":")
119-
o.metadata[kv[0]] = kv[1]
136+
o.metadata[kv[0]] = metadata[kv[0]]
120137
}
121138
return nil
122139
}

pkg/commands/addmetadata_test.go

Lines changed: 75 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,80 +17,12 @@ limitations under the License.
1717
package commands
1818

1919
import (
20-
"reflect"
2120
"testing"
2221

2322
"github.com/kubernetes-sigs/kustomize/pkg/constants"
2423
"github.com/kubernetes-sigs/kustomize/pkg/fs"
2524
)
2625

27-
func TestParseValidateInput(t *testing.T) {
28-
var testcases = []struct {
29-
input string
30-
valid bool
31-
name string
32-
expectedData map[string]string
33-
kind KindOfAdd
34-
}{
35-
{
36-
input: "otters:cute",
37-
valid: true,
38-
name: "Adds single input",
39-
expectedData: map[string]string{
40-
"otters": "cute",
41-
},
42-
kind: label,
43-
},
44-
{
45-
input: "owls:great,unicorns:magical",
46-
valid: true,
47-
name: "Adds two items",
48-
expectedData: map[string]string{
49-
"owls": "great",
50-
"unicorns": "magical",
51-
},
52-
kind: label,
53-
},
54-
{
55-
input: "123:45",
56-
valid: true,
57-
name: "Numeric input is allowed",
58-
expectedData: map[string]string{
59-
"123": "45",
60-
},
61-
kind: annotation,
62-
},
63-
{
64-
input: " ",
65-
valid: false,
66-
name: "Empty space input",
67-
expectedData: nil,
68-
kind: annotation,
69-
},
70-
}
71-
var o addMetadataOptions
72-
for _, tc := range testcases {
73-
args := []string{tc.input}
74-
err := o.ValidateAndParse(args, tc.kind)
75-
if err != nil && tc.valid {
76-
t.Errorf("for test case %s, unexpected cmd error: %v", tc.name, err)
77-
}
78-
if err == nil && !tc.valid {
79-
t.Errorf("unexpected error: expected invalid format error for test case %v", tc.name)
80-
}
81-
//o.metadata should be the same as expectedData
82-
if tc.valid {
83-
if !reflect.DeepEqual(o.metadata, tc.expectedData) {
84-
t.Errorf("unexpected error: for test case %s, unexpected data was added", tc.name)
85-
}
86-
} else {
87-
if len(o.metadata) != 0 {
88-
t.Errorf("unexpected error: for test case %s, expected no data to be added", tc.name)
89-
}
90-
}
91-
}
92-
}
93-
9426
func TestRunAddAnnotation(t *testing.T) {
9527
fakeFS := fs.MakeFakeFS()
9628
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
@@ -125,6 +57,44 @@ func TestAddAnnotationNoArgs(t *testing.T) {
12557
t.Errorf("incorrect error: %v", err.Error())
12658
}
12759
}
60+
61+
func TestAddAnnotationInvalidFormat(t *testing.T) {
62+
fakeFS := fs.MakeFakeFS()
63+
cmd := newCmdAddAnnotation(fakeFS)
64+
args := []string{"exclamation!:point"}
65+
err := cmd.RunE(cmd, args)
66+
if err == nil {
67+
t.Errorf("expected an error but error is %v", err)
68+
}
69+
if err != nil && err.Error() != "invalid annotation format: exclamation!:point" {
70+
t.Errorf("incorrect error: %v", err.Error())
71+
}
72+
}
73+
74+
func TestAddAnnotationNoKey(t *testing.T) {
75+
fakeFS := fs.MakeFakeFS()
76+
cmd := newCmdAddAnnotation(fakeFS)
77+
args := []string{":nokey"}
78+
err := cmd.RunE(cmd, args)
79+
if err == nil {
80+
t.Errorf("expected an error but error is %v", err)
81+
}
82+
if err != nil && err.Error() != "invalid annotation format: :nokey" {
83+
t.Errorf("incorrect error: %v", err.Error())
84+
}
85+
}
86+
87+
func TestAddAnnotationNoValue(t *testing.T) {
88+
fakeFS := fs.MakeFakeFS()
89+
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
90+
cmd := newCmdAddAnnotation(fakeFS)
91+
args := []string{"no:,value"}
92+
err := cmd.RunE(cmd, args)
93+
if err != nil {
94+
t.Errorf("unexpected error: %v", err.Error())
95+
}
96+
}
97+
12898
func TestAddAnnotationMultipleArgs(t *testing.T) {
12999
fakeFS := fs.MakeFakeFS()
130100
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
@@ -164,7 +134,6 @@ func TestRunAddLabel(t *testing.T) {
164134

165135
func TestAddLabelNoArgs(t *testing.T) {
166136
fakeFS := fs.MakeFakeFS()
167-
168137
cmd := newCmdAddLabel(fakeFS)
169138
err := cmd.Execute()
170139
if err == nil {
@@ -175,6 +144,43 @@ func TestAddLabelNoArgs(t *testing.T) {
175144
}
176145
}
177146

147+
func TestAddLabelInvalidFormat(t *testing.T) {
148+
fakeFS := fs.MakeFakeFS()
149+
cmd := newCmdAddLabel(fakeFS)
150+
args := []string{"exclamation!:point"}
151+
err := cmd.RunE(cmd, args)
152+
if err == nil {
153+
t.Errorf("expected an error but error is: %v", err)
154+
}
155+
if err != nil && err.Error() != "invalid label format: exclamation!:point" {
156+
t.Errorf("incorrect error: %v", err.Error())
157+
}
158+
}
159+
160+
func TestAddLabelNoKey(t *testing.T) {
161+
fakeFS := fs.MakeFakeFS()
162+
cmd := newCmdAddLabel(fakeFS)
163+
args := []string{":nokey"}
164+
err := cmd.RunE(cmd, args)
165+
if err == nil {
166+
t.Errorf("expected an error but error is: %v", err)
167+
}
168+
if err != nil && err.Error() != "invalid label format: :nokey" {
169+
t.Errorf("incorrect error: %v", err.Error())
170+
}
171+
}
172+
173+
func TestAddLabelNoValue(t *testing.T) {
174+
fakeFS := fs.MakeFakeFS()
175+
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
176+
cmd := newCmdAddLabel(fakeFS)
177+
args := []string{"no,value:"}
178+
err := cmd.RunE(cmd, args)
179+
if err != nil {
180+
t.Errorf("unexpected error: %v", err.Error())
181+
}
182+
}
183+
178184
func TestAddLabelMultipleArgs(t *testing.T) {
179185
fakeFS := fs.MakeFakeFS()
180186
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))

pkg/validate/validate.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)