mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-10-31 13:50:01 +00:00 
			
		
		
		
	Move RestartApiserver() into e2e/network
The function is called from e2e/network test only, so this moves the function into the test for reducing e2e/framework/util.go code and removing invalid dependency on e2e test framework.
This commit is contained in:
		| @@ -1115,86 +1115,6 @@ func AllNodesReady(c clientset.Interface, timeout time.Duration) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // RestartApiserver restarts the kube-apiserver. | ||||
| func RestartApiserver(namespace string, cs clientset.Interface) error { | ||||
| 	// TODO: Make it work for all providers. | ||||
| 	if !ProviderIs("gce", "gke", "aws") { | ||||
| 		return fmt.Errorf("unsupported provider for RestartApiserver: %s", TestContext.Provider) | ||||
| 	} | ||||
| 	if ProviderIs("gce", "aws") { | ||||
| 		initialRestartCount, err := getApiserverRestartCount(cs) | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("failed to get apiserver's restart count: %v", err) | ||||
| 		} | ||||
| 		if err := sshRestartMaster(); err != nil { | ||||
| 			return fmt.Errorf("failed to restart apiserver: %v", err) | ||||
| 		} | ||||
| 		return waitForApiserverRestarted(cs, initialRestartCount) | ||||
| 	} | ||||
| 	// GKE doesn't allow ssh access, so use a same-version master | ||||
| 	// upgrade to teardown/recreate master. | ||||
| 	v, err := cs.Discovery().ServerVersion() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return masterUpgradeGKE(namespace, v.GitVersion[1:]) // strip leading 'v' | ||||
| } | ||||
|  | ||||
| func sshRestartMaster() error { | ||||
| 	if !ProviderIs("gce", "aws") { | ||||
| 		return fmt.Errorf("unsupported provider for sshRestartMaster: %s", TestContext.Provider) | ||||
| 	} | ||||
| 	var command string | ||||
| 	if ProviderIs("gce") { | ||||
| 		command = "pidof kube-apiserver | xargs sudo kill" | ||||
| 	} else { | ||||
| 		command = "sudo /etc/init.d/kube-apiserver restart" | ||||
| 	} | ||||
| 	Logf("Restarting master via ssh, running: %v", command) | ||||
| 	result, err := e2essh.SSH(command, net.JoinHostPort(GetMasterHost(), sshPort), TestContext.Provider) | ||||
| 	if err != nil || result.Code != 0 { | ||||
| 		e2essh.LogResult(result) | ||||
| 		return fmt.Errorf("couldn't restart apiserver: %v", err) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // waitForApiserverRestarted waits until apiserver's restart count increased. | ||||
| func waitForApiserverRestarted(c clientset.Interface, initialRestartCount int32) error { | ||||
| 	for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(5 * time.Second) { | ||||
| 		restartCount, err := getApiserverRestartCount(c) | ||||
| 		if err != nil { | ||||
| 			Logf("Failed to get apiserver's restart count: %v", err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if restartCount > initialRestartCount { | ||||
| 			Logf("Apiserver has restarted.") | ||||
| 			return nil | ||||
| 		} | ||||
| 		Logf("Waiting for apiserver restart count to increase") | ||||
| 	} | ||||
| 	return fmt.Errorf("timed out waiting for apiserver to be restarted") | ||||
| } | ||||
|  | ||||
| func getApiserverRestartCount(c clientset.Interface) (int32, error) { | ||||
| 	label := labels.SelectorFromSet(labels.Set(map[string]string{"component": "kube-apiserver"})) | ||||
| 	listOpts := metav1.ListOptions{LabelSelector: label.String()} | ||||
| 	pods, err := c.CoreV1().Pods(metav1.NamespaceSystem).List(context.TODO(), listOpts) | ||||
| 	if err != nil { | ||||
| 		return -1, err | ||||
| 	} | ||||
| 	if len(pods.Items) != 1 { | ||||
| 		return -1, fmt.Errorf("unexpected number of apiserver pod: %d", len(pods.Items)) | ||||
| 	} | ||||
| 	for _, s := range pods.Items[0].Status.ContainerStatuses { | ||||
| 		if s.Name != "kube-apiserver" { | ||||
| 			continue | ||||
| 		} | ||||
| 		return s.RestartCount, nil | ||||
| 	} | ||||
| 	return -1, fmt.Errorf("Failed to find kube-apiserver container in pod") | ||||
| } | ||||
|  | ||||
| // RestartControllerManager restarts the kube-controller-manager. | ||||
| func RestartControllerManager() error { | ||||
| 	// TODO: Make it work for all providers and distros. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user