features.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 features
  14. import (
  15. "fmt"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "github.com/pkg/errors"
  20. "k8s.io/apimachinery/pkg/util/version"
  21. "k8s.io/component-base/featuregate"
  22. )
  23. const (
  24. // IPv6DualStack is expected to be alpha in v1.16
  25. IPv6DualStack = "IPv6DualStack"
  26. // PublicKeysECDSA is expected to be alpha in v1.19
  27. PublicKeysECDSA = "PublicKeysECDSA"
  28. )
  29. // InitFeatureGates are the default feature gates for the init command
  30. var InitFeatureGates = FeatureList{
  31. IPv6DualStack: {FeatureSpec: featuregate.FeatureSpec{Default: false, PreRelease: featuregate.Alpha}},
  32. PublicKeysECDSA: {FeatureSpec: featuregate.FeatureSpec{Default: false, PreRelease: featuregate.Alpha}},
  33. }
  34. // Feature represents a feature being gated
  35. type Feature struct {
  36. featuregate.FeatureSpec
  37. MinimumVersion *version.Version
  38. HiddenInHelpText bool
  39. DeprecationMessage string
  40. }
  41. // FeatureList represents a list of feature gates
  42. type FeatureList map[string]Feature
  43. // ValidateVersion ensures that a feature gate list is compatible with the chosen Kubernetes version
  44. func ValidateVersion(allFeatures FeatureList, requestedFeatures map[string]bool, requestedVersion string) error {
  45. if requestedVersion == "" {
  46. return nil
  47. }
  48. parsedExpVersion, err := version.ParseSemantic(requestedVersion)
  49. if err != nil {
  50. return errors.Wrapf(err, "error parsing version %s", requestedVersion)
  51. }
  52. for k := range requestedFeatures {
  53. if minVersion := allFeatures[k].MinimumVersion; minVersion != nil {
  54. if !parsedExpVersion.AtLeast(minVersion) {
  55. return errors.Errorf(
  56. "the requested Kubernetes version (%s) is incompatible with the %s feature gate, which needs %s as a minimum",
  57. requestedVersion, k, minVersion)
  58. }
  59. }
  60. }
  61. return nil
  62. }
  63. // Enabled indicates whether a feature name has been enabled
  64. func Enabled(featureList map[string]bool, featureName string) bool {
  65. if enabled, ok := featureList[string(featureName)]; ok {
  66. return enabled
  67. }
  68. return InitFeatureGates[string(featureName)].Default
  69. }
  70. // Supports indicates whether a feature name is supported on the given
  71. // feature set
  72. func Supports(featureList FeatureList, featureName string) bool {
  73. for k, v := range featureList {
  74. if featureName == string(k) {
  75. return v.PreRelease != featuregate.Deprecated
  76. }
  77. }
  78. return false
  79. }
  80. // Keys returns a slice of feature names for a given feature set
  81. func Keys(featureList FeatureList) []string {
  82. var list []string
  83. for k := range featureList {
  84. list = append(list, string(k))
  85. }
  86. return list
  87. }
  88. // KnownFeatures returns a slice of strings describing the FeatureList features.
  89. func KnownFeatures(f *FeatureList) []string {
  90. var known []string
  91. for k, v := range *f {
  92. if v.HiddenInHelpText {
  93. continue
  94. }
  95. pre := ""
  96. if v.PreRelease != featuregate.GA {
  97. pre = fmt.Sprintf("%s - ", v.PreRelease)
  98. }
  99. known = append(known, fmt.Sprintf("%s=true|false (%sdefault=%t)", k, pre, v.Default))
  100. }
  101. sort.Strings(known)
  102. return known
  103. }
  104. // NewFeatureGate parses a string of the form "key1=value1,key2=value2,..." into a
  105. // map[string]bool of known keys or returns an error.
  106. func NewFeatureGate(f *FeatureList, value string) (map[string]bool, error) {
  107. featureGate := map[string]bool{}
  108. for _, s := range strings.Split(value, ",") {
  109. if len(s) == 0 {
  110. continue
  111. }
  112. arr := strings.SplitN(s, "=", 2)
  113. if len(arr) != 2 {
  114. return nil, errors.Errorf("missing bool value for feature-gate key:%s", s)
  115. }
  116. k := strings.TrimSpace(arr[0])
  117. v := strings.TrimSpace(arr[1])
  118. featureSpec, ok := (*f)[k]
  119. if !ok {
  120. return nil, errors.Errorf("unrecognized feature-gate key: %s", k)
  121. }
  122. if featureSpec.PreRelease == featuregate.Deprecated {
  123. return nil, errors.Errorf("feature-gate key is deprecated: %s", k)
  124. }
  125. boolValue, err := strconv.ParseBool(v)
  126. if err != nil {
  127. return nil, errors.Errorf("invalid value %v for feature-gate key: %s, use true|false instead", v, k)
  128. }
  129. featureGate[k] = boolValue
  130. }
  131. return featureGate, nil
  132. }
  133. // CheckDeprecatedFlags takes a list of existing feature gate flags and validates against the current feature flag set.
  134. // It used during upgrades for ensuring consistency of feature gates used in an existing cluster, that might
  135. // be created with a previous version of kubeadm, with the set of features currently supported by kubeadm
  136. func CheckDeprecatedFlags(f *FeatureList, features map[string]bool) map[string]string {
  137. deprecatedMsg := map[string]string{}
  138. for k := range features {
  139. featureSpec, ok := (*f)[k]
  140. if !ok {
  141. // This case should never happen, it is implemented only as a sentinel
  142. // for removal of flags executed when flags are still in use (always before deprecate, then after one cycle remove)
  143. deprecatedMsg[k] = fmt.Sprintf("Unknown feature gate flag: %s", k)
  144. }
  145. if featureSpec.PreRelease == featuregate.Deprecated {
  146. if _, ok := deprecatedMsg[k]; !ok {
  147. deprecatedMsg[k] = featureSpec.DeprecationMessage
  148. }
  149. }
  150. }
  151. return deprecatedMsg
  152. }