create_serviceaccount.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. serviceAccountLong = dedent.Dedent(`
  24. Create a service account with the specified name.`)
  25. serviceAccountExample = dedent.Dedent(`
  26. # Create a new service account named my-service-account
  27. $ kubectl create serviceaccount my-service-account`)
  28. )
  29. // NewCmdCreateServiceAccount is a macro command to create a new service account
  30. func NewCmdCreateServiceAccount(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command {
  31. cmd := &cobra.Command{
  32. Use: "serviceaccount NAME [--dry-run]",
  33. Aliases: []string{"sa"},
  34. Short: "Create a service account with the specified name",
  35. Long: serviceAccountLong,
  36. Example: serviceAccountExample,
  37. Run: func(cmd *cobra.Command, args []string) {
  38. err := CreateServiceAccount(f, cmdOut, cmd, args)
  39. cmdutil.CheckErr(err)
  40. },
  41. }
  42. cmdutil.AddApplyAnnotationFlags(cmd)
  43. cmdutil.AddValidateFlags(cmd)
  44. cmdutil.AddPrinterFlags(cmd)
  45. cmdutil.AddInclude3rdPartyFlags(cmd)
  46. cmdutil.AddGeneratorFlags(cmd, cmdutil.ServiceAccountV1GeneratorName)
  47. return cmd
  48. }
  49. // CreateServiceAccount implements the behavior to run the create service account command
  50. func CreateServiceAccount(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error {
  51. name, err := NameFromCommandArgs(cmd, args)
  52. if err != nil {
  53. return err
  54. }
  55. var generator kubectl.StructuredGenerator
  56. switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName {
  57. case cmdutil.ServiceAccountV1GeneratorName:
  58. generator = &kubectl.ServiceAccountGeneratorV1{Name: name}
  59. default:
  60. return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))
  61. }
  62. return RunCreateSubcommand(f, cmd, cmdOut, &CreateSubcommandOptions{
  63. Name: name,
  64. StructuredGenerator: generator,
  65. DryRun: cmdutil.GetDryRunFlag(cmd),
  66. OutputFormat: cmdutil.GetFlagString(cmd, "output"),
  67. })
  68. }