compute.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. fmt.Printf("[upgrade/versions] Cluster version: %s\n", clusterVersionStr)
  75. // Get current kubeadm CLI version
  76. kubeadmVersionStr, kubeadmVersion, err := versionGetterImpl.KubeadmVersion()
  77. if err != nil {
  78. return upgrades, err
  79. }
  80. fmt.Printf("[upgrade/versions] kubeadm version: %s\n", kubeadmVersionStr)
  81. // Get and output the current latest stable version
  82. stableVersionStr, stableVersion, err := versionGetterImpl.VersionFromCILabel("stable", "stable version")
  83. if err != nil {
  84. fmt.Printf("[upgrade/versions] WARNING: %v\n", err)
  85. fmt.Println("[upgrade/versions] WARNING: Falling back to current kubeadm version as latest stable version")
  86. stableVersionStr, stableVersion = kubeadmVersionStr, kubeadmVersion
  87. } else {
  88. fmt.Printf("[upgrade/versions] Latest %s: %s\n", "stable version", stableVersionStr)
  89. }
  90. // Get the kubelet versions in the cluster
  91. kubeletVersions, err := versionGetterImpl.KubeletVersions()
  92. if err != nil {
  93. return upgrades, err
  94. }
  95. // Get current etcd version
  96. etcdVersion, err := etcdClient.GetVersion()
  97. if err != nil {
  98. return upgrades, err
  99. }
  100. currentDNSType, dnsVersion, err := dns.DeployedDNSAddon(client)
  101. if err != nil {
  102. return nil, err
  103. }
  104. // Construct a descriptor for the current state of the world
  105. beforeState := ClusterState{
  106. KubeVersion: clusterVersionStr,
  107. DNSType: currentDNSType,
  108. DNSVersion: dnsVersion,
  109. KubeadmVersion: kubeadmVersionStr,
  110. KubeletVersions: kubeletVersions,
  111. EtcdVersion: etcdVersion,
  112. }
  113. // Do a "dumb guess" that a new minor upgrade is available just because the latest stable version is higher than the cluster version
  114. // This guess will be corrected once we know if there is a patch version available
  115. canDoMinorUpgrade := clusterVersion.LessThan(stableVersion)
  116. // A patch version doesn't exist if the cluster version is higher than or equal to the current stable version
  117. // 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)
  118. // a stable-1.8 branch doesn't exist yet. Hence this check.
  119. if patchVersionBranchExists(clusterVersion, stableVersion) {
  120. currentBranch := getBranchFromVersion(clusterVersionStr)
  121. versionLabel := fmt.Sprintf("stable-%s", currentBranch)
  122. description := fmt.Sprintf("version in the v%s series", currentBranch)
  123. // Get and output the latest patch version for the cluster branch
  124. patchVersionStr, patchVersion, err := versionGetterImpl.VersionFromCILabel(versionLabel, description)
  125. if err != nil {
  126. fmt.Printf("[upgrade/versions] WARNING: %v\n", err)
  127. } else {
  128. fmt.Printf("[upgrade/versions] Latest %s: %s\n", description, patchVersionStr)
  129. // Check if a minor version upgrade is possible when a patch release exists
  130. // It's only possible if the latest patch version is higher than the current patch version
  131. // If that's the case, they must be on different branches => a newer minor version can be upgraded to
  132. canDoMinorUpgrade = minorUpgradePossibleWithPatchRelease(stableVersion, patchVersion)
  133. // If the cluster version is lower than the newest patch version, we should inform about the possible upgrade
  134. if patchUpgradePossible(clusterVersion, patchVersion) {
  135. // The kubeadm version has to be upgraded to the latest patch version
  136. newKubeadmVer := patchVersionStr
  137. if kubeadmVersion.AtLeast(patchVersion) {
  138. // In this case, the kubeadm CLI version is new enough. Don't display an update suggestion for kubeadm by making .NewKubeadmVersion equal .CurrentKubeadmVersion
  139. newKubeadmVer = kubeadmVersionStr
  140. }
  141. upgrades = append(upgrades, Upgrade{
  142. Description: description,
  143. Before: beforeState,
  144. After: ClusterState{
  145. KubeVersion: patchVersionStr,
  146. DNSType: dnsType,
  147. DNSVersion: kubeadmconstants.GetDNSVersion(dnsType),
  148. KubeadmVersion: newKubeadmVer,
  149. EtcdVersion: getSuggestedEtcdVersion(patchVersionStr),
  150. // KubeletVersions is unset here as it is not used anywhere in .After
  151. },
  152. })
  153. }
  154. }
  155. }
  156. if canDoMinorUpgrade {
  157. upgrades = append(upgrades, Upgrade{
  158. Description: "stable version",
  159. Before: beforeState,
  160. After: ClusterState{
  161. KubeVersion: stableVersionStr,
  162. DNSType: dnsType,
  163. DNSVersion: kubeadmconstants.GetDNSVersion(dnsType),
  164. KubeadmVersion: stableVersionStr,
  165. EtcdVersion: getSuggestedEtcdVersion(stableVersionStr),
  166. // KubeletVersions is unset here as it is not used anywhere in .After
  167. },
  168. })
  169. }
  170. if experimentalUpgradesAllowed || rcUpgradesAllowed {
  171. // dl.k8s.io/release/latest.txt is ALWAYS an alpha.X version
  172. // 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.
  173. // 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
  174. // latest-1.7 version is v1.7.4-beta.0
  175. // 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
  176. // 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
  177. // 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
  178. // 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.
  179. // The flow looks like this (with time on the X axis):
  180. // 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
  181. // v1.9.0-alpha.0 -> v1.9.0-alpha.1 -> v1.9.0-alpha.2
  182. // Get and output the current latest unstable version
  183. latestVersionStr, latestVersion, err := versionGetterImpl.VersionFromCILabel("latest", "experimental version")
  184. if err != nil {
  185. return upgrades, err
  186. }
  187. fmt.Printf("[upgrade/versions] Latest %s: %s\n", "experimental version", latestVersionStr)
  188. minorUnstable := latestVersion.Components()[1]
  189. // Get and output the current latest unstable version
  190. previousBranch := fmt.Sprintf("latest-1.%d", minorUnstable-1)
  191. previousBranchLatestVersionStr, previousBranchLatestVersion, err := versionGetterImpl.VersionFromCILabel(previousBranch, "")
  192. if err != nil {
  193. return upgrades, err
  194. }
  195. fmt.Printf("[upgrade/versions] Latest %s: %s\n", "", previousBranchLatestVersionStr)
  196. // If that previous latest version is an RC, RCs are allowed and the cluster version is lower than the RC version, show the upgrade
  197. if rcUpgradesAllowed && rcUpgradePossible(clusterVersion, previousBranchLatestVersion) {
  198. upgrades = append(upgrades, Upgrade{
  199. Description: "release candidate version",
  200. Before: beforeState,
  201. After: ClusterState{
  202. KubeVersion: previousBranchLatestVersionStr,
  203. DNSType: dnsType,
  204. DNSVersion: kubeadmconstants.GetDNSVersion(dnsType),
  205. KubeadmVersion: previousBranchLatestVersionStr,
  206. EtcdVersion: getSuggestedEtcdVersion(previousBranchLatestVersionStr),
  207. // KubeletVersions is unset here as it is not used anywhere in .After
  208. },
  209. })
  210. }
  211. // Show the possibility if experimental upgrades are allowed
  212. if experimentalUpgradesAllowed && clusterVersion.LessThan(latestVersion) {
  213. // Default to assume that the experimental version to show is the unstable one
  214. unstableKubeVersion := latestVersionStr
  215. unstableKubeDNSVersion := kubeadmconstants.GetDNSVersion(dnsType)
  216. // Ẃe should not display alpha.0. The previous branch's beta/rc versions are more relevant due how the kube branching process works.
  217. if latestVersion.PreRelease() == "alpha.0" {
  218. unstableKubeVersion = previousBranchLatestVersionStr
  219. unstableKubeDNSVersion = kubeadmconstants.GetDNSVersion(dnsType)
  220. }
  221. upgrades = append(upgrades, Upgrade{
  222. Description: "experimental version",
  223. Before: beforeState,
  224. After: ClusterState{
  225. KubeVersion: unstableKubeVersion,
  226. DNSType: dnsType,
  227. DNSVersion: unstableKubeDNSVersion,
  228. KubeadmVersion: unstableKubeVersion,
  229. EtcdVersion: getSuggestedEtcdVersion(unstableKubeVersion),
  230. // KubeletVersions is unset here as it is not used anywhere in .After
  231. },
  232. })
  233. }
  234. }
  235. // Add a newline in the end of this output to leave some space to the next output section
  236. fmt.Println("")
  237. return upgrades, nil
  238. }
  239. func getBranchFromVersion(version string) string {
  240. v := versionutil.MustParseGeneric(version)
  241. return fmt.Sprintf("%d.%d", v.Major(), v.Minor())
  242. }
  243. func patchVersionBranchExists(clusterVersion, stableVersion *versionutil.Version) bool {
  244. return stableVersion.AtLeast(clusterVersion)
  245. }
  246. func patchUpgradePossible(clusterVersion, patchVersion *versionutil.Version) bool {
  247. return clusterVersion.LessThan(patchVersion)
  248. }
  249. func rcUpgradePossible(clusterVersion, previousBranchLatestVersion *versionutil.Version) bool {
  250. return strings.HasPrefix(previousBranchLatestVersion.PreRelease(), "rc") && clusterVersion.LessThan(previousBranchLatestVersion)
  251. }
  252. func minorUpgradePossibleWithPatchRelease(stableVersion, patchVersion *versionutil.Version) bool {
  253. return patchVersion.LessThan(stableVersion)
  254. }
  255. func getSuggestedEtcdVersion(kubernetesVersion string) string {
  256. etcdVersion, err := kubeadmconstants.EtcdSupportedVersion(kubernetesVersion)
  257. if err != nil {
  258. fmt.Printf("[upgrade/versions] WARNING: No recommended etcd for requested Kubernetes version (%s)\n", kubernetesVersion)
  259. return "N/A"
  260. }
  261. return etcdVersion.String()
  262. }