compute.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 upgrade
  14. import (
  15. "fmt"
  16. "strings"
  17. versionutil "k8s.io/apimachinery/pkg/util/version"
  18. clientset "k8s.io/client-go/kubernetes"
  19. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  20. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  21. "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns"
  22. etcdutil "k8s.io/kubernetes/cmd/kubeadm/app/util/etcd"
  23. )
  24. // Upgrade defines an upgrade possibility to upgrade from a current version to a new one
  25. type Upgrade struct {
  26. Description string
  27. Before ClusterState
  28. After ClusterState
  29. }
  30. // CanUpgradeKubelets returns whether an upgrade of any kubelet in the cluster is possible
  31. func (u *Upgrade) CanUpgradeKubelets() bool {
  32. // If there are multiple different versions now, an upgrade is possible (even if only for a subset of the nodes)
  33. if len(u.Before.KubeletVersions) > 1 {
  34. return true
  35. }
  36. // Don't report something available for upgrade if we don't know the current state
  37. if len(u.Before.KubeletVersions) == 0 {
  38. return false
  39. }
  40. // if the same version number existed both before and after, we don't have to upgrade it
  41. _, sameVersionFound := u.Before.KubeletVersions[u.After.KubeVersion]
  42. return !sameVersionFound
  43. }
  44. // CanUpgradeEtcd returns whether an upgrade of etcd is possible
  45. func (u *Upgrade) CanUpgradeEtcd() bool {
  46. return u.Before.EtcdVersion != u.After.EtcdVersion
  47. }
  48. // ClusterState describes the state of certain versions for a cluster
  49. type ClusterState struct {
  50. // KubeVersion describes the version of the Kubernetes API Server, Controller Manager, Scheduler and Proxy.
  51. KubeVersion string
  52. // DNSType describes the type of DNS add-on used in the cluster.
  53. DNSType kubeadmapi.DNSAddOnType
  54. // DNSVersion describes the version of the DNS add-on.
  55. DNSVersion string
  56. // KubeadmVersion describes the version of the kubeadm CLI
  57. KubeadmVersion string
  58. // KubeletVersions is a map with a version number linked to the amount of kubelets running that version in the cluster
  59. KubeletVersions map[string]uint16
  60. // EtcdVersion represents the version of etcd used in the cluster
  61. EtcdVersion string
  62. }
  63. // GetAvailableUpgrades fetches all versions from the specified VersionGetter and computes which
  64. // kinds of upgrades can be performed
  65. func GetAvailableUpgrades(versionGetterImpl VersionGetter, experimentalUpgradesAllowed, rcUpgradesAllowed bool, etcdClient etcdutil.ClusterInterrogator, dnsType kubeadmapi.DNSAddOnType, client clientset.Interface) ([]Upgrade, error) {
  66. fmt.Println("[upgrade] Fetching available versions to upgrade to")
  67. // Collect the upgrades kubeadm can do in this list
  68. upgrades := []Upgrade{}
  69. // Get the cluster version
  70. clusterVersionStr, clusterVersion, err := versionGetterImpl.ClusterVersion()
  71. if err != nil {
  72. return upgrades, err
  73. }
  74. // Get current kubeadm CLI version
  75. kubeadmVersionStr, kubeadmVersion, err := versionGetterImpl.KubeadmVersion()
  76. if err != nil {
  77. return upgrades, err
  78. }
  79. // Get and output the current latest stable version
  80. stableVersionStr, stableVersion, err := versionGetterImpl.VersionFromCILabel("stable", "stable version")
  81. if err != nil {
  82. fmt.Printf("[upgrade/versions] WARNING: %v\n", err)
  83. fmt.Println("[upgrade/versions] WARNING: Falling back to current kubeadm version as latest stable version")
  84. stableVersionStr, stableVersion = kubeadmVersionStr, kubeadmVersion
  85. }
  86. // Get the kubelet versions in the cluster
  87. kubeletVersions, err := versionGetterImpl.KubeletVersions()
  88. if err != nil {
  89. return upgrades, err
  90. }
  91. // Get current etcd version
  92. etcdVersion, err := etcdClient.GetVersion()
  93. if err != nil {
  94. return upgrades, err
  95. }
  96. currentDNSType, dnsVersion, err := dns.DeployedDNSAddon(client)
  97. if err != nil {
  98. return nil, err
  99. }
  100. // Construct a descriptor for the current state of the world
  101. beforeState := ClusterState{
  102. KubeVersion: clusterVersionStr,
  103. DNSType: currentDNSType,
  104. DNSVersion: dnsVersion,
  105. KubeadmVersion: kubeadmVersionStr,
  106. KubeletVersions: kubeletVersions,
  107. EtcdVersion: etcdVersion,
  108. }
  109. // Do a "dumb guess" that a new minor upgrade is available just because the latest stable version is higher than the cluster version
  110. // This guess will be corrected once we know if there is a patch version available
  111. canDoMinorUpgrade := clusterVersion.LessThan(stableVersion)
  112. // A patch version doesn't exist if the cluster version is higher than or equal to the current stable version
  113. // in the case that a user is trying to upgrade from, let's say, v1.8.0-beta.2 to v1.8.0-rc.1 (given we support such upgrades experimentally)
  114. // a stable-1.8 branch doesn't exist yet. Hence this check.
  115. if patchVersionBranchExists(clusterVersion, stableVersion) {
  116. currentBranch := getBranchFromVersion(clusterVersionStr)
  117. versionLabel := fmt.Sprintf("stable-%s", currentBranch)
  118. description := fmt.Sprintf("version in the v%s series", currentBranch)
  119. // Get and output the latest patch version for the cluster branch
  120. patchVersionStr, patchVersion, err := versionGetterImpl.VersionFromCILabel(versionLabel, description)
  121. if err != nil {
  122. fmt.Printf("[upgrade/versions] WARNING: %v\n", err)
  123. } else {
  124. // Check if a minor version upgrade is possible when a patch release exists
  125. // It's only possible if the latest patch version is higher than the current patch version
  126. // If that's the case, they must be on different branches => a newer minor version can be upgraded to
  127. canDoMinorUpgrade = minorUpgradePossibleWithPatchRelease(stableVersion, patchVersion)
  128. // If the cluster version is lower than the newest patch version, we should inform about the possible upgrade
  129. if patchUpgradePossible(clusterVersion, patchVersion) {
  130. // The kubeadm version has to be upgraded to the latest patch version
  131. newKubeadmVer := patchVersionStr
  132. if kubeadmVersion.AtLeast(patchVersion) {
  133. // In this case, the kubeadm CLI version is new enough. Don't display an update suggestion for kubeadm by making .NewKubeadmVersion equal .CurrentKubeadmVersion
  134. newKubeadmVer = kubeadmVersionStr
  135. }
  136. upgrades = append(upgrades, Upgrade{
  137. Description: description,
  138. Before: beforeState,
  139. After: ClusterState{
  140. KubeVersion: patchVersionStr,
  141. DNSType: dnsType,
  142. DNSVersion: kubeadmconstants.GetDNSVersion(dnsType),
  143. KubeadmVersion: newKubeadmVer,
  144. EtcdVersion: getSuggestedEtcdVersion(patchVersionStr),
  145. // KubeletVersions is unset here as it is not used anywhere in .After
  146. },
  147. })
  148. }
  149. }
  150. }
  151. if canDoMinorUpgrade {
  152. upgrades = append(upgrades, Upgrade{
  153. Description: "stable version",
  154. Before: beforeState,
  155. After: ClusterState{
  156. KubeVersion: stableVersionStr,
  157. DNSType: dnsType,
  158. DNSVersion: kubeadmconstants.GetDNSVersion(dnsType),
  159. KubeadmVersion: stableVersionStr,
  160. EtcdVersion: getSuggestedEtcdVersion(stableVersionStr),
  161. // KubeletVersions is unset here as it is not used anywhere in .After
  162. },
  163. })
  164. }
  165. if experimentalUpgradesAllowed || rcUpgradesAllowed {
  166. // dl.k8s.io/release/latest.txt is ALWAYS an alpha.X version
  167. // dl.k8s.io/release/latest-1.X.txt is first v1.X.0-alpha.0 -> v1.X.0-alpha.Y, then v1.X.0-beta.0 to v1.X.0-beta.Z, then v1.X.0-rc.1 to v1.X.0-rc.W.
  168. // After the v1.X.0 release, latest-1.X.txt is always a beta.0 version. Let's say the latest stable version on the v1.7 branch is v1.7.3, then the
  169. // latest-1.7 version is v1.7.4-beta.0
  170. // Worth noticing is that when the release-1.X branch is cut; there are two versions tagged: v1.X.0-beta.0 AND v1.(X+1).alpha.0
  171. // The v1.(X+1).alpha.0 is pretty much useless and should just be ignored, as more betas may be released that have more features than the initial v1.(X+1).alpha.0
  172. // So what we do below is getting the latest overall version, always an v1.X.0-alpha.Y version. Then we get latest-1.(X-1) version. This version may be anything
  173. // between v1.(X-1).0-beta.0 and v1.(X-1).Z-beta.0. At some point in time, latest-1.(X-1) will point to v1.(X-1).0-rc.1. Then we should show it.
  174. // The flow looks like this (with time on the X axis):
  175. // v1.8.0-alpha.1 -> v1.8.0-alpha.2 -> v1.8.0-alpha.3 | release-1.8 branch | v1.8.0-beta.0 -> v1.8.0-beta.1 -> v1.8.0-beta.2 -> v1.8.0-rc.1 -> v1.8.0 -> v1.8.1
  176. // v1.9.0-alpha.0 -> v1.9.0-alpha.1 -> v1.9.0-alpha.2
  177. // Get and output the current latest unstable version
  178. latestVersionStr, latestVersion, err := versionGetterImpl.VersionFromCILabel("latest", "experimental version")
  179. if err != nil {
  180. return upgrades, err
  181. }
  182. minorUnstable := latestVersion.Components()[1]
  183. // Get and output the current latest unstable version
  184. previousBranch := fmt.Sprintf("latest-1.%d", minorUnstable-1)
  185. previousBranchLatestVersionStr, previousBranchLatestVersion, err := versionGetterImpl.VersionFromCILabel(previousBranch, "")
  186. if err != nil {
  187. return upgrades, err
  188. }
  189. // If that previous latest version is an RC, RCs are allowed and the cluster version is lower than the RC version, show the upgrade
  190. if rcUpgradesAllowed && rcUpgradePossible(clusterVersion, previousBranchLatestVersion) {
  191. upgrades = append(upgrades, Upgrade{
  192. Description: "release candidate version",
  193. Before: beforeState,
  194. After: ClusterState{
  195. KubeVersion: previousBranchLatestVersionStr,
  196. DNSType: dnsType,
  197. DNSVersion: kubeadmconstants.GetDNSVersion(dnsType),
  198. KubeadmVersion: previousBranchLatestVersionStr,
  199. EtcdVersion: getSuggestedEtcdVersion(previousBranchLatestVersionStr),
  200. // KubeletVersions is unset here as it is not used anywhere in .After
  201. },
  202. })
  203. }
  204. // Show the possibility if experimental upgrades are allowed
  205. if experimentalUpgradesAllowed && clusterVersion.LessThan(latestVersion) {
  206. // Default to assume that the experimental version to show is the unstable one
  207. unstableKubeVersion := latestVersionStr
  208. unstableKubeDNSVersion := kubeadmconstants.GetDNSVersion(dnsType)
  209. // Ẃe should not display alpha.0. The previous branch's beta/rc versions are more relevant due how the kube branching process works.
  210. if latestVersion.PreRelease() == "alpha.0" {
  211. unstableKubeVersion = previousBranchLatestVersionStr
  212. unstableKubeDNSVersion = kubeadmconstants.GetDNSVersion(dnsType)
  213. }
  214. upgrades = append(upgrades, Upgrade{
  215. Description: "experimental version",
  216. Before: beforeState,
  217. After: ClusterState{
  218. KubeVersion: unstableKubeVersion,
  219. DNSType: dnsType,
  220. DNSVersion: unstableKubeDNSVersion,
  221. KubeadmVersion: unstableKubeVersion,
  222. EtcdVersion: getSuggestedEtcdVersion(unstableKubeVersion),
  223. // KubeletVersions is unset here as it is not used anywhere in .After
  224. },
  225. })
  226. }
  227. }
  228. // Add a newline in the end of this output to leave some space to the next output section
  229. fmt.Println("")
  230. return upgrades, nil
  231. }
  232. func getBranchFromVersion(version string) string {
  233. v := versionutil.MustParseGeneric(version)
  234. return fmt.Sprintf("%d.%d", v.Major(), v.Minor())
  235. }
  236. func patchVersionBranchExists(clusterVersion, stableVersion *versionutil.Version) bool {
  237. return stableVersion.AtLeast(clusterVersion)
  238. }
  239. func patchUpgradePossible(clusterVersion, patchVersion *versionutil.Version) bool {
  240. return clusterVersion.LessThan(patchVersion)
  241. }
  242. func rcUpgradePossible(clusterVersion, previousBranchLatestVersion *versionutil.Version) bool {
  243. return strings.HasPrefix(previousBranchLatestVersion.PreRelease(), "rc") && clusterVersion.LessThan(previousBranchLatestVersion)
  244. }
  245. func minorUpgradePossibleWithPatchRelease(stableVersion, patchVersion *versionutil.Version) bool {
  246. return patchVersion.LessThan(stableVersion)
  247. }
  248. func getSuggestedEtcdVersion(kubernetesVersion string) string {
  249. etcdVersion, err := kubeadmconstants.EtcdSupportedVersion(kubernetesVersion)
  250. if err != nil {
  251. fmt.Printf("[upgrade/versions] WARNING: No recommended etcd for requested Kubernetes version (%s)\n", kubernetesVersion)
  252. return "N/A"
  253. }
  254. return etcdVersion.String()
  255. }