create_serviceaccount.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 create
  14. import (
  15. "github.com/spf13/cobra"
  16. "k8s.io/cli-runtime/pkg/genericclioptions"
  17. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  18. "k8s.io/kubernetes/pkg/kubectl/generate"
  19. generateversioned "k8s.io/kubernetes/pkg/kubectl/generate/versioned"
  20. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  21. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  22. )
  23. var (
  24. serviceAccountLong = templates.LongDesc(i18n.T(`
  25. Create a service account with the specified name.`))
  26. serviceAccountExample = templates.Examples(i18n.T(`
  27. # Create a new service account named my-service-account
  28. kubectl create serviceaccount my-service-account`))
  29. )
  30. // ServiceAccountOpts holds the options for 'create serviceaccount' sub command
  31. type ServiceAccountOpts struct {
  32. CreateSubcommandOptions *CreateSubcommandOptions
  33. }
  34. // NewCmdCreateServiceAccount is a macro command to create a new service account
  35. func NewCmdCreateServiceAccount(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
  36. options := &ServiceAccountOpts{
  37. CreateSubcommandOptions: NewCreateSubcommandOptions(ioStreams),
  38. }
  39. cmd := &cobra.Command{
  40. Use: "serviceaccount NAME [--dry-run]",
  41. DisableFlagsInUseLine: true,
  42. Aliases: []string{"sa"},
  43. Short: i18n.T("Create a service account with the specified name"),
  44. Long: serviceAccountLong,
  45. Example: serviceAccountExample,
  46. Run: func(cmd *cobra.Command, args []string) {
  47. cmdutil.CheckErr(options.Complete(f, cmd, args))
  48. cmdutil.CheckErr(options.Run())
  49. },
  50. }
  51. options.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)
  52. cmdutil.AddApplyAnnotationFlags(cmd)
  53. cmdutil.AddValidateFlags(cmd)
  54. cmdutil.AddGeneratorFlags(cmd, generateversioned.ServiceAccountV1GeneratorName)
  55. return cmd
  56. }
  57. // Complete completes all the required options
  58. func (o *ServiceAccountOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
  59. name, err := NameFromCommandArgs(cmd, args)
  60. if err != nil {
  61. return err
  62. }
  63. var generator generate.StructuredGenerator
  64. switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
  65. case generateversioned.ServiceAccountV1GeneratorName:
  66. generator = &generateversioned.ServiceAccountGeneratorV1{Name: name}
  67. default:
  68. return errUnsupportedGenerator(cmd, generatorName)
  69. }
  70. return o.CreateSubcommandOptions.Complete(f, cmd, args, generator)
  71. }
  72. // Run calls the CreateSubcommandOptions.Run in ServiceAccountOpts instance
  73. func (o *ServiceAccountOpts) Run() error {
  74. return o.CreateSubcommandOptions.Run()
  75. }