validation.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 options
  14. import (
  15. "errors"
  16. "fmt"
  17. "net"
  18. "strings"
  19. apiextensionsapiserver "k8s.io/apiextensions-apiserver/pkg/apiserver"
  20. utilfeature "k8s.io/apiserver/pkg/util/feature"
  21. "k8s.io/component-base/metrics"
  22. aggregatorscheme "k8s.io/kube-aggregator/pkg/apiserver/scheme"
  23. "k8s.io/kubernetes/pkg/api/legacyscheme"
  24. "k8s.io/kubernetes/pkg/features"
  25. netutils "k8s.io/utils/net"
  26. )
  27. // TODO: Longer term we should read this from some config store, rather than a flag.
  28. // validateClusterIPFlags is expected to be called after Complete()
  29. func validateClusterIPFlags(options *ServerRunOptions) []error {
  30. var errs []error
  31. // validate that primary has been processed by user provided values or it has been defaulted
  32. if options.PrimaryServiceClusterIPRange.IP == nil {
  33. errs = append(errs, errors.New("--service-cluster-ip-range must contain at least one valid cidr"))
  34. }
  35. serviceClusterIPRangeList := strings.Split(options.ServiceClusterIPRanges, ",")
  36. if len(serviceClusterIPRangeList) > 2 {
  37. errs = append(errs, errors.New("--service-cluster-ip-range must not contain more than two entries"))
  38. }
  39. // Complete() expected to have set Primary* and Secondary*
  40. // primary CIDR validation
  41. var ones, bits = options.PrimaryServiceClusterIPRange.Mask.Size()
  42. if bits-ones > 20 {
  43. errs = append(errs, errors.New("specified --service-cluster-ip-range is too large"))
  44. }
  45. // Secondary IP validation
  46. secondaryServiceClusterIPRangeUsed := (options.SecondaryServiceClusterIPRange.IP != nil)
  47. if secondaryServiceClusterIPRangeUsed && !utilfeature.DefaultFeatureGate.Enabled(features.IPv6DualStack) {
  48. errs = append(errs, fmt.Errorf("--secondary-service-cluster-ip-range can only be used if %v feature is enabled", string(features.IPv6DualStack)))
  49. }
  50. // note: While the cluster might be dualstack (i.e. pods with multiple IPs), the user may choose
  51. // to only ingress traffic within and into the cluster on one IP family only. this family is decided
  52. // by the range set on --service-cluster-ip-range. If/when the user decides to use dual stack services
  53. // the Secondary* must be of different IPFamily than --service-cluster-ip-range
  54. if secondaryServiceClusterIPRangeUsed {
  55. // Should be dualstack IPFamily(PrimaryServiceClusterIPRange) != IPFamily(SecondaryServiceClusterIPRange)
  56. dualstack, err := netutils.IsDualStackCIDRs([]*net.IPNet{&options.PrimaryServiceClusterIPRange, &options.SecondaryServiceClusterIPRange})
  57. if err != nil {
  58. errs = append(errs, errors.New("error attempting to validate dualstack for --service-cluster-ip-range and --secondary-service-cluster-ip-range"))
  59. }
  60. if !dualstack {
  61. errs = append(errs, errors.New("--service-cluster-ip-range and --secondary-service-cluster-ip-range must be of different IP family"))
  62. }
  63. // should be smallish sized cidr, this thing is kept in etcd
  64. // bigger cidr (specially those offered by IPv6) will add no value
  65. // significantly increase snapshotting time.
  66. var ones, bits = options.SecondaryServiceClusterIPRange.Mask.Size()
  67. if bits-ones > 20 {
  68. errs = append(errs, errors.New("specified --secondary-service-cluster-ip-range is too large"))
  69. }
  70. }
  71. return errs
  72. }
  73. func validateServiceNodePort(options *ServerRunOptions) []error {
  74. var errs []error
  75. if options.KubernetesServiceNodePort < 0 || options.KubernetesServiceNodePort > 65535 {
  76. errs = append(errs, fmt.Errorf("--kubernetes-service-node-port %v must be between 0 and 65535, inclusive. If 0, the Kubernetes master service will be of type ClusterIP", options.KubernetesServiceNodePort))
  77. }
  78. if options.KubernetesServiceNodePort > 0 && !options.ServiceNodePortRange.Contains(options.KubernetesServiceNodePort) {
  79. errs = append(errs, fmt.Errorf("kubernetes service port range %v doesn't contain %v", options.ServiceNodePortRange, (options.KubernetesServiceNodePort)))
  80. }
  81. return errs
  82. }
  83. func validateTokenRequest(options *ServerRunOptions) []error {
  84. var errs []error
  85. enableAttempted := options.ServiceAccountSigningKeyFile != "" ||
  86. options.Authentication.ServiceAccounts.Issuer != "" ||
  87. len(options.Authentication.APIAudiences) != 0
  88. enableSucceeded := options.ServiceAccountIssuer != nil
  89. if enableAttempted && !utilfeature.DefaultFeatureGate.Enabled(features.TokenRequest) {
  90. errs = append(errs, errors.New("the TokenRequest feature is not enabled but --service-account-signing-key-file, --service-account-issuer and/or --api-audiences flags were passed"))
  91. }
  92. if utilfeature.DefaultFeatureGate.Enabled(features.BoundServiceAccountTokenVolume) && !utilfeature.DefaultFeatureGate.Enabled(features.TokenRequest) {
  93. errs = append(errs, errors.New("the BoundServiceAccountTokenVolume feature depends on the TokenRequest feature, but the TokenRequest features is not enabled"))
  94. }
  95. if !enableAttempted && utilfeature.DefaultFeatureGate.Enabled(features.BoundServiceAccountTokenVolume) {
  96. errs = append(errs, errors.New("--service-account-signing-key-file and --service-account-issuer are required flags"))
  97. }
  98. if enableAttempted && !enableSucceeded {
  99. errs = append(errs, errors.New("--service-account-signing-key-file, --service-account-issuer, and --api-audiences should be specified together"))
  100. }
  101. return errs
  102. }
  103. // Validate checks ServerRunOptions and return a slice of found errs.
  104. func (s *ServerRunOptions) Validate() []error {
  105. var errs []error
  106. if s.MasterCount <= 0 {
  107. errs = append(errs, fmt.Errorf("--apiserver-count should be a positive number, but value '%d' provided", s.MasterCount))
  108. }
  109. errs = append(errs, s.Etcd.Validate()...)
  110. errs = append(errs, validateClusterIPFlags(s)...)
  111. errs = append(errs, validateServiceNodePort(s)...)
  112. errs = append(errs, s.SecureServing.Validate()...)
  113. errs = append(errs, s.Authentication.Validate()...)
  114. errs = append(errs, s.Authorization.Validate()...)
  115. errs = append(errs, s.Audit.Validate()...)
  116. errs = append(errs, s.Admission.Validate()...)
  117. errs = append(errs, s.InsecureServing.Validate()...)
  118. errs = append(errs, s.APIEnablement.Validate(legacyscheme.Scheme, apiextensionsapiserver.Scheme, aggregatorscheme.Scheme)...)
  119. errs = append(errs, validateTokenRequest(s)...)
  120. errs = append(errs, metrics.ValidateShowHiddenMetricsVersion(s.ShowHiddenMetricsForVersion)...)
  121. return errs
  122. }