-
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.
add kustomize edit add generator command
- Loading branch information
1 parent
1843b6f
commit 92197fd
Showing
5 changed files
with
165 additions
and
17 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,72 @@ | ||
// Copyright 2022 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package add | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/kustfile" | ||
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/util" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
type addGeneratorOptions struct { | ||
generatorFilePaths []string | ||
} | ||
|
||
// newCmdAddGenerator adds the name of a file containing a generator | ||
// configuration to the kustomization file. | ||
func newCmdAddGenerator(fSys filesys.FileSystem) *cobra.Command { | ||
var o addGeneratorOptions | ||
cmd := &cobra.Command{ | ||
Use: "generator", | ||
Short: "Add the name of a file containing a generator configuration to the kustomization file.", | ||
Example: ` | ||
add generator {filepath}`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
err := o.Validate(fSys, args) | ||
if err != nil { | ||
return err | ||
} | ||
return o.RunAddGenerator(fSys) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
// Validate validates add generator command. | ||
func (o *addGeneratorOptions) Validate(fSys filesys.FileSystem, args []string) error { | ||
// TODO: Add validation for the format of the generator. | ||
if len(args) == 0 { | ||
return errors.New("must specify a yaml file which contains a generator plugin resource") | ||
} | ||
var err error | ||
o.generatorFilePaths, err = util.GlobPatterns(fSys, args) | ||
return err | ||
} | ||
|
||
// RunAddGenerator runs add generator command (do real work). | ||
func (o *addGeneratorOptions) RunAddGenerator(fSys filesys.FileSystem) error { | ||
if len(o.generatorFilePaths) == 0 { | ||
return nil | ||
} | ||
mf, err := kustfile.NewKustomizationFile(fSys) | ||
if err != nil { | ||
return err | ||
} | ||
m, err := mf.Read() | ||
if err != nil { | ||
return err | ||
} | ||
for _, t := range o.generatorFilePaths { | ||
if kustfile.StringInSlice(t, m.Generators) { | ||
log.Printf("generator %s already in kustomization file", t) | ||
continue | ||
} | ||
m.Generators = append(m.Generators, t) | ||
} | ||
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,82 @@ | ||
// Copyright 2019 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/v4/commands/internal/kustfile" | ||
testutils_test "sigs.k8s.io/kustomize/kustomize/v4/commands/internal/testutils" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
const ( | ||
generatorFileName = "myWonderfulGenerator.yaml" | ||
generatorFileContent = ` | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, | ||
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
` | ||
) | ||
|
||
func TestAddGeneratorHappyPath(t *testing.T) { | ||
fSys := filesys.MakeEmptyDirInMemory() | ||
err := fSys.WriteFile(generatorFileName, []byte(generatorFileContent)) | ||
require.NoError(t, err) | ||
err = fSys.WriteFile(generatorFileName+"another", []byte(generatorFileContent)) | ||
require.NoError(t, err) | ||
testutils_test.WriteTestKustomization(fSys) | ||
|
||
cmd := newCmdAddGenerator(fSys) | ||
args := []string{generatorFileName + "*"} | ||
assert.NoError(t, cmd.RunE(cmd, args)) | ||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(content), generatorFileName) | ||
assert.Contains(t, string(content), generatorFileName+"another") | ||
} | ||
|
||
func TestAddGeneratorAlreadyThere(t *testing.T) { | ||
fSys := filesys.MakeEmptyDirInMemory() | ||
err := fSys.WriteFile(generatorFileName, []byte(generatorFileName)) | ||
require.NoError(t, err) | ||
testutils_test.WriteTestKustomization(fSys) | ||
|
||
cmd := newCmdAddGenerator(fSys) | ||
args := []string{generatorFileName} | ||
assert.NoError(t, cmd.RunE(cmd, args)) | ||
|
||
// adding an existing generator shouldn't return an error | ||
assert.NoError(t, cmd.RunE(cmd, args)) | ||
|
||
// There can be only one. May it be the... | ||
mf, err := kustfile.NewKustomizationFile(fSys) | ||
assert.NoError(t, err) | ||
m, err := mf.Read() | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1, len(m.Generators)) | ||
assert.Equal(t, generatorFileName, m.Generators[0]) | ||
} | ||
|
||
func TestAddGeneratorNoArgs(t *testing.T) { | ||
fSys := filesys.MakeFsInMemory() | ||
|
||
cmd := newCmdAddGenerator(fSys) | ||
err := cmd.Execute() | ||
assert.EqualError(t, err, "must specify a yaml file which contains a generator plugin resource") | ||
} | ||
|
||
func TestAddGeneratorMissingKustomizationYAML(t *testing.T) { | ||
fSys := filesys.MakeEmptyDirInMemory() | ||
err := fSys.WriteFile(generatorFileName, []byte(generatorFileContent)) | ||
require.NoError(t, err) | ||
err = fSys.WriteFile(generatorFileName+"another", []byte(generatorFileContent)) | ||
require.NoError(t, err) | ||
|
||
cmd := newCmdAddGenerator(fSys) | ||
args := []string{generatorFileName + "*"} | ||
err = cmd.RunE(cmd, args) | ||
assert.EqualError(t, err, "Missing kustomization file 'kustomization.yaml'.\n") | ||
} |
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
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