validation.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. apiextensionsapiserver "k8s.io/apiextensions-apiserver/pkg/apiserver"
  18. utilfeature "k8s.io/apiserver/pkg/util/feature"
  19. aggregatorscheme "k8s.io/kube-aggregator/pkg/apiserver/scheme"
  20. "k8s.io/kubernetes/pkg/api/legacyscheme"
  21. "k8s.io/kubernetes/pkg/features"
  22. )
  23. // TODO: Longer term we should read this from some config store, rather than a flag.
  24. func validateClusterIPFlags(options *ServerRunOptions) []error {
  25. var errs []error
  26. if options.ServiceClusterIPRange.IP == nil {
  27. errs = append(errs, errors.New("no --service-cluster-ip-range specified"))
  28. }
  29. var ones, bits = options.ServiceClusterIPRange.Mask.Size()
  30. if bits-ones > 20 {
  31. errs = append(errs, errors.New("specified --service-cluster-ip-range is too large"))
  32. }
  33. return errs
  34. }
  35. func validateServiceNodePort(options *ServerRunOptions) []error {
  36. var errs []error
  37. if options.KubernetesServiceNodePort < 0 || options.KubernetesServiceNodePort > 65535 {
  38. 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))
  39. }
  40. if options.KubernetesServiceNodePort > 0 && !options.ServiceNodePortRange.Contains(options.KubernetesServiceNodePort) {
  41. errs = append(errs, fmt.Errorf("kubernetes service port range %v doesn't contain %v", options.ServiceNodePortRange, (options.KubernetesServiceNodePort)))
  42. }
  43. return errs
  44. }
  45. func validateTokenRequest(options *ServerRunOptions) []error {
  46. var errs []error
  47. enableAttempted := options.ServiceAccountSigningKeyFile != "" ||
  48. options.Authentication.ServiceAccounts.Issuer != "" ||
  49. len(options.Authentication.APIAudiences) != 0
  50. enableSucceeded := options.ServiceAccountIssuer != nil
  51. if enableAttempted && !utilfeature.DefaultFeatureGate.Enabled(features.TokenRequest) {
  52. 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"))
  53. }
  54. if utilfeature.DefaultFeatureGate.Enabled(features.BoundServiceAccountTokenVolume) && !utilfeature.DefaultFeatureGate.Enabled(features.TokenRequest) {
  55. errs = append(errs, errors.New("the BoundServiceAccountTokenVolume feature depends on the TokenRequest feature, but the TokenRequest features is not enabled"))
  56. }
  57. if !enableAttempted && utilfeature.DefaultFeatureGate.Enabled(features.BoundServiceAccountTokenVolume) {
  58. errs = append(errs, errors.New("--service-account-signing-key-file and --service-account-issuer are required flags"))
  59. }
  60. if enableAttempted && !enableSucceeded {
  61. errs = append(errs, errors.New("--service-account-signing-key-file, --service-account-issuer, and --api-audiences should be specified together"))
  62. }
  63. return errs
  64. }
  65. // Validate checks ServerRunOptions and return a slice of found errs.
  66. func (s *ServerRunOptions) Validate() []error {
  67. var errs []error
  68. if s.MasterCount <= 0 {
  69. errs = append(errs, fmt.Errorf("--apiserver-count should be a positive number, but value '%d' provided", s.MasterCount))
  70. }
  71. errs = append(errs, s.Etcd.Validate()...)
  72. errs = append(errs, validateClusterIPFlags(s)...)
  73. errs = append(errs, validateServiceNodePort(s)...)
  74. errs = append(errs, s.SecureServing.Validate()...)
  75. errs = append(errs, s.Authentication.Validate()...)
  76. errs = append(errs, s.Authorization.Validate()...)
  77. errs = append(errs, s.Audit.Validate()...)
  78. errs = append(errs, s.Admission.Validate()...)
  79. errs = append(errs, s.InsecureServing.Validate()...)
  80. errs = append(errs, s.APIEnablement.Validate(legacyscheme.Scheme, apiextensionsapiserver.Scheme, aggregatorscheme.Scheme)...)
  81. errs = append(errs, validateTokenRequest(s)...)
  82. return errs
  83. }