features.go 5.2 KB

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