func deleteAllStatefulSets(c clientset.Interface, ns string) { pst := &statefulSetTester{c: c} psList, err := c.Apps().StatefulSets(ns).List(v1.ListOptions{LabelSelector: labels.Everything().String()}) framework.ExpectNoError(err) // Scale down each statefulset, then delete it completely. // Deleting a pvc without doing this will leak volumes, #25101. errList := []string{} for _, ps := range psList.Items { framework.Logf("Scaling statefulset %v to 0", ps.Name) if err := pst.scale(&ps, 0); err != nil { errList = append(errList, fmt.Sprintf("%v", err)) } pst.waitForStatus(&ps, 0) framework.Logf("Deleting statefulset %v", ps.Name) if err := c.Apps().StatefulSets(ps.Namespace).Delete(ps.Name, nil); err != nil { errList = append(errList, fmt.Sprintf("%v", err)) } } // pvs are global, so we need to wait for the exact ones bound to the statefulset pvcs. pvNames := sets.NewString() // TODO: Don't assume all pvcs in the ns belong to a statefulset pvcPollErr := wait.PollImmediate(statefulsetPoll, statefulsetTimeout, func() (bool, error) { pvcList, err := c.Core().PersistentVolumeClaims(ns).List(v1.ListOptions{LabelSelector: labels.Everything().String()}) if err != nil { framework.Logf("WARNING: Failed to list pvcs, retrying %v", err) return false, nil } for _, pvc := range pvcList.Items { pvNames.Insert(pvc.Spec.VolumeName) // TODO: Double check that there are no pods referencing the pvc framework.Logf("Deleting pvc: %v with volume %v", pvc.Name, pvc.Spec.VolumeName) if err := c.Core().PersistentVolumeClaims(ns).Delete(pvc.Name, nil); err != nil { return false, nil } } return true, nil }) if pvcPollErr != nil { errList = append(errList, fmt.Sprintf("Timeout waiting for pvc deletion.")) } pollErr := wait.PollImmediate(statefulsetPoll, statefulsetTimeout, func() (bool, error) { pvList, err := c.Core().PersistentVolumes().List(v1.ListOptions{LabelSelector: labels.Everything().String()}) if err != nil { framework.Logf("WARNING: Failed to list pvs, retrying %v", err) return false, nil } waitingFor := []string{} for _, pv := range pvList.Items { if pvNames.Has(pv.Name) { waitingFor = append(waitingFor, fmt.Sprintf("%v: %+v", pv.Name, pv.Status)) } } if len(waitingFor) == 0 { return true, nil } framework.Logf("Still waiting for pvs of statefulset to disappear:\n%v", strings.Join(waitingFor, "\n")) return false, nil }) if pollErr != nil { errList = append(errList, fmt.Sprintf("Timeout waiting for pv provisioner to delete pvs, this might mean the test leaked pvs.")) } if len(errList) != 0 { framework.ExpectNoError(fmt.Errorf("%v", strings.Join(errList, "\n"))) } }
func verifyExpectedRcsExistAndGetExpectedPods(c clientset.Interface) ([]string, error) { expectedPods := []string{} // Iterate over the labels that identify the replication controllers that we // want to check. The rcLabels contains the value values for the k8s-app key // that identify the replication controllers that we want to check. Using a label // rather than an explicit name is preferred because the names will typically have // a version suffix e.g. heapster-monitoring-v1 and this will change after a rolling // update e.g. to heapster-monitoring-v2. By using a label query we can check for the // situation when a heapster-monitoring-v1 and heapster-monitoring-v2 replication controller // is running (which would be an error except during a rolling update). for _, rcLabel := range rcLabels { selector := labels.Set{"k8s-app": rcLabel}.AsSelector() options := v1.ListOptions{LabelSelector: selector.String()} deploymentList, err := c.Extensions().Deployments(api.NamespaceSystem).List(options) if err != nil { return nil, err } rcList, err := c.Core().ReplicationControllers(api.NamespaceSystem).List(options) if err != nil { return nil, err } psList, err := c.Apps().StatefulSets(api.NamespaceSystem).List(options) if err != nil { return nil, err } if (len(rcList.Items) + len(deploymentList.Items) + len(psList.Items)) != 1 { return nil, fmt.Errorf("expected to find one replica for RC or deployment with label %s but got %d", rcLabel, len(rcList.Items)) } // Check all the replication controllers. for _, rc := range rcList.Items { selector := labels.Set(rc.Spec.Selector).AsSelector() options := v1.ListOptions{LabelSelector: selector.String()} podList, err := c.Core().Pods(api.NamespaceSystem).List(options) if err != nil { return nil, err } for _, pod := range podList.Items { if pod.DeletionTimestamp != nil { continue } expectedPods = append(expectedPods, string(pod.UID)) } } // Do the same for all deployments. for _, rc := range deploymentList.Items { selector := labels.Set(rc.Spec.Selector.MatchLabels).AsSelector() options := v1.ListOptions{LabelSelector: selector.String()} podList, err := c.Core().Pods(api.NamespaceSystem).List(options) if err != nil { return nil, err } for _, pod := range podList.Items { if pod.DeletionTimestamp != nil { continue } expectedPods = append(expectedPods, string(pod.UID)) } } // And for pet sets. for _, ps := range psList.Items { selector := labels.Set(ps.Spec.Selector.MatchLabels).AsSelector() options := v1.ListOptions{LabelSelector: selector.String()} podList, err := c.Core().Pods(api.NamespaceSystem).List(options) if err != nil { return nil, err } for _, pod := range podList.Items { if pod.DeletionTimestamp != nil { continue } expectedPods = append(expectedPods, string(pod.UID)) } } } return expectedPods, nil }
}) AfterEach(func() { if CurrentGinkgoTestDescription().Failed { dumpDebugInfo(c, ns) } framework.Logf("Deleting all statefulset in ns %v", ns) deleteAllStatefulSets(c, ns) }) It("should provide basic identity", func() { By("Creating statefulset " + psName + " in namespace " + ns) *(ps.Spec.Replicas) = 3 setInitializedAnnotation(ps, "false") _, err := c.Apps().StatefulSets(ns).Create(ps) Expect(err).NotTo(HaveOccurred()) pst := statefulSetTester{c: c} By("Saturating stateful set " + ps.Name) pst.saturate(ps) By("Verifying statefulset mounted data directory is usable") framework.ExpectNoError(pst.checkMount(ps, "/data")) By("Verifying statefulset provides a stable hostname for each pod") framework.ExpectNoError(pst.checkHostname(ps)) cmd := "echo $(hostname) > /data/hostname; sync;" By("Running " + cmd + " in all stateful pods")
}) AfterEach(func() { if CurrentGinkgoTestDescription().Failed { dumpDebugInfo(c, ns) } framework.Logf("Deleting all statefulset in ns %v", ns) deleteAllStatefulSets(c, ns) }) It("should provide basic identity", func() { By("Creating statefulset " + ssName + " in namespace " + ns) *(ss.Spec.Replicas) = 3 setInitializedAnnotation(ss, "false") _, err := c.Apps().StatefulSets(ns).Create(ss) Expect(err).NotTo(HaveOccurred()) sst := statefulSetTester{c: c} By("Saturating stateful set " + ss.Name) sst.saturate(ss) By("Verifying statefulset mounted data directory is usable") framework.ExpectNoError(sst.checkMount(ss, "/data")) By("Verifying statefulset provides a stable hostname for each pod") framework.ExpectNoError(sst.checkHostname(ss)) cmd := "echo $(hostname) > /data/hostname; sync;" By("Running " + cmd + " in all stateful pods")