From 92197fda9ec761faf8f9872cabb4a639f4cebb1c Mon Sep 17 00:00:00 2001 From: natasha41575 Date: Thu, 30 Dec 2021 17:09:05 -0800 Subject: [PATCH] add kustomize edit add generator command --- kustomize/commands/edit/add/addgenerator.go | 72 ++++++++++++++++ .../commands/edit/add/addgenerator_test.go | 82 +++++++++++++++++++ kustomize/commands/edit/add/addtransformer.go | 25 ++---- .../commands/edit/add/addtransformer_test.go | 2 +- kustomize/commands/edit/add/all.go | 1 + 5 files changed, 165 insertions(+), 17 deletions(-) create mode 100644 kustomize/commands/edit/add/addgenerator.go create mode 100644 kustomize/commands/edit/add/addgenerator_test.go diff --git a/kustomize/commands/edit/add/addgenerator.go b/kustomize/commands/edit/add/addgenerator.go new file mode 100644 index 0000000000..4029c56c90 --- /dev/null +++ b/kustomize/commands/edit/add/addgenerator.go @@ -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) +} diff --git a/kustomize/commands/edit/add/addgenerator_test.go b/kustomize/commands/edit/add/addgenerator_test.go new file mode 100644 index 0000000000..f2fba1b64e --- /dev/null +++ b/kustomize/commands/edit/add/addgenerator_test.go @@ -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") +} diff --git a/kustomize/commands/edit/add/addtransformer.go b/kustomize/commands/edit/add/addtransformer.go index c37004e9d2..f4ee7e0a7e 100644 --- a/kustomize/commands/edit/add/addtransformer.go +++ b/kustomize/commands/edit/add/addtransformer.go @@ -21,14 +21,13 @@ type addTransformerOptions struct { // configuration to the kustomization file. func newCmdAddTransformer(fSys filesys.FileSystem) *cobra.Command { var o addTransformerOptions - cmd := &cobra.Command{ Use: "transformer", Short: "Add the name of a file containing a transformer configuration to the kustomization file.", Example: ` add transformer {filepath}`, RunE: func(cmd *cobra.Command, args []string) error { - err := o.Validate(args) + err := o.Validate(fSys, args) if err != nil { return err } @@ -39,41 +38,35 @@ func newCmdAddTransformer(fSys filesys.FileSystem) *cobra.Command { } // Validate validates add transformer command. -func (o *addTransformerOptions) Validate(args []string) error { +func (o *addTransformerOptions) Validate(fSys filesys.FileSystem, args []string) error { + // TODO: Add validation for the format of the transformer. if len(args) == 0 { - return errors.New("must specify a transformer file") + return errors.New("must specify a yaml file which contains a transformer plugin resource") } - o.transformerFilePaths = args - return nil + var err error + o.transformerFilePaths, err = util.GlobPatterns(fSys, args) + return err } // RunAddTransformer runs add transformer command (do real work). func (o *addTransformerOptions) RunAddTransformer(fSys filesys.FileSystem) error { - transformers, err := util.GlobPatterns(fSys, o.transformerFilePaths) - if err != nil { - return err - } - if len(transformers) == 0 { + if len(o.transformerFilePaths) == 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 transformers { + for _, t := range o.transformerFilePaths { if kustfile.StringInSlice(t, m.Transformers) { log.Printf("transformer %s already in kustomization file", t) continue } m.Transformers = append(m.Transformers, t) } - return mf.Write(m) } diff --git a/kustomize/commands/edit/add/addtransformer_test.go b/kustomize/commands/edit/add/addtransformer_test.go index 1b9ecb3f87..b24881c1af 100644 --- a/kustomize/commands/edit/add/addtransformer_test.go +++ b/kustomize/commands/edit/add/addtransformer_test.go @@ -65,7 +65,7 @@ func TestAddTransformerNoArgs(t *testing.T) { cmd := newCmdAddTransformer(fSys) err := cmd.Execute() - assert.EqualError(t, err, "must specify a transformer file") + assert.EqualError(t, err, "must specify a yaml file which contains a transformer plugin resource") } func TestAddTransformerMissingKustomizationYAML(t *testing.T) { diff --git a/kustomize/commands/edit/add/all.go b/kustomize/commands/edit/add/all.go index 08e0587509..e9309e557f 100644 --- a/kustomize/commands/edit/add/all.go +++ b/kustomize/commands/edit/add/all.go @@ -60,6 +60,7 @@ func NewCmdAdd( newCmdAddLabel(fSys, ldr.Validator().MakeLabelValidator()), newCmdAddAnnotation(fSys, ldr.Validator().MakeAnnotationValidator()), newCmdAddTransformer(fSys), + newCmdAddGenerator(fSys), ) return c }