-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new commands kustomize edit set buildmetadata and kustomize edit remo…
…ve buildmetadata
- Loading branch information
1 parent
cfc6dd6
commit 058c639
Showing
10 changed files
with
362 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2022 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package remove | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/kustfile" | ||
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/util" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
type removeBuildMetadataOptions struct { | ||
*util.BuildMetadataValidator | ||
buildMetadataOptions []string | ||
} | ||
|
||
// newCmdRemoveBuildMetadata removes options to the kustomization's buildMetada field. | ||
func newCmdRemoveBuildMetadata(fSys filesys.FileSystem) *cobra.Command { | ||
var o removeBuildMetadataOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "buildmetadata", | ||
Short: "Removes one or more buildMetadata options to the kustomization.yaml in the current directory", | ||
Long: `Removes one or more buildMetadata options to the kustomization.yaml in the current directory. | ||
The following options are valid: | ||
- originAnnotations | ||
- transformerAnnotations | ||
- managedByLabel | ||
originAnnotations will remove the annotation config.kubernetes.io/origin to each resource, describing where | ||
each resource originated from. | ||
transformerAnnotations will remove the annotation alpha.config.kubernetes.io/transformations to each resource, | ||
describing the transformers that have acted upon the resource. | ||
managedByLabel will remove the label app.kubernetes.io/managed-by to each resource, describing which version | ||
of kustomize managed the resource.`, | ||
Example: ` | ||
remove buildmetadata {option1},{option2}`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var err error | ||
o.buildMetadataOptions, err = o.BuildMetadataValidator.Validate(args) | ||
if err != nil { | ||
return err | ||
} | ||
return o.RunRemoveBuildMetadata(fSys) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
// RunRemoveBuildMetadata runs removeBuildMetadata command (do real work). | ||
func (o *removeBuildMetadataOptions) RunRemoveBuildMetadata(fSys filesys.FileSystem) error { | ||
mf, err := kustfile.NewKustomizationFile(fSys) | ||
if err != nil { | ||
return err | ||
} | ||
m, err := mf.Read() | ||
if err != nil { | ||
return err | ||
} | ||
if len(o.buildMetadataOptions) == 0 { | ||
m.BuildMetadata = nil | ||
} else { | ||
var newOptions []string | ||
for _, opt := range m.BuildMetadata { | ||
if !kustfile.StringInSlice(opt, o.buildMetadataOptions) { | ||
newOptions = append(newOptions, opt) | ||
} | ||
} | ||
m.BuildMetadata = newOptions | ||
} | ||
return mf.Write(m) | ||
} |
65 changes: 65 additions & 0 deletions
65
kustomize/commands/edit/remove/removebuildmetadata_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2022 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package remove | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"sigs.k8s.io/kustomize/api/types" | ||
testutils_test "sigs.k8s.io/kustomize/kustomize/v4/commands/internal/testutils" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
func TestRemoveBuildMetadata(t *testing.T) { | ||
tests := map[string]struct { | ||
input string | ||
args []string | ||
expectedErr string | ||
}{ | ||
"happy path": { | ||
input: ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
buildMetadata: [originAnnotations, transformerAnnotations, managedByLabel]`, | ||
args: []string{types.OriginAnnotations}, | ||
}, | ||
"option already there": { | ||
input: ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
buildMetadata: [originAnnotations]`, | ||
args: []string{types.OriginAnnotations}, | ||
}, | ||
"invalid option": { | ||
input: ``, | ||
args: []string{"invalid_option"}, | ||
expectedErr: "invalid buildMetadata option: invalid_option", | ||
}, | ||
"too many args": { | ||
input: ``, | ||
args: []string{"option1", "option2"}, | ||
expectedErr: "too many arguments: [option1 option2]; to provide multiple buildMetadata options, please separate options by comma", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomizationWith(fSys, []byte(tc.input)) | ||
cmd := newCmdRemoveBuildMetadata(fSys) | ||
err := cmd.RunE(cmd, tc.args) | ||
if tc.expectedErr != "" { | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), tc.expectedErr) | ||
} else { | ||
assert.NoError(t, err) | ||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
for _, opt := range strings.Split(tc.args[0], ",") { | ||
assert.NotContains(t, string(content), opt) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2022 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package set | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/kustfile" | ||
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/util" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
type setBuildMetadataOptions struct { | ||
*util.BuildMetadataValidator | ||
buildMetadataOptions []string | ||
} | ||
|
||
// newCmdSetBuildMetadata sets options in the kustomization's buildMetada field. | ||
func newCmdSetBuildMetadata(fSys filesys.FileSystem) *cobra.Command { | ||
var o setBuildMetadataOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "buildmetadata", | ||
Short: "Sets one or more buildMetadata options to the kustomization.yaml in the current directory", | ||
Long: `Sets one or more buildMetadata options to the kustomization.yaml in the current directory. | ||
Existing options in the buildMetadata field will be replaced entirely by the new options set by this command. | ||
The following options are valid: | ||
- originAnnotations | ||
- transformerAnnotations | ||
- managedByLabel | ||
originAnnotations will add the annotation config.kubernetes.io/origin to each resource, describing where | ||
each resource originated from. | ||
transformerAnnotations will add the annotation alpha.config.kubernetes.io/transformations to each resource, | ||
describing the transformers that have acted upon the resource. | ||
managedByLabel will add the label app.kubernetes.io/managed-by to each resource, describing which version | ||
of kustomize managed the resource.`, | ||
Example: ` | ||
set buildmetadata {option1},{option2}`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var err error | ||
o.buildMetadataOptions, err = o.BuildMetadataValidator.Validate(args) | ||
if err != nil { | ||
return err | ||
} | ||
return o.RunSetBuildMetadata(fSys) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
// RunSetBuildMetadata runs setBuildMetadata command (do real work). | ||
func (o *setBuildMetadataOptions) RunSetBuildMetadata(fSys filesys.FileSystem) error { | ||
mf, err := kustfile.NewKustomizationFile(fSys) | ||
if err != nil { | ||
return err | ||
} | ||
m, err := mf.Read() | ||
if err != nil { | ||
return err | ||
} | ||
m.BuildMetadata = o.buildMetadataOptions | ||
return mf.Write(m) | ||
} |
Oops, something went wrong.