mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Implements labels and annotations as subcommands of edit
This commit is contained in:
111
pkg/commands/addannotation.go
Normal file
111
pkg/commands/addannotation.go
Normal file
@@ -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)
|
||||
}
|
||||
17
pkg/commands/addannotation_test.go
Normal file
17
pkg/commands/addannotation_test.go
Normal file
@@ -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
|
||||
119
pkg/commands/addlabel.go
Normal file
119
pkg/commands/addlabel.go
Normal file
@@ -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)
|
||||
}
|
||||
17
pkg/commands/addlabel_test.go
Normal file
17
pkg/commands/addlabel_test.go
Normal file
@@ -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
|
||||
@@ -96,6 +96,15 @@ func newCmdAdd(fsys fs.FileSystem) *cobra.Command {
|
||||
# Adds one or more base directories to the kustomization
|
||||
kustomize edit add base <filepath>
|
||||
kustomize edit add base <filepath1>,<filepath2>,<filepath3>
|
||||
<<<<<<< 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user