util.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2019 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 gce
  14. import (
  15. "fmt"
  16. "strings"
  17. "time"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/util/wait"
  21. clientset "k8s.io/client-go/kubernetes"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  24. )
  25. // RecreateNodes recreates the given nodes in a managed instance group.
  26. func RecreateNodes(c clientset.Interface, nodes []v1.Node) error {
  27. // Build mapping from zone to nodes in that zone.
  28. nodeNamesByZone := make(map[string][]string)
  29. for i := range nodes {
  30. node := &nodes[i]
  31. zone := framework.TestContext.CloudConfig.Zone
  32. if z, ok := node.Labels[v1.LabelZoneFailureDomain]; ok {
  33. zone = z
  34. }
  35. nodeNamesByZone[zone] = append(nodeNamesByZone[zone], node.Name)
  36. }
  37. // Find the sole managed instance group name
  38. var instanceGroup string
  39. if strings.Index(framework.TestContext.CloudConfig.NodeInstanceGroup, ",") >= 0 {
  40. return fmt.Errorf("Test does not support cluster setup with more than one managed instance group: %s", framework.TestContext.CloudConfig.NodeInstanceGroup)
  41. }
  42. instanceGroup = framework.TestContext.CloudConfig.NodeInstanceGroup
  43. // Recreate the nodes.
  44. for zone, nodeNames := range nodeNamesByZone {
  45. args := []string{
  46. "compute",
  47. fmt.Sprintf("--project=%s", framework.TestContext.CloudConfig.ProjectID),
  48. "instance-groups",
  49. "managed",
  50. "recreate-instances",
  51. instanceGroup,
  52. }
  53. args = append(args, fmt.Sprintf("--instances=%s", strings.Join(nodeNames, ",")))
  54. args = append(args, fmt.Sprintf("--zone=%s", zone))
  55. e2elog.Logf("Recreating instance group %s.", instanceGroup)
  56. stdout, stderr, err := framework.RunCmd("gcloud", args...)
  57. if err != nil {
  58. return fmt.Errorf("error recreating nodes: %s\nstdout: %s\nstderr: %s", err, stdout, stderr)
  59. }
  60. }
  61. return nil
  62. }
  63. // WaitForNodeBootIdsToChange waits for the boot ids of the given nodes to change in order to verify the node has been recreated.
  64. func WaitForNodeBootIdsToChange(c clientset.Interface, nodes []v1.Node, timeout time.Duration) error {
  65. errMsg := []string{}
  66. for i := range nodes {
  67. node := &nodes[i]
  68. if err := wait.Poll(30*time.Second, timeout, func() (bool, error) {
  69. newNode, err := c.CoreV1().Nodes().Get(node.Name, metav1.GetOptions{})
  70. if err != nil {
  71. e2elog.Logf("Could not get node info: %s. Retrying in %v.", err, 30*time.Second)
  72. return false, nil
  73. }
  74. return node.Status.NodeInfo.BootID != newNode.Status.NodeInfo.BootID, nil
  75. }); err != nil {
  76. errMsg = append(errMsg, "Error waiting for node %s boot ID to change: %s", node.Name, err.Error())
  77. }
  78. }
  79. if len(errMsg) > 0 {
  80. return fmt.Errorf(strings.Join(errMsg, ","))
  81. }
  82. return nil
  83. }