common.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Copyright 2018 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 config
  14. import (
  15. "bytes"
  16. "net"
  17. "reflect"
  18. "strings"
  19. "github.com/pkg/errors"
  20. "k8s.io/klog"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/apimachinery/pkg/runtime/schema"
  23. netutil "k8s.io/apimachinery/pkg/util/net"
  24. "k8s.io/apimachinery/pkg/util/version"
  25. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  26. kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
  27. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  28. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  29. kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
  30. )
  31. // MarshalKubeadmConfigObject marshals an Object registered in the kubeadm scheme. If the object is a InitConfiguration or ClusterConfiguration, some extra logic is run
  32. func MarshalKubeadmConfigObject(obj runtime.Object) ([]byte, error) {
  33. switch internalcfg := obj.(type) {
  34. case *kubeadmapi.InitConfiguration:
  35. return MarshalInitConfigurationToBytes(internalcfg, kubeadmapiv1beta2.SchemeGroupVersion)
  36. default:
  37. return kubeadmutil.MarshalToYamlForCodecs(obj, kubeadmapiv1beta2.SchemeGroupVersion, kubeadmscheme.Codecs)
  38. }
  39. }
  40. // validateSupportedVersion checks if the supplied GroupVersion is not on the lists of old unsupported or deprecated GVs.
  41. // If it is, an error is returned.
  42. func validateSupportedVersion(gv schema.GroupVersion, allowDeprecated bool) error {
  43. // The support matrix will look something like this now and in the future:
  44. // v1.10 and earlier: v1alpha1
  45. // v1.11: v1alpha1 read-only, writes only v1alpha2 config
  46. // v1.12: v1alpha2 read-only, writes only v1alpha3 config. Errors if the user tries to use v1alpha1
  47. // v1.13: v1alpha3 read-only, writes only v1beta1 config. Errors if the user tries to use v1alpha1 or v1alpha2
  48. // v1.14: v1alpha3 convert only, writes only v1beta1 config. Errors if the user tries to use v1alpha1 or v1alpha2
  49. // v1.15: v1beta1 read-only, writes only v1beta2 config. Errors if the user tries to use v1alpha1, v1alpha2 or v1alpha3
  50. oldKnownAPIVersions := map[string]string{
  51. "kubeadm.k8s.io/v1alpha1": "v1.11",
  52. "kubeadm.k8s.io/v1alpha2": "v1.12",
  53. "kubeadm.k8s.io/v1alpha3": "v1.14",
  54. }
  55. // Deprecated API versions are supported by us, but can only be used for migration.
  56. deprecatedAPIVersions := map[string]struct{}{
  57. "kubeadm.k8s.io/v1beta1": {},
  58. }
  59. gvString := gv.String()
  60. if useKubeadmVersion := oldKnownAPIVersions[gvString]; useKubeadmVersion != "" {
  61. return errors.Errorf("your configuration file uses an old API spec: %q. Please use kubeadm %s instead and run 'kubeadm config migrate --old-config old.yaml --new-config new.yaml', which will write the new, similar spec using a newer API version.", gv.String(), useKubeadmVersion)
  62. }
  63. if _, present := deprecatedAPIVersions[gvString]; present && !allowDeprecated {
  64. klog.Warningf("your configuration file uses a deprecated API spec: %q. Please use 'kubeadm config migrate --old-config old.yaml --new-config new.yaml', which will write the new, similar spec using a newer API version.", gv)
  65. }
  66. return nil
  67. }
  68. // NormalizeKubernetesVersion resolves version labels, sets alternative
  69. // image registry if requested for CI builds, and validates minimal
  70. // version that kubeadm SetInitDynamicDefaultssupports.
  71. func NormalizeKubernetesVersion(cfg *kubeadmapi.ClusterConfiguration) error {
  72. // Requested version is automatic CI build, thus use KubernetesCI Image Repository for core images
  73. if kubeadmutil.KubernetesIsCIVersion(cfg.KubernetesVersion) {
  74. cfg.CIImageRepository = constants.DefaultCIImageRepository
  75. }
  76. // Parse and validate the version argument and resolve possible CI version labels
  77. ver, err := kubeadmutil.KubernetesReleaseVersion(cfg.KubernetesVersion)
  78. if err != nil {
  79. return err
  80. }
  81. cfg.KubernetesVersion = ver
  82. // Parse the given kubernetes version and make sure it's higher than the lowest supported
  83. k8sVersion, err := version.ParseSemantic(cfg.KubernetesVersion)
  84. if err != nil {
  85. return errors.Wrapf(err, "couldn't parse Kubernetes version %q", cfg.KubernetesVersion)
  86. }
  87. if k8sVersion.LessThan(constants.MinimumControlPlaneVersion) {
  88. return errors.Errorf("this version of kubeadm only supports deploying clusters with the control plane version >= %s. Current version: %s", constants.MinimumControlPlaneVersion.String(), cfg.KubernetesVersion)
  89. }
  90. return nil
  91. }
  92. // LowercaseSANs can be used to force all SANs to be lowercase so it passes IsDNS1123Subdomain
  93. func LowercaseSANs(sans []string) {
  94. for i, san := range sans {
  95. lowercase := strings.ToLower(san)
  96. if lowercase != san {
  97. klog.V(1).Infof("lowercasing SAN %q to %q", san, lowercase)
  98. sans[i] = lowercase
  99. }
  100. }
  101. }
  102. // VerifyAPIServerBindAddress can be used to verify if a bind address for the API Server is 0.0.0.0,
  103. // in which case this address is not valid and should not be used.
  104. func VerifyAPIServerBindAddress(address string) error {
  105. ip := net.ParseIP(address)
  106. if ip == nil {
  107. return errors.Errorf("cannot parse IP address: %s", address)
  108. }
  109. // There are users with network setups where default routes are present, but network interfaces
  110. // use only link-local addresses (e.g. as described in RFC5549).
  111. // In many cases that matching global unicast IP address can be found on loopback interface,
  112. // so kubeadm allows users to specify address=Loopback for handling supporting the scenario above.
  113. // Nb. SetAPIEndpointDynamicDefaults will try to translate loopback to a valid address afterwards
  114. if ip.IsLoopback() {
  115. return nil
  116. }
  117. if !ip.IsGlobalUnicast() {
  118. return errors.Errorf("cannot use %q as the bind address for the API Server", address)
  119. }
  120. return nil
  121. }
  122. // ChooseAPIServerBindAddress is a wrapper for netutil.ResolveBindAddress that also handles
  123. // the case where no default routes were found and an IP for the API server could not be obtained.
  124. func ChooseAPIServerBindAddress(bindAddress net.IP) (net.IP, error) {
  125. ip, err := netutil.ResolveBindAddress(bindAddress)
  126. if err != nil {
  127. if netutil.IsNoRoutesError(err) {
  128. klog.Warningf("WARNING: could not obtain a bind address for the API Server: %v; using: %s", err, constants.DefaultAPIServerBindAddress)
  129. defaultIP := net.ParseIP(constants.DefaultAPIServerBindAddress)
  130. if defaultIP == nil {
  131. return nil, errors.Errorf("cannot parse default IP address: %s", constants.DefaultAPIServerBindAddress)
  132. }
  133. return defaultIP, nil
  134. }
  135. return nil, err
  136. }
  137. if bindAddress != nil && !bindAddress.IsUnspecified() && !reflect.DeepEqual(ip, bindAddress) {
  138. klog.Warningf("WARNING: overriding requested API server bind address: requested %q, actual %q", bindAddress, ip)
  139. }
  140. return ip, nil
  141. }
  142. // MigrateOldConfig migrates an old configuration from a byte slice into a new one (returned again as a byte slice).
  143. // Only kubeadm kinds are migrated. Others are silently ignored.
  144. func MigrateOldConfig(oldConfig []byte) ([]byte, error) {
  145. newConfig := [][]byte{}
  146. gvkmap, err := kubeadmutil.SplitYAMLDocuments(oldConfig)
  147. if err != nil {
  148. return []byte{}, err
  149. }
  150. gvks := []schema.GroupVersionKind{}
  151. for gvk := range gvkmap {
  152. gvks = append(gvks, gvk)
  153. }
  154. // Migrate InitConfiguration and ClusterConfiguration if there are any in the config
  155. if kubeadmutil.GroupVersionKindsHasInitConfiguration(gvks...) || kubeadmutil.GroupVersionKindsHasClusterConfiguration(gvks...) {
  156. o, err := documentMapToInitConfiguration(gvkmap, true)
  157. if err != nil {
  158. return []byte{}, err
  159. }
  160. b, err := MarshalKubeadmConfigObject(o)
  161. if err != nil {
  162. return []byte{}, err
  163. }
  164. newConfig = append(newConfig, b)
  165. }
  166. // Migrate JoinConfiguration if there is any
  167. if kubeadmutil.GroupVersionKindsHasJoinConfiguration(gvks...) {
  168. o, err := documentMapToJoinConfiguration(gvkmap, true)
  169. if err != nil {
  170. return []byte{}, err
  171. }
  172. b, err := MarshalKubeadmConfigObject(o)
  173. if err != nil {
  174. return []byte{}, err
  175. }
  176. newConfig = append(newConfig, b)
  177. }
  178. return bytes.Join(newConfig, []byte(constants.YAMLDocumentSeparator)), nil
  179. }