insecure_serving.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "net"
  17. "strconv"
  18. "github.com/spf13/pflag"
  19. apiserveroptions "k8s.io/apiserver/pkg/server/options"
  20. schedulerappconfig "k8s.io/kubernetes/cmd/kube-scheduler/app/config"
  21. kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
  22. )
  23. // CombinedInsecureServingOptions sets up to two insecure listeners for healthz and metrics. The flags
  24. // override the ComponentConfig and DeprecatedInsecureServingOptions values for both.
  25. type CombinedInsecureServingOptions struct {
  26. Healthz *apiserveroptions.DeprecatedInsecureServingOptionsWithLoopback
  27. Metrics *apiserveroptions.DeprecatedInsecureServingOptionsWithLoopback
  28. BindPort int // overrides the structs above on ApplyTo, ignored on ApplyToFromLoadedConfig
  29. BindAddress string // overrides the structs above on ApplyTo, ignored on ApplyToFromLoadedConfig
  30. }
  31. // AddFlags adds flags for the insecure serving options.
  32. func (o *CombinedInsecureServingOptions) AddFlags(fs *pflag.FlagSet) {
  33. if o == nil {
  34. return
  35. }
  36. fs.StringVar(&o.BindAddress, "address", o.BindAddress, "DEPRECATED: the IP address on which to listen for the --port port (set to 0.0.0.0 for all IPv4 interfaces and :: for all IPv6 interfaces). See --bind-address instead.")
  37. // MarkDeprecated hides the flag from the help. We don't want that:
  38. // fs.MarkDeprecated("address", "see --bind-address instead.")
  39. fs.IntVar(&o.BindPort, "port", o.BindPort, "DEPRECATED: the port on which to serve HTTP insecurely without authentication and authorization. If 0, don't serve HTTPS at all. See --secure-port instead.")
  40. // MarkDeprecated hides the flag from the help. We don't want that:
  41. // fs.MarkDeprecated("port", "see --secure-port instead.")
  42. }
  43. func (o *CombinedInsecureServingOptions) applyTo(c *schedulerappconfig.Config, componentConfig *kubeschedulerconfig.KubeSchedulerConfiguration) error {
  44. if err := updateAddressFromDeprecatedInsecureServingOptions(&componentConfig.HealthzBindAddress, o.Healthz); err != nil {
  45. return err
  46. }
  47. if err := updateAddressFromDeprecatedInsecureServingOptions(&componentConfig.MetricsBindAddress, o.Metrics); err != nil {
  48. return err
  49. }
  50. if err := o.Healthz.ApplyTo(&c.InsecureServing, &c.LoopbackClientConfig); err != nil {
  51. return err
  52. }
  53. if o.Metrics != nil && (c.ComponentConfig.MetricsBindAddress != c.ComponentConfig.HealthzBindAddress || o.Healthz == nil) {
  54. if err := o.Metrics.ApplyTo(&c.InsecureMetricsServing, &c.LoopbackClientConfig); err != nil {
  55. return err
  56. }
  57. }
  58. return nil
  59. }
  60. // ApplyTo applies the insecure serving options to the given scheduler app configuration, and updates the componentConfig.
  61. func (o *CombinedInsecureServingOptions) ApplyTo(c *schedulerappconfig.Config, componentConfig *kubeschedulerconfig.KubeSchedulerConfiguration) error {
  62. if o == nil {
  63. componentConfig.HealthzBindAddress = ""
  64. componentConfig.MetricsBindAddress = ""
  65. return nil
  66. }
  67. if o.Healthz != nil {
  68. o.Healthz.BindPort = o.BindPort
  69. o.Healthz.BindAddress = net.ParseIP(o.BindAddress)
  70. }
  71. if o.Metrics != nil {
  72. o.Metrics.BindPort = o.BindPort
  73. o.Metrics.BindAddress = net.ParseIP(o.BindAddress)
  74. }
  75. return o.applyTo(c, componentConfig)
  76. }
  77. // ApplyToFromLoadedConfig updates the insecure serving options from the component config and then appies it to the given scheduler app configuration.
  78. func (o *CombinedInsecureServingOptions) ApplyToFromLoadedConfig(c *schedulerappconfig.Config, componentConfig *kubeschedulerconfig.KubeSchedulerConfiguration) error {
  79. if o == nil {
  80. return nil
  81. }
  82. if err := updateDeprecatedInsecureServingOptionsFromAddress(o.Healthz, componentConfig.HealthzBindAddress); err != nil {
  83. return fmt.Errorf("invalid healthz address: %v", err)
  84. }
  85. if err := updateDeprecatedInsecureServingOptionsFromAddress(o.Metrics, componentConfig.MetricsBindAddress); err != nil {
  86. return fmt.Errorf("invalid metrics address: %v", err)
  87. }
  88. return o.applyTo(c, componentConfig)
  89. }
  90. func updateAddressFromDeprecatedInsecureServingOptions(addr *string, is *apiserveroptions.DeprecatedInsecureServingOptionsWithLoopback) error {
  91. if is == nil {
  92. *addr = ""
  93. } else {
  94. if is.Listener != nil {
  95. *addr = is.Listener.Addr().String()
  96. } else if is.BindPort == 0 {
  97. *addr = ""
  98. } else {
  99. *addr = net.JoinHostPort(is.BindAddress.String(), strconv.Itoa(is.BindPort))
  100. }
  101. }
  102. return nil
  103. }
  104. func updateDeprecatedInsecureServingOptionsFromAddress(is *apiserveroptions.DeprecatedInsecureServingOptionsWithLoopback, addr string) error {
  105. if is == nil {
  106. return nil
  107. }
  108. if len(addr) == 0 {
  109. is.BindPort = 0
  110. return nil
  111. }
  112. host, portInt, err := splitHostIntPort(addr)
  113. if err != nil {
  114. return fmt.Errorf("invalid address %q", addr)
  115. }
  116. is.BindAddress = net.ParseIP(host)
  117. is.BindPort = portInt
  118. return nil
  119. }
  120. // Validate validates the insecure serving options.
  121. func (o *CombinedInsecureServingOptions) Validate() []error {
  122. if o == nil {
  123. return nil
  124. }
  125. errors := []error{}
  126. if o.BindPort < 0 || o.BindPort > 65335 {
  127. errors = append(errors, fmt.Errorf("--port %v must be between 0 and 65335, inclusive. 0 for turning off insecure (HTTP) port", o.BindPort))
  128. }
  129. if len(o.BindAddress) > 0 && net.ParseIP(o.BindAddress) == nil {
  130. errors = append(errors, fmt.Errorf("--address %v is an invalid IP address", o.BindAddress))
  131. }
  132. return errors
  133. }