Init writes only a manifest to the current dir.
This commit is contained in:
parent
66fd85dc9a
commit
0e4382bac8
|
|
@ -20,15 +20,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"path"
|
"errors"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/kubectl/pkg/kinflate"
|
"k8s.io/kubectl/pkg/kinflate"
|
||||||
"k8s.io/kubectl/pkg/kinflate/util/fs"
|
"k8s.io/kubectl/pkg/kinflate/util/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
const appname = "helloworld"
|
|
||||||
|
|
||||||
const manifestTemplate = `apiVersion: manifest.k8s.io/v1alpha1
|
const manifestTemplate = `apiVersion: manifest.k8s.io/v1alpha1
|
||||||
kind: Manifest
|
kind: Manifest
|
||||||
metadata:
|
metadata:
|
||||||
|
|
@ -52,44 +50,19 @@ secrets: []
|
||||||
recursive: true
|
recursive: true
|
||||||
`
|
`
|
||||||
|
|
||||||
const deploymentTemplate = `apiVersion: extensions/v1beta1
|
|
||||||
kind: Deployment
|
|
||||||
metadata:
|
|
||||||
name: helloworld
|
|
||||||
spec:
|
|
||||||
template:
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- name: nginx
|
|
||||||
image: nginx
|
|
||||||
`
|
|
||||||
|
|
||||||
const serviceTemplate = `apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: helloworld
|
|
||||||
labels:
|
|
||||||
app: helloworld
|
|
||||||
spec:
|
|
||||||
ports:
|
|
||||||
- port: 8888
|
|
||||||
selector:
|
|
||||||
app: helloworld
|
|
||||||
`
|
|
||||||
|
|
||||||
type initOptions struct {
|
type initOptions struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCmdInit make the init command.
|
// NewCmdInit makes the init command.
|
||||||
func NewCmdInit(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
|
func NewCmdInit(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
|
||||||
var o initOptions
|
var o initOptions
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "init",
|
Use: "init",
|
||||||
Short: "TDB",
|
Short: "Creates a file called \"" + kinflate.KubeManifestFileName + "\" in the current directory",
|
||||||
Long: "TBD",
|
Long: "Creates a file called \"" +
|
||||||
Example: `
|
kinflate.KubeManifestFileName + "\" in the current directory with example values.",
|
||||||
TBD`,
|
Example: `init`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
err := o.Validate(cmd, args)
|
err := o.Validate(cmd, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -107,6 +80,9 @@ func NewCmdInit(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
|
||||||
|
|
||||||
// Validate validates init command.
|
// Validate validates init command.
|
||||||
func (o *initOptions) Validate(cmd *cobra.Command, args []string) error {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -115,34 +91,11 @@ func (o *initOptions) Complete(cmd *cobra.Command, args []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunKinflate runs init command (do real work).
|
// RunInit writes a manifest file.
|
||||||
func (o *initOptions) RunInit(cmd *cobra.Command, out, errOut io.Writer, fs fs.FileSystem) error {
|
func (o *initOptions) RunInit(
|
||||||
if _, err := fs.Stat("helloworld"); err == nil {
|
cmd *cobra.Command, out, errOut io.Writer, fs fs.FileSystem) error {
|
||||||
return fmt.Errorf("%q already exists", appname)
|
if _, err := fs.Stat(kinflate.KubeManifestFileName); err == nil {
|
||||||
|
return fmt.Errorf("%q already exists", kinflate.KubeManifestFileName)
|
||||||
}
|
}
|
||||||
err := fs.Mkdir(appname, 0744)
|
return fs.WriteFile(kinflate.KubeManifestFileName, []byte(manifestTemplate))
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = writefile(kinflate.KubeManifestFileName, []byte(manifestTemplate), fs)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = writefile("deployment.yaml", []byte(deploymentTemplate), fs)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = writefile("service.yaml", []byte(serviceTemplate), fs)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func writefile(name string, content []byte, fs fs.FileSystem) error {
|
|
||||||
f, err := fs.Create(path.Join(appname, name))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
_, err = f.Write(content)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,9 @@ package commands
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/kubectl/pkg/kinflate"
|
||||||
"k8s.io/kubectl/pkg/kinflate/util/fs"
|
"k8s.io/kubectl/pkg/kinflate/util/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -34,7 +34,7 @@ func TestInitHappyPath(t *testing.T) {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := fakeFS.Open(path.Join(appname, "Kube-manifest.yaml"))
|
f, err := fakeFS.Open(kinflate.KubeManifestFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -43,37 +43,20 @@ func TestInitHappyPath(t *testing.T) {
|
||||||
t.Fatalf("actual: %v doesn't match expected: %v",
|
t.Fatalf("actual: %v doesn't match expected: %v",
|
||||||
string(file.GetContent()), manifestTemplate)
|
string(file.GetContent()), manifestTemplate)
|
||||||
}
|
}
|
||||||
f, err = fakeFS.Open(path.Join(appname, "deployment.yaml"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
file = f.(*fs.FakeFile)
|
|
||||||
if !file.ContentMatches([]byte(deploymentTemplate)) {
|
|
||||||
t.Fatalf("actual: %v doesn't match expected: %v",
|
|
||||||
string(file.GetContent()), deploymentTemplate)
|
|
||||||
}
|
|
||||||
f, err = fakeFS.Open(path.Join(appname, "service.yaml"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
file = f.(*fs.FakeFile)
|
|
||||||
if !file.ContentMatches([]byte(serviceTemplate)) {
|
|
||||||
t.Fatalf("actual: %v doesn't match expected: %v",
|
|
||||||
string(file.GetContent()), serviceTemplate)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInitFileAlreadyExist(t *testing.T) {
|
func TestInitFileAlreadyExist(t *testing.T) {
|
||||||
buf := bytes.NewBuffer([]byte{})
|
content := "hey there"
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.Mkdir(appname, 0766)
|
fakeFS.WriteFile(kinflate.KubeManifestFileName, []byte(content))
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
cmd := NewCmdInit(buf, os.Stderr, fakeFS)
|
cmd := NewCmdInit(buf, os.Stderr, fakeFS)
|
||||||
err := cmd.Execute()
|
err := cmd.Execute()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected error")
|
t.Fatalf("expected error")
|
||||||
}
|
}
|
||||||
if err.Error() != `"helloworld" already exists` {
|
if err.Error() != `"`+kinflate.KubeManifestFileName+`" already exists` {
|
||||||
t.Fatalf("actual err: %v doesn't match expected error: %v", err, `"helloworld" already exists`)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue