Skip to content

Commit

Permalink
Merge pull request kubernetes#89442 from oomichi/RestartApiserver
Browse files Browse the repository at this point in the history
Move RestartApiserver() into e2e/network
  • Loading branch information
k8s-ci-robot authored Mar 26, 2020
2 parents 3cf4832 + 7398c83 commit 73da172
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 83 deletions.
5 changes: 3 additions & 2 deletions test/e2e/framework/nodes_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func MasterUpgrade(f *Framework, v string) error {
case "gce":
return masterUpgradeGCE(v, false)
case "gke":
return masterUpgradeGKE(f.Namespace.Name, v)
return MasterUpgradeGKE(f.Namespace.Name, v)
case "kubernetes-anywhere":
return masterUpgradeKubernetesAnywhere(v)
default:
Expand Down Expand Up @@ -113,7 +113,8 @@ func appendContainerCommandGroupIfNeeded(args []string) []string {
return args
}

func masterUpgradeGKE(namespace string, v string) error {
// MasterUpgradeGKE upgrades master node to the specified version on GKE.
func MasterUpgradeGKE(namespace string, v string) error {
Logf("Upgrading master to %q", v)
args := []string{
"container",
Expand Down
80 changes: 0 additions & 80 deletions test/e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
85 changes: 84 additions & 1 deletion test/e2e/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ const (
// AffinityConfirmCount is the number of needed continuous requests to confirm that
// affinity is enabled.
AffinityConfirmCount = 15

// ssh port
sshPort = "22"
)

var (
Expand Down Expand Up @@ -1107,7 +1110,7 @@ var _ = SIGDescribe("Services", func() {

// Restart apiserver
ginkgo.By("Restarting apiserver")
if err := framework.RestartApiserver(ns, cs); err != nil {
if err := restartApiserver(ns, cs); err != nil {
framework.Failf("error restarting apiserver: %v", err)
}
ginkgo.By("Waiting for apiserver to come up by polling /healthz")
Expand Down Expand Up @@ -3431,3 +3434,83 @@ func validateEndpointsPorts(c clientset.Interface, namespace, serviceName string
}
return fmt.Errorf("Timed out waiting for service %s in namespace %s to expose endpoints %v (%v elapsed)", serviceName, namespace, expectedEndpoints, framework.ServiceStartTimeout)
}

// restartApiserver restarts the kube-apiserver.
func restartApiserver(namespace string, cs clientset.Interface) error {
// TODO: Make it work for all providers.
if !framework.ProviderIs("gce", "gke", "aws") {
return fmt.Errorf("unsupported provider for RestartApiserver: %s", framework.TestContext.Provider)
}
if framework.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 framework.MasterUpgradeGKE(namespace, v.GitVersion[1:]) // strip leading 'v'
}

func sshRestartMaster() error {
if !framework.ProviderIs("gce", "aws") {
return fmt.Errorf("unsupported provider for sshRestartMaster: %s", framework.TestContext.Provider)
}
var command string
if framework.ProviderIs("gce") {
command = "pidof kube-apiserver | xargs sudo kill"
} else {
command = "sudo /etc/init.d/kube-apiserver restart"
}
framework.Logf("Restarting master via ssh, running: %v", command)
result, err := e2essh.SSH(command, net.JoinHostPort(framework.GetMasterHost(), sshPort), framework.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 {
framework.Logf("Failed to get apiserver's restart count: %v", err)
continue
}
if restartCount > initialRestartCount {
framework.Logf("Apiserver has restarted.")
return nil
}
framework.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")
}

0 comments on commit 73da172

Please sign in to comment.