mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Merge pull request #333 from monopole/deleteCode
Remove duplicate code.
This commit is contained in:
@@ -22,6 +22,7 @@ import (
|
|||||||
|
|
||||||
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
||||||
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||||
|
"github.com/kubernetes-sigs/kustomize/pkg/types"
|
||||||
"github.com/kubernetes-sigs/kustomize/pkg/validators"
|
"github.com/kubernetes-sigs/kustomize/pkg/validators"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@@ -56,18 +57,13 @@ func newCmdAddAnnotation(fSys fs.FileSystem, v validators.MapValidatorFunc) *cob
|
|||||||
var o addMetadataOptions
|
var o addMetadataOptions
|
||||||
o.kind = annotation
|
o.kind = annotation
|
||||||
o.mapValidator = v
|
o.mapValidator = v
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "annotation",
|
Use: "annotation",
|
||||||
Short: "Adds one or more commonAnnotations to " + constants.KustomizationFileName,
|
Short: "Adds one or more commonAnnotations to " + constants.KustomizationFileName,
|
||||||
Example: `
|
Example: `
|
||||||
add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`,
|
add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
err := o.ValidateAndParse(args)
|
return o.runE(args, fSys, o.addAnnotations)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return o.RunAddAnnotation(fSys)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return cmd
|
return cmd
|
||||||
@@ -78,25 +74,41 @@ func newCmdAddLabel(fSys fs.FileSystem, v validators.MapValidatorFunc) *cobra.Co
|
|||||||
var o addMetadataOptions
|
var o addMetadataOptions
|
||||||
o.kind = label
|
o.kind = label
|
||||||
o.mapValidator = v
|
o.mapValidator = v
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "label",
|
Use: "label",
|
||||||
Short: "Adds one or more commonLabels to " + constants.KustomizationFileName,
|
Short: "Adds one or more commonLabels to " + constants.KustomizationFileName,
|
||||||
Example: `
|
Example: `
|
||||||
add label {labelKey1:labelValue1},{labelKey2:labelValue2}`,
|
add label {labelKey1:labelValue1},{labelKey2:labelValue2}`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
err := o.ValidateAndParse(args)
|
return o.runE(args, fSys, o.addLabels)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return o.RunAddLabel(fSys)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateAndParse validates addLabel and addAnnotation commands and parses them into o.metadata
|
func (o *addMetadataOptions) runE(
|
||||||
func (o *addMetadataOptions) ValidateAndParse(args []string) error {
|
args []string, fSys fs.FileSystem, adder func(*types.Kustomization) error) error {
|
||||||
|
err := o.validateAndParse(args)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
kf, err := newKustomizationFile(constants.KustomizationFileName, fSys)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m, err := kf.read()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = adder(m)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return kf.write(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// validateAndParse validates `add` commands and parses them into o.metadata
|
||||||
|
func (o *addMetadataOptions) validateAndParse(args []string) error {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return fmt.Errorf("must specify %s", o.kind)
|
return fmt.Errorf("must specify %s", o.kind)
|
||||||
}
|
}
|
||||||
@@ -120,10 +132,10 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error)
|
|||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
kv := strings.Split(input, ":")
|
kv := strings.Split(input, ":")
|
||||||
if len(kv[0]) < 1 {
|
if len(kv[0]) < 1 {
|
||||||
return nil, makeError(o.kind, input, "empty key")
|
return nil, o.makeError(input, "empty key")
|
||||||
}
|
}
|
||||||
if len(kv) > 2 {
|
if len(kv) > 2 {
|
||||||
return nil, makeError(o.kind, input, "too many colons")
|
return nil, o.makeError(input, "too many colons")
|
||||||
}
|
}
|
||||||
if len(kv) > 1 {
|
if len(kv) > 1 {
|
||||||
result[kv[0]] = kv[1]
|
result[kv[0]] = kv[1]
|
||||||
@@ -134,54 +146,30 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error)
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunAddAnnotation runs addAnnotation command, doing the real work.
|
func (o *addMetadataOptions) addAnnotations(m *types.Kustomization) error {
|
||||||
func (o *addMetadataOptions) RunAddAnnotation(fsys fs.FileSystem) error {
|
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m, err := mf.read()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.CommonAnnotations == nil {
|
if m.CommonAnnotations == nil {
|
||||||
m.CommonAnnotations = make(map[string]string)
|
m.CommonAnnotations = make(map[string]string)
|
||||||
}
|
}
|
||||||
|
return o.writeToMap(m.CommonAnnotations, annotation)
|
||||||
for key, value := range o.metadata {
|
|
||||||
if _, ok := m.CommonAnnotations[key]; ok {
|
|
||||||
return fmt.Errorf("%s %s already in kustomization file", o.kind, key)
|
|
||||||
}
|
|
||||||
m.CommonAnnotations[key] = value
|
|
||||||
}
|
|
||||||
return mf.write(m)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunAddLabel runs addLabel command, doing the real work.
|
func (o *addMetadataOptions) addLabels(m *types.Kustomization) error {
|
||||||
func (o *addMetadataOptions) RunAddLabel(fsys fs.FileSystem) error {
|
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m, err := mf.read()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.CommonLabels == nil {
|
if m.CommonLabels == nil {
|
||||||
m.CommonLabels = make(map[string]string)
|
m.CommonLabels = make(map[string]string)
|
||||||
}
|
}
|
||||||
|
return o.writeToMap(m.CommonLabels, label)
|
||||||
|
}
|
||||||
|
|
||||||
for key, value := range o.metadata {
|
func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) error {
|
||||||
if _, ok := m.CommonLabels[key]; ok {
|
for k, v := range o.metadata {
|
||||||
return fmt.Errorf("%s %s already in kustomization file", o.kind, key)
|
if _, ok := m[k]; ok {
|
||||||
|
return fmt.Errorf("%s %s already in kustomization file", kind, k)
|
||||||
}
|
}
|
||||||
m.CommonLabels[key] = value
|
m[k] = v
|
||||||
}
|
}
|
||||||
return mf.write(m)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeError(k kindOfAdd, input string, message string) error {
|
func (o *addMetadataOptions) makeError(input string, message string) error {
|
||||||
return fmt.Errorf("invalid %s: %s (%s)", k, input, message)
|
return fmt.Errorf("invalid %s: %s (%s)", o.kind, input, message)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,27 +21,41 @@ import (
|
|||||||
|
|
||||||
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
||||||
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||||
|
"github.com/kubernetes-sigs/kustomize/pkg/types"
|
||||||
"github.com/kubernetes-sigs/kustomize/pkg/validators"
|
"github.com/kubernetes-sigs/kustomize/pkg/validators"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRunAddAnnotation(t *testing.T) {
|
func makeKustomization(t *testing.T) *types.Kustomization {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
||||||
|
kf, err := newKustomizationFile(constants.KustomizationFileName, fakeFS)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected new error %v", err)
|
||||||
|
}
|
||||||
|
m, err := kf.read()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected read error %v", err)
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunAddAnnotation(t *testing.T) {
|
||||||
var o addMetadataOptions
|
var o addMetadataOptions
|
||||||
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
|
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
|
||||||
|
|
||||||
err := o.RunAddAnnotation(fakeFS)
|
m := makeKustomization(t)
|
||||||
|
err := o.addAnnotations(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: could not write to kustomization file")
|
t.Errorf("unexpected error: could not write to kustomization file")
|
||||||
}
|
}
|
||||||
// adding the same test input should not work
|
// adding the same test input should not work
|
||||||
err = o.RunAddAnnotation(fakeFS)
|
err = o.addAnnotations(m)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected already in kustomization file error")
|
t.Errorf("expected already in kustomization file error")
|
||||||
}
|
}
|
||||||
// adding new annotations should work
|
// adding new annotations should work
|
||||||
o.metadata = map[string]string{"new": "annotation"}
|
o.metadata = map[string]string{"new": "annotation"}
|
||||||
err = o.RunAddAnnotation(fakeFS)
|
err = o.addAnnotations(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: could not write to kustomization file")
|
t.Errorf("unexpected error: could not write to kustomization file")
|
||||||
}
|
}
|
||||||
@@ -149,23 +163,22 @@ func TestAddAnnotationMultipleArgs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunAddLabel(t *testing.T) {
|
func TestRunAddLabel(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
|
||||||
var o addMetadataOptions
|
var o addMetadataOptions
|
||||||
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
|
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
|
||||||
|
|
||||||
err := o.RunAddLabel(fakeFS)
|
m := makeKustomization(t)
|
||||||
|
err := o.addLabels(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: could not write to kustomization file")
|
t.Errorf("unexpected error: could not write to kustomization file")
|
||||||
}
|
}
|
||||||
// adding the same test input should not work
|
// adding the same test input should not work
|
||||||
err = o.RunAddLabel(fakeFS)
|
err = o.addLabels(m)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected already in kustomization file error")
|
t.Errorf("expected already in kustomization file error")
|
||||||
}
|
}
|
||||||
// adding new labels should work
|
// adding new labels should work
|
||||||
o.metadata = map[string]string{"new": "label"}
|
o.metadata = map[string]string{"new": "label"}
|
||||||
err = o.RunAddLabel(fakeFS)
|
err = o.addLabels(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: could not write to kustomization file")
|
t.Errorf("unexpected error: could not write to kustomization file")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user