-
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 34d49ae
Showing
6 changed files
with
304 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2022 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package remove | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/kustomize/api/types" | ||
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/kustfile" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
type removeBuildMetadataOptions struct { | ||
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 { | ||
err := o.Validate(args) | ||
if err != nil { | ||
return err | ||
} | ||
return o.RunRemoveBuildMetadata(fSys) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
// Validate validates removeBuildMetadata command. | ||
func (o *removeBuildMetadataOptions) Validate(args []string) error { | ||
if len(args) == 0 { | ||
return errors.New("must specify a buildMetadata option") | ||
} | ||
if len(args) > 1 { | ||
return fmt.Errorf("too many arguments: %s; to provide multiple buildMetadata options, please separate options by comma", args) | ||
} | ||
o.buildMetadataOptions = strings.Split(args[0], ",") | ||
for _, opt := range o.buildMetadataOptions { | ||
if !kustfile.StringInSlice(opt, types.BuildMetadataOptions) { | ||
return fmt.Errorf("invalid buildMetadata option: %s", opt) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// 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 | ||
} | ||
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) | ||
} |
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
// 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 TestRemoveBuildMetadataHappyPath(t *testing.T) { | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomizationWith(fSys, | ||
[]byte(`buildMetadata: [originAnnotations, transformerAnnotations, managedByLabel]`)) | ||
cmd := newCmdRemoveBuildMetadata(fSys) | ||
args := []string{strings.Join(types.BuildMetadataOptions, ",")} | ||
assert.NoError(t, cmd.RunE(cmd, args)) | ||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
for _, opt := range types.BuildMetadataOptions { | ||
assert.NotContains(t, string(content), opt) | ||
} | ||
} | ||
|
||
func TestRemoveBuildMetadataInvalidArg(t *testing.T) { | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomization(fSys) | ||
cmd := newCmdRemoveBuildMetadata(fSys) | ||
args := []string{"invalid_option"} | ||
err := cmd.RunE(cmd, args) | ||
assert.Error(t, err) | ||
assert.Equal(t, "invalid buildMetadata option: invalid_option", err.Error()) | ||
} | ||
|
||
func TestRemoveBuildMetadataTooManyArgs(t *testing.T) { | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomization(fSys) | ||
cmd := newCmdRemoveBuildMetadata(fSys) | ||
args := []string{"option1", "option2"} | ||
err := cmd.RunE(cmd, args) | ||
assert.Error(t, err) | ||
assert.Equal(t, "too many arguments: [option1 option2]; to provide multiple buildMetadata options, please separate options by comma", err.Error()) | ||
} |
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,82 @@ | ||
// Copyright 2022 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package set | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/kustomize/api/types" | ||
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/kustfile" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
type setBuildMetadataOptions struct { | ||
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 { | ||
err := o.Validate(args) | ||
if err != nil { | ||
return err | ||
} | ||
return o.RunSetBuildMetadata(fSys) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
// Validate validates setBuildMetadata command. | ||
func (o *setBuildMetadataOptions) Validate(args []string) error { | ||
if len(args) == 0 { | ||
return errors.New("must specify a buildMetadata option") | ||
} | ||
if len(args) > 1 { | ||
return fmt.Errorf("too many arguments: %s; to provide multiple buildMetadata options, please separate options by comma", args) | ||
} | ||
o.buildMetadataOptions = strings.Split(args[0], ",") | ||
for _, opt := range o.buildMetadataOptions { | ||
if !kustfile.StringInSlice(opt, types.BuildMetadataOptions) { | ||
return fmt.Errorf("invalid buildMetadata option: %s", opt) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// 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) | ||
} |
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,85 @@ | ||
// Copyright 2022 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package set | ||
|
||
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 TestSetBuildMetadataHappyPath(t *testing.T) { | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomization(fSys) | ||
cmd := newCmdSetBuildMetadata(fSys) | ||
args := []string{strings.Join(types.BuildMetadataOptions, ",")} | ||
assert.NoError(t, cmd.RunE(cmd, args)) | ||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
for _, opt := range types.BuildMetadataOptions { | ||
assert.Contains(t, string(content), opt) | ||
} | ||
} | ||
|
||
func TestSetBuildMetadataAlreadyThere(t *testing.T) { | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomization(fSys) | ||
cmd := newCmdSetBuildMetadata(fSys) | ||
args := []string{strings.Join(types.BuildMetadataOptions, ",")} | ||
assert.NoError(t, cmd.RunE(cmd, args)) | ||
err := cmd.RunE(cmd, args) | ||
assert.NoError(t, cmd.RunE(cmd, args)) | ||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
for _, opt := range types.BuildMetadataOptions { | ||
assert.Contains(t, string(content), opt) | ||
} | ||
} | ||
|
||
func TestSetBuildMetadataInvalidArg(t *testing.T) { | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomization(fSys) | ||
cmd := newCmdSetBuildMetadata(fSys) | ||
args := []string{"invalid_option"} | ||
err := cmd.RunE(cmd, args) | ||
assert.Error(t, err) | ||
assert.Equal(t, "invalid buildMetadata option: invalid_option", err.Error()) | ||
} | ||
|
||
func TestSetBuildMetadataTooManyArgs(t *testing.T) { | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomization(fSys) | ||
cmd := newCmdSetBuildMetadata(fSys) | ||
args := []string{"option1", "option2"} | ||
err := cmd.RunE(cmd, args) | ||
assert.Error(t, err) | ||
assert.Equal(t, "too many arguments: [option1 option2]; to provide multiple buildMetadata options, please separate options by comma", err.Error()) | ||
} | ||
|
||
func TestSetBuildMetadataForRemovingValue(t *testing.T) { | ||
fSys := filesys.MakeFsInMemory() | ||
testutils_test.WriteTestKustomization(fSys) | ||
cmd := newCmdSetBuildMetadata(fSys) | ||
args := []string{strings.Join(types.BuildMetadataOptions, ",")} | ||
assert.NoError(t, cmd.RunE(cmd, args)) | ||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
for _, opt := range types.BuildMetadataOptions { | ||
assert.Contains(t, string(content), opt) | ||
} | ||
|
||
args2 := []string{types.BuildMetadataOptions[0]} | ||
assert.NoError(t, cmd.RunE(cmd, args2)) | ||
content, err = testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
|
||
// ensure that the entire field was reset | ||
assert.Contains(t, string(content), types.BuildMetadataOptions[0]) | ||
assert.NotContains(t, string(content), types.BuildMetadataOptions[1]) | ||
assert.NotContains(t, string(content), types.BuildMetadataOptions[2]) | ||
} |