start addresource command

This commit is contained in:
Jeffrey Regan 2018-01-30 14:46:46 -08:00
parent d0a39a4d08
commit a5ccaf6f6e
5 changed files with 134 additions and 7 deletions

View File

@ -0,0 +1,73 @@
/*
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 (
"io"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/kinflate/util/fs"
)
type addResourceOptions struct {
}
// NewCmdAddResource adds the name of a file containing a resource to the manifest.
func NewCmdAddResource(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
var o addResourceOptions
cmd := &cobra.Command{
Use: "addresource",
Short: "Add the name of a file containing a resource to the manifest.",
Long: "Add the name of a file containing a resource to the manifest.",
Example: `
addresource {filepath}`,
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.RunAddResource(cmd, out, errOut, fs)
},
}
return cmd
}
// Validate validates addResource command.
func (o *addResourceOptions) Validate(cmd *cobra.Command, args []string) error {
return nil
}
// Complete completes addResource command.
func (o *addResourceOptions) Complete(cmd *cobra.Command, args []string) error {
return nil
}
// RunAddResource runs addResource command (do real work).
func (o *addResourceOptions) RunAddResource(cmd *cobra.Command, out, errOut io.Writer, fs fs.FileSystem) error {
// error if unable to read the file argument
// error if unable to find kubemanist in current directory
// error if unable to parse Kube-manifest from the current directory
// error (or just INFO) and exit if the resource is already in the manifest
// add the resource
// write the new manifest, error if trouble writing it.
return nil
}

View File

@ -0,0 +1,36 @@
/*
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/kinflate/util/fs"
)
func TestAddResource(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fakeFS := fs.MakeFakeFS()
cmd := NewCmdAddResource(buf, os.Stderr, fakeFS)
err := cmd.Execute()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}

View File

@ -23,6 +23,7 @@ import (
"path" "path"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/kubectl/pkg/kinflate"
"k8s.io/kubectl/pkg/kinflate/util/fs" "k8s.io/kubectl/pkg/kinflate/util/fs"
) )
@ -124,7 +125,7 @@ func (o *initOptions) RunInit(cmd *cobra.Command, out, errOut io.Writer, fs fs.F
return err return err
} }
err = writefile("Kube-manifest.yaml", []byte(manifestTemplate), fs) err = writefile(kinflate.KubeManifestFileName, []byte(manifestTemplate), fs)
if err != nil { if err != nil {
return err return err
} }

19
pkg/kinflate/constants.go Normal file
View File

@ -0,0 +1,19 @@
/*
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 kinflate
const KubeManifestFileName = "Kube-manifest.yaml"

View File

@ -35,8 +35,6 @@ import (
"k8s.io/kubectl/pkg/scheme" "k8s.io/kubectl/pkg/scheme"
) )
const kubeManifestFileName = "Kube-manifest.yaml"
// loadManifestPkg loads a manifest file and parse it in to the Manifest object. // loadManifestPkg loads a manifest file and parse it in to the Manifest object.
func loadManifestPkg(filename string) (*manifest.Manifest, error) { func loadManifestPkg(filename string) (*manifest.Manifest, error) {
bytes, err := ioutil.ReadFile(filename) bytes, err := ioutil.ReadFile(filename)
@ -74,10 +72,10 @@ func LoadFromManifestPath(mPath string,
return nil, err return nil, err
} }
if f.IsDir() { if f.IsDir() {
mPath = path.Join(mPath, kubeManifestFileName) mPath = path.Join(mPath, KubeManifestFileName)
} else { } else {
if !strings.HasSuffix(mPath, kubeManifestFileName) { if !strings.HasSuffix(mPath, KubeManifestFileName) {
return nil, fmt.Errorf("expecting file: %q, but got: %q", kubeManifestFileName, mPath) return nil, fmt.Errorf("expecting file: %q, but got: %q", KubeManifestFileName, mPath)
} }
} }
manifest, err := loadManifestPkg(mPath) manifest, err := loadManifestPkg(mPath)
@ -132,7 +130,7 @@ func dirToMap(dirname string, into map[kutil.GroupVersionKindName]*unstructured.
return fmt.Errorf("%q is expected to be an dir", dirname) return fmt.Errorf("%q is expected to be an dir", dirname)
} }
kubeManifestFileAbsName := path.Join(dirname, kubeManifestFileName) kubeManifestFileAbsName := path.Join(dirname, KubeManifestFileName)
_, err = os.Stat(kubeManifestFileAbsName) _, err = os.Stat(kubeManifestFileAbsName)
switch { switch {
case err != nil && !os.IsNotExist(err): case err != nil && !os.IsNotExist(err):