mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Merge pull request #407 from monopole/cmdSplit
Improve command package isolation.
This commit is contained in:
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -57,8 +57,8 @@ Use different transformer configurations by passing files to kustomize
|
|||||||
build somedir -t some-transformer-configfile,another-transformer-configfile
|
build somedir -t some-transformer-configfile,another-transformer-configfile
|
||||||
`
|
`
|
||||||
|
|
||||||
// newCmdBuild creates a new build command.
|
// NewCmdBuild creates a new build command.
|
||||||
func newCmdBuild(
|
func NewCmdBuild(
|
||||||
out io.Writer, fs fs.FileSystem,
|
out io.Writer, fs fs.FileSystem,
|
||||||
decoder ifc.Decoder) *cobra.Command {
|
decoder ifc.Decoder) *cobra.Command {
|
||||||
var o buildOptions
|
var o buildOptions
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -22,12 +22,12 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sigs.k8s.io/kustomize/internal/k8sdeps"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ghodss/yaml"
|
"github.com/ghodss/yaml"
|
||||||
|
"sigs.k8s.io/kustomize/internal/k8sdeps"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
)
|
)
|
||||||
@@ -99,7 +99,7 @@ func TestBuild(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
// sanity check that we found the right folder
|
// sanity check that we found the right folder
|
||||||
if !stringInSlice("simple", testcases) {
|
if !kustfile.StringInSlice("simple", testcases) {
|
||||||
t.Fatalf("Error locating testcases")
|
t.Fatalf("Error locating testcases")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
description: simple
|
description: simple
|
||||||
args: []
|
args: []
|
||||||
filename: ../examplelayout/simple/instances/exampleinstance/
|
filename: ../../examplelayout/simple/instances/exampleinstance/
|
||||||
expectedStdout: testdata/testcase-simple/expected.yaml
|
expectedStdout: testdata/testcase-simple/expected.yaml
|
||||||
expectedDiff: testdata/testcase-simple/expected.diff
|
expectedDiff: testdata/testcase-simple/expected.diff
|
||||||
@@ -22,6 +22,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/build"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/edit"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/misc"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
)
|
)
|
||||||
@@ -44,10 +47,10 @@ See https://sigs.k8s.io/kustomize
|
|||||||
|
|
||||||
c.AddCommand(
|
c.AddCommand(
|
||||||
// TODO: Make consistent API for newCmd* functions.
|
// TODO: Make consistent API for newCmd* functions.
|
||||||
newCmdBuild(stdOut, fsys, decoder),
|
build.NewCmdBuild(stdOut, fsys, decoder),
|
||||||
newCmdEdit(fsys, validator),
|
edit.NewCmdEdit(fsys, validator),
|
||||||
newCmdConfig(fsys),
|
misc.NewCmdConfig(fsys),
|
||||||
newCmdVersion(stdOut),
|
misc.NewCmdVersion(stdOut),
|
||||||
)
|
)
|
||||||
c.PersistentFlags().AddGoFlagSet(flag.CommandLine)
|
c.PersistentFlags().AddGoFlagSet(flag.CommandLine)
|
||||||
|
|
||||||
@@ -56,85 +59,3 @@ See https://sigs.k8s.io/kustomize
|
|||||||
flag.CommandLine.Parse([]string{})
|
flag.CommandLine.Parse([]string{})
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// newCmdEdit returns an instance of 'edit' subcommand.
|
|
||||||
func newCmdEdit(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
|
|
||||||
c := &cobra.Command{
|
|
||||||
Use: "edit",
|
|
||||||
Short: "Edits a kustomization file",
|
|
||||||
Long: "",
|
|
||||||
Example: `
|
|
||||||
# Adds a configmap to the kustomization file
|
|
||||||
kustomize edit add configmap NAME --from-literal=k=v
|
|
||||||
|
|
||||||
# Sets the nameprefix field
|
|
||||||
kustomize edit set nameprefix <prefix-value>
|
|
||||||
`,
|
|
||||||
Args: cobra.MinimumNArgs(1),
|
|
||||||
}
|
|
||||||
c.AddCommand(
|
|
||||||
newCmdAdd(fsys, v),
|
|
||||||
newCmdSet(fsys, v),
|
|
||||||
)
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
// newAddCommand returns an instance of 'add' subcommand.
|
|
||||||
func newCmdAdd(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
|
|
||||||
c := &cobra.Command{
|
|
||||||
Use: "add",
|
|
||||||
Short: "Adds configmap/resource/patch/base to the kustomization file.",
|
|
||||||
Long: "",
|
|
||||||
Example: `
|
|
||||||
# Adds a configmap to the kustomization file
|
|
||||||
kustomize edit add configmap NAME --from-literal=k=v
|
|
||||||
|
|
||||||
# Adds a resource to the kustomization
|
|
||||||
kustomize edit add resource <filepath>
|
|
||||||
|
|
||||||
# Adds a patch to the kustomization
|
|
||||||
kustomize edit add patch <filepath>
|
|
||||||
|
|
||||||
# Adds one or more base directories to the kustomization
|
|
||||||
kustomize edit add base <filepath>
|
|
||||||
kustomize edit add base <filepath1>,<filepath2>,<filepath3>
|
|
||||||
|
|
||||||
# Adds one or more commonLabels to the kustomization
|
|
||||||
kustomize edit add label {labelKey1:labelValue1},{labelKey2:labelValue2}
|
|
||||||
|
|
||||||
# Adds one or more commonAnnotations to the kustomization
|
|
||||||
kustomize edit add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}
|
|
||||||
`,
|
|
||||||
Args: cobra.MinimumNArgs(1),
|
|
||||||
}
|
|
||||||
c.AddCommand(
|
|
||||||
newCmdAddResource(fsys),
|
|
||||||
newCmdAddPatch(fsys),
|
|
||||||
newCmdAddConfigMap(fsys),
|
|
||||||
newCmdAddBase(fsys),
|
|
||||||
newCmdAddLabel(fsys, v.MakeLabelValidator()),
|
|
||||||
newCmdAddAnnotation(fsys, v.MakeAnnotationValidator()),
|
|
||||||
)
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
// newSetCommand returns an instance of 'set' subcommand.
|
|
||||||
func newCmdSet(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
|
|
||||||
c := &cobra.Command{
|
|
||||||
Use: "set",
|
|
||||||
Short: "Sets the value of different fields in kustomization file.",
|
|
||||||
Long: "",
|
|
||||||
Example: `
|
|
||||||
# Sets the nameprefix field
|
|
||||||
kustomize edit set nameprefix <prefix-value>
|
|
||||||
`,
|
|
||||||
Args: cobra.MinimumNArgs(1),
|
|
||||||
}
|
|
||||||
|
|
||||||
c.AddCommand(
|
|
||||||
newCmdSetNamePrefix(fsys),
|
|
||||||
newCmdSetNamespace(fsys, v),
|
|
||||||
newCmdSetImageTag(fsys),
|
|
||||||
)
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -22,7 +22,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
)
|
)
|
||||||
@@ -71,12 +71,12 @@ func (o *addBaseOptions) Complete(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
// RunAddBase runs addBase command (do real work).
|
// RunAddBase runs addBase command (do real work).
|
||||||
func (o *addBaseOptions) RunAddBase(fsys fs.FileSystem) error {
|
func (o *addBaseOptions) RunAddBase(fsys fs.FileSystem) error {
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := mf.read()
|
m, err := mf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -87,12 +87,12 @@ func (o *addBaseOptions) RunAddBase(fsys fs.FileSystem) error {
|
|||||||
if !fsys.Exists(path) {
|
if !fsys.Exists(path) {
|
||||||
return errors.New(path + " does not exist")
|
return errors.New(path + " does not exist")
|
||||||
}
|
}
|
||||||
if stringInSlice(path, m.Bases) {
|
if kustfile.StringInSlice(path, m.Bases) {
|
||||||
return fmt.Errorf("base %s already in kustomization file", path)
|
return fmt.Errorf("base %s already in kustomization file", path)
|
||||||
}
|
}
|
||||||
m.Bases = append(m.Bases, path)
|
m.Bases = append(m.Bases, path)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mf.write(m)
|
return mf.Write(m)
|
||||||
}
|
}
|
||||||
@@ -14,13 +14,13 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"strings"
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
)
|
)
|
||||||
@@ -35,7 +35,7 @@ func TestAddBaseHappyPath(t *testing.T) {
|
|||||||
for _, base := range bases {
|
for _, base := range bases {
|
||||||
fakeFS.Mkdir(base)
|
fakeFS.Mkdir(base)
|
||||||
}
|
}
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdAddBase(fakeFS)
|
cmd := newCmdAddBase(fakeFS)
|
||||||
args := []string{baseDirectoryPaths}
|
args := []string{baseDirectoryPaths}
|
||||||
@@ -62,7 +62,7 @@ func TestAddBaseAlreadyThere(t *testing.T) {
|
|||||||
for _, base := range bases {
|
for _, base := range bases {
|
||||||
fakeFS.Mkdir(base)
|
fakeFS.Mkdir(base)
|
||||||
}
|
}
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdAddBase(fakeFS)
|
cmd := newCmdAddBase(fakeFS)
|
||||||
args := []string{baseDirectoryPaths}
|
args := []string{baseDirectoryPaths}
|
||||||
@@ -79,7 +79,7 @@ func TestAddBaseAlreadyThere(t *testing.T) {
|
|||||||
for _, base := range bases {
|
for _, base := range bases {
|
||||||
msg := "base " + base + " already in kustomization file"
|
msg := "base " + base + " already in kustomization file"
|
||||||
expectedErrors = append(expectedErrors, msg)
|
expectedErrors = append(expectedErrors, msg)
|
||||||
if !stringInSlice(msg, expectedErrors) {
|
if !kustfile.StringInSlice(msg, expectedErrors) {
|
||||||
t.Errorf("unexpected error %v", err)
|
t.Errorf("unexpected error %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,13 +14,14 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/types"
|
||||||
@@ -91,11 +92,11 @@ func (o *addMetadataOptions) runE(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
kf, err := newKustomizationFile(constants.KustomizationFileName, fSys)
|
kf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fSys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m, err := kf.read()
|
m, err := kf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -103,7 +104,7 @@ func (o *addMetadataOptions) runE(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return kf.write(m)
|
return kf.Write(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateAndParse validates `add` commands and parses them into o.metadata
|
// validateAndParse validates `add` commands and parses them into o.metadata
|
||||||
@@ -14,11 +14,12 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/types"
|
||||||
@@ -27,12 +28,12 @@ import (
|
|||||||
|
|
||||||
func makeKustomization(t *testing.T) *types.Kustomization {
|
func makeKustomization(t *testing.T) *types.Kustomization {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
kf, err := newKustomizationFile(constants.KustomizationFileName, fakeFS)
|
kf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fakeFS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected new error %v", err)
|
t.Errorf("unexpected new error %v", err)
|
||||||
}
|
}
|
||||||
m, err := kf.read()
|
m, err := kf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected read error %v", err)
|
t.Errorf("unexpected read error %v", err)
|
||||||
}
|
}
|
||||||
@@ -92,7 +93,7 @@ func TestAddAnnotationInvalidFormat(t *testing.T) {
|
|||||||
|
|
||||||
func TestAddAnnotationManyArgs(t *testing.T) {
|
func TestAddAnnotationManyArgs(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
v := validators.MakeHappyMapValidator(t)
|
v := validators.MakeHappyMapValidator(t)
|
||||||
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
|
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
|
||||||
args := []string{"k1:v1,k2:v2,k3:v3,k4:v5"}
|
args := []string{"k1:v1,k2:v2,k3:v3,k4:v5"}
|
||||||
@@ -135,7 +136,7 @@ func TestAddAnnotationTooManyColons(t *testing.T) {
|
|||||||
|
|
||||||
func TestAddAnnotationNoValue(t *testing.T) {
|
func TestAddAnnotationNoValue(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
v := validators.MakeHappyMapValidator(t)
|
v := validators.MakeHappyMapValidator(t)
|
||||||
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
|
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
|
||||||
args := []string{"no:,value"}
|
args := []string{"no:,value"}
|
||||||
@@ -148,7 +149,7 @@ func TestAddAnnotationNoValue(t *testing.T) {
|
|||||||
|
|
||||||
func TestAddAnnotationMultipleArgs(t *testing.T) {
|
func TestAddAnnotationMultipleArgs(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
v := validators.MakeHappyMapValidator(t)
|
v := validators.MakeHappyMapValidator(t)
|
||||||
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
|
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
|
||||||
args := []string{"this:annotation", "has:spaces"}
|
args := []string{"this:annotation", "has:spaces"}
|
||||||
@@ -245,7 +246,7 @@ func TestAddLabelTooManyColons(t *testing.T) {
|
|||||||
|
|
||||||
func TestAddLabelNoValue(t *testing.T) {
|
func TestAddLabelNoValue(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
v := validators.MakeHappyMapValidator(t)
|
v := validators.MakeHappyMapValidator(t)
|
||||||
cmd := newCmdAddLabel(fakeFS, v.Validator)
|
cmd := newCmdAddLabel(fakeFS, v.Validator)
|
||||||
args := []string{"no,value:"}
|
args := []string{"no,value:"}
|
||||||
@@ -258,7 +259,7 @@ func TestAddLabelNoValue(t *testing.T) {
|
|||||||
|
|
||||||
func TestAddLabelMultipleArgs(t *testing.T) {
|
func TestAddLabelMultipleArgs(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
v := validators.MakeHappyMapValidator(t)
|
v := validators.MakeHappyMapValidator(t)
|
||||||
cmd := newCmdAddLabel(fakeFS, v.Validator)
|
cmd := newCmdAddLabel(fakeFS, v.Validator)
|
||||||
args := []string{"this:input", "has:spaces"}
|
args := []string{"this:input", "has:spaces"}
|
||||||
@@ -14,14 +14,14 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/pkg/patch"
|
"sigs.k8s.io/kustomize/pkg/patch"
|
||||||
@@ -79,23 +79,23 @@ func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := mf.read()
|
m, err := mf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, p := range patches {
|
for _, p := range patches {
|
||||||
if patch.Exist(m.PatchesStrategicMerge, p) || stringInSlice(p, m.Patches) {
|
if patch.Exist(m.PatchesStrategicMerge, p) || kustfile.StringInSlice(p, m.Patches) {
|
||||||
log.Printf("patch %s already in kustomization file", p)
|
log.Printf("patch %s already in kustomization file", p)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
m.PatchesStrategicMerge = patch.Append(m.PatchesStrategicMerge, p)
|
m.PatchesStrategicMerge = patch.Append(m.PatchesStrategicMerge, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
return mf.write(m)
|
return mf.Write(m)
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -37,7 +37,7 @@ func TestAddPatchHappyPath(t *testing.T) {
|
|||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(patchFileName, []byte(patchFileContent))
|
fakeFS.WriteFile(patchFileName, []byte(patchFileContent))
|
||||||
fakeFS.WriteFile(patchFileName+"another", []byte(patchFileContent))
|
fakeFS.WriteFile(patchFileName+"another", []byte(patchFileContent))
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdAddPatch(fakeFS)
|
cmd := newCmdAddPatch(fakeFS)
|
||||||
args := []string{patchFileName + "*"}
|
args := []string{patchFileName + "*"}
|
||||||
@@ -60,7 +60,7 @@ func TestAddPatchHappyPath(t *testing.T) {
|
|||||||
func TestAddPatchAlreadyThere(t *testing.T) {
|
func TestAddPatchAlreadyThere(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(patchFileName, []byte(patchFileContent))
|
fakeFS.WriteFile(patchFileName, []byte(patchFileContent))
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdAddPatch(fakeFS)
|
cmd := newCmdAddPatch(fakeFS)
|
||||||
args := []string{patchFileName}
|
args := []string{patchFileName}
|
||||||
@@ -14,14 +14,14 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
)
|
)
|
||||||
@@ -78,23 +78,23 @@ func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := mf.read()
|
m, err := mf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, resource := range resources {
|
for _, resource := range resources {
|
||||||
if stringInSlice(resource, m.Resources) {
|
if kustfile.StringInSlice(resource, m.Resources) {
|
||||||
log.Printf("resource %s already in kustomization file", resource)
|
log.Printf("resource %s already in kustomization file", resource)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
m.Resources = append(m.Resources, resource)
|
m.Resources = append(m.Resources, resource)
|
||||||
}
|
}
|
||||||
|
|
||||||
return mf.write(m)
|
return mf.Write(m)
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
@@ -29,22 +29,6 @@ const (
|
|||||||
resourceFileContent = `
|
resourceFileContent = `
|
||||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
|
||||||
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
||||||
`
|
|
||||||
kustomizationContent = `namePrefix: some-prefix
|
|
||||||
# Labels to add to all objects and selectors.
|
|
||||||
# These labels would also be used to form the selector for apply --prune
|
|
||||||
# Named differently than “labels” to avoid confusion with metadata for this object
|
|
||||||
commonLabels:
|
|
||||||
app: helloworld
|
|
||||||
commonAnnotations:
|
|
||||||
note: This is an example annotation
|
|
||||||
resources: []
|
|
||||||
#- service.yaml
|
|
||||||
#- ../some-dir/
|
|
||||||
# There could also be configmaps in Base, which would make these overlays
|
|
||||||
configMapGenerator: []
|
|
||||||
# There could be secrets in Base, if just using a fork/rebase workflow
|
|
||||||
secretGenerator: []
|
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -52,7 +36,7 @@ func TestAddResourceHappyPath(t *testing.T) {
|
|||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
||||||
fakeFS.WriteFile(resourceFileName+"another", []byte(resourceFileContent))
|
fakeFS.WriteFile(resourceFileName+"another", []byte(resourceFileContent))
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdAddResource(fakeFS)
|
cmd := newCmdAddResource(fakeFS)
|
||||||
args := []string{resourceFileName + "*"}
|
args := []string{resourceFileName + "*"}
|
||||||
@@ -75,7 +59,7 @@ func TestAddResourceHappyPath(t *testing.T) {
|
|||||||
func TestAddResourceAlreadyThere(t *testing.T) {
|
func TestAddResourceAlreadyThere(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdAddResource(fakeFS)
|
cmd := newCmdAddResource(fakeFS)
|
||||||
args := []string{resourceFileName}
|
args := []string{resourceFileName}
|
||||||
62
pkg/commands/edit/add/all.go
Normal file
62
pkg/commands/edit/add/all.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package add
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewCmdAdd returns an instance of 'add' subcommand.
|
||||||
|
func NewCmdAdd(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
|
||||||
|
c := &cobra.Command{
|
||||||
|
Use: "add",
|
||||||
|
Short: "Adds configmap/resource/patch/base to the kustomization file.",
|
||||||
|
Long: "",
|
||||||
|
Example: `
|
||||||
|
# Adds a configmap to the kustomization file
|
||||||
|
kustomize edit add configmap NAME --from-literal=k=v
|
||||||
|
|
||||||
|
# Adds a resource to the kustomization
|
||||||
|
kustomize edit add resource <filepath>
|
||||||
|
|
||||||
|
# Adds a patch to the kustomization
|
||||||
|
kustomize edit add patch <filepath>
|
||||||
|
|
||||||
|
# Adds one or more base directories to the kustomization
|
||||||
|
kustomize edit add base <filepath>
|
||||||
|
kustomize edit add base <filepath1>,<filepath2>,<filepath3>
|
||||||
|
|
||||||
|
# Adds one or more commonLabels to the kustomization
|
||||||
|
kustomize edit add label {labelKey1:labelValue1},{labelKey2:labelValue2}
|
||||||
|
|
||||||
|
# Adds one or more commonAnnotations to the kustomization
|
||||||
|
kustomize edit add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}
|
||||||
|
`,
|
||||||
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
}
|
||||||
|
c.AddCommand(
|
||||||
|
newCmdAddResource(fsys),
|
||||||
|
newCmdAddPatch(fsys),
|
||||||
|
newCmdAddConfigMap(fsys),
|
||||||
|
newCmdAddBase(fsys),
|
||||||
|
newCmdAddLabel(fsys, v.MakeLabelValidator()),
|
||||||
|
newCmdAddAnnotation(fsys, v.MakeAnnotationValidator()),
|
||||||
|
)
|
||||||
|
return c
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -14,13 +14,13 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/configmapandsecret"
|
"sigs.k8s.io/kustomize/pkg/configmapandsecret"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
@@ -28,6 +28,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// newCmdAddConfigMap returns a new command.
|
||||||
func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
|
func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
|
||||||
var flagsAndArgs cMapFlagsAndArgs
|
var flagsAndArgs cMapFlagsAndArgs
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@@ -56,12 +57,12 @@ func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load the kustomization file.
|
// Load the kustomization file.
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fSys)
|
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fSys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
kustomization, err := mf.read()
|
kustomization, err := mf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -76,7 +77,7 @@ func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write out the kustomization file with added configmap.
|
// Write out the kustomization file with added configmap.
|
||||||
return mf.write(kustomization)
|
return mf.Write(kustomization)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package add
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
47
pkg/commands/edit/all.go
Normal file
47
pkg/commands/edit/all.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package edit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/edit/add"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/edit/set"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewCmdEdit returns an instance of 'edit' subcommand.
|
||||||
|
func NewCmdEdit(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
|
||||||
|
c := &cobra.Command{
|
||||||
|
Use: "edit",
|
||||||
|
Short: "Edits a kustomization file",
|
||||||
|
Long: "",
|
||||||
|
Example: `
|
||||||
|
# Adds a configmap to the kustomization file
|
||||||
|
kustomize edit add configmap NAME --from-literal=k=v
|
||||||
|
|
||||||
|
# Sets the nameprefix field
|
||||||
|
kustomize edit set nameprefix <prefix-value>
|
||||||
|
`,
|
||||||
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
}
|
||||||
|
c.AddCommand(
|
||||||
|
add.NewCmdAdd(fsys, v),
|
||||||
|
set.NewCmdSet(fsys, v),
|
||||||
|
)
|
||||||
|
return c
|
||||||
|
}
|
||||||
44
pkg/commands/edit/set/all.go
Normal file
44
pkg/commands/edit/set/all.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package set
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewCmdSet returns an instance of 'set' subcommand.
|
||||||
|
func NewCmdSet(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
|
||||||
|
c := &cobra.Command{
|
||||||
|
Use: "set",
|
||||||
|
Short: "Sets the value of different fields in kustomization file.",
|
||||||
|
Long: "",
|
||||||
|
Example: `
|
||||||
|
# Sets the nameprefix field
|
||||||
|
kustomize edit set nameprefix <prefix-value>
|
||||||
|
`,
|
||||||
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
}
|
||||||
|
|
||||||
|
c.AddCommand(
|
||||||
|
newCmdSetNamePrefix(fsys),
|
||||||
|
newCmdSetNamespace(fsys, v),
|
||||||
|
newCmdSetImageTag(fsys),
|
||||||
|
)
|
||||||
|
return c
|
||||||
|
}
|
||||||
@@ -14,13 +14,13 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package set
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
)
|
)
|
||||||
@@ -74,14 +74,14 @@ func (o *setNamePrefixOptions) Complete(cmd *cobra.Command, args []string) error
|
|||||||
|
|
||||||
// RunSetNamePrefix runs setNamePrefix command (does real work).
|
// RunSetNamePrefix runs setNamePrefix command (does real work).
|
||||||
func (o *setNamePrefixOptions) RunSetNamePrefix(fsys fs.FileSystem) error {
|
func (o *setNamePrefixOptions) RunSetNamePrefix(fsys fs.FileSystem) error {
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m, err := mf.read()
|
m, err := mf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m.NamePrefix = o.prefix
|
m.NamePrefix = o.prefix
|
||||||
return mf.write(m)
|
return mf.Write(m)
|
||||||
}
|
}
|
||||||
@@ -14,12 +14,11 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package set
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
@@ -31,7 +30,7 @@ const (
|
|||||||
|
|
||||||
func TestSetNamePrefixHappyPath(t *testing.T) {
|
func TestSetNamePrefixHappyPath(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdSetNamePrefix(fakeFS)
|
cmd := newCmdSetNamePrefix(fakeFS)
|
||||||
args := []string{goodPrefixValue}
|
args := []string{goodPrefixValue}
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package set
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -23,7 +23,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/types"
|
||||||
@@ -100,11 +100,11 @@ func (o *setImageTagOptions) Validate(args []string) error {
|
|||||||
|
|
||||||
// RunSetImageTags runs setImageTags command (does real work).
|
// RunSetImageTags runs setImageTags command (does real work).
|
||||||
func (o *setImageTagOptions) RunSetImageTags(fsys fs.FileSystem) error {
|
func (o *setImageTagOptions) RunSetImageTags(fsys fs.FileSystem) error {
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m, err := mf.read()
|
m, err := mf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -127,5 +127,5 @@ func (o *setImageTagOptions) RunSetImageTags(fsys fs.FileSystem) error {
|
|||||||
|
|
||||||
m.ImageTags = imageTags
|
m.ImageTags = imageTags
|
||||||
|
|
||||||
return mf.write(m)
|
return mf.Write(m)
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package set
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
@@ -26,7 +26,7 @@ import (
|
|||||||
|
|
||||||
func TestSetImageTagsHappyPath(t *testing.T) {
|
func TestSetImageTagsHappyPath(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdSetImageTag(fakeFS)
|
cmd := newCmdSetImageTag(fakeFS)
|
||||||
args := []string{"image1:tag1", "image2:tag2", "localhost:5000/operator:1.0.0",
|
args := []string{"image1:tag1", "image2:tag2", "localhost:5000/operator:1.0.0",
|
||||||
@@ -57,7 +57,7 @@ imageTags:
|
|||||||
|
|
||||||
func TestSetImageTagsOverride(t *testing.T) {
|
func TestSetImageTagsOverride(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdSetImageTag(fakeFS)
|
cmd := newCmdSetImageTag(fakeFS)
|
||||||
args := []string{"image1:tag1", "image2:tag1"}
|
args := []string{"image1:tag1", "image2:tag1"}
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package set
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
@@ -72,14 +73,14 @@ func (o *setNamespaceOptions) Validate(args []string) error {
|
|||||||
|
|
||||||
// RunSetNamespace runs setNamespace command (does real work).
|
// RunSetNamespace runs setNamespace command (does real work).
|
||||||
func (o *setNamespaceOptions) RunSetNamespace(fsys fs.FileSystem) error {
|
func (o *setNamespaceOptions) RunSetNamespace(fsys fs.FileSystem) error {
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m, err := mf.read()
|
m, err := mf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m.Namespace = o.namespace
|
m.Namespace = o.namespace
|
||||||
return mf.write(m)
|
return mf.Write(m)
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package set
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -32,7 +32,7 @@ const (
|
|||||||
|
|
||||||
func TestSetNamespaceHappyPath(t *testing.T) {
|
func TestSetNamespaceHappyPath(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
|
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
|
||||||
args := []string{goodNamespaceValue}
|
args := []string{goodNamespaceValue}
|
||||||
@@ -52,7 +52,7 @@ func TestSetNamespaceHappyPath(t *testing.T) {
|
|||||||
|
|
||||||
func TestSetNamespaceOverride(t *testing.T) {
|
func TestSetNamespaceOverride(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteTestKustomization()
|
||||||
|
|
||||||
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
|
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
|
||||||
args := []string{goodNamespaceValue}
|
args := []string{goodNamespaceValue}
|
||||||
@@ -90,7 +90,6 @@ func TestSetNamespaceNoArgs(t *testing.T) {
|
|||||||
|
|
||||||
func TestSetNamespaceInvalid(t *testing.T) {
|
func TestSetNamespaceInvalid(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
|
||||||
|
|
||||||
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
|
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
|
||||||
args := []string{"/badnamespace/"}
|
args := []string{"/badnamespace/"}
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package kustfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -77,7 +77,8 @@ type kustomizationFile struct {
|
|||||||
originalFields []*commentedField
|
originalFields []*commentedField
|
||||||
}
|
}
|
||||||
|
|
||||||
func newKustomizationFile(mPath string, fsys fs.FileSystem) (*kustomizationFile, error) { // nolint
|
// NewKustomizationFile returns a new instance.
|
||||||
|
func NewKustomizationFile(mPath string, fsys fs.FileSystem) (*kustomizationFile, error) { // nolint
|
||||||
mf := &kustomizationFile{path: mPath, fsys: fsys}
|
mf := &kustomizationFile{path: mPath, fsys: fsys}
|
||||||
err := mf.validate()
|
err := mf.validate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -104,7 +105,7 @@ func (mf *kustomizationFile) validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mf *kustomizationFile) read() (*types.Kustomization, error) {
|
func (mf *kustomizationFile) Read() (*types.Kustomization, error) {
|
||||||
data, err := mf.fsys.ReadFile(mf.path)
|
data, err := mf.fsys.ReadFile(mf.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -121,7 +122,7 @@ func (mf *kustomizationFile) read() (*types.Kustomization, error) {
|
|||||||
return &kustomization, err
|
return &kustomization, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mf *kustomizationFile) write(kustomization *types.Kustomization) error {
|
func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error {
|
||||||
if kustomization == nil {
|
if kustomization == nil {
|
||||||
return errors.New("util: kustomization file arg is nil")
|
return errors.New("util: kustomization file arg is nil")
|
||||||
}
|
}
|
||||||
@@ -132,7 +133,8 @@ func (mf *kustomizationFile) write(kustomization *types.Kustomization) error {
|
|||||||
return mf.fsys.WriteFile(mf.path, data)
|
return mf.fsys.WriteFile(mf.path, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringInSlice(str string, list []string) bool {
|
// StringInSlice returns true if the string is in the slice.
|
||||||
|
func StringInSlice(str string, list []string) bool {
|
||||||
for _, v := range list {
|
for _, v := range list {
|
||||||
if v == str {
|
if v == str {
|
||||||
return true
|
return true
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package kustfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -33,16 +33,16 @@ func TestWriteAndRead(t *testing.T) {
|
|||||||
|
|
||||||
fsys := fs.MakeFakeFS()
|
fsys := fs.MakeFakeFS()
|
||||||
fsys.Create(constants.KustomizationFileName)
|
fsys.Create(constants.KustomizationFileName)
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected Error: %v", err)
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := mf.write(kustomization); err != nil {
|
if err := mf.Write(kustomization); err != nil {
|
||||||
t.Fatalf("Couldn't write kustomization file: %v\n", err)
|
t.Fatalf("Couldn't write kustomization file: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := mf.read()
|
content, err := mf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Couldn't read kustomization file: %v\n", err)
|
t.Fatalf("Couldn't read kustomization file: %v\n", err)
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ func TestWriteAndRead(t *testing.T) {
|
|||||||
|
|
||||||
func TestEmptyFile(t *testing.T) {
|
func TestEmptyFile(t *testing.T) {
|
||||||
fsys := fs.MakeFakeFS()
|
fsys := fs.MakeFakeFS()
|
||||||
_, err := newKustomizationFile("", fsys)
|
_, err := NewKustomizationFile("", fsys)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("Create kustomizationFile from empty filename should fail")
|
t.Fatalf("Create kustomizationFile from empty filename should fail")
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,7 @@ func TestNewNotExist(t *testing.T) {
|
|||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.Mkdir(".")
|
fakeFS.Mkdir(".")
|
||||||
fakeFS.Create(badSuffix)
|
fakeFS.Create(badSuffix)
|
||||||
_, err := newKustomizationFile(constants.KustomizationFileName, fakeFS)
|
_, err := NewKustomizationFile(constants.KustomizationFileName, fakeFS)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expect an error")
|
t.Fatalf("expect an error")
|
||||||
}
|
}
|
||||||
@@ -72,14 +72,14 @@ func TestNewNotExist(t *testing.T) {
|
|||||||
if !strings.Contains(err.Error(), contained) {
|
if !strings.Contains(err.Error(), contained) {
|
||||||
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
||||||
}
|
}
|
||||||
_, err = newKustomizationFile(constants.KustomizationFileName, fakeFS)
|
_, err = NewKustomizationFile(constants.KustomizationFileName, fakeFS)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expect an error")
|
t.Fatalf("expect an error")
|
||||||
}
|
}
|
||||||
if !strings.Contains(err.Error(), contained) {
|
if !strings.Contains(err.Error(), contained) {
|
||||||
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
||||||
}
|
}
|
||||||
_, err = newKustomizationFile(badSuffix, fakeFS)
|
_, err = NewKustomizationFile(badSuffix, fakeFS)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expect an error")
|
t.Fatalf("expect an error")
|
||||||
}
|
}
|
||||||
@@ -116,15 +116,15 @@ patchesStrategicMerge:
|
|||||||
fsys := fs.MakeFakeFS()
|
fsys := fs.MakeFakeFS()
|
||||||
fsys.Create(constants.KustomizationFileName)
|
fsys.Create(constants.KustomizationFileName)
|
||||||
fsys.WriteFile(constants.KustomizationFileName, kustomizationContentWithComments)
|
fsys.WriteFile(constants.KustomizationFileName, kustomizationContentWithComments)
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected Error: %v", err)
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
}
|
}
|
||||||
kustomization, err := mf.read()
|
kustomization, err := mf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected Error: %v", err)
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
}
|
}
|
||||||
if err = mf.write(kustomization); err != nil {
|
if err = mf.Write(kustomization); err != nil {
|
||||||
t.Fatalf("Unexpected Error: %v", err)
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
}
|
}
|
||||||
bytes, _ := fsys.ReadFile(mf.path)
|
bytes, _ := fsys.ReadFile(mf.path)
|
||||||
@@ -207,16 +207,16 @@ patchesStrategicMerge:
|
|||||||
fsys := fs.MakeFakeFS()
|
fsys := fs.MakeFakeFS()
|
||||||
fsys.Create(constants.KustomizationFileName)
|
fsys.Create(constants.KustomizationFileName)
|
||||||
fsys.WriteFile(constants.KustomizationFileName, kustomizationContentWithComments)
|
fsys.WriteFile(constants.KustomizationFileName, kustomizationContentWithComments)
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected Error: %v", err)
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
kustomization, err := mf.read()
|
kustomization, err := mf.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected Error: %v", err)
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
}
|
}
|
||||||
if err = mf.write(kustomization); err != nil {
|
if err = mf.Write(kustomization); err != nil {
|
||||||
t.Fatalf("Unexpected Error: %v", err)
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
}
|
}
|
||||||
bytes, _ := fsys.ReadFile(mf.path)
|
bytes, _ := fsys.ReadFile(mf.path)
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user