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
|
|
|
|
|
|
|
"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 {
|
2014-12-31 23:35:52 +00:00
|
|
|
flags := &struct {
|
|
|
|
Filenames util.StringList
|
|
|
|
}{}
|
2014-10-06 01:24:19 +00:00
|
|
|
cmd := &cobra.Command{
|
2015-02-20 21:28:43 +00:00
|
|
|
Use: "create -f filename",
|
|
|
|
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) {
|
2014-11-26 03:50:31 +00:00
|
|
|
schema, err := f.Validator(cmd)
|
|
|
|
checkErr(err)
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2015-01-02 18:08:37 +00:00
|
|
|
cmdNamespace, err := f.DefaultNamespace(cmd)
|
|
|
|
checkErr(err)
|
|
|
|
|
2014-12-31 23:35:52 +00:00
|
|
|
mapper, typer := f.Object(cmd)
|
2015-02-05 00:14:48 +00:00
|
|
|
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand(cmd)).
|
2014-12-31 23:35:52 +00:00
|
|
|
ContinueOnError().
|
2015-01-02 18:08:37 +00:00
|
|
|
NamespaceParam(cmdNamespace).RequireNamespace().
|
2014-12-31 23:35:52 +00:00
|
|
|
FilenameParam(flags.Filenames...).
|
|
|
|
Flatten().
|
|
|
|
Do()
|
2015-02-10 14:32:26 +00:00
|
|
|
checkErr(r.Err())
|
2014-11-04 18:59:23 +00:00
|
|
|
|
2015-01-14 18:06:53 +00:00
|
|
|
count := 0
|
2014-12-31 23:35:52 +00:00
|
|
|
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
|
|
|
|
}
|
2015-02-06 16:57:52 +00:00
|
|
|
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, data)
|
|
|
|
if err != nil {
|
2014-12-31 23:35:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-01-14 18:06:53 +00:00
|
|
|
count++
|
2015-02-06 16:57:52 +00:00
|
|
|
info.Refresh(obj, true)
|
2014-12-31 23:35:52 +00:00
|
|
|
fmt.Fprintf(out, "%s\n", info.Name)
|
|
|
|
return nil
|
|
|
|
})
|
2014-10-06 01:24:19 +00:00
|
|
|
checkErr(err)
|
2015-01-14 18:06:53 +00:00
|
|
|
if count == 0 {
|
|
|
|
checkErr(fmt.Errorf("no objects passed to create"))
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
},
|
|
|
|
}
|
2014-12-31 23:35:52 +00:00
|
|
|
cmd.Flags().VarP(&flags.Filenames, "filename", "f", "Filename, directory, or URL to file to use to create the resource")
|
2014-10-06 01:24:19 +00:00
|
|
|
return cmd
|
|
|
|
}
|