unset.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "reflect"
  19. "github.com/spf13/cobra"
  20. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  21. "k8s.io/client-go/tools/clientcmd"
  22. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  23. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  24. )
  25. type unsetOptions struct {
  26. configAccess clientcmd.ConfigAccess
  27. propertyName string
  28. }
  29. var (
  30. unsetLong = templates.LongDesc(`
  31. Unsets an individual value in a kubeconfig file
  32. PROPERTY_NAME is a dot delimited name where each token represents either an attribute name or a map key. Map keys may not contain dots.`)
  33. unsetExample = templates.Examples(`
  34. # Unset the current-context.
  35. kubectl config unset current-context
  36. # Unset namespace in foo context.
  37. kubectl config unset contexts.foo.namespace`)
  38. )
  39. // NewCmdConfigUnset returns a Command instance for 'config unset' sub command
  40. func NewCmdConfigUnset(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
  41. options := &unsetOptions{configAccess: configAccess}
  42. cmd := &cobra.Command{
  43. Use: "unset PROPERTY_NAME",
  44. DisableFlagsInUseLine: true,
  45. Short: i18n.T("Unsets an individual value in a kubeconfig file"),
  46. Long: unsetLong,
  47. Example: unsetExample,
  48. Run: func(cmd *cobra.Command, args []string) {
  49. cmdutil.CheckErr(options.complete(cmd, args))
  50. cmdutil.CheckErr(options.run(out))
  51. },
  52. }
  53. return cmd
  54. }
  55. func (o unsetOptions) run(out io.Writer) error {
  56. err := o.validate()
  57. if err != nil {
  58. return err
  59. }
  60. config, err := o.configAccess.GetStartingConfig()
  61. if err != nil {
  62. return err
  63. }
  64. steps, err := newNavigationSteps(o.propertyName)
  65. if err != nil {
  66. return err
  67. }
  68. err = modifyConfig(reflect.ValueOf(config), steps, "", true, true)
  69. if err != nil {
  70. return err
  71. }
  72. if err := clientcmd.ModifyConfig(o.configAccess, *config, false); err != nil {
  73. return err
  74. }
  75. if _, err := fmt.Fprintf(out, "Property %q unset.\n", o.propertyName); err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. func (o *unsetOptions) complete(cmd *cobra.Command, args []string) error {
  81. if len(args) != 1 {
  82. return helpErrorf(cmd, "Unexpected args: %v", args)
  83. }
  84. o.propertyName = args[0]
  85. return nil
  86. }
  87. func (o unsetOptions) validate() error {
  88. if len(o.propertyName) == 0 {
  89. return errors.New("you must specify a property")
  90. }
  91. return nil
  92. }