Skip to content

Commit 7398c83

Browse files
author
Kenichi Omichi
committed
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.
1 parent 0f1a27b commit 7398c83

File tree

3 files changed

+87
-83
lines changed

3 files changed

+87
-83
lines changed

test/e2e/framework/nodes_util.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func MasterUpgrade(f *Framework, v string) error {
5252
case "gce":
5353
return masterUpgradeGCE(v, false)
5454
case "gke":
55-
return masterUpgradeGKE(f.Namespace.Name, v)
55+
return MasterUpgradeGKE(f.Namespace.Name, v)
5656
case "kubernetes-anywhere":
5757
return masterUpgradeKubernetesAnywhere(v)
5858
default:
@@ -113,7 +113,8 @@ func appendContainerCommandGroupIfNeeded(args []string) []string {
113113
return args
114114
}
115115

116-
func masterUpgradeGKE(namespace string, v string) error {
116+
// MasterUpgradeGKE upgrades master node to the specified version on GKE.
117+
func MasterUpgradeGKE(namespace string, v string) error {
117118
Logf("Upgrading master to %q", v)
118119
args := []string{
119120
"container",

test/e2e/framework/util.go

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,86 +1115,6 @@ func AllNodesReady(c clientset.Interface, timeout time.Duration) error {
11151115
return nil
11161116
}
11171117

1118-
// RestartApiserver restarts the kube-apiserver.
1119-
func RestartApiserver(namespace string, cs clientset.Interface) error {
1120-
// TODO: Make it work for all providers.
1121-
if !ProviderIs("gce", "gke", "aws") {
1122-
return fmt.Errorf("unsupported provider for RestartApiserver: %s", TestContext.Provider)
1123-
}
1124-
if ProviderIs("gce", "aws") {
1125-
initialRestartCount, err := getApiserverRestartCount(cs)
1126-
if err != nil {
1127-
return fmt.Errorf("failed to get apiserver's restart count: %v", err)
1128-
}
1129-
if err := sshRestartMaster(); err != nil {
1130-
return fmt.Errorf("failed to restart apiserver: %v", err)
1131-
}
1132-
return waitForApiserverRestarted(cs, initialRestartCount)
1133-
}
1134-
// GKE doesn't allow ssh access, so use a same-version master
1135-
// upgrade to teardown/recreate master.
1136-
v, err := cs.Discovery().ServerVersion()
1137-
if err != nil {
1138-
return err
1139-
}
1140-
return masterUpgradeGKE(namespace, v.GitVersion[1:]) // strip leading 'v'
1141-
}
1142-
1143-
func sshRestartMaster() error {
1144-
if !ProviderIs("gce", "aws") {
1145-
return fmt.Errorf("unsupported provider for sshRestartMaster: %s", TestContext.Provider)
1146-
}
1147-
var command string
1148-
if ProviderIs("gce") {
1149-
command = "pidof kube-apiserver | xargs sudo kill"
1150-
} else {
1151-
command = "sudo /etc/init.d/kube-apiserver restart"
1152-
}
1153-
Logf("Restarting master via ssh, running: %v", command)
1154-
result, err := e2essh.SSH(command, net.JoinHostPort(GetMasterHost(), sshPort), TestContext.Provider)
1155-
if err != nil || result.Code != 0 {
1156-
e2essh.LogResult(result)
1157-
return fmt.Errorf("couldn't restart apiserver: %v", err)
1158-
}
1159-
return nil
1160-
}
1161-
1162-
// waitForApiserverRestarted waits until apiserver's restart count increased.
1163-
func waitForApiserverRestarted(c clientset.Interface, initialRestartCount int32) error {
1164-
for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(5 * time.Second) {
1165-
restartCount, err := getApiserverRestartCount(c)
1166-
if err != nil {
1167-
Logf("Failed to get apiserver's restart count: %v", err)
1168-
continue
1169-
}
1170-
if restartCount > initialRestartCount {
1171-
Logf("Apiserver has restarted.")
1172-
return nil
1173-
}
1174-
Logf("Waiting for apiserver restart count to increase")
1175-
}
1176-
return fmt.Errorf("timed out waiting for apiserver to be restarted")
1177-
}
1178-
1179-
func getApiserverRestartCount(c clientset.Interface) (int32, error) {
1180-
label := labels.SelectorFromSet(labels.Set(map[string]string{"component": "kube-apiserver"}))
1181-
listOpts := metav1.ListOptions{LabelSelector: label.String()}
1182-
pods, err := c.CoreV1().Pods(metav1.NamespaceSystem).List(context.TODO(), listOpts)
1183-
if err != nil {
1184-
return -1, err
1185-
}
1186-
if len(pods.Items) != 1 {
1187-
return -1, fmt.Errorf("unexpected number of apiserver pod: %d", len(pods.Items))
1188-
}
1189-
for _, s := range pods.Items[0].Status.ContainerStatuses {
1190-
if s.Name != "kube-apiserver" {
1191-
continue
1192-
}
1193-
return s.RestartCount, nil
1194-
}
1195-
return -1, fmt.Errorf("Failed to find kube-apiserver container in pod")
1196-
}
1197-
11981118
// RestartControllerManager restarts the kube-controller-manager.
11991119
func RestartControllerManager() error {
12001120
// TODO: Make it work for all providers and distros.

test/e2e/network/service.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ const (
8484
// AffinityConfirmCount is the number of needed continuous requests to confirm that
8585
// affinity is enabled.
8686
AffinityConfirmCount = 15
87+
88+
// ssh port
89+
sshPort = "22"
8790
)
8891

8992
var (
@@ -1107,7 +1110,7 @@ var _ = SIGDescribe("Services", func() {
11071110

11081111
// Restart apiserver
11091112
ginkgo.By("Restarting apiserver")
1110-
if err := framework.RestartApiserver(ns, cs); err != nil {
1113+
if err := restartApiserver(ns, cs); err != nil {
11111114
framework.Failf("error restarting apiserver: %v", err)
11121115
}
11131116
ginkgo.By("Waiting for apiserver to come up by polling /healthz")
@@ -3431,3 +3434,83 @@ func validateEndpointsPorts(c clientset.Interface, namespace, serviceName string
34313434
}
34323435
return fmt.Errorf("Timed out waiting for service %s in namespace %s to expose endpoints %v (%v elapsed)", serviceName, namespace, expectedEndpoints, framework.ServiceStartTimeout)
34333436
}
3437+
3438+
// restartApiserver restarts the kube-apiserver.
3439+
func restartApiserver(namespace string, cs clientset.Interface) error {
3440+
// TODO: Make it work for all providers.
3441+
if !framework.ProviderIs("gce", "gke", "aws") {
3442+
return fmt.Errorf("unsupported provider for RestartApiserver: %s", framework.TestContext.Provider)
3443+
}
3444+
if framework.ProviderIs("gce", "aws") {
3445+
initialRestartCount, err := getApiserverRestartCount(cs)
3446+
if err != nil {
3447+
return fmt.Errorf("failed to get apiserver's restart count: %v", err)
3448+
}
3449+
if err := sshRestartMaster(); err != nil {
3450+
return fmt.Errorf("failed to restart apiserver: %v", err)
3451+
}
3452+
return waitForApiserverRestarted(cs, initialRestartCount)
3453+
}
3454+
// GKE doesn't allow ssh access, so use a same-version master
3455+
// upgrade to teardown/recreate master.
3456+
v, err := cs.Discovery().ServerVersion()
3457+
if err != nil {
3458+
return err
3459+
}
3460+
return framework.MasterUpgradeGKE(namespace, v.GitVersion[1:]) // strip leading 'v'
3461+
}
3462+
3463+
func sshRestartMaster() error {
3464+
if !framework.ProviderIs("gce", "aws") {
3465+
return fmt.Errorf("unsupported provider for sshRestartMaster: %s", framework.TestContext.Provider)
3466+
}
3467+
var command string
3468+
if framework.ProviderIs("gce") {
3469+
command = "pidof kube-apiserver | xargs sudo kill"
3470+
} else {
3471+
command = "sudo /etc/init.d/kube-apiserver restart"
3472+
}
3473+
framework.Logf("Restarting master via ssh, running: %v", command)
3474+
result, err := e2essh.SSH(command, net.JoinHostPort(framework.GetMasterHost(), sshPort), framework.TestContext.Provider)
3475+
if err != nil || result.Code != 0 {
3476+
e2essh.LogResult(result)
3477+
return fmt.Errorf("couldn't restart apiserver: %v", err)
3478+
}
3479+
return nil
3480+
}
3481+
3482+
// waitForApiserverRestarted waits until apiserver's restart count increased.
3483+
func waitForApiserverRestarted(c clientset.Interface, initialRestartCount int32) error {
3484+
for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(5 * time.Second) {
3485+
restartCount, err := getApiserverRestartCount(c)
3486+
if err != nil {
3487+
framework.Logf("Failed to get apiserver's restart count: %v", err)
3488+
continue
3489+
}
3490+
if restartCount > initialRestartCount {
3491+
framework.Logf("Apiserver has restarted.")
3492+
return nil
3493+
}
3494+
framework.Logf("Waiting for apiserver restart count to increase")
3495+
}
3496+
return fmt.Errorf("timed out waiting for apiserver to be restarted")
3497+
}
3498+
3499+
func getApiserverRestartCount(c clientset.Interface) (int32, error) {
3500+
label := labels.SelectorFromSet(labels.Set(map[string]string{"component": "kube-apiserver"}))
3501+
listOpts := metav1.ListOptions{LabelSelector: label.String()}
3502+
pods, err := c.CoreV1().Pods(metav1.NamespaceSystem).List(context.TODO(), listOpts)
3503+
if err != nil {
3504+
return -1, err
3505+
}
3506+
if len(pods.Items) != 1 {
3507+
return -1, fmt.Errorf("unexpected number of apiserver pod: %d", len(pods.Items))
3508+
}
3509+
for _, s := range pods.Items[0].Status.ContainerStatuses {
3510+
if s.Name != "kube-apiserver" {
3511+
continue
3512+
}
3513+
return s.RestartCount, nil
3514+
}
3515+
return -1, fmt.Errorf("Failed to find kube-apiserver container in pod")
3516+
}

0 commit comments

Comments
 (0)