2014-10-06 01:24:19 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
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"
|
2015-06-05 03:52:46 +00:00
|
|
|
"strings"
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2014-12-31 23:35:52 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-10-21 14:41:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2014-10-06 01:24:19 +00:00
|
|
|
)
|
|
|
|
|
2015-08-14 18:46:43 +00:00
|
|
|
// CreateOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
|
|
|
|
// referencing the cmd.Flags()
|
|
|
|
type CreateOptions struct {
|
|
|
|
Filenames []string
|
|
|
|
}
|
|
|
|
|
2015-02-20 21:28:43 +00:00
|
|
|
const (
|
|
|
|
create_long = `Create a resource by filename or stdin.
|
|
|
|
|
|
|
|
JSON and YAML formats are accepted.`
|
2015-08-12 16:50:09 +00:00
|
|
|
create_example = `# Create a pod using the data in pod.json.
|
2016-02-29 14:41:09 +00:00
|
|
|
kubectl create -f ./pod.json
|
2015-02-20 21:28:43 +00:00
|
|
|
|
2015-08-12 16:50:09 +00:00
|
|
|
# Create a pod based on the JSON passed into stdin.
|
2016-02-29 14:41:09 +00:00
|
|
|
cat pod.json | kubectl create -f -`
|
2015-02-20 21:28:43 +00:00
|
|
|
)
|
|
|
|
|
2015-04-07 18:21:25 +00:00
|
|
|
func NewCmdCreate(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
2015-08-14 18:46:43 +00:00
|
|
|
options := &CreateOptions{}
|
|
|
|
|
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-10-21 14:41:42 +00:00
|
|
|
if len(options.Filenames) == 0 {
|
|
|
|
cmd.Help()
|
|
|
|
return
|
|
|
|
}
|
2015-03-24 18:21:52 +00:00
|
|
|
cmdutil.CheckErr(ValidateArgs(cmd, args))
|
2015-07-01 22:47:43 +00:00
|
|
|
cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd))
|
2015-08-14 18:46:43 +00:00
|
|
|
cmdutil.CheckErr(RunCreate(f, cmd, out, options))
|
2015-03-09 22:08:16 +00:00
|
|
|
},
|
|
|
|
}
|
2015-03-29 03:38:46 +00:00
|
|
|
|
|
|
|
usage := "Filename, directory, or URL to file to use to create the resource"
|
2015-08-14 18:46:43 +00:00
|
|
|
kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
|
2015-03-17 15:49:35 +00:00
|
|
|
cmd.MarkFlagRequired("filename")
|
2015-09-10 21:58:09 +00:00
|
|
|
cmdutil.AddValidateFlags(cmd)
|
2015-07-01 22:47:43 +00:00
|
|
|
cmdutil.AddOutputFlagsForMutation(cmd)
|
2015-11-04 21:47:08 +00:00
|
|
|
cmdutil.AddApplyAnnotationFlags(cmd)
|
2016-01-22 18:33:23 +00:00
|
|
|
cmdutil.AddRecordFlag(cmd)
|
2015-10-21 14:41:42 +00:00
|
|
|
|
|
|
|
// create subcommands
|
|
|
|
cmd.AddCommand(NewCmdCreateNamespace(f, out))
|
|
|
|
cmd.AddCommand(NewCmdCreateSecret(f, out))
|
2016-02-19 02:24:21 +00:00
|
|
|
cmd.AddCommand(NewCmdCreateConfigMap(f, out))
|
2016-02-17 18:15:32 +00:00
|
|
|
cmd.AddCommand(NewCmdCreateServiceAccount(f, out))
|
2015-03-09 22:08:16 +00:00
|
|
|
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-08-14 18:46:43 +00:00
|
|
|
func RunCreate(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *CreateOptions) error {
|
2015-09-11 03:54:22 +00:00
|
|
|
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir"))
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
|
2015-06-26 20:49:34 +00:00
|
|
|
cmdNamespace, enforceNamespace, 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-12-21 05:37:49 +00:00
|
|
|
r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
|
2015-05-07 20:53:43 +00:00
|
|
|
Schema(schema).
|
2015-03-09 22:08:16 +00:00
|
|
|
ContinueOnError().
|
2015-06-26 20:49:34 +00:00
|
|
|
NamespaceParam(cmdNamespace).DefaultNamespace().
|
2015-08-14 18:46:43 +00:00
|
|
|
FilenameParam(enforceNamespace, options.Filenames...).
|
2015-03-09 22:08:16 +00:00
|
|
|
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
|
2015-06-15 02:48:56 +00:00
|
|
|
err = r.Visit(func(info *resource.Info, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-21 05:37:49 +00:00
|
|
|
if err := kubectl.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), info, f.JSONEncoder()); err != nil {
|
2015-09-10 21:32:57 +00:00
|
|
|
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
|
|
|
}
|
|
|
|
|
2016-01-22 18:33:23 +00:00
|
|
|
if cmdutil.ShouldRecord(cmd, info) {
|
|
|
|
if err := cmdutil.RecordChangeCause(info.Object, f.Command()); err != nil {
|
|
|
|
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 02:29:56 +00:00
|
|
|
if err := createAndRefresh(info); err != nil {
|
2015-06-23 12:26:27 +00:00
|
|
|
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
2015-03-09 22:08:16 +00:00
|
|
|
}
|
2015-09-10 21:32:57 +00:00
|
|
|
|
2015-03-09 22:08:16 +00:00
|
|
|
count++
|
2015-08-05 14:21:47 +00:00
|
|
|
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
|
2015-07-01 22:47:43 +00:00
|
|
|
if !shortOutput {
|
|
|
|
printObjectSpecificMessage(info.Object, out)
|
|
|
|
}
|
|
|
|
cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, "created")
|
2015-03-09 22:08:16 +00:00
|
|
|
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
|
|
|
}
|
2015-06-05 03:52:46 +00:00
|
|
|
|
|
|
|
func printObjectSpecificMessage(obj runtime.Object, out io.Writer) {
|
|
|
|
switch obj := obj.(type) {
|
|
|
|
case *api.Service:
|
|
|
|
if obj.Spec.Type == api.ServiceTypeNodePort {
|
2015-07-14 18:47:36 +00:00
|
|
|
msg := fmt.Sprintf(
|
|
|
|
`You have exposed your service on an external port on all nodes in your
|
|
|
|
cluster. If you want to expose this service to the external internet, you may
|
|
|
|
need to set up firewall rules for the service port(s) (%s) to serve traffic.
|
|
|
|
|
2015-07-16 09:03:32 +00:00
|
|
|
See http://releases.k8s.io/HEAD/docs/user-guide/services-firewalls.md for more details.
|
2015-07-14 18:47:36 +00:00
|
|
|
`,
|
|
|
|
makePortsString(obj.Spec.Ports, true))
|
2015-06-05 03:52:46 +00:00
|
|
|
out.Write([]byte(msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 22:10:22 +00:00
|
|
|
func makePortsString(ports []api.ServicePort, useNodePort bool) string {
|
2015-06-05 03:52:46 +00:00
|
|
|
pieces := make([]string, len(ports))
|
|
|
|
for ix := range ports {
|
2015-06-19 22:10:22 +00:00
|
|
|
var port int
|
|
|
|
if useNodePort {
|
|
|
|
port = ports[ix].NodePort
|
|
|
|
} else {
|
|
|
|
port = ports[ix].Port
|
|
|
|
}
|
|
|
|
pieces[ix] = fmt.Sprintf("%s:%d", strings.ToLower(string(ports[ix].Protocol)), port)
|
2015-06-05 03:52:46 +00:00
|
|
|
}
|
|
|
|
return strings.Join(pieces, ",")
|
|
|
|
}
|
2015-11-05 02:29:56 +00:00
|
|
|
|
|
|
|
// createAndRefresh creates an object from input info and refreshes info with that object
|
|
|
|
func createAndRefresh(info *resource.Info) error {
|
|
|
|
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, info.Object)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
info.Refresh(obj, true)
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-21 14:41:42 +00:00
|
|
|
|
|
|
|
// NameFromCommandArgs is a utility function for commands that assume the first argument is a resource name
|
|
|
|
func NameFromCommandArgs(cmd *cobra.Command, args []string) (string, error) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return "", cmdutil.UsageError(cmd, "NAME is required")
|
|
|
|
}
|
|
|
|
return args[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSubcommandOptions is an options struct to support create subcommands
|
|
|
|
type CreateSubcommandOptions struct {
|
|
|
|
// Name of resource being created
|
|
|
|
Name string
|
|
|
|
// StructuredGenerator is the resource generator for the object being created
|
|
|
|
StructuredGenerator kubectl.StructuredGenerator
|
|
|
|
// DryRun is true if the command should be simulated but not run against the server
|
|
|
|
DryRun bool
|
|
|
|
// OutputFormat
|
|
|
|
OutputFormat string
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunCreateSubcommand executes a create subcommand using the specified options
|
|
|
|
func RunCreateSubcommand(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *CreateSubcommandOptions) error {
|
|
|
|
namespace, _, err := f.DefaultNamespace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obj, err := options.StructuredGenerator.StructuredGenerate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mapper, typer := f.Object()
|
|
|
|
gvk, err := typer.ObjectKind(obj)
|
|
|
|
mapping, err := mapper.RESTMapping(unversioned.GroupKind{Group: gvk.Group, Kind: gvk.Kind}, gvk.Version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-21 05:37:49 +00:00
|
|
|
client, err := f.ClientForMapping(mapping)
|
2015-10-21 14:41:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-21 05:37:49 +00:00
|
|
|
resourceMapper := &resource.Mapper{
|
|
|
|
ObjectTyper: typer,
|
|
|
|
RESTMapper: mapper,
|
|
|
|
ClientMapper: resource.ClientMapperFunc(f.ClientForMapping),
|
|
|
|
}
|
2016-03-11 04:49:00 +00:00
|
|
|
info, err := resourceMapper.InfoForObject(obj, nil)
|
2015-10-21 14:41:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-21 05:37:49 +00:00
|
|
|
if err := kubectl.UpdateApplyAnnotation(info, f.JSONEncoder()); err != nil {
|
2015-10-21 14:41:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !options.DryRun {
|
|
|
|
obj, err = resource.NewHelper(client, mapping).Create(namespace, false, info.Object)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if useShortOutput := options.OutputFormat == "name"; useShortOutput || len(options.OutputFormat) == 0 {
|
|
|
|
cmdutil.PrintSuccess(mapper, useShortOutput, out, mapping.Resource, options.Name, "created")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.PrintObject(cmd, obj, out)
|
|
|
|
}
|