staticpods_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. "crypto/sha256"
  16. "crypto/x509"
  17. "fmt"
  18. "io/ioutil"
  19. "math/big"
  20. "os"
  21. "path/filepath"
  22. "strings"
  23. "testing"
  24. "time"
  25. "github.com/pkg/errors"
  26. "go.etcd.io/etcd/pkg/transport"
  27. "k8s.io/client-go/tools/clientcmd"
  28. certutil "k8s.io/client-go/util/cert"
  29. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  30. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  31. certsphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/certs"
  32. "k8s.io/kubernetes/cmd/kubeadm/app/phases/certs/renewal"
  33. controlplanephase "k8s.io/kubernetes/cmd/kubeadm/app/phases/controlplane"
  34. etcdphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/etcd"
  35. kubeconfigphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/kubeconfig"
  36. "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
  37. certstestutil "k8s.io/kubernetes/cmd/kubeadm/app/util/certs"
  38. configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
  39. etcdutil "k8s.io/kubernetes/cmd/kubeadm/app/util/etcd"
  40. "k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
  41. testutil "k8s.io/kubernetes/cmd/kubeadm/test"
  42. )
  43. const (
  44. waitForHashes = "wait-for-hashes"
  45. waitForHashChange = "wait-for-hash-change"
  46. waitForPodsWithLabel = "wait-for-pods-with-label"
  47. testConfiguration = `
  48. apiVersion: kubeadm.k8s.io/v1beta2
  49. kind: InitConfiguration
  50. nodeRegistration:
  51. name: foo
  52. criSocket: ""
  53. localAPIEndpoint:
  54. advertiseAddress: 192.168.2.2
  55. bindPort: 6443
  56. bootstrapTokens:
  57. - token: ce3aa5.5ec8455bb76b379f
  58. ttl: 24h
  59. ---
  60. apiVersion: kubeadm.k8s.io/v1beta2
  61. kind: ClusterConfiguration
  62. apiServer:
  63. certSANs: null
  64. extraArgs: null
  65. certificatesDir: %s
  66. etcd:
  67. local:
  68. dataDir: %s
  69. image: ""
  70. imageRepository: k8s.gcr.io
  71. kubernetesVersion: %s
  72. networking:
  73. dnsDomain: cluster.local
  74. podSubnet: ""
  75. serviceSubnet: 10.96.0.0/12
  76. useHyperKubeImage: false
  77. `
  78. )
  79. // fakeWaiter is a fake apiclient.Waiter that returns errors it was initialized with
  80. type fakeWaiter struct {
  81. errsToReturn map[string]error
  82. }
  83. func NewFakeStaticPodWaiter(errsToReturn map[string]error) apiclient.Waiter {
  84. return &fakeWaiter{
  85. errsToReturn: errsToReturn,
  86. }
  87. }
  88. // WaitForAPI just returns a dummy nil, to indicate that the program should just proceed
  89. func (w *fakeWaiter) WaitForAPI() error {
  90. return nil
  91. }
  92. // WaitForPodsWithLabel just returns an error if set from errsToReturn
  93. func (w *fakeWaiter) WaitForPodsWithLabel(kvLabel string) error {
  94. return w.errsToReturn[waitForPodsWithLabel]
  95. }
  96. // WaitForPodToDisappear just returns a dummy nil, to indicate that the program should just proceed
  97. func (w *fakeWaiter) WaitForPodToDisappear(podName string) error {
  98. return nil
  99. }
  100. // SetTimeout is a no-op; we don't use it in this implementation
  101. func (w *fakeWaiter) SetTimeout(_ time.Duration) {}
  102. // WaitForStaticPodControlPlaneHashes returns an error if set from errsToReturn
  103. func (w *fakeWaiter) WaitForStaticPodControlPlaneHashes(_ string) (map[string]string, error) {
  104. return map[string]string{}, w.errsToReturn[waitForHashes]
  105. }
  106. // WaitForStaticPodSingleHash returns an error if set from errsToReturn
  107. func (w *fakeWaiter) WaitForStaticPodSingleHash(_ string, _ string) (string, error) {
  108. return "", w.errsToReturn[waitForHashes]
  109. }
  110. // WaitForStaticPodHashChange returns an error if set from errsToReturn
  111. func (w *fakeWaiter) WaitForStaticPodHashChange(_, _, _ string) error {
  112. return w.errsToReturn[waitForHashChange]
  113. }
  114. // WaitForHealthyKubelet returns a dummy nil just to implement the interface
  115. func (w *fakeWaiter) WaitForHealthyKubelet(_ time.Duration, _ string) error {
  116. return nil
  117. }
  118. // WaitForKubeletAndFunc is a wrapper for WaitForHealthyKubelet that also blocks for a function
  119. func (w *fakeWaiter) WaitForKubeletAndFunc(f func() error) error {
  120. return nil
  121. }
  122. type fakeStaticPodPathManager struct {
  123. kubernetesDir string
  124. kustomizeDir string
  125. realManifestDir string
  126. tempManifestDir string
  127. backupManifestDir string
  128. backupEtcdDir string
  129. MoveFileFunc func(string, string) error
  130. }
  131. func NewFakeStaticPodPathManager(moveFileFunc func(string, string) error) (StaticPodPathManager, error) {
  132. kubernetesDir, err := ioutil.TempDir("", "kubeadm-pathmanager-")
  133. if err != nil {
  134. return nil, errors.Wrapf(err, "couldn't create a temporary directory for the upgrade")
  135. }
  136. realManifestDir := filepath.Join(kubernetesDir, constants.ManifestsSubDirName)
  137. if err := os.Mkdir(realManifestDir, 0700); err != nil {
  138. return nil, errors.Wrapf(err, "couldn't create a realManifestDir for the upgrade")
  139. }
  140. upgradedManifestDir := filepath.Join(kubernetesDir, "upgraded-manifests")
  141. if err := os.Mkdir(upgradedManifestDir, 0700); err != nil {
  142. return nil, errors.Wrapf(err, "couldn't create a upgradedManifestDir for the upgrade")
  143. }
  144. backupManifestDir := filepath.Join(kubernetesDir, "backup-manifests")
  145. if err := os.Mkdir(backupManifestDir, 0700); err != nil {
  146. return nil, errors.Wrap(err, "couldn't create a backupManifestDir for the upgrade")
  147. }
  148. backupEtcdDir := filepath.Join(kubernetesDir, "kubeadm-backup-etcd")
  149. if err := os.Mkdir(backupEtcdDir, 0700); err != nil {
  150. return nil, err
  151. }
  152. return &fakeStaticPodPathManager{
  153. kubernetesDir: kubernetesDir,
  154. realManifestDir: realManifestDir,
  155. tempManifestDir: upgradedManifestDir,
  156. backupManifestDir: backupManifestDir,
  157. backupEtcdDir: backupEtcdDir,
  158. MoveFileFunc: moveFileFunc,
  159. }, nil
  160. }
  161. func (spm *fakeStaticPodPathManager) MoveFile(oldPath, newPath string) error {
  162. return spm.MoveFileFunc(oldPath, newPath)
  163. }
  164. func (spm *fakeStaticPodPathManager) KubernetesDir() string {
  165. return spm.kubernetesDir
  166. }
  167. func (spm *fakeStaticPodPathManager) KustomizeDir() string {
  168. return spm.kustomizeDir
  169. }
  170. func (spm *fakeStaticPodPathManager) RealManifestPath(component string) string {
  171. return constants.GetStaticPodFilepath(component, spm.realManifestDir)
  172. }
  173. func (spm *fakeStaticPodPathManager) RealManifestDir() string {
  174. return spm.realManifestDir
  175. }
  176. func (spm *fakeStaticPodPathManager) TempManifestPath(component string) string {
  177. return constants.GetStaticPodFilepath(component, spm.tempManifestDir)
  178. }
  179. func (spm *fakeStaticPodPathManager) TempManifestDir() string {
  180. return spm.tempManifestDir
  181. }
  182. func (spm *fakeStaticPodPathManager) BackupManifestPath(component string) string {
  183. return constants.GetStaticPodFilepath(component, spm.backupManifestDir)
  184. }
  185. func (spm *fakeStaticPodPathManager) BackupManifestDir() string {
  186. return spm.backupManifestDir
  187. }
  188. func (spm *fakeStaticPodPathManager) BackupEtcdDir() string {
  189. return spm.backupEtcdDir
  190. }
  191. func (spm *fakeStaticPodPathManager) CleanupDirs() error {
  192. if err := os.RemoveAll(spm.TempManifestDir()); err != nil {
  193. return err
  194. }
  195. if err := os.RemoveAll(spm.BackupManifestDir()); err != nil {
  196. return err
  197. }
  198. return os.RemoveAll(spm.BackupEtcdDir())
  199. }
  200. type fakeTLSEtcdClient struct{ TLS bool }
  201. func (c fakeTLSEtcdClient) WaitForClusterAvailable(retries int, retryInterval time.Duration) (bool, error) {
  202. return true, nil
  203. }
  204. func (c fakeTLSEtcdClient) CheckClusterHealth() error {
  205. return nil
  206. }
  207. func (c fakeTLSEtcdClient) GetClusterVersions() (map[string]string, error) {
  208. return map[string]string{
  209. "https://1.2.3.4:2379": "3.1.12",
  210. }, nil
  211. }
  212. func (c fakeTLSEtcdClient) GetVersion() (string, error) {
  213. return "3.1.12", nil
  214. }
  215. func (c fakeTLSEtcdClient) Sync() error { return nil }
  216. func (c fakeTLSEtcdClient) AddMember(name string, peerAddrs string) ([]etcdutil.Member, error) {
  217. return []etcdutil.Member{}, nil
  218. }
  219. func (c fakeTLSEtcdClient) GetMemberID(peerURL string) (uint64, error) {
  220. return 0, nil
  221. }
  222. func (c fakeTLSEtcdClient) RemoveMember(id uint64) ([]etcdutil.Member, error) {
  223. return []etcdutil.Member{}, nil
  224. }
  225. type fakePodManifestEtcdClient struct{ ManifestDir, CertificatesDir string }
  226. func (c fakePodManifestEtcdClient) WaitForClusterAvailable(retries int, retryInterval time.Duration) (bool, error) {
  227. return true, nil
  228. }
  229. func (c fakePodManifestEtcdClient) CheckClusterHealth() error {
  230. // Make sure the certificates generated from the upgrade are readable from disk
  231. tlsInfo := transport.TLSInfo{
  232. CertFile: filepath.Join(c.CertificatesDir, constants.EtcdCACertName),
  233. KeyFile: filepath.Join(c.CertificatesDir, constants.EtcdHealthcheckClientCertName),
  234. TrustedCAFile: filepath.Join(c.CertificatesDir, constants.EtcdHealthcheckClientKeyName),
  235. }
  236. _, err := tlsInfo.ClientConfig()
  237. return err
  238. }
  239. func (c fakePodManifestEtcdClient) GetClusterVersions() (map[string]string, error) {
  240. return map[string]string{
  241. "https://1.2.3.4:2379": "3.1.12",
  242. }, nil
  243. }
  244. func (c fakePodManifestEtcdClient) GetVersion() (string, error) {
  245. return "3.1.12", nil
  246. }
  247. func (c fakePodManifestEtcdClient) Sync() error { return nil }
  248. func (c fakePodManifestEtcdClient) AddMember(name string, peerAddrs string) ([]etcdutil.Member, error) {
  249. return []etcdutil.Member{}, nil
  250. }
  251. func (c fakePodManifestEtcdClient) GetMemberID(peerURL string) (uint64, error) {
  252. return 0, nil
  253. }
  254. func (c fakePodManifestEtcdClient) RemoveMember(id uint64) ([]etcdutil.Member, error) {
  255. return []etcdutil.Member{}, nil
  256. }
  257. func TestStaticPodControlPlane(t *testing.T) {
  258. tests := []struct {
  259. description string
  260. waitErrsToReturn map[string]error
  261. moveFileFunc func(string, string) error
  262. skipKubeConfig string
  263. expectedErr bool
  264. manifestShouldChange bool
  265. }{
  266. {
  267. description: "error-free case should succeed",
  268. waitErrsToReturn: map[string]error{
  269. waitForHashes: nil,
  270. waitForHashChange: nil,
  271. waitForPodsWithLabel: nil,
  272. },
  273. moveFileFunc: func(oldPath, newPath string) error {
  274. return os.Rename(oldPath, newPath)
  275. },
  276. expectedErr: false,
  277. manifestShouldChange: true,
  278. },
  279. {
  280. description: "any wait error should result in a rollback and an abort",
  281. waitErrsToReturn: map[string]error{
  282. waitForHashes: errors.New("boo! failed"),
  283. waitForHashChange: nil,
  284. waitForPodsWithLabel: nil,
  285. },
  286. moveFileFunc: func(oldPath, newPath string) error {
  287. return os.Rename(oldPath, newPath)
  288. },
  289. expectedErr: true,
  290. manifestShouldChange: false,
  291. },
  292. {
  293. description: "any wait error should result in a rollback and an abort",
  294. waitErrsToReturn: map[string]error{
  295. waitForHashes: nil,
  296. waitForHashChange: errors.New("boo! failed"),
  297. waitForPodsWithLabel: nil,
  298. },
  299. moveFileFunc: func(oldPath, newPath string) error {
  300. return os.Rename(oldPath, newPath)
  301. },
  302. expectedErr: true,
  303. manifestShouldChange: false,
  304. },
  305. {
  306. description: "any wait error should result in a rollback and an abort",
  307. waitErrsToReturn: map[string]error{
  308. waitForHashes: nil,
  309. waitForHashChange: nil,
  310. waitForPodsWithLabel: errors.New("boo! failed"),
  311. },
  312. moveFileFunc: func(oldPath, newPath string) error {
  313. return os.Rename(oldPath, newPath)
  314. },
  315. expectedErr: true,
  316. manifestShouldChange: false,
  317. },
  318. {
  319. description: "any path-moving error should result in a rollback and an abort",
  320. waitErrsToReturn: map[string]error{
  321. waitForHashes: nil,
  322. waitForHashChange: nil,
  323. waitForPodsWithLabel: nil,
  324. },
  325. moveFileFunc: func(oldPath, newPath string) error {
  326. // fail for kube-apiserver move
  327. if strings.Contains(newPath, "kube-apiserver") {
  328. return errors.New("moving the kube-apiserver file failed")
  329. }
  330. return os.Rename(oldPath, newPath)
  331. },
  332. expectedErr: true,
  333. manifestShouldChange: false,
  334. },
  335. {
  336. description: "any path-moving error should result in a rollback and an abort",
  337. waitErrsToReturn: map[string]error{
  338. waitForHashes: nil,
  339. waitForHashChange: nil,
  340. waitForPodsWithLabel: nil,
  341. },
  342. moveFileFunc: func(oldPath, newPath string) error {
  343. // fail for kube-controller-manager move
  344. if strings.Contains(newPath, "kube-controller-manager") {
  345. return errors.New("moving the kube-apiserver file failed")
  346. }
  347. return os.Rename(oldPath, newPath)
  348. },
  349. expectedErr: true,
  350. manifestShouldChange: false,
  351. },
  352. {
  353. description: "any path-moving error should result in a rollback and an abort; even though this is the last component (kube-apiserver and kube-controller-manager healthy)",
  354. waitErrsToReturn: map[string]error{
  355. waitForHashes: nil,
  356. waitForHashChange: nil,
  357. waitForPodsWithLabel: nil,
  358. },
  359. moveFileFunc: func(oldPath, newPath string) error {
  360. // fail for kube-scheduler move
  361. if strings.Contains(newPath, "kube-scheduler") {
  362. return errors.New("moving the kube-apiserver file failed")
  363. }
  364. return os.Rename(oldPath, newPath)
  365. },
  366. expectedErr: true,
  367. manifestShouldChange: false,
  368. },
  369. {
  370. description: "any cert renew error should result in a rollback and an abort; even though this is the last component (kube-apiserver and kube-controller-manager healthy)",
  371. waitErrsToReturn: map[string]error{
  372. waitForHashes: nil,
  373. waitForHashChange: nil,
  374. waitForPodsWithLabel: nil,
  375. },
  376. moveFileFunc: func(oldPath, newPath string) error {
  377. return os.Rename(oldPath, newPath)
  378. },
  379. skipKubeConfig: constants.SchedulerKubeConfigFileName,
  380. expectedErr: true,
  381. manifestShouldChange: false,
  382. },
  383. {
  384. description: "any cert renew error should result in a rollback and an abort; even though this is admin.conf (kube-apiserver and kube-controller-manager and kube-scheduler healthy)",
  385. waitErrsToReturn: map[string]error{
  386. waitForHashes: nil,
  387. waitForHashChange: nil,
  388. waitForPodsWithLabel: nil,
  389. },
  390. moveFileFunc: func(oldPath, newPath string) error {
  391. return os.Rename(oldPath, newPath)
  392. },
  393. skipKubeConfig: constants.AdminKubeConfigFileName,
  394. expectedErr: true,
  395. manifestShouldChange: false,
  396. },
  397. }
  398. for _, rt := range tests {
  399. t.Run(rt.description, func(t *testing.T) {
  400. waiter := NewFakeStaticPodWaiter(rt.waitErrsToReturn)
  401. pathMgr, err := NewFakeStaticPodPathManager(rt.moveFileFunc)
  402. if err != nil {
  403. t.Fatalf("couldn't run NewFakeStaticPodPathManager: %v", err)
  404. }
  405. defer os.RemoveAll(pathMgr.(*fakeStaticPodPathManager).KubernetesDir())
  406. tmpKubernetesDir := pathMgr.(*fakeStaticPodPathManager).KubernetesDir()
  407. tempCertsDir, err := ioutil.TempDir("", "kubeadm-certs")
  408. if err != nil {
  409. t.Fatalf("couldn't create temporary certificates directory: %v", err)
  410. }
  411. defer os.RemoveAll(tempCertsDir)
  412. tmpEtcdDataDir, err := ioutil.TempDir("", "kubeadm-etcd-data")
  413. if err != nil {
  414. t.Fatalf("couldn't create temporary etcd data directory: %v", err)
  415. }
  416. defer os.RemoveAll(tmpEtcdDataDir)
  417. oldcfg, err := getConfig(constants.MinimumControlPlaneVersion.String(), tempCertsDir, tmpEtcdDataDir)
  418. if err != nil {
  419. t.Fatalf("couldn't create config: %v", err)
  420. }
  421. tree, err := certsphase.GetCertsWithoutEtcd().AsMap().CertTree()
  422. if err != nil {
  423. t.Fatalf("couldn't get cert tree: %v", err)
  424. }
  425. if err := tree.CreateTree(oldcfg); err != nil {
  426. t.Fatalf("couldn't get create cert tree: %v", err)
  427. }
  428. for _, kubeConfig := range []string{
  429. constants.AdminKubeConfigFileName,
  430. constants.SchedulerKubeConfigFileName,
  431. constants.ControllerManagerKubeConfigFileName,
  432. } {
  433. if rt.skipKubeConfig == kubeConfig {
  434. continue
  435. }
  436. if err := kubeconfigphase.CreateKubeConfigFile(kubeConfig, tmpKubernetesDir, oldcfg); err != nil {
  437. t.Fatalf("couldn't create kubeconfig %q: %v", kubeConfig, err)
  438. }
  439. }
  440. // Initialize the directory with v1.7 manifests; should then be upgraded to v1.8 using the method
  441. err = controlplanephase.CreateInitStaticPodManifestFiles(pathMgr.RealManifestDir(), pathMgr.KustomizeDir(), oldcfg)
  442. if err != nil {
  443. t.Fatalf("couldn't run CreateInitStaticPodManifestFiles: %v", err)
  444. }
  445. err = etcdphase.CreateLocalEtcdStaticPodManifestFile(pathMgr.RealManifestDir(), pathMgr.KustomizeDir(), oldcfg.NodeRegistration.Name, &oldcfg.ClusterConfiguration, &oldcfg.LocalAPIEndpoint)
  446. if err != nil {
  447. t.Fatalf("couldn't run CreateLocalEtcdStaticPodManifestFile: %v", err)
  448. }
  449. // Get a hash of the v1.7 API server manifest to compare later (was the file re-written)
  450. oldHash, err := getAPIServerHash(pathMgr.RealManifestDir())
  451. if err != nil {
  452. t.Fatalf("couldn't read temp file: %v", err)
  453. }
  454. newcfg, err := getConfig(constants.CurrentKubernetesVersion.String(), tempCertsDir, tmpEtcdDataDir)
  455. if err != nil {
  456. t.Fatalf("couldn't create config: %v", err)
  457. }
  458. // create the kubeadm etcd certs
  459. caCert, caKey, err := certsphase.KubeadmCertEtcdCA.CreateAsCA(newcfg)
  460. if err != nil {
  461. t.Fatalf("couldn't create new CA certificate: %v", err)
  462. }
  463. for _, cert := range []*certsphase.KubeadmCert{
  464. &certsphase.KubeadmCertEtcdServer,
  465. &certsphase.KubeadmCertEtcdPeer,
  466. &certsphase.KubeadmCertEtcdHealthcheck,
  467. &certsphase.KubeadmCertEtcdAPIClient,
  468. } {
  469. if err := cert.CreateFromCA(newcfg, caCert, caKey); err != nil {
  470. t.Fatalf("couldn't create certificate %s: %v", cert.Name, err)
  471. }
  472. }
  473. actualErr := StaticPodControlPlane(
  474. nil,
  475. waiter,
  476. pathMgr,
  477. newcfg,
  478. true,
  479. true,
  480. fakeTLSEtcdClient{
  481. TLS: false,
  482. },
  483. fakePodManifestEtcdClient{
  484. ManifestDir: pathMgr.RealManifestDir(),
  485. CertificatesDir: newcfg.CertificatesDir,
  486. },
  487. )
  488. if (actualErr != nil) != rt.expectedErr {
  489. t.Errorf(
  490. "failed UpgradeStaticPodControlPlane\n%s\n\texpected error: %t\n\tgot: %t\n\tactual error: %v",
  491. rt.description,
  492. rt.expectedErr,
  493. (actualErr != nil),
  494. actualErr,
  495. )
  496. }
  497. newHash, err := getAPIServerHash(pathMgr.RealManifestDir())
  498. if err != nil {
  499. t.Fatalf("couldn't read temp file: %v", err)
  500. }
  501. if (oldHash != newHash) != rt.manifestShouldChange {
  502. t.Errorf(
  503. "failed StaticPodControlPlane\n%s\n\texpected manifest change: %t\n\tgot: %t\n\tnewHash: %v",
  504. rt.description,
  505. rt.manifestShouldChange,
  506. (oldHash != newHash),
  507. newHash,
  508. )
  509. }
  510. })
  511. }
  512. }
  513. func getAPIServerHash(dir string) (string, error) {
  514. manifestPath := constants.GetStaticPodFilepath(constants.KubeAPIServer, dir)
  515. fileBytes, err := ioutil.ReadFile(manifestPath)
  516. if err != nil {
  517. return "", err
  518. }
  519. return fmt.Sprintf("%x", sha256.Sum256(fileBytes)), nil
  520. }
  521. func getConfig(version, certsDir, etcdDataDir string) (*kubeadmapi.InitConfiguration, error) {
  522. configBytes := []byte(fmt.Sprintf(testConfiguration, certsDir, etcdDataDir, version))
  523. // Unmarshal the config
  524. return configutil.BytesToInitConfiguration(configBytes)
  525. }
  526. func getTempDir(t *testing.T, name string) (string, func()) {
  527. dir, err := ioutil.TempDir(os.TempDir(), name)
  528. if err != nil {
  529. t.Fatalf("couldn't make temporary directory: %v", err)
  530. }
  531. return dir, func() {
  532. os.RemoveAll(dir)
  533. }
  534. }
  535. func TestCleanupDirs(t *testing.T) {
  536. tests := []struct {
  537. name string
  538. keepManifest, keepEtcd bool
  539. }{
  540. {
  541. name: "save manifest backup",
  542. keepManifest: true,
  543. },
  544. {
  545. name: "save both etcd and manifest",
  546. keepManifest: true,
  547. keepEtcd: true,
  548. },
  549. {
  550. name: "save nothing",
  551. },
  552. }
  553. for _, test := range tests {
  554. t.Run(test.name, func(t *testing.T) {
  555. realKubernetesDir, cleanup := getTempDir(t, "realKubernetesDir")
  556. defer cleanup()
  557. tempManifestDir, cleanup := getTempDir(t, "tempManifestDir")
  558. defer cleanup()
  559. backupManifestDir, cleanup := getTempDir(t, "backupManifestDir")
  560. defer cleanup()
  561. backupEtcdDir, cleanup := getTempDir(t, "backupEtcdDir")
  562. defer cleanup()
  563. mgr := NewKubeStaticPodPathManager(realKubernetesDir, "", tempManifestDir, backupManifestDir, backupEtcdDir, test.keepManifest, test.keepEtcd)
  564. err := mgr.CleanupDirs()
  565. if err != nil {
  566. t.Errorf("unexpected error cleaning up: %v", err)
  567. }
  568. if _, err := os.Stat(tempManifestDir); !os.IsNotExist(err) {
  569. t.Errorf("%q should not have existed", tempManifestDir)
  570. }
  571. _, err = os.Stat(backupManifestDir)
  572. if test.keepManifest {
  573. if err != nil {
  574. t.Errorf("unexpected error getting backup manifest dir")
  575. }
  576. } else {
  577. if !os.IsNotExist(err) {
  578. t.Error("expected backup manifest to not exist")
  579. }
  580. }
  581. _, err = os.Stat(backupEtcdDir)
  582. if test.keepEtcd {
  583. if err != nil {
  584. t.Errorf("unexpected error getting backup etcd dir")
  585. }
  586. } else {
  587. if !os.IsNotExist(err) {
  588. t.Error("expected backup etcd dir to not exist")
  589. }
  590. }
  591. })
  592. }
  593. }
  594. func TestRenewCertsByComponent(t *testing.T) {
  595. caCert, caKey := certstestutil.SetupCertificateAuthority(t)
  596. tests := []struct {
  597. name string
  598. component string
  599. externalCA bool
  600. externalFrontProxyCA bool
  601. skipCreateEtcdCA bool
  602. shouldErrorOnRenew bool
  603. certsShouldExist []*certsphase.KubeadmCert
  604. certsShouldBeRenewed []*certsphase.KubeadmCert // NB. If empty, it will assume certsShouldBeRenewed == certsShouldExist
  605. kubeConfigShouldExist []string
  606. }{
  607. {
  608. name: "all CA exist, all certs should be rotated for etcd",
  609. component: constants.Etcd,
  610. certsShouldExist: []*certsphase.KubeadmCert{
  611. &certsphase.KubeadmCertEtcdServer,
  612. &certsphase.KubeadmCertEtcdPeer,
  613. &certsphase.KubeadmCertEtcdHealthcheck,
  614. },
  615. },
  616. {
  617. name: "all CA exist, all certs should be rotated for apiserver",
  618. component: constants.KubeAPIServer,
  619. certsShouldExist: []*certsphase.KubeadmCert{
  620. &certsphase.KubeadmCertEtcdAPIClient,
  621. &certsphase.KubeadmCertAPIServer,
  622. &certsphase.KubeadmCertKubeletClient,
  623. &certsphase.KubeadmCertFrontProxyClient,
  624. },
  625. },
  626. {
  627. name: "external CA, renew only certificates not signed by CA for apiserver",
  628. component: constants.KubeAPIServer,
  629. certsShouldExist: []*certsphase.KubeadmCert{
  630. &certsphase.KubeadmCertEtcdAPIClient,
  631. &certsphase.KubeadmCertFrontProxyClient,
  632. &certsphase.KubeadmCertAPIServer,
  633. &certsphase.KubeadmCertKubeletClient,
  634. },
  635. certsShouldBeRenewed: []*certsphase.KubeadmCert{
  636. &certsphase.KubeadmCertEtcdAPIClient,
  637. &certsphase.KubeadmCertFrontProxyClient,
  638. },
  639. externalCA: true,
  640. },
  641. {
  642. name: "external front-proxy-CA, renew only certificates not signed by front-proxy-CA for apiserver",
  643. component: constants.KubeAPIServer,
  644. certsShouldExist: []*certsphase.KubeadmCert{
  645. &certsphase.KubeadmCertEtcdAPIClient,
  646. &certsphase.KubeadmCertFrontProxyClient,
  647. &certsphase.KubeadmCertAPIServer,
  648. &certsphase.KubeadmCertKubeletClient,
  649. },
  650. certsShouldBeRenewed: []*certsphase.KubeadmCert{
  651. &certsphase.KubeadmCertEtcdAPIClient,
  652. &certsphase.KubeadmCertAPIServer,
  653. &certsphase.KubeadmCertKubeletClient,
  654. },
  655. externalFrontProxyCA: true,
  656. },
  657. {
  658. name: "all CA exist, should be rotated for scheduler",
  659. component: constants.KubeScheduler,
  660. kubeConfigShouldExist: []string{
  661. constants.SchedulerKubeConfigFileName,
  662. },
  663. },
  664. {
  665. name: "all CA exist, should be rotated for controller manager",
  666. component: constants.KubeControllerManager,
  667. kubeConfigShouldExist: []string{
  668. constants.ControllerManagerKubeConfigFileName,
  669. },
  670. },
  671. {
  672. name: "missing a cert to renew",
  673. component: constants.Etcd,
  674. shouldErrorOnRenew: true,
  675. certsShouldExist: []*certsphase.KubeadmCert{
  676. &certsphase.KubeadmCertEtcdServer,
  677. &certsphase.KubeadmCertEtcdPeer,
  678. },
  679. },
  680. {
  681. name: "no CA, cannot continue",
  682. component: constants.Etcd,
  683. skipCreateEtcdCA: true,
  684. shouldErrorOnRenew: true,
  685. },
  686. }
  687. for _, test := range tests {
  688. t.Run(test.name, func(t *testing.T) {
  689. // Setup up basic requities
  690. tmpDir := testutil.SetupTempDir(t)
  691. defer os.RemoveAll(tmpDir)
  692. cfg := testutil.GetDefaultInternalConfig(t)
  693. cfg.CertificatesDir = tmpDir
  694. if err := pkiutil.WriteCertAndKey(tmpDir, constants.CACertAndKeyBaseName, caCert, caKey); err != nil {
  695. t.Fatalf("couldn't write out CA: %v", err)
  696. }
  697. if test.externalCA {
  698. os.Remove(filepath.Join(tmpDir, constants.CAKeyName))
  699. }
  700. if err := pkiutil.WriteCertAndKey(tmpDir, constants.FrontProxyCACertAndKeyBaseName, caCert, caKey); err != nil {
  701. t.Fatalf("couldn't write out front-proxy-CA: %v", err)
  702. }
  703. if test.externalFrontProxyCA {
  704. os.Remove(filepath.Join(tmpDir, constants.FrontProxyCAKeyName))
  705. }
  706. if !test.skipCreateEtcdCA {
  707. if err := pkiutil.WriteCertAndKey(tmpDir, constants.EtcdCACertAndKeyBaseName, caCert, caKey); err != nil {
  708. t.Fatalf("couldn't write out etcd-CA: %v", err)
  709. }
  710. }
  711. certMaps := make(map[string]big.Int)
  712. // Create expected certs and load to recorde the serial numbers
  713. for _, kubeCert := range test.certsShouldExist {
  714. if err := kubeCert.CreateFromCA(cfg, caCert, caKey); err != nil {
  715. t.Fatalf("couldn't create certificate %q: %v", kubeCert.Name, err)
  716. }
  717. cert, err := pkiutil.TryLoadCertFromDisk(tmpDir, kubeCert.BaseName)
  718. if err != nil {
  719. t.Fatalf("couldn't load certificate %q: %v", kubeCert.Name, err)
  720. }
  721. certMaps[kubeCert.Name] = *cert.SerialNumber
  722. }
  723. // Create expected kubeconfigs
  724. for _, kubeConfig := range test.kubeConfigShouldExist {
  725. if err := kubeconfigphase.CreateKubeConfigFile(kubeConfig, tmpDir, cfg); err != nil {
  726. t.Fatalf("couldn't create kubeconfig %q: %v", kubeConfig, err)
  727. }
  728. newCerts, err := getEmbeddedCerts(tmpDir, kubeConfig)
  729. if err != nil {
  730. t.Fatalf("error reading embedded certs from %s: %v", kubeConfig, err)
  731. }
  732. certMaps[kubeConfig] = *newCerts[0].SerialNumber
  733. }
  734. // Renew everything
  735. rm, err := renewal.NewManager(&cfg.ClusterConfiguration, tmpDir)
  736. if err != nil {
  737. t.Fatalf("Failed to create the certificate renewal manager: %v", err)
  738. }
  739. err = renewCertsByComponent(cfg, test.component, rm)
  740. if test.shouldErrorOnRenew {
  741. if err == nil {
  742. t.Fatal("expected renewal error, got nothing")
  743. }
  744. // expected error, got error
  745. return
  746. }
  747. if err != nil {
  748. t.Fatalf("couldn't renew certificates: %v", err)
  749. }
  750. // See if the certificate serial numbers change
  751. for _, kubeCert := range test.certsShouldExist {
  752. newCert, err := pkiutil.TryLoadCertFromDisk(tmpDir, kubeCert.BaseName)
  753. if err != nil {
  754. t.Errorf("couldn't load new certificate %q: %v", kubeCert.Name, err)
  755. continue
  756. }
  757. oldSerial := certMaps[kubeCert.Name]
  758. shouldBeRenewed := true
  759. if test.certsShouldBeRenewed != nil {
  760. shouldBeRenewed = false
  761. for _, x := range test.certsShouldBeRenewed {
  762. if x.Name == kubeCert.Name {
  763. shouldBeRenewed = true
  764. }
  765. }
  766. }
  767. if shouldBeRenewed && oldSerial.Cmp(newCert.SerialNumber) == 0 {
  768. t.Errorf("certifitate %v was not reissued when expected", kubeCert.Name)
  769. }
  770. if !shouldBeRenewed && oldSerial.Cmp(newCert.SerialNumber) != 0 {
  771. t.Errorf("certifitate %v was reissued when not expected", kubeCert.Name)
  772. }
  773. }
  774. // See if the embedded certificate serial numbers change
  775. for _, kubeConfig := range test.kubeConfigShouldExist {
  776. newCerts, err := getEmbeddedCerts(tmpDir, kubeConfig)
  777. if err != nil {
  778. t.Fatalf("error reading embedded certs from %s: %v", kubeConfig, err)
  779. }
  780. oldSerial := certMaps[kubeConfig]
  781. if oldSerial.Cmp(newCerts[0].SerialNumber) == 0 {
  782. t.Errorf("certifitate %v was not reissued", kubeConfig)
  783. }
  784. }
  785. })
  786. }
  787. }
  788. func getEmbeddedCerts(tmpDir, kubeConfig string) ([]*x509.Certificate, error) {
  789. kubeconfigPath := filepath.Join(tmpDir, kubeConfig)
  790. newConfig, err := clientcmd.LoadFromFile(kubeconfigPath)
  791. if err != nil {
  792. return nil, errors.Wrapf(err, "failed to load kubeconfig file %s", kubeconfigPath)
  793. }
  794. authInfoName := newConfig.Contexts[newConfig.CurrentContext].AuthInfo
  795. authInfo := newConfig.AuthInfos[authInfoName]
  796. return certutil.ParseCertsPEM(authInfo.ClientCertificateData)
  797. }
  798. func TestGetPathManagerForUpgrade(t *testing.T) {
  799. externalEtcd := &kubeadmapi.InitConfiguration{
  800. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  801. Etcd: kubeadmapi.Etcd{
  802. External: &kubeadmapi.ExternalEtcd{
  803. Endpoints: []string{"10.100.0.1:2379", "10.100.0.2:2379", "10.100.0.3:2379"},
  804. },
  805. },
  806. },
  807. }
  808. stackedEtcd := &kubeadmapi.InitConfiguration{}
  809. tests := []struct {
  810. name string
  811. cfg *kubeadmapi.InitConfiguration
  812. etcdUpgrade bool
  813. shouldDeleteEtcd bool
  814. }{
  815. {
  816. name: "external etcd but no etcd upgrade",
  817. cfg: externalEtcd,
  818. etcdUpgrade: false,
  819. shouldDeleteEtcd: true,
  820. },
  821. {
  822. name: "external etcd with etcd upgrade",
  823. cfg: externalEtcd,
  824. etcdUpgrade: true,
  825. shouldDeleteEtcd: true,
  826. },
  827. {
  828. name: "stacked etcd but no etcd upgrade",
  829. cfg: stackedEtcd,
  830. etcdUpgrade: false,
  831. shouldDeleteEtcd: true,
  832. },
  833. {
  834. name: "stacked etcd with etcd upgrade",
  835. cfg: stackedEtcd,
  836. etcdUpgrade: true,
  837. shouldDeleteEtcd: false,
  838. },
  839. }
  840. for _, test := range tests {
  841. t.Run(test.name, func(t *testing.T) {
  842. // Use a temporary directory
  843. tmpdir, err := ioutil.TempDir("", "TestGetPathManagerForUpgrade")
  844. if err != nil {
  845. t.Fatalf("unexpected error making temporary directory: %v", err)
  846. }
  847. defer func() {
  848. os.RemoveAll(tmpdir)
  849. }()
  850. pathmgr, err := GetPathManagerForUpgrade(tmpdir, "", test.cfg, test.etcdUpgrade)
  851. if err != nil {
  852. t.Fatalf("unexpected error creating path manager: %v", err)
  853. }
  854. if _, err := os.Stat(pathmgr.BackupManifestDir()); os.IsNotExist(err) {
  855. t.Errorf("expected manifest dir %s to exist, but it did not (%v)", pathmgr.BackupManifestDir(), err)
  856. }
  857. if _, err := os.Stat(pathmgr.BackupEtcdDir()); os.IsNotExist(err) {
  858. t.Errorf("expected etcd dir %s to exist, but it did not (%v)", pathmgr.BackupEtcdDir(), err)
  859. }
  860. if err := pathmgr.CleanupDirs(); err != nil {
  861. t.Fatalf("unexpected error cleaning up directories: %v", err)
  862. }
  863. if _, err := os.Stat(pathmgr.BackupManifestDir()); os.IsNotExist(err) {
  864. t.Errorf("expected manifest dir %s to exist, but it did not (%v)", pathmgr.BackupManifestDir(), err)
  865. }
  866. if test.shouldDeleteEtcd {
  867. if _, err := os.Stat(pathmgr.BackupEtcdDir()); !os.IsNotExist(err) {
  868. t.Errorf("expected etcd dir %s not to exist, but it did (%v)", pathmgr.BackupEtcdDir(), err)
  869. }
  870. } else {
  871. if _, err := os.Stat(pathmgr.BackupEtcdDir()); os.IsNotExist(err) {
  872. t.Errorf("expected etcd dir %s to exist, but it did not", pathmgr.BackupEtcdDir())
  873. }
  874. }
  875. })
  876. }
  877. }