common.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. case *kubeadmapi.ClusterConfiguration:
  37. return MarshalClusterConfigurationToBytes(internalcfg, kubeadmapiv1beta2.SchemeGroupVersion)
  38. default:
  39. return kubeadmutil.MarshalToYamlForCodecs(obj, kubeadmapiv1beta2.SchemeGroupVersion, kubeadmscheme.Codecs)
  40. }
  41. }
  42. // validateSupportedVersion checks if the supplied GroupVersion is not on the lists of old unsupported or deprecated GVs.
  43. // If it is, an error is returned.
  44. func validateSupportedVersion(gv schema.GroupVersion, allowDeprecated bool) error {
  45. // The support matrix will look something like this now and in the future:
  46. // v1.10 and earlier: v1alpha1
  47. // v1.11: v1alpha1 read-only, writes only v1alpha2 config
  48. // v1.12: v1alpha2 read-only, writes only v1alpha3 config. Errors if the user tries to use v1alpha1
  49. // v1.13: v1alpha3 read-only, writes only v1beta1 config. Errors if the user tries to use v1alpha1 or v1alpha2
  50. // v1.14: v1alpha3 convert only, writes only v1beta1 config. Errors if the user tries to use v1alpha1 or v1alpha2
  51. // v1.15: v1beta1 read-only, writes only v1beta2 config. Errors if the user tries to use v1alpha1, v1alpha2 or v1alpha3
  52. oldKnownAPIVersions := map[string]string{
  53. "kubeadm.k8s.io/v1alpha1": "v1.11",
  54. "kubeadm.k8s.io/v1alpha2": "v1.12",
  55. "kubeadm.k8s.io/v1alpha3": "v1.14",
  56. }
  57. // Deprecated API versions are supported by us, but can only be used for migration.
  58. deprecatedAPIVersions := map[string]struct{}{}
  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. return errors.Errorf("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.String())
  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. if !ip.IsGlobalUnicast() {
  110. return errors.Errorf("cannot use %q as the bind address for the API Server", address)
  111. }
  112. return nil
  113. }
  114. // ChooseAPIServerBindAddress is a wrapper for netutil.ChooseBindAddress that also handles
  115. // the case where no default routes were found and an IP for the API server could not be obatained.
  116. func ChooseAPIServerBindAddress(bindAddress net.IP) (net.IP, error) {
  117. ip, err := netutil.ChooseBindAddress(bindAddress)
  118. if err != nil {
  119. if netutil.IsNoRoutesError(err) {
  120. klog.Warningf("WARNING: could not obtain a bind address for the API Server: %v; using: %s", err, constants.DefaultAPIServerBindAddress)
  121. defaultIP := net.ParseIP(constants.DefaultAPIServerBindAddress)
  122. if defaultIP == nil {
  123. return nil, errors.Errorf("cannot parse default IP address: %s", constants.DefaultAPIServerBindAddress)
  124. }
  125. return defaultIP, nil
  126. }
  127. return nil, err
  128. }
  129. if bindAddress != nil && !bindAddress.IsUnspecified() && !reflect.DeepEqual(ip, bindAddress) {
  130. klog.Warningf("WARNING: overriding requested API server bind address: requested %q, actual %q", bindAddress, ip)
  131. }
  132. return ip, nil
  133. }
  134. // MigrateOldConfig migrates an old configuration from a byte slice into a new one (returned again as a byte slice).
  135. // Only kubeadm kinds are migrated. Others are silently ignored.
  136. func MigrateOldConfig(oldConfig []byte) ([]byte, error) {
  137. newConfig := [][]byte{}
  138. gvkmap, err := kubeadmutil.SplitYAMLDocuments(oldConfig)
  139. if err != nil {
  140. return []byte{}, err
  141. }
  142. gvks := []schema.GroupVersionKind{}
  143. for gvk := range gvkmap {
  144. gvks = append(gvks, gvk)
  145. }
  146. // Migrate InitConfiguration and ClusterConfiguration if there are any in the config
  147. if kubeadmutil.GroupVersionKindsHasInitConfiguration(gvks...) || kubeadmutil.GroupVersionKindsHasClusterConfiguration(gvks...) {
  148. o, err := documentMapToInitConfiguration(gvkmap, true)
  149. if err != nil {
  150. return []byte{}, err
  151. }
  152. b, err := MarshalKubeadmConfigObject(o)
  153. if err != nil {
  154. return []byte{}, err
  155. }
  156. newConfig = append(newConfig, b)
  157. }
  158. // Migrate JoinConfiguration if there is any
  159. if kubeadmutil.GroupVersionKindsHasJoinConfiguration(gvks...) {
  160. o, err := documentMapToJoinConfiguration(gvkmap, true)
  161. if err != nil {
  162. return []byte{}, err
  163. }
  164. b, err := MarshalKubeadmConfigObject(o)
  165. if err != nil {
  166. return []byte{}, err
  167. }
  168. newConfig = append(newConfig, b)
  169. }
  170. return bytes.Join(newConfig, []byte(constants.YAMLDocumentSeparator)), nil
  171. }