forked from kubernetes-sigs/kustomize
-
Notifications
You must be signed in to change notification settings - Fork 0
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
e219b88
commit 5308f95
Showing
5 changed files
with
165 additions
and
2 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,72 @@ | ||
// Copyright 2023 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package add | ||
|
||
import ( | ||
"errors" | ||
"github.com/spf13/cobra" | ||
"log" | ||
"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 will be: | ||
- be either a file, or an inline string | ||
`, | ||
Example: `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 | ||
} | ||
|
||
for _, r := range m.Replacements { | ||
if r.Replacement.Source != nil || r.Replacement.Targets != nil { | ||
if r.Path == o.Replacement.Path { | ||
log.Printf("replacement %#v already in kustomization file", r) | ||
return errors.New("already in kust") | ||
} | ||
} | ||
m.Replacements = append(m.Replacements, r) | ||
} | ||
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.NoError(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