features.go 5.3 KB

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