Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kustomize edit add replacement #5444

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 79 additions & 0 deletions kustomize/commands/edit/add/addreplacement.go
@@ -0,0 +1,79 @@
// Copyright 2023 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package add

import (
"errors"
"fmt"

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

type addReplacementOptions struct {
Replacement types.ReplacementField
charles-chenzz marked this conversation as resolved.
Show resolved Hide resolved
}
charles-chenzz marked this conversation as resolved.
Show resolved Hide resolved

func newCmdAddReplacement(fSys filesys.FileSystem) *cobra.Command {
var o addReplacementOptions
cmd := &cobra.Command{
Use: "replacement",
Short: "add an item to replacement field",
Long: `this command will add an item to replacement field in the kustomization file.
The item must be a file path.
`,
Example: `
# Adds a replacement file to the kustomization file
kustomize edit add replacement --path {filepath}
`,
RunE: func(cmd *cobra.Command, args []string) error {
err := o.Validate()
if err != nil {
return err
}
return o.RunAddReplacement(fSys)
},
}

cmd.Flags().StringVar(&o.Replacement.Path, "path", "", "Path to the replacement file.")
return cmd
}

// Validate validate add replacement command
func (o *addReplacementOptions) Validate() error {
if o.Replacement.Path == "" {
return errors.New("must provide path to add replacement")
}
return nil
}

// RunAddReplacement runs addReplacement command
func (o *addReplacementOptions) RunAddReplacement(fSys filesys.FileSystem) error {
mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil {
return fmt.Errorf("failed to load kustomization file: %w", err)
}
charles-chenzz marked this conversation as resolved.
Show resolved Hide resolved

m, err := mf.Read()
if err != nil {
return fmt.Errorf("failed to read kustomization file: %w", err)
}

for _, r := range m.Replacements {
if len(r.Path) > 0 && r.Path == o.Replacement.Path {
return fmt.Errorf("replacement for path %q already in %s file", r.Path, konfig.DefaultKustomizationFileName())
}
}
m.Replacements = append(m.Replacements, o.Replacement)
Comment on lines +66 to +71
Copy link
Member

@koba1t koba1t Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @charles-chenzz
Could you add a check to file exists for replacement file?

I think other edit add command is implemented that.


err = mf.Write(m)
if err != nil {
return fmt.Errorf("failed to write kustomization file: %w", err)
}

return nil
}
72 changes: 72 additions & 0 deletions kustomize/commands/edit/add/addreplacement_test.go
charles-chenzz marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,72 @@
// Copyright 2023 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package add

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

const (
replaceFileName = "replacement.yaml"
replaceFileContent = `this is just a test file`
)

func TestAddReplacementWithFilePath(t *testing.T) {
fSys := filesys.MakeEmptyDirInMemory()
err := fSys.WriteFile(replaceFileName, []byte(replaceFileContent))
require.NoError(t, err)
testutils_test.WriteTestKustomization(fSys)

cmd := newCmdAddReplacement(fSys)
args := []string{
"--path", patchFileName,
}
cmd.SetArgs(args)
assert.NoError(t, cmd.Execute())
_, err = testutils_test.ReadTestKustomization(fSys)
assert.NoError(t, err)

kf, err := kustfile.NewKustomizationFile(fSys)
require.NoError(t, err)

kustomization, err := kf.Read()
require.NoError(t, err)

expectedPath := []string{replaceFileName, patchFileName}

for k, replacement := range kustomization.Replacements {
require.Equal(t, expectedPath[k], replacement.Path)
}
}

func TestAddReplacementAlreadyThere(t *testing.T) {
fSys := filesys.MakeEmptyDirInMemory()
err := fSys.WriteFile(replaceFileName, []byte(replaceFileContent))
require.NoError(t, err)
testutils_test.WriteTestKustomization(fSys)
charles-chenzz marked this conversation as resolved.
Show resolved Hide resolved

cmd := newCmdAddReplacement(fSys)
args := []string{
"--path", patchFileName,
}
cmd.SetArgs(args)
assert.NoError(t, cmd.Execute())

assert.Error(t, cmd.Execute())
}

func TestAddReplacementNoArgs(t *testing.T) {
fSys := filesys.MakeEmptyDirInMemory()

cmd := newCmdAddReplacement(fSys)
err := cmd.Execute()
assert.Error(t, err)
assert.Equal(t, "must provide path to add replacement", err.Error())
}
4 changes: 4 additions & 0 deletions kustomize/commands/edit/add/all.go
Expand Up @@ -47,6 +47,9 @@ func NewCmdAdd(

# Adds a transformer configuration to the kustomization
kustomize edit add transformer <filepath>

# Adds a replacement to the kustomization
kustomize edit add replacement --path {filepath}
charles-chenzz marked this conversation as resolved.
Show resolved Hide resolved
`,
Args: cobra.MinimumNArgs(1),
}
Expand All @@ -62,6 +65,7 @@ func NewCmdAdd(
newCmdAddAnnotation(fSys, ldr.Validator().MakeAnnotationValidator()),
newCmdAddTransformer(fSys),
newCmdAddGenerator(fSys),
newCmdAddReplacement(fSys),
)
return c
}