create_context.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2014 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 config
  14. import (
  15. "errors"
  16. "fmt"
  17. "io"
  18. "github.com/renstrom/dedent"
  19. "github.com/spf13/cobra"
  20. "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
  21. clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
  22. "k8s.io/kubernetes/pkg/util"
  23. )
  24. type createContextOptions struct {
  25. configAccess clientcmd.ConfigAccess
  26. name string
  27. cluster util.StringFlag
  28. authInfo util.StringFlag
  29. namespace util.StringFlag
  30. }
  31. var (
  32. create_context_long = dedent.Dedent(`
  33. Sets a context entry in kubeconfig
  34. Specifying a name that already exists will merge new fields on top of existing values for those fields.`)
  35. create_context_example = dedent.Dedent(`
  36. # Set the user field on the gce context entry without touching other values
  37. kubectl config set-context gce --user=cluster-admin`)
  38. )
  39. func NewCmdConfigSetContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
  40. options := &createContextOptions{configAccess: configAccess}
  41. cmd := &cobra.Command{
  42. Use: fmt.Sprintf("set-context NAME [--%v=cluster_nickname] [--%v=user_nickname] [--%v=namespace]", clientcmd.FlagClusterName, clientcmd.FlagAuthInfoName, clientcmd.FlagNamespace),
  43. Short: "Sets a context entry in kubeconfig",
  44. Long: create_context_long,
  45. Example: create_context_example,
  46. Run: func(cmd *cobra.Command, args []string) {
  47. if !options.complete(cmd) {
  48. return
  49. }
  50. err := options.run()
  51. if err != nil {
  52. fmt.Fprintf(out, "%v\n", err)
  53. } else {
  54. fmt.Fprintf(out, "context %q set.\n", options.name)
  55. }
  56. },
  57. }
  58. cmd.Flags().Var(&options.cluster, clientcmd.FlagClusterName, clientcmd.FlagClusterName+" for the context entry in kubeconfig")
  59. cmd.Flags().Var(&options.authInfo, clientcmd.FlagAuthInfoName, clientcmd.FlagAuthInfoName+" for the context entry in kubeconfig")
  60. cmd.Flags().Var(&options.namespace, clientcmd.FlagNamespace, clientcmd.FlagNamespace+" for the context entry in kubeconfig")
  61. return cmd
  62. }
  63. func (o createContextOptions) run() error {
  64. err := o.validate()
  65. if err != nil {
  66. return err
  67. }
  68. config, err := o.configAccess.GetStartingConfig()
  69. if err != nil {
  70. return err
  71. }
  72. startingStanza, exists := config.Contexts[o.name]
  73. if !exists {
  74. startingStanza = clientcmdapi.NewContext()
  75. }
  76. context := o.modifyContext(*startingStanza)
  77. config.Contexts[o.name] = &context
  78. if err := clientcmd.ModifyConfig(o.configAccess, *config, true); err != nil {
  79. return err
  80. }
  81. return nil
  82. }
  83. func (o *createContextOptions) modifyContext(existingContext clientcmdapi.Context) clientcmdapi.Context {
  84. modifiedContext := existingContext
  85. if o.cluster.Provided() {
  86. modifiedContext.Cluster = o.cluster.Value()
  87. }
  88. if o.authInfo.Provided() {
  89. modifiedContext.AuthInfo = o.authInfo.Value()
  90. }
  91. if o.namespace.Provided() {
  92. modifiedContext.Namespace = o.namespace.Value()
  93. }
  94. return modifiedContext
  95. }
  96. func (o *createContextOptions) complete(cmd *cobra.Command) bool {
  97. args := cmd.Flags().Args()
  98. if len(args) != 1 {
  99. cmd.Help()
  100. return false
  101. }
  102. o.name = args[0]
  103. return true
  104. }
  105. func (o createContextOptions) validate() error {
  106. if len(o.name) == 0 {
  107. return errors.New("you must specify a non-empty context name")
  108. }
  109. return nil
  110. }