-
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.
- Loading branch information
1 parent
0122aa8
commit b015c9b
Showing
5 changed files
with
153 additions
and
32 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,82 @@ | ||
// 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 | ||
o.Replacement.Source = &types.SourceSelector{} | ||
o.Replacement.Targets = append(o.Replacement.Targets, &types.TargetSelector{}) | ||
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 will be: | ||
- 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 err | ||
} | ||
|
||
m, err := mf.Read() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if o.Replacement.Targets != nil { | ||
o.Replacement.Targets = nil | ||
} | ||
if o.Replacement.Source != nil { | ||
o.Replacement.Source = nil | ||
} | ||
|
||
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) | ||
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,58 @@ | ||
package add | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
"testing" | ||
) | ||
|
||
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()) | ||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
for i := 1; i < len(args); i += 2 { | ||
assert.Contains(t, string(content), args[i]) | ||
} | ||
} | ||
|
||
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
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