mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-17 09:49:13 +00:00
manifest becomes kustomization
This commit is contained in:
@@ -31,14 +31,13 @@ type addResourceOptions struct {
|
||||
resourceFilePath string
|
||||
}
|
||||
|
||||
// newCmdAddResource adds the name of a file containing a resource to the manifest.
|
||||
// newCmdAddResource adds the name of a file containing a resource to the kustomization file.
|
||||
func newCmdAddResource(out, errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
var o addResourceOptions
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "resource",
|
||||
Short: "Add the name of a file containing a resource to the manifest.",
|
||||
Long: "Add the name of a file containing a resource to the manifest.",
|
||||
Short: "Add the name of a file containing a resource to the kustomization file.",
|
||||
Example: `
|
||||
add resource {filepath}`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
@@ -86,7 +85,7 @@ func (o *addResourceOptions) RunAddResource(out, errOut io.Writer, fsys fs.FileS
|
||||
return err
|
||||
}
|
||||
|
||||
mf, err := newManifestFile(constants.KustomizeFileName, fsys)
|
||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -97,7 +96,7 @@ func (o *addResourceOptions) RunAddResource(out, errOut io.Writer, fsys fs.FileS
|
||||
}
|
||||
|
||||
if stringInSlice(o.resourceFilePath, m.Resources) {
|
||||
return fmt.Errorf("resource %s already in manifest", o.resourceFilePath)
|
||||
return fmt.Errorf("resource %s already in kustomization file", o.resourceFilePath)
|
||||
}
|
||||
|
||||
m.Resources = append(m.Resources, o.resourceFilePath)
|
||||
|
||||
@@ -39,7 +39,7 @@ func TestAddResourceHappyPath(t *testing.T) {
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
fakeFS := fs.MakeFakeFS()
|
||||
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
||||
fakeFS.WriteFile(constants.KustomizeFileName, []byte(manifestTemplate))
|
||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationTemplate))
|
||||
|
||||
cmd := newCmdAddResource(buf, os.Stderr, fakeFS)
|
||||
args := []string{resourceFileName}
|
||||
@@ -47,12 +47,12 @@ func TestAddResourceHappyPath(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected cmd error: %v", err)
|
||||
}
|
||||
content, err := fakeFS.ReadFile(constants.KustomizeFileName)
|
||||
content, err := fakeFS.ReadFile(constants.KustomizationFileName)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected read error: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(content), resourceFileName) {
|
||||
t.Errorf("expected resource name in manifest")
|
||||
t.Errorf("expected resource name in kustomization")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ func TestAddResourceAlreadyThere(t *testing.T) {
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
fakeFS := fs.MakeFakeFS()
|
||||
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
||||
fakeFS.WriteFile(constants.KustomizeFileName, []byte(manifestTemplate))
|
||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationTemplate))
|
||||
|
||||
cmd := newCmdAddResource(buf, os.Stderr, fakeFS)
|
||||
args := []string{resourceFileName}
|
||||
@@ -74,7 +74,7 @@ func TestAddResourceAlreadyThere(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Errorf("expected already there problem")
|
||||
}
|
||||
if err.Error() != "resource "+resourceFileName+" already in manifest" {
|
||||
if err.Error() != "resource "+resourceFileName+" already in kustomization file" {
|
||||
t.Errorf("unexpected error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ import (
|
||||
)
|
||||
|
||||
type buildOptions struct {
|
||||
manifestPath string
|
||||
kustomizationPath string
|
||||
}
|
||||
|
||||
// newCmdBuild creates a new build command.
|
||||
@@ -43,7 +43,7 @@ func newCmdBuild(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "build [path]",
|
||||
Short: "Print current configuration per contents of " + constants.KustomizeFileName,
|
||||
Short: "Print current configuration per contents of " + constants.KustomizationFileName,
|
||||
Example: `
|
||||
# Use the kustomize.yaml file under somedir/ to generate a set of api resources.
|
||||
build somedir/`,
|
||||
@@ -66,13 +66,13 @@ func newCmdBuild(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
|
||||
// Validate validates build command.
|
||||
func (o *buildOptions) Validate(args []string) error {
|
||||
if len(args) > 1 {
|
||||
return errors.New("specify one path to manifest")
|
||||
return errors.New("specify one path to kustomization file")
|
||||
}
|
||||
if len(args) == 0 {
|
||||
o.manifestPath = "./"
|
||||
o.kustomizationPath = "./"
|
||||
return nil
|
||||
}
|
||||
o.manifestPath = args[0]
|
||||
o.kustomizationPath = args[0]
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ func (o *buildOptions) Validate(args []string) error {
|
||||
func (o *buildOptions) RunBuild(out, errOut io.Writer, fs fs.FileSystem) error {
|
||||
l := loader.Init([]loader.SchemeLoader{loader.NewFileLoader(fs)})
|
||||
|
||||
absPath, err := filepath.Abs(o.manifestPath)
|
||||
absPath, err := filepath.Abs(o.kustomizationPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func TestBuildValidate(t *testing.T) {
|
||||
{"noargs", []string{}, "./", ""},
|
||||
{"file", []string{"beans"}, "beans", ""},
|
||||
{"path", []string{"a/b/c"}, "a/b/c", ""},
|
||||
{"path", []string{"too", "many"}, "", "specify one path to manifest"},
|
||||
{"path", []string{"too", "many"}, "", "specify one path to kustomization file"},
|
||||
}
|
||||
for _, mycase := range cases {
|
||||
opts := buildOptions{}
|
||||
@@ -68,8 +68,8 @@ func TestBuildValidate(t *testing.T) {
|
||||
t.Errorf("%s: unknown error %v", mycase.name, e)
|
||||
continue
|
||||
}
|
||||
if opts.manifestPath != mycase.path {
|
||||
t.Errorf("%s: expected path '%s', got '%s'", mycase.name, mycase.path, opts.manifestPath)
|
||||
if opts.kustomizationPath != mycase.path {
|
||||
t.Errorf("%s: expected path '%s', got '%s'", mycase.name, mycase.path, opts.kustomizationPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -115,7 +115,7 @@ func TestBuild(t *testing.T) {
|
||||
}
|
||||
|
||||
ops := &buildOptions{
|
||||
manifestPath: testcase.Filename,
|
||||
kustomizationPath: testcase.Filename,
|
||||
}
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
err = ops.RunBuild(buf, os.Stderr, fs)
|
||||
|
||||
@@ -60,10 +60,10 @@ More info at https://github.com/kubernetes/kubectl/tree/master/cmd/kustomize
|
||||
func newCmdEdit(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "edit",
|
||||
Short: "Edits a manifest file",
|
||||
Short: "Edits a kustomization file",
|
||||
Long: "",
|
||||
Example: `
|
||||
# Adds a configmap to the manifest
|
||||
# Adds a configmap to the kustomization file
|
||||
kustomize edit add configmap NAME --from-literal=k=v
|
||||
|
||||
# Sets the nameprefix field
|
||||
@@ -82,16 +82,16 @@ func newCmdEdit(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
func newCmdAdd(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Adds configmap/resource/secret to the manifest.",
|
||||
Short: "Adds configmap/resource/secret to the kustomization file.",
|
||||
Long: "",
|
||||
Example: `
|
||||
# Adds a configmap to the manifest
|
||||
# Adds a configmap to the kustomization file
|
||||
kustomize edit add configmap NAME --from-literal=k=v
|
||||
|
||||
# Adds a secret to the manifest
|
||||
# Adds a secret to the kustomization file
|
||||
kustomize edit add secret NAME --from-literal=k=v
|
||||
|
||||
# Adds a resource to the manifest
|
||||
# Adds a resource to the kustomization
|
||||
kustomize edit add resource <filepath>
|
||||
`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
@@ -107,7 +107,7 @@ func newCmdAdd(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
func newCmdSet(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "set",
|
||||
Short: "Sets the value of different fields in manifest.",
|
||||
Short: "Sets the value of different fields in kustomization file.",
|
||||
Long: "",
|
||||
Example: `
|
||||
# Sets the nameprefix field
|
||||
|
||||
@@ -32,13 +32,13 @@ func newCmdAddConfigMap(errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
var config dataConfig
|
||||
cmd := &cobra.Command{
|
||||
Use: "configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1]",
|
||||
Short: "Adds a configmap to the kustomize config file.",
|
||||
Short: "Adds a configmap to the kustomization file.",
|
||||
Long: "",
|
||||
Example: `
|
||||
# Adds a configmap to the kustomize config file (with a specified key)
|
||||
# Adds a configmap to the kustomization file (with a specified key)
|
||||
kustomize edit add configmap my-configmap --from-file=my-key=file/path --from-literal=my-literal=12345
|
||||
|
||||
# Adds a configmap to the kustomize config file (key is the filename)
|
||||
# Adds a configmap to the kustomization file (key is the filename)
|
||||
kustomize edit add configmap my-configmap --from-file=file/path
|
||||
|
||||
# Adds a configmap from env-file
|
||||
@@ -50,8 +50,8 @@ func newCmdAddConfigMap(errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
// Load in the kustomize config file.
|
||||
mf, err := newManifestFile(constants.KustomizeFileName, fsys)
|
||||
// Load in the kustomization file.
|
||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -61,28 +61,40 @@ func newCmdAddConfigMap(errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add the config map to the kustomize config file.
|
||||
// Add the config map to the kustomization file.
|
||||
err = addConfigMap(m, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write out the kustomize config file with added configmap.
|
||||
// Write out the kustomization file with added configmap.
|
||||
return mf.write(m)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringSliceVar(&config.FileSources, "from-file", []string{}, "Key file can be specified using its file path, in which case file basename will be used as configmap key, or optionally with a key and file path, in which case the given key will be used. Specifying a directory will iterate each named file in the directory whose basename is a valid configmap key.")
|
||||
cmd.Flags().StringArrayVar(&config.LiteralSources, "from-literal", []string{}, "Specify a key and literal value to insert in configmap (i.e. mykey=somevalue)")
|
||||
cmd.Flags().StringVar(&config.EnvFileSource, "from-env-file", "", "Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).")
|
||||
cmd.Flags().StringSliceVar(
|
||||
&config.FileSources,
|
||||
"from-file",
|
||||
[]string{},
|
||||
"Key file can be specified using its file path, in which case file basename will be used as configmap key, or optionally with a key and file path, in which case the given key will be used. Specifying a directory will iterate each named file in the directory whose basename is a valid configmap key.")
|
||||
cmd.Flags().StringArrayVar(
|
||||
&config.LiteralSources,
|
||||
"from-literal",
|
||||
[]string{},
|
||||
"Specify a key and literal value to insert in configmap (i.e. mykey=somevalue)")
|
||||
cmd.Flags().StringVar(
|
||||
&config.EnvFileSource,
|
||||
"from-env-file",
|
||||
"",
|
||||
"Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// addConfigMap updates a configmap within a kustomize config file, using the data in config.
|
||||
// Note: error may leave kustomize config file in an undefined state. Suggest passing a copy
|
||||
// of kustomize config file.
|
||||
func addConfigMap(m *types.Manifest, config dataConfig) error {
|
||||
// addConfigMap updates a configmap within a kustomization file, using the data in config.
|
||||
// Note: error may leave kustomization file in an undefined state. Suggest passing a copy
|
||||
// of kustomization file.
|
||||
func addConfigMap(m *types.Kustomization, config dataConfig) error {
|
||||
cm := getOrCreateConfigMap(m, config.Name)
|
||||
|
||||
err := mergeData(&cm.DataSources, config)
|
||||
@@ -99,13 +111,13 @@ func addConfigMap(m *types.Manifest, config dataConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getOrCreateConfigMap(m *types.Manifest, name string) *types.ConfigMapArgs {
|
||||
func getOrCreateConfigMap(m *types.Kustomization, name string) *types.ConfigMapArgs {
|
||||
for i, v := range m.ConfigMapGenerator {
|
||||
if name == v.Name {
|
||||
return &m.ConfigMapGenerator[i]
|
||||
}
|
||||
}
|
||||
// config map not found, create new one and add it to the kustomize config file.
|
||||
// config map not found, create new one and add it to the kustomization file.
|
||||
cm := &types.ConfigMapArgs{Name: name}
|
||||
m.ConfigMapGenerator = append(m.ConfigMapGenerator, *cm)
|
||||
return &m.ConfigMapGenerator[len(m.ConfigMapGenerator)-1]
|
||||
|
||||
@@ -32,34 +32,34 @@ func TestNewAddConfigMapIsNotNil(t *testing.T) {
|
||||
func TestGetOrCreateConfigMap(t *testing.T) {
|
||||
cmName := "test-config-name"
|
||||
|
||||
manifest := &types.Manifest{
|
||||
kustomization := &types.Kustomization{
|
||||
NamePrefix: "test-name-prefix",
|
||||
}
|
||||
|
||||
if len(manifest.ConfigMapGenerator) != 0 {
|
||||
t.Fatal("Initial manifest should not have any configmaps")
|
||||
if len(kustomization.ConfigMapGenerator) != 0 {
|
||||
t.Fatal("Initial kustomization should not have any configmaps")
|
||||
}
|
||||
cm := getOrCreateConfigMap(manifest, cmName)
|
||||
cm := getOrCreateConfigMap(kustomization, cmName)
|
||||
|
||||
if cm == nil {
|
||||
t.Fatalf("ConfigMap should always be non-nil")
|
||||
}
|
||||
|
||||
if len(manifest.ConfigMapGenerator) != 1 {
|
||||
t.Fatalf("Manifest should have newly created configmap")
|
||||
if len(kustomization.ConfigMapGenerator) != 1 {
|
||||
t.Fatalf("Kustomization should have newly created configmap")
|
||||
}
|
||||
|
||||
if &manifest.ConfigMapGenerator[len(manifest.ConfigMapGenerator)-1] != cm {
|
||||
if &kustomization.ConfigMapGenerator[len(kustomization.ConfigMapGenerator)-1] != cm {
|
||||
t.Fatalf("Pointer address for newly inserted configmap should be same")
|
||||
}
|
||||
|
||||
existingCM := getOrCreateConfigMap(manifest, cmName)
|
||||
existingCM := getOrCreateConfigMap(kustomization, cmName)
|
||||
|
||||
if existingCM != cm {
|
||||
t.Fatalf("should have returned an existing cm with name: %v", cmName)
|
||||
}
|
||||
|
||||
if len(manifest.ConfigMapGenerator) != 1 {
|
||||
if len(kustomization.ConfigMapGenerator) != 1 {
|
||||
t.Fatalf("Should not insert configmap for an existing name: %v", cmName)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ import (
|
||||
)
|
||||
|
||||
type diffOptions struct {
|
||||
manifestPath string
|
||||
kustomizationPath string
|
||||
}
|
||||
|
||||
// newCmdDiff makes the diff command.
|
||||
@@ -56,7 +56,7 @@ func newCmdDiff(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&o.manifestPath, "filename", "f", "", "Pass in a kustomize.yaml file or a directory that contains the file.")
|
||||
cmd.Flags().StringVarP(&o.kustomizationPath, "filename", "f", "", "Pass in a kustomize.yaml file or a directory that contains the file.")
|
||||
cmd.MarkFlagRequired("filename")
|
||||
return cmd
|
||||
}
|
||||
@@ -85,7 +85,7 @@ func (o *diffOptions) RunDiff(out, errOut io.Writer, fs fs.FileSystem) error {
|
||||
|
||||
l := loader.Init([]loader.SchemeLoader{loader.NewFileLoader(fs)})
|
||||
|
||||
absPath, err := filepath.Abs(o.manifestPath)
|
||||
absPath, err := filepath.Abs(o.kustomizationPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ func TestDiff(t *testing.T) {
|
||||
}
|
||||
|
||||
diffOps := &diffOptions{
|
||||
manifestPath: testcase.Filename,
|
||||
kustomizationPath: testcase.Filename,
|
||||
}
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
err = diffOps.RunDiff(buf, os.Stderr, fs)
|
||||
|
||||
@@ -27,11 +27,7 @@ import (
|
||||
"k8s.io/kubectl/pkg/kustomize/util/fs"
|
||||
)
|
||||
|
||||
const manifestTemplate = `apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: helloworld
|
||||
description: helloworld does useful stuff.
|
||||
const kustomizationTemplate = `kustomizationName: helloworld
|
||||
namePrefix: some-prefix
|
||||
# Labels to add to all objects and selectors.
|
||||
# These labels would also be used to form the selector for apply --prune
|
||||
@@ -58,9 +54,9 @@ func newCmdInit(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "init",
|
||||
Short: "Creates a file called \"" + constants.KustomizeFileName + "\" in the current directory",
|
||||
Short: "Creates a file called \"" + constants.KustomizationFileName + "\" in the current directory",
|
||||
Long: "Creates a file called \"" +
|
||||
constants.KustomizeFileName + "\" in the current directory with example values.",
|
||||
constants.KustomizationFileName + "\" in the current directory with example values.",
|
||||
Example: `init`,
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
@@ -91,10 +87,10 @@ func (o *initOptions) Complete(cmd *cobra.Command, args []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RunInit writes a manifest file.
|
||||
// RunInit writes a kustomization file.
|
||||
func (o *initOptions) RunInit(out, errOut io.Writer, fs fs.FileSystem) error {
|
||||
if _, err := fs.Stat(constants.KustomizeFileName); err == nil {
|
||||
return fmt.Errorf("%q already exists", constants.KustomizeFileName)
|
||||
if _, err := fs.Stat(constants.KustomizationFileName); err == nil {
|
||||
return fmt.Errorf("%q already exists", constants.KustomizationFileName)
|
||||
}
|
||||
return fs.WriteFile(constants.KustomizeFileName, []byte(manifestTemplate))
|
||||
return fs.WriteFile(constants.KustomizationFileName, []byte(kustomizationTemplate))
|
||||
}
|
||||
|
||||
@@ -34,21 +34,21 @@ func TestInitHappyPath(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
f, err := fakeFS.Open(constants.KustomizeFileName)
|
||||
f, err := fakeFS.Open(constants.KustomizationFileName)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
file := f.(*fs.FakeFile)
|
||||
if !file.ContentMatches([]byte(manifestTemplate)) {
|
||||
if !file.ContentMatches([]byte(kustomizationTemplate)) {
|
||||
t.Fatalf("actual: %v doesn't match expected: %v",
|
||||
string(file.GetContent()), manifestTemplate)
|
||||
string(file.GetContent()), kustomizationTemplate)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitFileAlreadyExist(t *testing.T) {
|
||||
content := "hey there"
|
||||
fakeFS := fs.MakeFakeFS()
|
||||
fakeFS.WriteFile(constants.KustomizeFileName, []byte(content))
|
||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(content))
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
cmd := newCmdInit(buf, os.Stderr, fakeFS)
|
||||
@@ -56,7 +56,7 @@ func TestInitFileAlreadyExist(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
if err.Error() != `"`+constants.KustomizeFileName+`" already exists` {
|
||||
if err.Error() != `"`+constants.KustomizationFileName+`" already exists` {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,19 +30,17 @@ type setNamePrefixOptions struct {
|
||||
prefix string
|
||||
}
|
||||
|
||||
// newCmdSetNamePrefix sets the value of the namePrefix field in the manifest.
|
||||
// newCmdSetNamePrefix sets the value of the namePrefix field in the kustomization.
|
||||
func newCmdSetNamePrefix(out, errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
var o setNamePrefixOptions
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "nameprefix",
|
||||
Short: "Sets the value of the namePrefix field in the manifest.",
|
||||
Long: "Sets the value of the namePrefix field in the manifest.",
|
||||
//
|
||||
Short: "Sets the value of the namePrefix field in the kustomization file.",
|
||||
Example: `
|
||||
The command
|
||||
set nameprefix acme-
|
||||
will add the field "namePrefix: acme-" to the manifest file if it doesn't exist,
|
||||
will add the field "namePrefix: acme-" to the kustomization file if it doesn't exist,
|
||||
and overwrite the value with "acme-" if the field does exist.
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
@@ -77,7 +75,7 @@ func (o *setNamePrefixOptions) Complete(cmd *cobra.Command, args []string) error
|
||||
|
||||
// RunSetNamePrefix runs setNamePrefix command (does real work).
|
||||
func (o *setNamePrefixOptions) RunSetNamePrefix(out, errOut io.Writer, fsys fs.FileSystem) error {
|
||||
mf, err := newManifestFile(constants.KustomizeFileName, fsys)
|
||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ const (
|
||||
func TestSetNamePrefixHappyPath(t *testing.T) {
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
fakeFS := fs.MakeFakeFS()
|
||||
fakeFS.WriteFile(constants.KustomizeFileName, []byte(manifestTemplate))
|
||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationTemplate))
|
||||
|
||||
cmd := newCmdSetNamePrefix(buf, os.Stderr, fakeFS)
|
||||
args := []string{goodPrefixValue}
|
||||
@@ -42,12 +42,12 @@ func TestSetNamePrefixHappyPath(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected cmd error: %v", err)
|
||||
}
|
||||
content, err := fakeFS.ReadFile(constants.KustomizeFileName)
|
||||
content, err := fakeFS.ReadFile(constants.KustomizationFileName)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected read error: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(content), goodPrefixValue) {
|
||||
t.Errorf("expected prefix value in manifest")
|
||||
t.Errorf("expected prefix value in kustomization file")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,13 +30,13 @@ import (
|
||||
"k8s.io/kubectl/pkg/kustomize/util/fs"
|
||||
)
|
||||
|
||||
type manifestFile struct {
|
||||
mPath string
|
||||
fsys fs.FileSystem
|
||||
type kustomizationFile struct {
|
||||
path string
|
||||
fsys fs.FileSystem
|
||||
}
|
||||
|
||||
func newManifestFile(mPath string, fsys fs.FileSystem) (*manifestFile, error) {
|
||||
mf := &manifestFile{mPath: mPath, fsys: fsys}
|
||||
func newKustomizationFile(mPath string, fsys fs.FileSystem) (*kustomizationFile, error) {
|
||||
mf := &kustomizationFile{path: mPath, fsys: fsys}
|
||||
err := mf.validate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -44,52 +44,52 @@ func newManifestFile(mPath string, fsys fs.FileSystem) (*manifestFile, error) {
|
||||
return mf, nil
|
||||
}
|
||||
|
||||
func (mf *manifestFile) validate() error {
|
||||
f, err := mf.fsys.Stat(mf.mPath)
|
||||
func (mf *kustomizationFile) validate() error {
|
||||
f, err := mf.fsys.Stat(mf.path)
|
||||
if err != nil {
|
||||
errorMsg := fmt.Sprintf("Missing kustomize config file '%s'.\n", mf.mPath)
|
||||
merr := interror.ManifestError{ManifestFilepath: mf.mPath, ErrorMsg: errorMsg}
|
||||
errorMsg := fmt.Sprintf("Missing kustomization file '%s'.\n", mf.path)
|
||||
merr := interror.KustomizationError{KustomizationPath: mf.path, ErrorMsg: errorMsg}
|
||||
return merr
|
||||
}
|
||||
if f.IsDir() {
|
||||
mf.mPath = path.Join(mf.mPath, constants.KustomizeFileName)
|
||||
_, err = mf.fsys.Stat(mf.mPath)
|
||||
mf.path = path.Join(mf.path, constants.KustomizationFileName)
|
||||
_, err = mf.fsys.Stat(mf.path)
|
||||
if err != nil {
|
||||
errorMsg := fmt.Sprintf("Missing kustomize config file '%s'.\n", mf.mPath)
|
||||
merr := interror.ManifestError{ManifestFilepath: mf.mPath, ErrorMsg: errorMsg}
|
||||
errorMsg := fmt.Sprintf("Missing kustomization file '%s'.\n", mf.path)
|
||||
merr := interror.KustomizationError{KustomizationPath: mf.path, ErrorMsg: errorMsg}
|
||||
return merr
|
||||
}
|
||||
} else {
|
||||
if !strings.HasSuffix(mf.mPath, constants.KustomizeFileName) {
|
||||
errorMsg := fmt.Sprintf("Kustomize config file path (%s) should have %s suffix\n", mf.mPath, constants.KustomizeSuffix)
|
||||
merr := interror.ManifestError{ManifestFilepath: mf.mPath, ErrorMsg: errorMsg}
|
||||
if !strings.HasSuffix(mf.path, constants.KustomizationFileName) {
|
||||
errorMsg := fmt.Sprintf("Kustomization file path (%s) should have %s suffix\n", mf.path, constants.KustomizationSuffix)
|
||||
merr := interror.KustomizationError{KustomizationPath: mf.path, ErrorMsg: errorMsg}
|
||||
return merr
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mf *manifestFile) read() (*types.Manifest, error) {
|
||||
bytes, err := mf.fsys.ReadFile(mf.mPath)
|
||||
func (mf *kustomizationFile) read() (*types.Kustomization, error) {
|
||||
bytes, err := mf.fsys.ReadFile(mf.path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var manifest types.Manifest
|
||||
err = yaml.Unmarshal(bytes, &manifest)
|
||||
var kustomization types.Kustomization
|
||||
err = yaml.Unmarshal(bytes, &kustomization)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &manifest, err
|
||||
return &kustomization, err
|
||||
}
|
||||
|
||||
func (mf *manifestFile) write(manifest *types.Manifest) error {
|
||||
if manifest == nil {
|
||||
return errors.New("util: kustomize config file arg is nil.")
|
||||
func (mf *kustomizationFile) write(kustomization *types.Kustomization) error {
|
||||
if kustomization == nil {
|
||||
return errors.New("util: kustomization file arg is nil.")
|
||||
}
|
||||
bytes, err := yaml.Marshal(manifest)
|
||||
bytes, err := yaml.Marshal(kustomization)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return mf.fsys.WriteFile(mf.mPath, bytes)
|
||||
return mf.fsys.WriteFile(mf.path, bytes)
|
||||
}
|
||||
|
||||
@@ -26,35 +26,35 @@ import (
|
||||
)
|
||||
|
||||
func TestWriteAndRead(t *testing.T) {
|
||||
manifest := &types.Manifest{
|
||||
kustomization := &types.Kustomization{
|
||||
NamePrefix: "prefix",
|
||||
}
|
||||
|
||||
fsys := fs.MakeFakeFS()
|
||||
fsys.Create("kustomize.yaml")
|
||||
mf, err := newManifestFile("kustomize.yaml", fsys)
|
||||
mf, err := newKustomizationFile("kustomize.yaml", fsys)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
|
||||
if err := mf.write(manifest); err != nil {
|
||||
t.Fatalf("Couldn't write manifest file: %v\n", err)
|
||||
if err := mf.write(kustomization); err != nil {
|
||||
t.Fatalf("Couldn't write kustomization file: %v\n", err)
|
||||
}
|
||||
|
||||
readManifest, err := mf.read()
|
||||
content, err := mf.read()
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't read manifest file: %v\n", err)
|
||||
t.Fatalf("Couldn't read kustomization file: %v\n", err)
|
||||
}
|
||||
if !reflect.DeepEqual(manifest, readManifest) {
|
||||
t.Fatal("Read manifest is different from written manifest")
|
||||
if !reflect.DeepEqual(kustomization, content) {
|
||||
t.Fatal("Read kustomization is different from written kustomization")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyFile(t *testing.T) {
|
||||
fsys := fs.MakeFakeFS()
|
||||
_, err := newManifestFile("", fsys)
|
||||
_, err := newKustomizationFile("", fsys)
|
||||
if err == nil {
|
||||
t.Fatalf("Creat manifestFile from empty filename should fail")
|
||||
t.Fatalf("Creat kustomizationFile from empty filename should fail")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,22 +63,22 @@ func TestNewNotExist(t *testing.T) {
|
||||
fakeFS := fs.MakeFakeFS()
|
||||
fakeFS.Mkdir(".", 0644)
|
||||
fakeFS.Create(badSuffix)
|
||||
_, err := newManifestFile("kustomize.yaml", fakeFS)
|
||||
_, err := newKustomizationFile("kustomize.yaml", fakeFS)
|
||||
if err == nil {
|
||||
t.Fatalf("expect an error")
|
||||
}
|
||||
contained := "Missing kustomize config file"
|
||||
contained := "Missing kustomization file"
|
||||
if !strings.Contains(err.Error(), contained) {
|
||||
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
||||
}
|
||||
_, err = newManifestFile("kustomize.yaml", fakeFS)
|
||||
_, err = newKustomizationFile("kustomize.yaml", fakeFS)
|
||||
if err == nil {
|
||||
t.Fatalf("expect an error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), contained) {
|
||||
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
||||
}
|
||||
_, err = newManifestFile(badSuffix, fakeFS)
|
||||
_, err = newKustomizationFile(badSuffix, fakeFS)
|
||||
if err == nil {
|
||||
t.Fatalf("expect an error")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user