mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
Drop the init command
This commit is contained in:
@@ -32,6 +32,23 @@ 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 = `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
|
||||||
|
# Named differently than “labels” to avoid confusion with metadata for this object
|
||||||
|
objectLabels:
|
||||||
|
app: helloworld
|
||||||
|
objectAnnotations:
|
||||||
|
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: []
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,7 +56,7 @@ func TestAddResourceHappyPath(t *testing.T) {
|
|||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationTemplate))
|
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
||||||
|
|
||||||
cmd := newCmdAddResource(buf, os.Stderr, fakeFS)
|
cmd := newCmdAddResource(buf, os.Stderr, fakeFS)
|
||||||
args := []string{resourceFileName}
|
args := []string{resourceFileName}
|
||||||
@@ -60,7 +77,7 @@ func TestAddResourceAlreadyThere(t *testing.T) {
|
|||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationTemplate))
|
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
||||||
|
|
||||||
cmd := newCmdAddResource(buf, os.Stderr, fakeFS)
|
cmd := newCmdAddResource(buf, os.Stderr, fakeFS)
|
||||||
args := []string{resourceFileName}
|
args := []string{resourceFileName}
|
||||||
|
|||||||
@@ -44,7 +44,6 @@ More info at https://github.com/kubernetes/kubectl/tree/master/cmd/kustomize
|
|||||||
c.AddCommand(
|
c.AddCommand(
|
||||||
newCmdBuild(stdOut, stdErr, fsys),
|
newCmdBuild(stdOut, stdErr, fsys),
|
||||||
newCmdDiff(stdOut, stdErr, fsys),
|
newCmdDiff(stdOut, stdErr, fsys),
|
||||||
newCmdInit(stdOut, stdErr, fsys),
|
|
||||||
newCmdEdit(stdOut, stdErr, fsys),
|
newCmdEdit(stdOut, stdErr, fsys),
|
||||||
version.NewCmdVersion(stdOut),
|
version.NewCmdVersion(stdOut),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
/*
|
|
||||||
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 commands
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"k8s.io/kubectl/pkg/kustomize/constants"
|
|
||||||
"k8s.io/kubectl/pkg/kustomize/util/fs"
|
|
||||||
)
|
|
||||||
|
|
||||||
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
|
|
||||||
# Named differently than “labels” to avoid confusion with metadata for this object
|
|
||||||
objectLabels:
|
|
||||||
app: helloworld
|
|
||||||
objectAnnotations:
|
|
||||||
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: []
|
|
||||||
`
|
|
||||||
|
|
||||||
type initOptions struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewCmdInit makes the init command.
|
|
||||||
func newCmdInit(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
|
|
||||||
var o initOptions
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "init",
|
|
||||||
Short: "Creates a file called \"" + constants.KustomizationFileName + "\" in the current directory",
|
|
||||||
Long: "Creates a file called \"" +
|
|
||||||
constants.KustomizationFileName + "\" in the current directory with example values.",
|
|
||||||
Example: `init`,
|
|
||||||
SilenceUsage: true,
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
err := o.Validate(cmd, args)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = o.Complete(cmd, args)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return o.RunInit(out, errOut, fs)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate validates init command.
|
|
||||||
func (o *initOptions) Validate(cmd *cobra.Command, args []string) error {
|
|
||||||
if len(args) > 0 {
|
|
||||||
return errors.New("The init command takes no arguments.")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Complete completes init command.
|
|
||||||
func (o *initOptions) Complete(cmd *cobra.Command, args []string) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RunInit writes a kustomization file.
|
|
||||||
func (o *initOptions) RunInit(out, errOut io.Writer, fs fs.FileSystem) error {
|
|
||||||
if _, err := fs.Stat(constants.KustomizationFileName); err == nil {
|
|
||||||
return fmt.Errorf("%q already exists", constants.KustomizationFileName)
|
|
||||||
}
|
|
||||||
return fs.WriteFile(constants.KustomizationFileName, []byte(kustomizationTemplate))
|
|
||||||
}
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
/*
|
|
||||||
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 commands
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"k8s.io/kubectl/pkg/kustomize/constants"
|
|
||||||
"k8s.io/kubectl/pkg/kustomize/util/fs"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestInitHappyPath(t *testing.T) {
|
|
||||||
buf := bytes.NewBuffer([]byte{})
|
|
||||||
fakeFS := fs.MakeFakeFS()
|
|
||||||
cmd := newCmdInit(buf, os.Stderr, fakeFS)
|
|
||||||
err := cmd.Execute()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := fakeFS.Open(constants.KustomizationFileName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
file := f.(*fs.FakeFile)
|
|
||||||
if !file.ContentMatches([]byte(kustomizationTemplate)) {
|
|
||||||
t.Fatalf("actual: %v doesn't match expected: %v",
|
|
||||||
string(file.GetContent()), kustomizationTemplate)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInitFileAlreadyExist(t *testing.T) {
|
|
||||||
content := "hey there"
|
|
||||||
fakeFS := fs.MakeFakeFS()
|
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(content))
|
|
||||||
|
|
||||||
buf := bytes.NewBuffer([]byte{})
|
|
||||||
cmd := newCmdInit(buf, os.Stderr, fakeFS)
|
|
||||||
err := cmd.Execute()
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("expected error")
|
|
||||||
}
|
|
||||||
if err.Error() != `"`+constants.KustomizationFileName+`" already exists` {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -34,7 +34,7 @@ const (
|
|||||||
func TestSetNamePrefixHappyPath(t *testing.T) {
|
func TestSetNamePrefixHappyPath(t *testing.T) {
|
||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationTemplate))
|
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
||||||
|
|
||||||
cmd := newCmdSetNamePrefix(buf, os.Stderr, fakeFS)
|
cmd := newCmdSetNamePrefix(buf, os.Stderr, fakeFS)
|
||||||
args := []string{goodPrefixValue}
|
args := []string{goodPrefixValue}
|
||||||
|
|||||||
Reference in New Issue
Block a user