-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40ce15c
commit d85bb6e
Showing
3 changed files
with
155 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
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 | ||
} |
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 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()) | ||
} |
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