diff --git a/pkg/commands/addannotation.go b/pkg/commands/addannotation.go new file mode 100644 index 000000000..225a1f335 --- /dev/null +++ b/pkg/commands/addannotation.go @@ -0,0 +1,111 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package commands + +import ( + "errors" + "fmt" + "strings" + "regexp" + + "github.com/spf13/cobra" + + "github.com/kubernetes-sigs/kustomize/pkg/constants" + "github.com/kubernetes-sigs/kustomize/pkg/fs" +) + +type addAnnotationOptions struct { + annotations string +} + +// newCmdAddAnnotation adds one or more commonAnnotations to the kustomization file. +func newCmdAddAnnotation(fsys fs.FileSystem) *cobra.Command { + var o addAnnotationOptions + + cmd := &cobra.Command{ + Use: "annotation", + Short: "Adds one or more commonAnnotations to the kustomization.yaml in current directory", + Example: ` + add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`, + RunE: func(cmd *cobra.Command, args []string) error { + err := o.Validate(args) + if err != nil { + return err + } + err = o.Complete(cmd, args) + if err != nil { + return err + } + return o.RunAddAnnotation(fsys) + }, + } + return cmd +} + +// Validate validates addAnnotation command. +func (o *addAnnotationOptions) Validate(args []string) error { + if len(args) < 1 { + return errors.New("must specify an annotation") + } + if len(args) > 1 { + return errors.New("annotations must be comma-separated, with no spaces. See help text for example.") + } + inputs := strings.Split(args[0],",") + for _, input := range inputs { + ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input) + if err != nil { + return err + } + if !ok { + return fmt.Errorf("invalid annotation format: %s", input) + } + } + o.annotations = args[0] + return nil +} + +// Complete completes addAnnotation command. +func (o *addAnnotationOptions) Complete(cmd *cobra.Command, args []string) error { + return nil +} + +// RunAddAnnotation runs addAnnotation command (do real work). +func (o *addAnnotationOptions) 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{ + m.CommonAnnotations = make(map[string]string) + } + annotations := strings.Split(o.annotations, ",") + for _, ann := range annotations { + kv := strings.Split(ann, ":") + if _, ok := m.CommonAnnotations[kv[0]]; ok { + return fmt.Errorf("annotation %s already in kustomization file", kv[0]) + } + m.CommonAnnotations[kv[0]] = kv[1] + } + + return mf.write(m) +} diff --git a/pkg/commands/addannotation_test.go b/pkg/commands/addannotation_test.go new file mode 100644 index 000000000..d40208840 --- /dev/null +++ b/pkg/commands/addannotation_test.go @@ -0,0 +1,17 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package commands \ No newline at end of file diff --git a/pkg/commands/addlabel.go b/pkg/commands/addlabel.go new file mode 100644 index 000000000..80accf280 --- /dev/null +++ b/pkg/commands/addlabel.go @@ -0,0 +1,119 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package commands + +import ( + "regexp" + "errors" + "fmt" + "strings" + + "github.com/spf13/cobra" + + "github.com/kubernetes-sigs/kustomize/pkg/constants" + "github.com/kubernetes-sigs/kustomize/pkg/fs" +) + +type addLabelOptions struct { + labels string +} + +// newCmdAddLabel adds one or more commonLabels to the kustomization file. +func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command { + var o addLabelOptions + + cmd := &cobra.Command{ + Use: "label", + Short: "Adds one or more commonLabels to the kustomization.yaml in current directory", + Example: ` + add label {labelKey1:labelValue1},{labelKey2:labelValue2}`, + RunE: func(cmd *cobra.Command, args []string) error { + err := o.Validate(args) + if err != nil { + return err + } + err = o.Complete(cmd, args) + if err != nil { + return err + } + return o.RunAddLabel(fsys) + }, + } + return cmd +} + +// Validate validates addLabel command. +// TODO: make sure label is of correct format key:value +func (o *addLabelOptions) Validate(args []string) error { + for _, arg := range args { + fmt.Println(arg + "****") + } + + if len(args) < 1 { + return errors.New("must specify a label") + } + if len(args) > 1 { + return errors.New("labels must be comma-separated, with no spaces. See help text for example.") + } + inputs := strings.Split(args[0],",") + for _, input := range inputs { + ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input) + if err != nil { + return err + } + if !ok { + return fmt.Errorf("invalid label format: %s", input) + } + + } + + o.labels = args[0] + return nil +} + +// Complete completes addLabel command. +func (o *addLabelOptions) Complete(cmd *cobra.Command, args []string) error { + return nil +} + +// RunAddLabel runs addLabel command (do real work). +func (o *addLabelOptions) 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{ + m.CommonLabels = make(map[string]string) + } + + labels := strings.Split(o.labels, ",") + for _, label := range labels { + kv := strings.Split(label, ":") + if _, ok := m.CommonLabels[kv[0]]; ok { + return fmt.Errorf("label %s already in kustomization file", kv[0]) + } + m.CommonLabels[kv[0]] = kv[1] + } + + return mf.write(m) +} diff --git a/pkg/commands/addlabel_test.go b/pkg/commands/addlabel_test.go new file mode 100644 index 000000000..d40208840 --- /dev/null +++ b/pkg/commands/addlabel_test.go @@ -0,0 +1,17 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package commands \ No newline at end of file diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index 9c7b2c36e..ba6dcdbea 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -96,6 +96,15 @@ func newCmdAdd(fsys fs.FileSystem) *cobra.Command { # Adds one or more base directories to the kustomization kustomize edit add base kustomize edit add base ,, +<<<<<<< HEAD +======= + + # Adds one or more commonLabels to the kustomization + kustomize edit add label {labelKey1:labelValue1},{labelKey2:labelValue2} + + # Adds one or more commonAnnotations to the kustomization + kustomize edit add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2} +>>>>>>> Implements labels and annotations as subcommands of edit `, Args: cobra.MinimumNArgs(1), } @@ -104,6 +113,8 @@ func newCmdAdd(fsys fs.FileSystem) *cobra.Command { newCmdAddPatch(fsys), newCmdAddConfigMap(fsys), newCmdAddBase(fsys), + newCmdAddLabel(fsys), + newCmdAddAnnotation(fsys), ) return c }