set_selector.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 set
  14. import (
  15. "fmt"
  16. "github.com/spf13/cobra"
  17. "k8s.io/klog"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/apimachinery/pkg/types"
  22. "k8s.io/apimachinery/pkg/util/validation"
  23. "k8s.io/cli-runtime/pkg/genericclioptions"
  24. "k8s.io/cli-runtime/pkg/printers"
  25. "k8s.io/cli-runtime/pkg/resource"
  26. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  27. "k8s.io/kubernetes/pkg/kubectl/scheme"
  28. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  29. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  30. )
  31. // SetSelectorOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
  32. // referencing the cmd.Flags()
  33. type SetSelectorOptions struct {
  34. // Bound
  35. ResourceBuilderFlags *genericclioptions.ResourceBuilderFlags
  36. PrintFlags *genericclioptions.PrintFlags
  37. RecordFlags *genericclioptions.RecordFlags
  38. dryrun bool
  39. // set by args
  40. resources []string
  41. selector *metav1.LabelSelector
  42. // computed
  43. WriteToServer bool
  44. PrintObj printers.ResourcePrinterFunc
  45. Recorder genericclioptions.Recorder
  46. ResourceFinder genericclioptions.ResourceFinder
  47. // set at initialization
  48. genericclioptions.IOStreams
  49. }
  50. var (
  51. selectorLong = templates.LongDesc(`
  52. Set the selector on a resource. Note that the new selector will overwrite the old selector if the resource had one prior to the invocation
  53. of 'set selector'.
  54. A selector must begin with a letter or number, and may contain letters, numbers, hyphens, dots, and underscores, up to %[1]d characters.
  55. If --resource-version is specified, then updates will use this resource version, otherwise the existing resource-version will be used.
  56. Note: currently selectors can only be set on Service objects.`)
  57. selectorExample = templates.Examples(`
  58. # set the labels and selector before creating a deployment/service pair.
  59. kubectl create service clusterip my-svc --clusterip="None" -o yaml --dry-run | kubectl set selector --local -f - 'environment=qa' -o yaml | kubectl create -f -
  60. kubectl create deployment my-dep -o yaml --dry-run | kubectl label --local -f - environment=qa -o yaml | kubectl create -f -`)
  61. )
  62. // NewSelectorOptions returns an initialized SelectorOptions instance
  63. func NewSelectorOptions(streams genericclioptions.IOStreams) *SetSelectorOptions {
  64. return &SetSelectorOptions{
  65. ResourceBuilderFlags: genericclioptions.NewResourceBuilderFlags().
  66. WithScheme(scheme.Scheme).
  67. WithAll(false).
  68. WithLocal(false).
  69. WithLatest(),
  70. PrintFlags: genericclioptions.NewPrintFlags("selector updated").WithTypeSetter(scheme.Scheme),
  71. RecordFlags: genericclioptions.NewRecordFlags(),
  72. Recorder: genericclioptions.NoopRecorder{},
  73. IOStreams: streams,
  74. }
  75. }
  76. // NewCmdSelector is the "set selector" command.
  77. func NewCmdSelector(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
  78. o := NewSelectorOptions(streams)
  79. cmd := &cobra.Command{
  80. Use: "selector (-f FILENAME | TYPE NAME) EXPRESSIONS [--resource-version=version]",
  81. DisableFlagsInUseLine: true,
  82. Short: i18n.T("Set the selector on a resource"),
  83. Long: fmt.Sprintf(selectorLong, validation.LabelValueMaxLength),
  84. Example: selectorExample,
  85. Run: func(cmd *cobra.Command, args []string) {
  86. cmdutil.CheckErr(o.Complete(f, cmd, args))
  87. cmdutil.CheckErr(o.Validate())
  88. cmdutil.CheckErr(o.RunSelector())
  89. },
  90. }
  91. o.ResourceBuilderFlags.AddFlags(cmd.Flags())
  92. o.PrintFlags.AddFlags(cmd)
  93. o.RecordFlags.AddFlags(cmd)
  94. cmd.Flags().String("resource-version", "", "If non-empty, the selectors update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource.")
  95. cmdutil.AddDryRunFlag(cmd)
  96. return cmd
  97. }
  98. // Complete assigns the SelectorOptions from args.
  99. func (o *SetSelectorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
  100. var err error
  101. o.RecordFlags.Complete(cmd)
  102. o.Recorder, err = o.RecordFlags.ToRecorder()
  103. if err != nil {
  104. return err
  105. }
  106. o.dryrun = cmdutil.GetDryRunFlag(cmd)
  107. o.resources, o.selector, err = getResourcesAndSelector(args)
  108. if err != nil {
  109. return err
  110. }
  111. o.ResourceFinder = o.ResourceBuilderFlags.ToBuilder(f, o.resources)
  112. o.WriteToServer = !(*o.ResourceBuilderFlags.Local || o.dryrun)
  113. if o.dryrun {
  114. o.PrintFlags.Complete("%s (dry run)")
  115. }
  116. printer, err := o.PrintFlags.ToPrinter()
  117. if err != nil {
  118. return err
  119. }
  120. o.PrintObj = printer.PrintObj
  121. return err
  122. }
  123. // Validate basic inputs
  124. func (o *SetSelectorOptions) Validate() error {
  125. if o.selector == nil {
  126. return fmt.Errorf("one selector is required")
  127. }
  128. return nil
  129. }
  130. // RunSelector executes the command.
  131. func (o *SetSelectorOptions) RunSelector() error {
  132. r := o.ResourceFinder.Do()
  133. return r.Visit(func(info *resource.Info, err error) error {
  134. patch := &Patch{Info: info}
  135. CalculatePatch(patch, scheme.DefaultJSONEncoder(), func(obj runtime.Object) ([]byte, error) {
  136. selectErr := updateSelectorForObject(info.Object, *o.selector)
  137. if selectErr != nil {
  138. return nil, selectErr
  139. }
  140. // record this change (for rollout history)
  141. if err := o.Recorder.Record(patch.Info.Object); err != nil {
  142. klog.V(4).Infof("error recording current command: %v", err)
  143. }
  144. return runtime.Encode(scheme.DefaultJSONEncoder(), info.Object)
  145. })
  146. if patch.Err != nil {
  147. return patch.Err
  148. }
  149. if !o.WriteToServer {
  150. return o.PrintObj(info.Object, o.Out)
  151. }
  152. actual, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch, nil)
  153. if err != nil {
  154. return err
  155. }
  156. return o.PrintObj(actual, o.Out)
  157. })
  158. }
  159. func updateSelectorForObject(obj runtime.Object, selector metav1.LabelSelector) error {
  160. copyOldSelector := func() (map[string]string, error) {
  161. if len(selector.MatchExpressions) > 0 {
  162. return nil, fmt.Errorf("match expression %v not supported on this object", selector.MatchExpressions)
  163. }
  164. dst := make(map[string]string)
  165. for label, value := range selector.MatchLabels {
  166. dst[label] = value
  167. }
  168. return dst, nil
  169. }
  170. var err error
  171. switch t := obj.(type) {
  172. case *v1.Service:
  173. t.Spec.Selector, err = copyOldSelector()
  174. default:
  175. err = fmt.Errorf("setting a selector is only supported for Services")
  176. }
  177. return err
  178. }
  179. // getResourcesAndSelector retrieves resources and the selector expression from the given args (assuming selectors the last arg)
  180. func getResourcesAndSelector(args []string) (resources []string, selector *metav1.LabelSelector, err error) {
  181. if len(args) == 0 {
  182. return []string{}, nil, nil
  183. }
  184. resources = args[:len(args)-1]
  185. selector, err = metav1.ParseToLabelSelector(args[len(args)-1])
  186. return resources, selector, err
  187. }