Track Job Pods completion in status
Through Job.status.uncountedPodUIDs and a Pod finalizer An annotation marks if a job should be tracked with new behavior A separate work queue is used to remove finalizers from orphan pods. Change-Id: I1862e930257a9d1f7f1b2b0a526ed15bc8c248ad
This commit is contained in:
@@ -17,9 +17,10 @@ limitations under the License.
|
||||
package controller
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
apps "k8s.io/api/apps/v1"
|
||||
"k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -63,6 +64,7 @@ func TestClaimPods(t *testing.T) {
|
||||
manager *PodControllerRefManager
|
||||
pods []*v1.Pod
|
||||
claimed []*v1.Pod
|
||||
patches int
|
||||
}
|
||||
var tests = []test{
|
||||
{
|
||||
@@ -74,6 +76,7 @@ func TestClaimPods(t *testing.T) {
|
||||
func() error { return nil }),
|
||||
pods: []*v1.Pod{newPod("pod1", productionLabel, nil), newPod("pod2", testLabel, nil)},
|
||||
claimed: []*v1.Pod{newPod("pod1", productionLabel, nil)},
|
||||
patches: 1,
|
||||
},
|
||||
func() test {
|
||||
controller := v1.ReplicationController{}
|
||||
@@ -135,6 +138,7 @@ func TestClaimPods(t *testing.T) {
|
||||
func() error { return nil }),
|
||||
pods: []*v1.Pod{newPod("pod1", productionLabel, &controller), newPod("pod2", testLabel, &controller)},
|
||||
claimed: []*v1.Pod{newPod("pod1", productionLabel, &controller)},
|
||||
patches: 1,
|
||||
}
|
||||
}(),
|
||||
func() test {
|
||||
@@ -157,22 +161,50 @@ func TestClaimPods(t *testing.T) {
|
||||
claimed: []*v1.Pod{podToDelete1},
|
||||
}
|
||||
}(),
|
||||
func() test {
|
||||
controller := v1.ReplicationController{}
|
||||
controller.UID = types.UID(controllerUID)
|
||||
return test{
|
||||
name: "Controller claims or release pods according to selector with finalizers",
|
||||
manager: NewPodControllerRefManager(&FakePodControl{},
|
||||
&controller,
|
||||
productionLabelSelector,
|
||||
controllerKind,
|
||||
func() error { return nil },
|
||||
"foo-finalizer", "bar-finalizer"),
|
||||
pods: []*v1.Pod{newPod("pod1", productionLabel, &controller), newPod("pod2", testLabel, &controller), newPod("pod3", productionLabel, nil)},
|
||||
claimed: []*v1.Pod{newPod("pod1", productionLabel, &controller), newPod("pod3", productionLabel, nil)},
|
||||
patches: 2,
|
||||
}
|
||||
}(),
|
||||
}
|
||||
for _, test := range tests {
|
||||
claimed, err := test.manager.ClaimPods(test.pods)
|
||||
if err != nil {
|
||||
t.Errorf("Test case `%s`, unexpected error: %v", test.name, err)
|
||||
} else if !reflect.DeepEqual(test.claimed, claimed) {
|
||||
t.Errorf("Test case `%s`, claimed wrong pods. Expected %v, got %v", test.name, podToStringSlice(test.claimed), podToStringSlice(claimed))
|
||||
}
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
claimed, err := test.manager.ClaimPods(test.pods)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
if diff := cmp.Diff(test.claimed, claimed); diff != "" {
|
||||
t.Errorf("Claimed wrong pods (-want,+got):\n%s", diff)
|
||||
}
|
||||
fakePodControl, ok := test.manager.podControl.(*FakePodControl)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if p := len(fakePodControl.Patches); p != test.patches {
|
||||
t.Errorf("ClaimPods issues %d patches, want %d", p, test.patches)
|
||||
}
|
||||
for _, p := range fakePodControl.Patches {
|
||||
patch := string(p)
|
||||
if uid := string(test.manager.Controller.GetUID()); !strings.Contains(patch, uid) {
|
||||
t.Errorf("Patch doesn't contain controller UID %s", uid)
|
||||
}
|
||||
for _, f := range test.manager.finalizers {
|
||||
if !strings.Contains(patch, f) {
|
||||
t.Errorf("Patch doesn't contain finalizer %q", f)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func podToStringSlice(pods []*v1.Pod) []string {
|
||||
var names []string
|
||||
for _, pod := range pods {
|
||||
names = append(names, pod.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
Reference in New Issue
Block a user