Bugfix prevent daemon controller to adopt controller revisions of other namespaces

This commit is contained in:
evertrain
2021-11-01 17:36:15 +08:00
parent 37efc5feec
commit 9ceb226c06
5 changed files with 132 additions and 12 deletions

View File

@@ -225,6 +225,19 @@ func addFailedPods(podStore cache.Store, nodeName string, label map[string]strin
}
}
func newControllerRevision(name string, namespace string, label map[string]string,
ownerReferences []metav1.OwnerReference) *apps.ControllerRevision {
return &apps.ControllerRevision{
TypeMeta: metav1.TypeMeta{APIVersion: "apps/v1"},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: label,
Namespace: namespace,
OwnerReferences: ownerReferences,
},
}
}
type fakePodControl struct {
sync.Mutex
*controller.FakePodControl

View File

@@ -406,7 +406,7 @@ func (dsc *DaemonSetsController) controlledHistories(ctx context.Context, ds *ap
// List all histories to include those that don't match the selector anymore
// but have a ControllerRef pointing to the controller.
histories, err := dsc.historyLister.List(labels.Everything())
histories, err := dsc.historyLister.ControllerRevisions(ds.Namespace).List(labels.Everything())
if err != nil {
return nil, err
}

View File

@@ -18,6 +18,7 @@ package daemon
import (
"context"
"reflect"
"testing"
"time"
@@ -635,3 +636,73 @@ func TestGetUnavailableNumbers(t *testing.T) {
})
}
}
func TestControlledHistories(t *testing.T) {
ds1 := newDaemonSet("ds1")
crOfDs1 := newControllerRevision(ds1.GetName()+"-x1", ds1.GetNamespace(), ds1.Spec.Template.Labels,
[]metav1.OwnerReference{*metav1.NewControllerRef(ds1, controllerKind)})
orphanCrInSameNsWithDs1 := newControllerRevision(ds1.GetName()+"-x2", ds1.GetNamespace(), ds1.Spec.Template.Labels, nil)
orphanCrNotInSameNsWithDs1 := newControllerRevision(ds1.GetName()+"-x3", ds1.GetNamespace()+"-other", ds1.Spec.Template.Labels, nil)
cases := []struct {
name string
manager *daemonSetsController
historyCRAll []*apps.ControllerRevision
expectControllerRevisions []*apps.ControllerRevision
}{
{
name: "controller revision in the same namespace",
manager: func() *daemonSetsController {
manager, _, _, err := newTestController(ds1, crOfDs1, orphanCrInSameNsWithDs1)
if err != nil {
t.Fatalf("error creating DaemonSets controller: %v", err)
}
manager.dsStore.Add(ds1)
return manager
}(),
historyCRAll: []*apps.ControllerRevision{crOfDs1, orphanCrInSameNsWithDs1},
expectControllerRevisions: []*apps.ControllerRevision{crOfDs1, orphanCrInSameNsWithDs1},
},
{
name: "Skip adopting the controller revision in namespace other than the one in which DS lives",
manager: func() *daemonSetsController {
manager, _, _, err := newTestController(ds1, orphanCrNotInSameNsWithDs1)
if err != nil {
t.Fatalf("error creating DaemonSets controller: %v", err)
}
manager.dsStore.Add(ds1)
return manager
}(),
historyCRAll: []*apps.ControllerRevision{orphanCrNotInSameNsWithDs1},
expectControllerRevisions: []*apps.ControllerRevision{},
},
}
for _, c := range cases {
for _, h := range c.historyCRAll {
c.manager.historyStore.Add(h)
}
crList, err := c.manager.controlledHistories(context.TODO(), ds1)
if err != nil {
t.Fatalf("Test case: %s. Unexpected error: %v", c.name, err)
}
if len(crList) != len(c.expectControllerRevisions) {
t.Errorf("Test case: %s, expect controllerrevision count %d but got:%d",
c.name, len(c.expectControllerRevisions), len(crList))
} else {
// check controller revisions match
for _, cr := range crList {
found := false
for _, expectCr := range c.expectControllerRevisions {
if reflect.DeepEqual(cr, expectCr) {
found = true
break
}
}
if !found {
t.Errorf("Test case: %s, controllerrevision %v not expected",
c.name, cr)
}
}
t.Logf("Test case: %s done", c.name)
}
}
}