create_deployment.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "io"
  17. "github.com/renstrom/dedent"
  18. "github.com/spf13/cobra"
  19. "k8s.io/kubernetes/pkg/kubectl"
  20. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  21. )
  22. var (
  23. deploymentLong = dedent.Dedent(`
  24. Create a deployment with the specified name.`)
  25. deploymentExample = dedent.Dedent(`
  26. # Create a new deployment named my-dep that runs the busybox image.
  27. kubectl create deployment my-dep --image=busybox`)
  28. )
  29. // NewCmdCreateDeployment is a macro command to create a new deployment
  30. func NewCmdCreateDeployment(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command {
  31. cmd := &cobra.Command{
  32. Use: "deployment NAME --image=image [--dry-run]",
  33. Aliases: []string{"dep"},
  34. Short: "Create a deployment with the specified name.",
  35. Long: deploymentLong,
  36. Example: deploymentExample,
  37. Run: func(cmd *cobra.Command, args []string) {
  38. err := CreateDeployment(f, cmdOut, cmd, args)
  39. cmdutil.CheckErr(err)
  40. },
  41. }
  42. cmdutil.AddApplyAnnotationFlags(cmd)
  43. cmdutil.AddValidateFlags(cmd)
  44. cmdutil.AddPrinterFlags(cmd)
  45. cmdutil.AddGeneratorFlags(cmd, cmdutil.DeploymentBasicV1Beta1GeneratorName)
  46. cmd.Flags().StringSlice("image", []string{}, "Image name to run.")
  47. cmd.MarkFlagRequired("image")
  48. return cmd
  49. }
  50. // CreateDeployment implements the behavior to run the create deployment command
  51. func CreateDeployment(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error {
  52. name, err := NameFromCommandArgs(cmd, args)
  53. if err != nil {
  54. return err
  55. }
  56. var generator kubectl.StructuredGenerator
  57. switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
  58. case cmdutil.DeploymentBasicV1Beta1GeneratorName:
  59. generator = &kubectl.DeploymentBasicGeneratorV1{Name: name, Images: cmdutil.GetFlagStringSlice(cmd, "image")}
  60. default:
  61. return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))
  62. }
  63. return RunCreateSubcommand(f, cmd, cmdOut, &CreateSubcommandOptions{
  64. Name: name,
  65. StructuredGenerator: generator,
  66. DryRun: cmdutil.GetDryRunFlag(cmd),
  67. OutputFormat: cmdutil.GetFlagString(cmd, "output"),
  68. })
  69. }