Init writes only a manifest to the current dir.

This commit is contained in:
Jeffrey Regan 2018-01-31 09:52:47 -08:00
parent 66fd85dc9a
commit 0e4382bac8
2 changed files with 22 additions and 86 deletions

View File

@ -20,15 +20,13 @@ import (
"fmt"
"io"
"path"
"errors"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/kinflate"
"k8s.io/kubectl/pkg/kinflate/util/fs"
)
const appname = "helloworld"
const manifestTemplate = `apiVersion: manifest.k8s.io/v1alpha1
kind: Manifest
metadata:
@ -52,44 +50,19 @@ secrets: []
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 {
}
// NewCmdInit make the init command.
// 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: "TDB",
Long: "TBD",
Example: `
TBD`,
Short: "Creates a file called \"" + kinflate.KubeManifestFileName + "\" in the current directory",
Long: "Creates a file called \"" +
kinflate.KubeManifestFileName + "\" in the current directory with example values.",
Example: `init`,
RunE: func(cmd *cobra.Command, args []string) error {
err := o.Validate(cmd, args)
if err != nil {
@ -107,6 +80,9 @@ func NewCmdInit(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
// 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
}
@ -115,34 +91,11 @@ func (o *initOptions) Complete(cmd *cobra.Command, args []string) error {
return nil
}
// RunKinflate runs init command (do real work).
func (o *initOptions) RunInit(cmd *cobra.Command, out, errOut io.Writer, fs fs.FileSystem) error {
if _, err := fs.Stat("helloworld"); err == nil {
return fmt.Errorf("%q already exists", appname)
// RunInit writes a manifest file.
func (o *initOptions) RunInit(
cmd *cobra.Command, out, errOut io.Writer, fs fs.FileSystem) error {
if _, err := fs.Stat(kinflate.KubeManifestFileName); err == nil {
return fmt.Errorf("%q already exists", kinflate.KubeManifestFileName)
}
err := fs.Mkdir(appname, 0744)
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
return fs.WriteFile(kinflate.KubeManifestFileName, []byte(manifestTemplate))
}

View File

@ -19,9 +19,9 @@ package commands
import (
"bytes"
"os"
"path"
"testing"
"k8s.io/kubectl/pkg/kinflate"
"k8s.io/kubectl/pkg/kinflate/util/fs"
)
@ -34,7 +34,7 @@ func TestInitHappyPath(t *testing.T) {
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 {
t.Fatalf("unexpected error: %v", err)
}
@ -43,37 +43,20 @@ func TestInitHappyPath(t *testing.T) {
t.Fatalf("actual: %v doesn't match expected: %v",
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) {
buf := bytes.NewBuffer([]byte{})
content := "hey there"
fakeFS := fs.MakeFakeFS()
fakeFS.Mkdir(appname, 0766)
fakeFS.WriteFile(kinflate.KubeManifestFileName, []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() != `"helloworld" already exists` {
t.Fatalf("actual err: %v doesn't match expected error: %v", err, `"helloworld" already exists`)
if err.Error() != `"`+kinflate.KubeManifestFileName+`" already exists` {
t.Fatalf("unexpected error: %v", err)
}
}