2016-05-04 05:31:26 +00:00
|
|
|
// +build integration,!no-etcd
|
|
|
|
|
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2016-05-04 05:31:26 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-06-08 18:44:10 +00:00
|
|
|
package garbagecollector
|
2016-05-04 05:31:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strconv"
|
2016-05-18 03:24:42 +00:00
|
|
|
"strings"
|
2016-05-04 05:31:26 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-08-09 22:55:15 +00:00
|
|
|
"github.com/golang/glog"
|
2016-06-30 17:56:41 +00:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
2016-05-04 05:31:26 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-05-16 07:14:51 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/errors"
|
2016-05-04 05:31:26 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
|
|
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_3"
|
|
|
|
"k8s.io/kubernetes/pkg/client/restclient"
|
|
|
|
"k8s.io/kubernetes/pkg/client/typed/dynamic"
|
|
|
|
"k8s.io/kubernetes/pkg/controller/garbagecollector"
|
2016-07-02 06:46:00 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller/garbagecollector/metaonly"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime/serializer"
|
2016-05-04 05:31:26 +00:00
|
|
|
"k8s.io/kubernetes/pkg/types"
|
|
|
|
"k8s.io/kubernetes/pkg/util/wait"
|
|
|
|
"k8s.io/kubernetes/test/integration/framework"
|
|
|
|
)
|
|
|
|
|
2016-05-18 03:24:42 +00:00
|
|
|
func getOrphanOptions() *api.DeleteOptions {
|
|
|
|
var trueVar = true
|
|
|
|
return &api.DeleteOptions{OrphanDependents: &trueVar}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNonOrphanOptions() *api.DeleteOptions {
|
|
|
|
var falseVar = false
|
|
|
|
return &api.DeleteOptions{OrphanDependents: &falseVar}
|
|
|
|
}
|
|
|
|
|
2016-05-04 05:31:26 +00:00
|
|
|
const garbageCollectedPodName = "test.pod.1"
|
|
|
|
const independentPodName = "test.pod.2"
|
|
|
|
const oneValidOwnerPodName = "test.pod.3"
|
|
|
|
const toBeDeletedRCName = "test.rc.1"
|
|
|
|
const remainingRCName = "test.rc.2"
|
|
|
|
|
2016-07-05 08:58:22 +00:00
|
|
|
func newPod(podName, podNamespace string, ownerReferences []v1.OwnerReference) *v1.Pod {
|
2016-05-04 05:31:26 +00:00
|
|
|
for i := 0; i < len(ownerReferences); i++ {
|
2016-05-18 03:24:42 +00:00
|
|
|
if len(ownerReferences[i].Kind) == 0 {
|
|
|
|
ownerReferences[i].Kind = "ReplicationController"
|
|
|
|
}
|
2016-05-04 05:31:26 +00:00
|
|
|
ownerReferences[i].APIVersion = "v1"
|
|
|
|
}
|
|
|
|
return &v1.Pod{
|
|
|
|
TypeMeta: unversioned.TypeMeta{
|
|
|
|
Kind: "Pod",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: podName,
|
2016-07-05 08:58:22 +00:00
|
|
|
Namespace: podNamespace,
|
2016-05-04 05:31:26 +00:00
|
|
|
OwnerReferences: ownerReferences,
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: "fake-name",
|
|
|
|
Image: "fakeimage",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:58:22 +00:00
|
|
|
func newOwnerRC(name, namespace string) *v1.ReplicationController {
|
2016-05-04 05:31:26 +00:00
|
|
|
return &v1.ReplicationController{
|
|
|
|
TypeMeta: unversioned.TypeMeta{
|
|
|
|
Kind: "ReplicationController",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-07-05 08:58:22 +00:00
|
|
|
Namespace: namespace,
|
2016-05-04 05:31:26 +00:00
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
Spec: v1.ReplicationControllerSpec{
|
|
|
|
Selector: map[string]string{"name": "test"},
|
|
|
|
Template: &v1.PodTemplateSpec{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Labels: map[string]string{"name": "test"},
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: "fake-name",
|
|
|
|
Image: "fakeimage",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:58:22 +00:00
|
|
|
func setup(t *testing.T) (*httptest.Server, *garbagecollector.GarbageCollector, clientset.Interface) {
|
2016-05-04 05:31:26 +00:00
|
|
|
masterConfig := framework.NewIntegrationTestMasterConfig()
|
|
|
|
masterConfig.EnableCoreControllers = false
|
2016-09-12 23:14:04 +00:00
|
|
|
masterConfig.EnableGarbageCollection = true
|
2016-07-05 08:58:22 +00:00
|
|
|
_, s := framework.RunAMaster(masterConfig)
|
2016-05-04 05:31:26 +00:00
|
|
|
|
|
|
|
clientSet, err := clientset.NewForConfig(&restclient.Config{Host: s.URL})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error in create clientset: %v", err)
|
|
|
|
}
|
|
|
|
groupVersionResources, err := clientSet.Discovery().ServerPreferredResources()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get supported resources from server: %v", err)
|
|
|
|
}
|
2016-07-02 06:46:00 +00:00
|
|
|
config := &restclient.Config{Host: s.URL}
|
|
|
|
config.ContentConfig.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: metaonly.NewMetadataCodecFactory()}
|
|
|
|
metaOnlyClientPool := dynamic.NewClientPool(config, dynamic.LegacyAPIPathResolverFunc)
|
|
|
|
config.ContentConfig.NegotiatedSerializer = nil
|
|
|
|
clientPool := dynamic.NewClientPool(config, dynamic.LegacyAPIPathResolverFunc)
|
|
|
|
gc, err := garbagecollector.NewGarbageCollector(metaOnlyClientPool, clientPool, groupVersionResources)
|
2016-05-04 05:31:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create garbage collector")
|
|
|
|
}
|
2016-07-05 08:58:22 +00:00
|
|
|
return s, gc, clientSet
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test simulates the cascading deletion.
|
|
|
|
func TestCascadingDeletion(t *testing.T) {
|
2016-08-09 22:55:15 +00:00
|
|
|
glog.V(6).Infof("TestCascadingDeletion starts")
|
|
|
|
defer glog.V(6).Infof("TestCascadingDeletion ends")
|
2016-07-05 08:58:22 +00:00
|
|
|
s, gc, clientSet := setup(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
ns := framework.CreateTestingNamespace("gc-cascading-deletion", s, t)
|
|
|
|
defer framework.DeleteTestingNamespace(ns, s, t)
|
|
|
|
|
|
|
|
rcClient := clientSet.Core().ReplicationControllers(ns.Name)
|
|
|
|
podClient := clientSet.Core().Pods(ns.Name)
|
2016-05-04 05:31:26 +00:00
|
|
|
|
2016-07-05 08:58:22 +00:00
|
|
|
toBeDeletedRC, err := rcClient.Create(newOwnerRC(toBeDeletedRCName, ns.Name))
|
2016-05-04 05:31:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create replication controller: %v", err)
|
|
|
|
}
|
2016-07-05 08:58:22 +00:00
|
|
|
remainingRC, err := rcClient.Create(newOwnerRC(remainingRCName, ns.Name))
|
2016-05-04 05:31:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create replication controller: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rcs, err := rcClient.List(api.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to list replication controllers: %v", err)
|
|
|
|
}
|
|
|
|
if len(rcs.Items) != 2 {
|
|
|
|
t.Fatalf("Expect only 2 replication controller")
|
|
|
|
}
|
|
|
|
|
|
|
|
// this pod should be cascadingly deleted.
|
2016-07-05 08:58:22 +00:00
|
|
|
pod := newPod(garbageCollectedPodName, ns.Name, []v1.OwnerReference{{UID: toBeDeletedRC.ObjectMeta.UID, Name: toBeDeletedRCName}})
|
2016-05-04 05:31:26 +00:00
|
|
|
_, err = podClient.Create(pod)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create Pod: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-06-18 15:51:56 +00:00
|
|
|
// this pod shouldn't be cascadingly deleted, because it has a valid reference.
|
2016-07-05 08:58:22 +00:00
|
|
|
pod = newPod(oneValidOwnerPodName, ns.Name, []v1.OwnerReference{
|
2016-05-04 05:31:26 +00:00
|
|
|
{UID: toBeDeletedRC.ObjectMeta.UID, Name: toBeDeletedRCName},
|
|
|
|
{UID: remainingRC.ObjectMeta.UID, Name: remainingRCName},
|
|
|
|
})
|
|
|
|
_, err = podClient.Create(pod)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create Pod: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this pod shouldn't be cascadingly deleted, because it doesn't have an owner.
|
2016-07-05 08:58:22 +00:00
|
|
|
pod = newPod(independentPodName, ns.Name, []v1.OwnerReference{})
|
2016-05-04 05:31:26 +00:00
|
|
|
_, err = podClient.Create(pod)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create Pod: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up watch
|
|
|
|
pods, err := podClient.List(api.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to list pods: %v", err)
|
|
|
|
}
|
|
|
|
if len(pods.Items) != 3 {
|
|
|
|
t.Fatalf("Expect only 3 pods")
|
|
|
|
}
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
go gc.Run(5, stopCh)
|
|
|
|
defer close(stopCh)
|
|
|
|
// delete one of the replication controller
|
2016-08-17 21:23:09 +00:00
|
|
|
if err := rcClient.Delete(toBeDeletedRCName, getNonOrphanOptions()); err != nil {
|
2016-05-04 05:31:26 +00:00
|
|
|
t.Fatalf("failed to delete replication controller: %v", err)
|
|
|
|
}
|
2016-05-17 16:31:32 +00:00
|
|
|
|
2016-05-16 07:14:51 +00:00
|
|
|
// wait for the garbage collector to drain its queue
|
|
|
|
if err := wait.Poll(10*time.Second, 120*time.Second, func() (bool, error) {
|
|
|
|
return gc.QueuesDrained(), nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
2016-05-17 16:31:32 +00:00
|
|
|
// sometimes the deletion of the RC takes long time to be observed by
|
|
|
|
// the gc, so wait for the garbage collector to observe the deletion of
|
|
|
|
// the toBeDeletedRC
|
2016-07-02 06:46:00 +00:00
|
|
|
if err := wait.Poll(10*time.Second, 60*time.Second, func() (bool, error) {
|
2016-05-17 16:31:32 +00:00
|
|
|
return !gc.GraphHasUID([]types.UID{toBeDeletedRC.ObjectMeta.UID}), nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// wait for the garbage collector to drain its queue again because it's
|
|
|
|
// possible it just processed the delete of the toBeDeletedRC.
|
|
|
|
if err := wait.Poll(10*time.Second, 120*time.Second, func() (bool, error) {
|
|
|
|
return gc.QueuesDrained(), nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:14:51 +00:00
|
|
|
t.Logf("garbage collector queues drained")
|
2016-05-04 05:31:26 +00:00
|
|
|
// checks the garbage collect doesn't delete pods it shouldn't do.
|
|
|
|
if _, err := podClient.Get(independentPodName); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := podClient.Get(oneValidOwnerPodName); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-16 07:14:51 +00:00
|
|
|
if _, err := podClient.Get(garbageCollectedPodName); err == nil || !errors.IsNotFound(err) {
|
2016-05-17 16:31:32 +00:00
|
|
|
t.Fatalf("expect pod %s to be garbage collected, got err= %v", garbageCollectedPodName, err)
|
2016-05-16 07:14:51 +00:00
|
|
|
}
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test simulates the case where an object is created with an owner that
|
|
|
|
// doesn't exist. It verifies the GC will delete such an object.
|
2016-06-17 14:20:49 +00:00
|
|
|
func TestCreateWithNonExistentOwner(t *testing.T) {
|
2016-08-09 22:55:15 +00:00
|
|
|
glog.V(6).Infof("TestCreateWithNonExistentOwner starts")
|
|
|
|
defer glog.V(6).Infof("TestCreateWithNonExistentOwner ends")
|
2016-07-05 08:58:22 +00:00
|
|
|
s, gc, clientSet := setup(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
ns := framework.CreateTestingNamespace("gc-non-existing-owner", s, t)
|
|
|
|
defer framework.DeleteTestingNamespace(ns, s, t)
|
|
|
|
|
|
|
|
podClient := clientSet.Core().Pods(ns.Name)
|
2016-05-04 05:31:26 +00:00
|
|
|
|
2016-07-05 08:58:22 +00:00
|
|
|
pod := newPod(garbageCollectedPodName, ns.Name, []v1.OwnerReference{{UID: "doesn't matter", Name: toBeDeletedRCName}})
|
2016-05-04 05:31:26 +00:00
|
|
|
_, err := podClient.Create(pod)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create Pod: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up watch
|
|
|
|
pods, err := podClient.List(api.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to list pods: %v", err)
|
|
|
|
}
|
|
|
|
if len(pods.Items) != 1 {
|
|
|
|
t.Fatalf("Expect only 1 pod")
|
|
|
|
}
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
go gc.Run(5, stopCh)
|
|
|
|
defer close(stopCh)
|
2016-05-16 07:14:51 +00:00
|
|
|
// wait for the garbage collector to drain its queue
|
|
|
|
if err := wait.Poll(10*time.Second, 120*time.Second, func() (bool, error) {
|
|
|
|
return gc.QueuesDrained(), nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
2016-05-16 07:14:51 +00:00
|
|
|
t.Logf("garbage collector queues drained")
|
|
|
|
if _, err := podClient.Get(garbageCollectedPodName); err == nil || !errors.IsNotFound(err) {
|
|
|
|
t.Fatalf("expect pod %s to be garbage collected", garbageCollectedPodName)
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:58:22 +00:00
|
|
|
func setupRCsPods(t *testing.T, gc *garbagecollector.GarbageCollector, clientSet clientset.Interface, nameSuffix, namespace string, initialFinalizers []string, options *api.DeleteOptions, wg *sync.WaitGroup, rcUIDs chan types.UID) {
|
2016-05-04 05:31:26 +00:00
|
|
|
defer wg.Done()
|
2016-07-05 08:58:22 +00:00
|
|
|
rcClient := clientSet.Core().ReplicationControllers(namespace)
|
|
|
|
podClient := clientSet.Core().Pods(namespace)
|
2016-05-04 05:31:26 +00:00
|
|
|
// create rc.
|
2016-05-18 03:24:42 +00:00
|
|
|
rcName := "test.rc." + nameSuffix
|
2016-07-05 08:58:22 +00:00
|
|
|
rc := newOwnerRC(rcName, namespace)
|
2016-05-18 03:24:42 +00:00
|
|
|
rc.ObjectMeta.Finalizers = initialFinalizers
|
|
|
|
rc, err := rcClient.Create(rc)
|
2016-05-04 05:31:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create replication controller: %v", err)
|
|
|
|
}
|
2016-05-18 03:24:42 +00:00
|
|
|
rcUIDs <- rc.ObjectMeta.UID
|
|
|
|
// create pods.
|
|
|
|
var podUIDs []types.UID
|
2016-05-04 05:31:26 +00:00
|
|
|
for j := 0; j < 3; j++ {
|
2016-05-18 03:24:42 +00:00
|
|
|
podName := "test.pod." + nameSuffix + "-" + strconv.Itoa(j)
|
2016-07-05 08:58:22 +00:00
|
|
|
pod := newPod(podName, namespace, []v1.OwnerReference{{UID: rc.ObjectMeta.UID, Name: rc.ObjectMeta.Name}})
|
2016-05-04 05:31:26 +00:00
|
|
|
_, err = podClient.Create(pod)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create Pod: %v", err)
|
|
|
|
}
|
2016-05-18 03:24:42 +00:00
|
|
|
podUIDs = append(podUIDs, pod.ObjectMeta.UID)
|
|
|
|
}
|
|
|
|
orphan := (options != nil && options.OrphanDependents != nil && *options.OrphanDependents) || (options == nil && len(initialFinalizers) != 0 && initialFinalizers[0] == api.FinalizerOrphan)
|
|
|
|
// if we intend to orphan the pods, we need wait for the gc to observe the
|
|
|
|
// creation of the pods, otherwise if the deletion of RC is observed before
|
|
|
|
// the creation of the pods, the pods will not be orphaned.
|
|
|
|
if orphan {
|
|
|
|
wait.Poll(5*time.Second, 60*time.Second, func() (bool, error) { return gc.GraphHasUID(podUIDs), nil })
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
|
|
|
// delete the rc
|
2016-05-18 03:24:42 +00:00
|
|
|
if err := rcClient.Delete(rc.ObjectMeta.Name, options); err != nil {
|
2016-05-04 05:31:26 +00:00
|
|
|
t.Fatalf("failed to delete replication controller: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:58:22 +00:00
|
|
|
func verifyRemainingObjects(t *testing.T, clientSet clientset.Interface, namespace string, rcNum, podNum int) (bool, error) {
|
|
|
|
rcClient := clientSet.Core().ReplicationControllers(namespace)
|
|
|
|
podClient := clientSet.Core().Pods(namespace)
|
2016-05-04 05:31:26 +00:00
|
|
|
pods, err := podClient.List(api.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Failed to list pods: %v", err)
|
|
|
|
}
|
2016-05-18 03:24:42 +00:00
|
|
|
var ret = true
|
|
|
|
if len(pods.Items) != podNum {
|
|
|
|
ret = false
|
|
|
|
t.Logf("expect %d pods, got %d pods", podNum, len(pods.Items))
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
|
|
|
rcs, err := rcClient.List(api.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Failed to list replication controllers: %v", err)
|
|
|
|
}
|
2016-05-18 03:24:42 +00:00
|
|
|
if len(rcs.Items) != rcNum {
|
|
|
|
ret = false
|
|
|
|
t.Logf("expect %d RCs, got %d RCs", rcNum, len(rcs.Items))
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
2016-05-18 03:24:42 +00:00
|
|
|
return ret, nil
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:57:48 +00:00
|
|
|
// The stress test is not very stressful, because we need to control the running
|
|
|
|
// time of our pre-submit tests to increase submit-queue throughput. We'll add
|
|
|
|
// e2e tests that put more stress.
|
2016-05-04 05:31:26 +00:00
|
|
|
func TestStressingCascadingDeletion(t *testing.T) {
|
|
|
|
t.Logf("starts garbage collector stress test")
|
2016-07-05 08:58:22 +00:00
|
|
|
s, gc, clientSet := setup(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
ns := framework.CreateTestingNamespace("gc-stressing-cascading-deletion", s, t)
|
|
|
|
defer framework.DeleteTestingNamespace(ns, s, t)
|
|
|
|
|
2016-05-04 05:31:26 +00:00
|
|
|
stopCh := make(chan struct{})
|
|
|
|
go gc.Run(5, stopCh)
|
|
|
|
defer close(stopCh)
|
|
|
|
|
2016-06-07 22:57:48 +00:00
|
|
|
const collections = 10
|
2016-05-04 05:31:26 +00:00
|
|
|
var wg sync.WaitGroup
|
2016-05-18 03:24:42 +00:00
|
|
|
wg.Add(collections * 4)
|
|
|
|
rcUIDs := make(chan types.UID, collections*4)
|
2016-05-04 05:31:26 +00:00
|
|
|
for i := 0; i < collections; i++ {
|
2016-08-17 21:23:09 +00:00
|
|
|
// rc is created with empty finalizers, deleted with nil delete options, pods will remain.
|
2016-07-05 08:58:22 +00:00
|
|
|
go setupRCsPods(t, gc, clientSet, "collection1-"+strconv.Itoa(i), ns.Name, []string{}, nil, &wg, rcUIDs)
|
2016-05-18 03:24:42 +00:00
|
|
|
// rc is created with the orphan finalizer, deleted with nil options, pods will remain.
|
2016-07-05 08:58:22 +00:00
|
|
|
go setupRCsPods(t, gc, clientSet, "collection2-"+strconv.Itoa(i), ns.Name, []string{api.FinalizerOrphan}, nil, &wg, rcUIDs)
|
2016-05-18 03:24:42 +00:00
|
|
|
// rc is created with the orphan finalizer, deleted with DeleteOptions.OrphanDependents=false, pods will be deleted.
|
2016-07-05 08:58:22 +00:00
|
|
|
go setupRCsPods(t, gc, clientSet, "collection3-"+strconv.Itoa(i), ns.Name, []string{api.FinalizerOrphan}, getNonOrphanOptions(), &wg, rcUIDs)
|
2016-05-18 03:24:42 +00:00
|
|
|
// rc is created with empty finalizers, deleted with DeleteOptions.OrphanDependents=true, pods will remain.
|
2016-07-05 08:58:22 +00:00
|
|
|
go setupRCsPods(t, gc, clientSet, "collection4-"+strconv.Itoa(i), ns.Name, []string{}, getOrphanOptions(), &wg, rcUIDs)
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
t.Logf("all pods are created, all replications controllers are created then deleted")
|
|
|
|
// wait for the garbage collector to drain its queue
|
|
|
|
if err := wait.Poll(10*time.Second, 300*time.Second, func() (bool, error) {
|
|
|
|
return gc.QueuesDrained(), nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Logf("garbage collector queues drained")
|
2016-05-18 03:24:42 +00:00
|
|
|
// wait for the RCs and Pods to reach the expected numbers. This shouldn't
|
|
|
|
// take long, because the queues are already drained.
|
2016-05-04 05:31:26 +00:00
|
|
|
if err := wait.Poll(5*time.Second, 30*time.Second, func() (bool, error) {
|
2016-05-18 03:24:42 +00:00
|
|
|
podsInEachCollection := 3
|
|
|
|
// see the comments on the calls to setupRCsPods for details
|
2016-08-17 21:23:09 +00:00
|
|
|
remainingGroups := 3
|
2016-07-05 08:58:22 +00:00
|
|
|
return verifyRemainingObjects(t, clientSet, ns.Name, 0, collections*podsInEachCollection*remainingGroups)
|
2016-05-04 05:31:26 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-18 03:24:42 +00:00
|
|
|
t.Logf("number of remaining replication controllers and pods are as expected")
|
|
|
|
|
|
|
|
// verify the remaining pods all have "orphan" in their names.
|
2016-07-05 08:58:22 +00:00
|
|
|
podClient := clientSet.Core().Pods(ns.Name)
|
2016-05-18 03:24:42 +00:00
|
|
|
pods, err := podClient.List(api.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, pod := range pods.Items {
|
2016-08-17 21:23:09 +00:00
|
|
|
if !strings.Contains(pod.ObjectMeta.Name, "collection1-") && !strings.Contains(pod.ObjectMeta.Name, "collection2-") && !strings.Contains(pod.ObjectMeta.Name, "collection4-") {
|
2016-05-18 03:24:42 +00:00
|
|
|
t.Errorf("got unexpected remaining pod: %#v", pod)
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 05:31:26 +00:00
|
|
|
|
|
|
|
// verify there is no node representing replication controllers in the gc's graph
|
|
|
|
uids := make([]types.UID, 0, collections)
|
|
|
|
for i := 0; i < collections; i++ {
|
|
|
|
uid := <-rcUIDs
|
|
|
|
uids = append(uids, uid)
|
|
|
|
}
|
|
|
|
if gc.GraphHasUID(uids) {
|
|
|
|
t.Errorf("Expect all nodes representing replication controllers are removed from the Propagator's graph")
|
|
|
|
}
|
2016-06-30 17:56:41 +00:00
|
|
|
metric := &dto.Metric{}
|
|
|
|
garbagecollector.EventProcessingLatency.Write(metric)
|
|
|
|
count := float64(metric.Summary.GetSampleCount())
|
|
|
|
sum := metric.Summary.GetSampleSum()
|
|
|
|
t.Logf("Average time spent in GC's eventQueue is %.1f microseconds", sum/count)
|
|
|
|
garbagecollector.DirtyProcessingLatency.Write(metric)
|
|
|
|
count = float64(metric.Summary.GetSampleCount())
|
|
|
|
sum = metric.Summary.GetSampleSum()
|
|
|
|
t.Logf("Average time spent in GC's dirtyQueue is %.1f microseconds", sum/count)
|
|
|
|
garbagecollector.OrphanProcessingLatency.Write(metric)
|
|
|
|
count = float64(metric.Summary.GetSampleCount())
|
|
|
|
sum = metric.Summary.GetSampleSum()
|
|
|
|
t.Logf("Average time spent in GC's orphanQueue is %.1f microseconds", sum/count)
|
2016-05-04 05:31:26 +00:00
|
|
|
}
|
2016-05-18 03:24:42 +00:00
|
|
|
|
|
|
|
func TestOrphaning(t *testing.T) {
|
2016-07-05 08:58:22 +00:00
|
|
|
s, gc, clientSet := setup(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
ns := framework.CreateTestingNamespace("gc-orphaning", s, t)
|
|
|
|
defer framework.DeleteTestingNamespace(ns, s, t)
|
|
|
|
|
|
|
|
podClient := clientSet.Core().Pods(ns.Name)
|
|
|
|
rcClient := clientSet.Core().ReplicationControllers(ns.Name)
|
2016-05-18 03:24:42 +00:00
|
|
|
// create the RC with the orphan finalizer set
|
2016-07-05 08:58:22 +00:00
|
|
|
toBeDeletedRC := newOwnerRC(toBeDeletedRCName, ns.Name)
|
2016-05-18 03:24:42 +00:00
|
|
|
toBeDeletedRC, err := rcClient.Create(toBeDeletedRC)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create replication controller: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// these pods should be ophaned.
|
|
|
|
var podUIDs []types.UID
|
|
|
|
podsNum := 3
|
|
|
|
for i := 0; i < podsNum; i++ {
|
|
|
|
podName := garbageCollectedPodName + strconv.Itoa(i)
|
2016-07-05 08:58:22 +00:00
|
|
|
pod := newPod(podName, ns.Name, []v1.OwnerReference{{UID: toBeDeletedRC.ObjectMeta.UID, Name: toBeDeletedRCName}})
|
2016-05-18 03:24:42 +00:00
|
|
|
_, err = podClient.Create(pod)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create Pod: %v", err)
|
|
|
|
}
|
|
|
|
podUIDs = append(podUIDs, pod.ObjectMeta.UID)
|
|
|
|
}
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
go gc.Run(5, stopCh)
|
|
|
|
defer close(stopCh)
|
|
|
|
|
|
|
|
// we need wait for the gc to observe the creation of the pods, otherwise if
|
|
|
|
// the deletion of RC is observed before the creation of the pods, the pods
|
|
|
|
// will not be orphaned.
|
|
|
|
wait.Poll(5*time.Second, 60*time.Second, func() (bool, error) { return gc.GraphHasUID(podUIDs), nil })
|
|
|
|
|
|
|
|
err = rcClient.Delete(toBeDeletedRCName, getOrphanOptions())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to gracefully delete the rc: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for the garbage collector to drain its queue
|
|
|
|
if err := wait.Poll(10*time.Second, 300*time.Second, func() (bool, error) {
|
|
|
|
return gc.QueuesDrained(), nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify pods don't have the ownerPod as an owner anymore
|
|
|
|
pods, err := podClient.List(api.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to list pods: %v", err)
|
|
|
|
}
|
|
|
|
if len(pods.Items) != podsNum {
|
|
|
|
t.Errorf("Expect %d pod(s), but got %#v", podsNum, pods)
|
|
|
|
}
|
|
|
|
for _, pod := range pods.Items {
|
|
|
|
if len(pod.ObjectMeta.OwnerReferences) != 0 {
|
|
|
|
t.Errorf("pod %s still has non-empty OwnerRefereces: %v", pod.ObjectMeta.Name, pod.ObjectMeta.OwnerReferences)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// verify the toBeDeleteRC is deleted
|
|
|
|
rcs, err := rcClient.List(api.ListOptions{})
|
|
|
|
if len(rcs.Items) != 0 {
|
|
|
|
t.Errorf("Expect RCs to be deleted, but got %#v", rcs.Items)
|
|
|
|
}
|
|
|
|
}
|