compute.go 13 KB

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