Skip to content

Commit

Permalink
kustomize edit add replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-chenzz committed Feb 5, 2024
1 parent 40ce15c commit d85bb6e
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
79 changes: 79 additions & 0 deletions kustomize/commands/edit/add/addreplacement.go
Original file line number Diff line number Diff line change
@@ -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
}

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 either a file, or an inline string.
`,
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)
}

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)

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
Original file line number Diff line number Diff line change
@@ -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)

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
Original file line number Diff line number Diff line change
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}
`,
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
}

0 comments on commit d85bb6e

Please sign in to comment.