upgrade.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 lifecycle
  14. import (
  15. "fmt"
  16. "os/exec"
  17. "path"
  18. "strings"
  19. "time"
  20. "k8s.io/apimachinery/pkg/util/wait"
  21. "k8s.io/apimachinery/pkg/version"
  22. clientset "k8s.io/client-go/kubernetes"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  25. )
  26. // RealVersion turns a version constants into a version string deployable on
  27. // GKE. See hack/get-build.sh for more information.
  28. func RealVersion(s string) (string, error) {
  29. e2elog.Logf("Getting real version for %q", s)
  30. v, _, err := framework.RunCmd(path.Join(framework.TestContext.RepoRoot, "hack/get-build.sh"), "-v", s)
  31. if err != nil {
  32. return v, fmt.Errorf("error getting real version for %q: %v", s, err)
  33. }
  34. e2elog.Logf("Version for %q is %q", s, v)
  35. return strings.TrimPrefix(strings.TrimSpace(v), "v"), nil
  36. }
  37. func traceRouteToMaster() {
  38. path, err := exec.LookPath("traceroute")
  39. if err != nil {
  40. e2elog.Logf("Could not find traceroute program")
  41. return
  42. }
  43. cmd := exec.Command(path, "-I", framework.GetMasterHost())
  44. out, err := cmd.Output()
  45. if len(out) != 0 {
  46. e2elog.Logf(string(out))
  47. }
  48. if exiterr, ok := err.(*exec.ExitError); err != nil && ok {
  49. e2elog.Logf("Error while running traceroute: %s", exiterr.Stderr)
  50. }
  51. }
  52. // CheckMasterVersion validates the master version
  53. func CheckMasterVersion(c clientset.Interface, want string) error {
  54. e2elog.Logf("Checking master version")
  55. var err error
  56. var v *version.Info
  57. waitErr := wait.PollImmediate(5*time.Second, 2*time.Minute, func() (bool, error) {
  58. v, err = c.Discovery().ServerVersion()
  59. if err != nil {
  60. traceRouteToMaster()
  61. return false, nil
  62. }
  63. return true, nil
  64. })
  65. if waitErr != nil {
  66. return fmt.Errorf("CheckMasterVersion() couldn't get the master version: %v", err)
  67. }
  68. // We do prefix trimming and then matching because:
  69. // want looks like: 0.19.3-815-g50e67d4
  70. // got looks like: v0.19.3-815-g50e67d4034e858-dirty
  71. got := strings.TrimPrefix(v.GitVersion, "v")
  72. if !strings.HasPrefix(got, want) {
  73. return fmt.Errorf("master had kube-apiserver version %s which does not start with %s", got, want)
  74. }
  75. e2elog.Logf("Master is at version %s", want)
  76. return nil
  77. }
  78. // CheckNodesVersions validates the nodes versions
  79. func CheckNodesVersions(cs clientset.Interface, want string) error {
  80. l := framework.GetReadySchedulableNodesOrDie(cs)
  81. for _, n := range l.Items {
  82. // We do prefix trimming and then matching because:
  83. // want looks like: 0.19.3-815-g50e67d4
  84. // kv/kvp look like: v0.19.3-815-g50e67d4034e858-dirty
  85. kv, kpv := strings.TrimPrefix(n.Status.NodeInfo.KubeletVersion, "v"),
  86. strings.TrimPrefix(n.Status.NodeInfo.KubeProxyVersion, "v")
  87. if !strings.HasPrefix(kv, want) {
  88. return fmt.Errorf("node %s had kubelet version %s which does not start with %s",
  89. n.ObjectMeta.Name, kv, want)
  90. }
  91. if !strings.HasPrefix(kpv, want) {
  92. return fmt.Errorf("node %s had kube-proxy version %s which does not start with %s",
  93. n.ObjectMeta.Name, kpv, want)
  94. }
  95. }
  96. return nil
  97. }