mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-20 22:15:00 +00:00
[doc]: https://github.com/golang/go/wiki/Modules#releasing-modules-v2-or-higher Per this Go modules [doc] a repo or branch that's already tagged v2 or higher should increment the major version (e.g. go to v3) when releasing their first Go module-based packages. At the moment, the kustomize repo has these top level packages in the sigs.k8s.io/kustomize module: - `cmd` - holds main program for kustomize Conceivably someone can depend on this package for integration tests. - `internal` - intentionally unreleased subpackages - `k8sdeps` - an adapter wrapping k8s dependencies This exists only for use in pre-Go-modules kustomize-into-kubectl integration and won't live much longer (as everything involved is switching to Go modules). - `pkg` - kustomize packages for export This should shrink in later versions, since the surface area is too large, containing sub-packages that should be in 'internal'. - `plugin` - holds main programs for plugins This PR changes the top level go.mod file from ``` module sigs.k8s.io/kustomize ``` to ``` module sigs.k8s.io/kustomize/v3 ``` and adjusts all import statements to reflect the change.
175 lines
4.7 KiB
Go
175 lines
4.7 KiB
Go
/*
|
|
Copyright 2019 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 remove
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/spf13/cobra"
|
|
"sigs.k8s.io/kustomize/v3/pkg/commands/kustfile"
|
|
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
|
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
|
|
"sigs.k8s.io/kustomize/v3/pkg/types"
|
|
"strings"
|
|
)
|
|
|
|
// kindOfAdd is the kind of metadata being added: label or annotation
|
|
type kindOfAdd int
|
|
|
|
const (
|
|
annotation kindOfAdd = iota
|
|
label
|
|
)
|
|
|
|
func (k kindOfAdd) String() string {
|
|
kinds := [...]string{
|
|
"annotation",
|
|
"label",
|
|
}
|
|
if k < 0 || k > 1 {
|
|
return "Unknown metadatakind"
|
|
}
|
|
return kinds[k]
|
|
}
|
|
|
|
type removeMetadataOptions struct {
|
|
ignore bool
|
|
metadata []string
|
|
arrayValidator func([]string) error
|
|
kind kindOfAdd
|
|
}
|
|
|
|
// newCmdRemoveLabel removes one or more commonAnnotations from the kustomization file.
|
|
func newCmdRemoveAnnotation(fSys fs.FileSystem, v func([]string) error) *cobra.Command {
|
|
var o removeMetadataOptions
|
|
o.kind = label
|
|
o.arrayValidator = v
|
|
cmd := &cobra.Command{
|
|
Use: "annotation",
|
|
Short: "Removes one or more commonAnnotations from " + pgmconfig.KustomizationFileNames[0],
|
|
Example: `
|
|
remove annotation {annotationKey1},{annotationKey2}`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return o.runE(args, fSys, o.removeAnnotations)
|
|
},
|
|
}
|
|
cmd.Flags().BoolVarP(&o.ignore, "ignore-non-existence", "i", false,
|
|
"ignore error if the given label doesn't exist",
|
|
)
|
|
return cmd
|
|
}
|
|
|
|
// newCmdRemoveLabel removes one or more commonLabels from the kustomization file.
|
|
func newCmdRemoveLabel(fSys fs.FileSystem, v func([]string) error) *cobra.Command {
|
|
var o removeMetadataOptions
|
|
o.kind = label
|
|
o.arrayValidator = v
|
|
cmd := &cobra.Command{
|
|
Use: "label",
|
|
Short: "Removes one or more commonLabels from " + pgmconfig.KustomizationFileNames[0],
|
|
Example: `
|
|
remove label {labelKey1},{labelKey2}`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return o.runE(args, fSys, o.removeLabels)
|
|
},
|
|
}
|
|
cmd.Flags().BoolVarP(&o.ignore, "ignore-non-existence", "i", false,
|
|
"ignore error if the given label doesn't exist",
|
|
)
|
|
return cmd
|
|
}
|
|
|
|
func (o *removeMetadataOptions) runE(
|
|
args []string, fSys fs.FileSystem, remover func(*types.Kustomization) error) error {
|
|
err := o.validateAndParse(args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
kf, err := kustfile.NewKustomizationFile(fSys)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
m, err := kf.Read()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = remover(m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return kf.Write(m)
|
|
}
|
|
|
|
// validateAndParse validates `remove` commands and parses them into o.metadata
|
|
func (o *removeMetadataOptions) validateAndParse(args []string) error {
|
|
if len(args) < 1 {
|
|
return fmt.Errorf("must specify %s", o.kind)
|
|
}
|
|
if len(args) > 1 {
|
|
return fmt.Errorf("%ss must be comma-separated, with no spaces", o.kind)
|
|
}
|
|
m, err := o.convertToArray(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = o.arrayValidator(m); err != nil {
|
|
return err
|
|
}
|
|
o.metadata = m
|
|
return nil
|
|
}
|
|
|
|
func (o *removeMetadataOptions) convertToArray(arg string) ([]string, error) {
|
|
inputs := strings.Split(arg, ",")
|
|
result := make([]string, 0, len(inputs))
|
|
|
|
for _, input := range inputs {
|
|
if len(input) == 0 {
|
|
return nil, o.makeError(input, "name is empty")
|
|
}
|
|
result = append(result, input)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (o *removeMetadataOptions) removeAnnotations(m *types.Kustomization) error {
|
|
if m.CommonAnnotations == nil && !o.ignore {
|
|
return fmt.Errorf("commonAnnotations is not defined in kustomization file")
|
|
}
|
|
return o.removeFromMap(m.CommonAnnotations, annotation)
|
|
}
|
|
|
|
func (o *removeMetadataOptions) removeLabels(m *types.Kustomization) error {
|
|
if m.CommonLabels == nil && !o.ignore {
|
|
return fmt.Errorf("commonLabels is not defined in kustomization file")
|
|
}
|
|
return o.removeFromMap(m.CommonLabels, label)
|
|
}
|
|
|
|
func (o *removeMetadataOptions) removeFromMap(m map[string]string, kind kindOfAdd) error {
|
|
for _, k := range o.metadata {
|
|
if _, ok := m[k]; !ok && !o.ignore {
|
|
return fmt.Errorf("%s %s is not defined in kustomization file", kind, k)
|
|
}
|
|
delete(m, k)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (o *removeMetadataOptions) makeError(input string, message string) error {
|
|
return fmt.Errorf("invalid %s: '%s' (%s)", o.kind, input, message)
|
|
}
|