2014-10-06 01:24:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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 cmd
|
|
|
|
|
|
|
|
import (
|
2014-10-27 02:21:31 +00:00
|
|
|
"fmt"
|
2014-10-06 01:24:19 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2014-12-31 23:35:52 +00:00
|
|
|
|
2015-03-04 00:24:29 +00:00
|
|
|
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
|
2014-12-31 23:35:52 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-10-06 01:24:19 +00:00
|
|
|
)
|
|
|
|
|
2015-02-20 21:28:43 +00:00
|
|
|
const (
|
|
|
|
create_long = `Create a resource by filename or stdin.
|
|
|
|
|
|
|
|
JSON and YAML formats are accepted.`
|
|
|
|
create_example = `// Create a pod using the data in pod.json.
|
|
|
|
$ kubectl create -f pod.json
|
|
|
|
|
|
|
|
// Create a pod based on the JSON passed into stdin.
|
|
|
|
$ cat pod.json | kubectl create -f -`
|
|
|
|
)
|
|
|
|
|
2014-10-27 02:21:31 +00:00
|
|
|
func (f *Factory) NewCmdCreate(out io.Writer) *cobra.Command {
|
2015-03-09 22:08:16 +00:00
|
|
|
var filenames util.StringList
|
2014-10-06 01:24:19 +00:00
|
|
|
cmd := &cobra.Command{
|
2015-03-11 17:22:08 +00:00
|
|
|
Use: "create -f FILENAME",
|
2015-02-20 21:28:43 +00:00
|
|
|
Short: "Create a resource by filename or stdin",
|
|
|
|
Long: create_long,
|
|
|
|
Example: create_example,
|
2014-10-06 01:24:19 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2015-03-24 18:21:52 +00:00
|
|
|
cmdutil.CheckErr(ValidateArgs(cmd, args))
|
|
|
|
cmdutil.CheckErr(RunCreate(f, out, cmd, filenames))
|
2015-03-09 22:08:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cmd.Flags().VarP(&filenames, "filename", "f", "Filename, directory, or URL to file to use to create the resource")
|
|
|
|
return cmd
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2015-03-24 18:21:52 +00:00
|
|
|
func ValidateArgs(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) != 0 {
|
|
|
|
return cmdutil.UsageError(cmd, "Unexpected args: %v", args)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-09 22:08:16 +00:00
|
|
|
func RunCreate(f *Factory, out io.Writer, cmd *cobra.Command, filenames util.StringList) error {
|
2015-03-14 10:45:18 +00:00
|
|
|
schema, err := f.Validator()
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
|
2015-03-14 10:45:18 +00:00
|
|
|
cmdNamespace, err := f.DefaultNamespace()
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-04 18:59:23 +00:00
|
|
|
|
2015-03-14 10:45:18 +00:00
|
|
|
mapper, typer := f.Object()
|
2015-03-09 22:08:16 +00:00
|
|
|
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand(cmd)).
|
|
|
|
ContinueOnError().
|
|
|
|
NamespaceParam(cmdNamespace).RequireNamespace().
|
|
|
|
FilenameParam(filenames...).
|
|
|
|
Flatten().
|
|
|
|
Do()
|
|
|
|
err = r.Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2015-03-09 22:08:16 +00:00
|
|
|
|
|
|
|
count := 0
|
|
|
|
err = r.Visit(func(info *resource.Info) error {
|
|
|
|
data, err := info.Mapping.Codec.Encode(info.Object)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := schema.ValidateBytes(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
info.Refresh(obj, true)
|
|
|
|
fmt.Fprintf(out, "%s\n", info.Name)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if count == 0 {
|
|
|
|
return fmt.Errorf("no objects passed to create")
|
|
|
|
}
|
|
|
|
return nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|