validation.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Copyright 2017 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 validation
  14. import (
  15. "fmt"
  16. "time"
  17. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  18. utilvalidation "k8s.io/apimachinery/pkg/util/validation"
  19. utilfeature "k8s.io/apiserver/pkg/util/feature"
  20. "k8s.io/kubernetes/pkg/features"
  21. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  22. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  23. )
  24. // ValidateKubeletConfiguration validates `kc` and returns an error if it is invalid
  25. func ValidateKubeletConfiguration(kc *kubeletconfig.KubeletConfiguration) error {
  26. allErrors := []error{}
  27. // Make a local copy of the global feature gates and combine it with the gates set by this configuration.
  28. // This allows us to validate the config against the set of gates it will actually run against.
  29. localFeatureGate := utilfeature.DefaultFeatureGate.DeepCopy()
  30. if err := localFeatureGate.SetFromMap(kc.FeatureGates); err != nil {
  31. return err
  32. }
  33. if kc.NodeLeaseDurationSeconds <= 0 {
  34. allErrors = append(allErrors, fmt.Errorf("invalid configuration: NodeLeaseDurationSeconds must be greater than 0"))
  35. }
  36. if !kc.CgroupsPerQOS && len(kc.EnforceNodeAllocatable) > 0 {
  37. allErrors = append(allErrors, fmt.Errorf("invalid configuration: EnforceNodeAllocatable (--enforce-node-allocatable) is not supported unless CgroupsPerQOS (--cgroups-per-qos) feature is turned on"))
  38. }
  39. if kc.SystemCgroups != "" && kc.CgroupRoot == "" {
  40. allErrors = append(allErrors, fmt.Errorf("invalid configuration: SystemCgroups (--system-cgroups) was specified and CgroupRoot (--cgroup-root) was not specified"))
  41. }
  42. if kc.EventBurst < 0 {
  43. allErrors = append(allErrors, fmt.Errorf("invalid configuration: EventBurst (--event-burst) %v must not be a negative number", kc.EventBurst))
  44. }
  45. if kc.EventRecordQPS < 0 {
  46. allErrors = append(allErrors, fmt.Errorf("invalid configuration: EventRecordQPS (--event-qps) %v must not be a negative number", kc.EventRecordQPS))
  47. }
  48. if kc.HealthzPort != 0 && utilvalidation.IsValidPortNum(int(kc.HealthzPort)) != nil {
  49. allErrors = append(allErrors, fmt.Errorf("invalid configuration: HealthzPort (--healthz-port) %v must be between 1 and 65535, inclusive", kc.HealthzPort))
  50. }
  51. if localFeatureGate.Enabled(features.CPUCFSQuotaPeriod) && utilvalidation.IsInRange(int(kc.CPUCFSQuotaPeriod.Duration), int(1*time.Microsecond), int(time.Second)) != nil {
  52. allErrors = append(allErrors, fmt.Errorf("invalid configuration: CPUCFSQuotaPeriod (--cpu-cfs-quota-period) %v must be between 1usec and 1sec, inclusive", kc.CPUCFSQuotaPeriod))
  53. }
  54. if utilvalidation.IsInRange(int(kc.ImageGCHighThresholdPercent), 0, 100) != nil {
  55. allErrors = append(allErrors, fmt.Errorf("invalid configuration: ImageGCHighThresholdPercent (--image-gc-high-threshold) %v must be between 0 and 100, inclusive", kc.ImageGCHighThresholdPercent))
  56. }
  57. if utilvalidation.IsInRange(int(kc.ImageGCLowThresholdPercent), 0, 100) != nil {
  58. allErrors = append(allErrors, fmt.Errorf("invalid configuration: ImageGCLowThresholdPercent (--image-gc-low-threshold) %v must be between 0 and 100, inclusive", kc.ImageGCLowThresholdPercent))
  59. }
  60. if kc.ImageGCLowThresholdPercent >= kc.ImageGCHighThresholdPercent {
  61. allErrors = append(allErrors, fmt.Errorf("invalid configuration: ImageGCLowThresholdPercent (--image-gc-low-threshold) %v must be less than ImageGCHighThresholdPercent (--image-gc-high-threshold) %v", kc.ImageGCLowThresholdPercent, kc.ImageGCHighThresholdPercent))
  62. }
  63. if utilvalidation.IsInRange(int(kc.IPTablesDropBit), 0, 31) != nil {
  64. allErrors = append(allErrors, fmt.Errorf("invalid configuration: IPTablesDropBit (--iptables-drop-bit) %v must be between 0 and 31, inclusive", kc.IPTablesDropBit))
  65. }
  66. if utilvalidation.IsInRange(int(kc.IPTablesMasqueradeBit), 0, 31) != nil {
  67. allErrors = append(allErrors, fmt.Errorf("invalid configuration: IPTablesMasqueradeBit (--iptables-masquerade-bit) %v must be between 0 and 31, inclusive", kc.IPTablesMasqueradeBit))
  68. }
  69. if kc.KubeAPIBurst < 0 {
  70. allErrors = append(allErrors, fmt.Errorf("invalid configuration: KubeAPIBurst (--kube-api-burst) %v must not be a negative number", kc.KubeAPIBurst))
  71. }
  72. if kc.KubeAPIQPS < 0 {
  73. allErrors = append(allErrors, fmt.Errorf("invalid configuration: KubeAPIQPS (--kube-api-qps) %v must not be a negative number", kc.KubeAPIQPS))
  74. }
  75. if kc.MaxOpenFiles < 0 {
  76. allErrors = append(allErrors, fmt.Errorf("invalid configuration: MaxOpenFiles (--max-open-files) %v must not be a negative number", kc.MaxOpenFiles))
  77. }
  78. if kc.MaxPods < 0 {
  79. allErrors = append(allErrors, fmt.Errorf("invalid configuration: MaxPods (--max-pods) %v must not be a negative number", kc.MaxPods))
  80. }
  81. if utilvalidation.IsInRange(int(kc.OOMScoreAdj), -1000, 1000) != nil {
  82. allErrors = append(allErrors, fmt.Errorf("invalid configuration: OOMScoreAdj (--oom-score-adj) %v must be between -1000 and 1000, inclusive", kc.OOMScoreAdj))
  83. }
  84. if kc.PodsPerCore < 0 {
  85. allErrors = append(allErrors, fmt.Errorf("invalid configuration: PodsPerCore (--pods-per-core) %v must not be a negative number", kc.PodsPerCore))
  86. }
  87. if utilvalidation.IsValidPortNum(int(kc.Port)) != nil {
  88. allErrors = append(allErrors, fmt.Errorf("invalid configuration: Port (--port) %v must be between 1 and 65535, inclusive", kc.Port))
  89. }
  90. if kc.ReadOnlyPort != 0 && utilvalidation.IsValidPortNum(int(kc.ReadOnlyPort)) != nil {
  91. allErrors = append(allErrors, fmt.Errorf("invalid configuration: ReadOnlyPort (--read-only-port) %v must be between 0 and 65535, inclusive", kc.ReadOnlyPort))
  92. }
  93. if kc.RegistryBurst < 0 {
  94. allErrors = append(allErrors, fmt.Errorf("invalid configuration: RegistryBurst (--registry-burst) %v must not be a negative number", kc.RegistryBurst))
  95. }
  96. if kc.RegistryPullQPS < 0 {
  97. allErrors = append(allErrors, fmt.Errorf("invalid configuration: RegistryPullQPS (--registry-qps) %v must not be a negative number", kc.RegistryPullQPS))
  98. }
  99. if kc.RotateCertificates && !localFeatureGate.Enabled(features.RotateKubeletClientCertificate) {
  100. allErrors = append(allErrors, fmt.Errorf("invalid configuration: RotateCertificates %v requires feature gate RotateKubeletClientCertificate", kc.RotateCertificates))
  101. }
  102. if kc.ServerTLSBootstrap && !localFeatureGate.Enabled(features.RotateKubeletServerCertificate) {
  103. allErrors = append(allErrors, fmt.Errorf("invalid configuration: ServerTLSBootstrap %v requires feature gate RotateKubeletServerCertificate", kc.ServerTLSBootstrap))
  104. }
  105. for _, val := range kc.EnforceNodeAllocatable {
  106. switch val {
  107. case kubetypes.NodeAllocatableEnforcementKey:
  108. case kubetypes.SystemReservedEnforcementKey:
  109. if kc.SystemReservedCgroup == "" {
  110. allErrors = append(allErrors, fmt.Errorf("invalid configuration: systemReservedCgroup (--system-reserved-cgroup) must be specified when system-reserved contained in EnforceNodeAllocatable (--enforce-node-allocatable)"))
  111. }
  112. case kubetypes.KubeReservedEnforcementKey:
  113. if kc.KubeReservedCgroup == "" {
  114. allErrors = append(allErrors, fmt.Errorf("invalid configuration: kubeReservedCgroup (--kube-reserved-cgroup) must be specified when kube-reserved contained in EnforceNodeAllocatable (--enforce-node-allocatable)"))
  115. }
  116. case kubetypes.NodeAllocatableNoneKey:
  117. if len(kc.EnforceNodeAllocatable) > 1 {
  118. allErrors = append(allErrors, fmt.Errorf("invalid configuration: EnforceNodeAllocatable (--enforce-node-allocatable) may not contain additional enforcements when '%s' is specified", kubetypes.NodeAllocatableNoneKey))
  119. }
  120. default:
  121. allErrors = append(allErrors, fmt.Errorf("invalid configuration: option %q specified for EnforceNodeAllocatable (--enforce-node-allocatable). Valid options are %q, %q, %q, or %q",
  122. val, kubetypes.NodeAllocatableEnforcementKey, kubetypes.SystemReservedEnforcementKey, kubetypes.KubeReservedEnforcementKey, kubetypes.NodeAllocatableNoneKey))
  123. }
  124. }
  125. switch kc.HairpinMode {
  126. case kubeletconfig.HairpinNone:
  127. case kubeletconfig.HairpinVeth:
  128. case kubeletconfig.PromiscuousBridge:
  129. default:
  130. allErrors = append(allErrors, fmt.Errorf("invalid configuration: option %q specified for HairpinMode (--hairpin-mode). Valid options are %q, %q or %q",
  131. kc.HairpinMode, kubeletconfig.HairpinNone, kubeletconfig.HairpinVeth, kubeletconfig.PromiscuousBridge))
  132. }
  133. if err := validateKubeletOSConfiguration(kc); err != nil {
  134. allErrors = append(allErrors, err)
  135. }
  136. return utilerrors.NewAggregate(allErrors)
  137. }