generic.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright 2018 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 options
  14. import (
  15. "fmt"
  16. "strings"
  17. "k8s.io/apimachinery/pkg/util/sets"
  18. cliflag "k8s.io/component-base/cli/flag"
  19. componentbaseconfig "k8s.io/component-base/config"
  20. "k8s.io/kubernetes/pkg/client/leaderelectionconfig"
  21. kubectrlmgrconfig "k8s.io/kubernetes/pkg/controller/apis/config"
  22. )
  23. // GenericControllerManagerConfigurationOptions holds the options which are generic.
  24. type GenericControllerManagerConfigurationOptions struct {
  25. *kubectrlmgrconfig.GenericControllerManagerConfiguration
  26. Debugging *DebuggingOptions
  27. }
  28. // NewGenericControllerManagerConfigurationOptions returns generic configuration default values for both
  29. // the kube-controller-manager and the cloud-contoller-manager. Any common changes should
  30. // be made here. Any individual changes should be made in that controller.
  31. func NewGenericControllerManagerConfigurationOptions(cfg *kubectrlmgrconfig.GenericControllerManagerConfiguration) *GenericControllerManagerConfigurationOptions {
  32. o := &GenericControllerManagerConfigurationOptions{
  33. GenericControllerManagerConfiguration: cfg,
  34. Debugging: &DebuggingOptions{
  35. DebuggingConfiguration: &componentbaseconfig.DebuggingConfiguration{},
  36. },
  37. }
  38. return o
  39. }
  40. // AddFlags adds flags related to generic for controller manager to the specified FlagSet.
  41. func (o *GenericControllerManagerConfigurationOptions) AddFlags(fss *cliflag.NamedFlagSets, allControllers, disabledByDefaultControllers []string) {
  42. if o == nil {
  43. return
  44. }
  45. o.Debugging.AddFlags(fss.FlagSet("debugging"))
  46. genericfs := fss.FlagSet("generic")
  47. genericfs.DurationVar(&o.MinResyncPeriod.Duration, "min-resync-period", o.MinResyncPeriod.Duration, "The resync period in reflectors will be random between MinResyncPeriod and 2*MinResyncPeriod.")
  48. genericfs.StringVar(&o.ClientConnection.ContentType, "kube-api-content-type", o.ClientConnection.ContentType, "Content type of requests sent to apiserver.")
  49. genericfs.Float32Var(&o.ClientConnection.QPS, "kube-api-qps", o.ClientConnection.QPS, "QPS to use while talking with kubernetes apiserver.")
  50. genericfs.Int32Var(&o.ClientConnection.Burst, "kube-api-burst", o.ClientConnection.Burst, "Burst to use while talking with kubernetes apiserver.")
  51. genericfs.DurationVar(&o.ControllerStartInterval.Duration, "controller-start-interval", o.ControllerStartInterval.Duration, "Interval between starting controller managers.")
  52. genericfs.StringSliceVar(&o.Controllers, "controllers", o.Controllers, fmt.Sprintf(""+
  53. "A list of controllers to enable. '*' enables all on-by-default controllers, 'foo' enables the controller "+
  54. "named 'foo', '-foo' disables the controller named 'foo'.\nAll controllers: %s\nDisabled-by-default controllers: %s",
  55. strings.Join(allControllers, ", "), strings.Join(disabledByDefaultControllers, ", ")))
  56. leaderelectionconfig.BindFlags(&o.LeaderElection, genericfs)
  57. }
  58. // ApplyTo fills up generic config with options.
  59. func (o *GenericControllerManagerConfigurationOptions) ApplyTo(cfg *kubectrlmgrconfig.GenericControllerManagerConfiguration) error {
  60. if o == nil {
  61. return nil
  62. }
  63. if err := o.Debugging.ApplyTo(&cfg.Debugging); err != nil {
  64. return err
  65. }
  66. cfg.Port = o.Port
  67. cfg.Address = o.Address
  68. cfg.MinResyncPeriod = o.MinResyncPeriod
  69. cfg.ClientConnection = o.ClientConnection
  70. cfg.ControllerStartInterval = o.ControllerStartInterval
  71. cfg.LeaderElection = o.LeaderElection
  72. cfg.Controllers = o.Controllers
  73. return nil
  74. }
  75. // Validate checks validation of GenericOptions.
  76. func (o *GenericControllerManagerConfigurationOptions) Validate(allControllers []string, disabledByDefaultControllers []string) []error {
  77. if o == nil {
  78. return nil
  79. }
  80. errs := []error{}
  81. errs = append(errs, o.Debugging.Validate()...)
  82. allControllersSet := sets.NewString(allControllers...)
  83. for _, controller := range o.Controllers {
  84. if controller == "*" {
  85. continue
  86. }
  87. controller = strings.TrimPrefix(controller, "-")
  88. if !allControllersSet.Has(controller) {
  89. errs = append(errs, fmt.Errorf("%q is not in the list of known controllers", controller))
  90. }
  91. }
  92. return errs
  93. }