Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Add injectFinalizer property
Browse files Browse the repository at this point in the history
Signed-off-by: Filipe Regadas <[email protected]>
  • Loading branch information
regadas committed Mar 5, 2021
1 parent 814fc86 commit d3f3689
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
5 changes: 4 additions & 1 deletion pkg/controller/nodes/task/k8s/plugin_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type PluginManager struct {
backOffController *backoff.Controller
resourceLevelMonitor *ResourceLevelMonitor
ignoreOwnerReferences bool
injectFinalizer *bool
}

func (e *PluginManager) AddObjectMetadata(taskCtx pluginsCore.TaskExecutionMetadata, o k8s.Resource, cfg *config.K8sPluginConfig) {
Expand All @@ -110,7 +111,8 @@ func (e *PluginManager) AddObjectMetadata(taskCtx pluginsCore.TaskExecutionMetad
o.SetOwnerReferences([]metav1.OwnerReference{taskCtx.GetOwnerReference()})
}

if cfg.InjectFinalizer {
injectFinalizer := e.injectFinalizer
if (injectFinalizer != nil && *injectFinalizer) || cfg.InjectFinalizer {
f := append(o.GetFinalizers(), finalizer)
o.SetFinalizers(f)
}
Expand Down Expand Up @@ -517,6 +519,7 @@ func NewPluginManager(ctx context.Context, iCtx pluginsCore.SetupContext, entry
kubeClient: kubeClient,
resourceLevelMonitor: rm,
ignoreOwnerReferences: entry.IgnoreOwnerReferences,
injectFinalizer: entry.InjectFinalizers,
}, nil
}

Expand Down
78 changes: 53 additions & 25 deletions pkg/controller/nodes/task/k8s/plugin_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ func getMockTaskExecutionMetadata() pluginsCore.TaskExecutionMetadata {
return taskExecutionMetadata
}

func getMockTaskExecutionMetadataCustom(
tid string,
ns string,
annotations map[string]string,
labels map[string]string,
ownerRef v12.OwnerReference) pluginsCore.TaskExecutionMetadata {
taskExecutionMetadata := &pluginsCoreMock.TaskExecutionMetadata{}
taskExecutionMetadata.On("GetNamespace").Return(ns)
taskExecutionMetadata.On("GetAnnotations").Return(annotations)
taskExecutionMetadata.On("GetLabels").Return(labels)
taskExecutionMetadata.On("GetOwnerReference").Return(ownerRef)

id := &pluginsCoreMock.TaskExecutionID{}
id.On("GetGeneratedName").Return(tid)
id.On("GetID").Return(core.TaskExecutionIdentifier{})
taskExecutionMetadata.On("GetTaskExecutionID").Return(id)
return taskExecutionMetadata
}

func dummySetupContext(fakeClient client.Client) pluginsCore.SetupContext {
setupContext := &pluginsCoreMock.SetupContext{}
var enqueueOwnerFunc = pluginsCore.EnqueueOwner(func(ownerId k8stypes.NamespacedName) error { return nil })
Expand Down Expand Up @@ -570,20 +589,36 @@ func TestPluginManager_CustomKubeClient(t *testing.T) {
func TestPluginManager_AddObjectMetadata_IgnoreOwnerReferences(t *testing.T) {
pluginManager := PluginManager{ignoreOwnerReferences: true}
genName := "genName"
execID := &pluginsCoreMock.TaskExecutionID{}
execID.On("GetGeneratedName").Return(genName)
tm := &pluginsCoreMock.TaskExecutionMetadata{}
tm.On("GetTaskExecutionID").Return(execID)
or := v12.OwnerReference{}
tm.On("GetOwnerReference").Return(or)
ns := "ns"
tm.On("GetNamespace").Return(ns)
tm.On("GetAnnotations").Return(map[string]string{"aKey": "aVal"})
or := v12.OwnerReference{}
l := map[string]string{"l1": "lv1"}
a := map[string]string{"aKey": "aVal"}
tm := getMockTaskExecutionMetadataCustom(genName, ns, a, l, or)

l := map[string]string{
"l1": "lv1",
}
tm.On("GetLabels").Return(l)
o := &v1.Pod{}
cfg := config.GetK8sPluginConfig()
pluginManager.AddObjectMetadata(tm, o, cfg)
assert.Equal(t, genName, o.GetName())
// empty OwnerReference since we are ignoring
assert.Equal(t, 0, len(o.GetOwnerReferences()))
assert.Equal(t, ns, o.GetNamespace())
assert.Equal(t, map[string]string{
"cluster-autoscaler.kubernetes.io/safe-to-evict": "false",
"aKey": "aVal",
}, o.GetAnnotations())
assert.Equal(t, l, o.GetLabels())
assert.Equal(t, 0, len(o.GetFinalizers()))
}

func TestPluginManager_AddObjectMetadata_InjectFinalizer(t *testing.T) {
injectFinalizer := true
pluginManager := PluginManager{ignoreOwnerReferences: true, injectFinalizer: &injectFinalizer}
genName := "genName"
ns := "ns"
or := v12.OwnerReference{}
l := map[string]string{"l1": "lv1"}
a := map[string]string{"aKey": "aVal"}
tm := getMockTaskExecutionMetadataCustom(genName, ns, a, l, or)

o := &v1.Pod{}
cfg := config.GetK8sPluginConfig()
Expand All @@ -597,25 +632,17 @@ func TestPluginManager_AddObjectMetadata_IgnoreOwnerReferences(t *testing.T) {
"aKey": "aVal",
}, o.GetAnnotations())
assert.Equal(t, l, o.GetLabels())
assert.Equal(t, 1, len(o.GetFinalizers()))
}

func TestPluginManager_AddObjectMetadata(t *testing.T) {
pluginManager := PluginManager{}
genName := "genName"
execID := &pluginsCoreMock.TaskExecutionID{}
execID.On("GetGeneratedName").Return(genName)
tm := &pluginsCoreMock.TaskExecutionMetadata{}
tm.On("GetTaskExecutionID").Return(execID)
or := v12.OwnerReference{}
tm.On("GetOwnerReference").Return(or)
ns := "ns"
tm.On("GetNamespace").Return(ns)
tm.On("GetAnnotations").Return(map[string]string{"aKey": "aVal"})

l := map[string]string{
"l1": "lv1",
}
tm.On("GetLabels").Return(l)
or := v12.OwnerReference{}
l := map[string]string{"l1": "lv1"}
a := map[string]string{"aKey": "aVal"}
tm := getMockTaskExecutionMetadataCustom(genName, ns, a, l, or)

o := &v1.Pod{}
cfg := config.GetK8sPluginConfig()
Expand All @@ -628,6 +655,7 @@ func TestPluginManager_AddObjectMetadata(t *testing.T) {
"aKey": "aVal",
}, o.GetAnnotations())
assert.Equal(t, l, o.GetLabels())
assert.Equal(t, 0, len(o.GetFinalizers()))
}

func TestResourceManagerConstruction(t *testing.T) {
Expand Down

0 comments on commit d3f3689

Please sign in to comment.