From aa79ee2a7cd21401f2b1cb267964e4f7cc12246d Mon Sep 17 00:00:00 2001 From: Changjian Gao Date: Mon, 23 Mar 2020 15:21:58 +0800 Subject: [PATCH 1/3] [controller] Set `BlockOwnerDeletion` to true when create PodGroup --- pkg/controllers/podgroup/pg_controller_handler.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/controllers/podgroup/pg_controller_handler.go b/pkg/controllers/podgroup/pg_controller_handler.go index f3c3b0c478..7b1163f463 100644 --- a/pkg/controllers/podgroup/pg_controller_handler.go +++ b/pkg/controllers/podgroup/pg_controller_handler.go @@ -110,11 +110,13 @@ func newPGOwnerReferences(pod *v1.Pod) []metav1.OwnerReference { } isController := true + blockOwnerDeletion := true return []metav1.OwnerReference{{ - APIVersion: v1.SchemeGroupVersion.Version, - Kind: "Pod", - Controller: &isController, - Name: pod.Name, - UID: pod.UID, + APIVersion: v1.SchemeGroupVersion.Version, + Kind: "Pod", + Controller: &isController, + Name: pod.Name, + UID: pod.UID, + BlockOwnerDeletion: &blockOwnerDeletion, }} } From 7c101beb1bd66eac40ad7b7d769cb0b1fdbb47be Mon Sep 17 00:00:00 2001 From: Changjian Gao Date: Fri, 3 Apr 2020 17:23:08 +0800 Subject: [PATCH 2/3] [controller] Use metav1.NewControllerRef() to create OwnerReference --- .../podgroup/pg_controller_handler.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pkg/controllers/podgroup/pg_controller_handler.go b/pkg/controllers/podgroup/pg_controller_handler.go index 7b1163f463..b73109b52a 100644 --- a/pkg/controllers/podgroup/pg_controller_handler.go +++ b/pkg/controllers/podgroup/pg_controller_handler.go @@ -20,6 +20,7 @@ import ( "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/klog" "volcano.sh/volcano/pkg/apis/helpers" @@ -109,14 +110,11 @@ func newPGOwnerReferences(pod *v1.Pod) []metav1.OwnerReference { } } - isController := true - blockOwnerDeletion := true - return []metav1.OwnerReference{{ - APIVersion: v1.SchemeGroupVersion.Version, - Kind: "Pod", - Controller: &isController, - Name: pod.Name, - UID: pod.UID, - BlockOwnerDeletion: &blockOwnerDeletion, - }} + gvk := schema.GroupVersionKind{ + Group: v1.SchemeGroupVersion.Group, + Version: v1.SchemeGroupVersion.Version, + Kind: "Pod", + } + ref := metav1.NewControllerRef(pod, gvk) + return []metav1.OwnerReference{*ref} } From 6c2db4dee879fe2b966a16d646b5dceaf9f7089d Mon Sep 17 00:00:00 2001 From: Changjian Gao Date: Fri, 3 Apr 2020 18:04:02 +0800 Subject: [PATCH 3/3] Fix test case --- pkg/controllers/podgroup/pg_controller_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/controllers/podgroup/pg_controller_test.go b/pkg/controllers/podgroup/pg_controller_test.go index 860c75336e..56053e9db9 100644 --- a/pkg/controllers/podgroup/pg_controller_test.go +++ b/pkg/controllers/podgroup/pg_controller_test.go @@ -42,6 +42,7 @@ func newFakeController() *Controller { func TestAddPodGroup(t *testing.T) { namespace := "test" isController := true + blockOwnerDeletion := true testCases := []struct { name string @@ -119,11 +120,12 @@ func TestAddPodGroup(t *testing.T) { Namespace: namespace, OwnerReferences: []metav1.OwnerReference{ { - APIVersion: "v1", - Kind: "Pod", - Name: "pod1", - UID: "7a09885b-b753-4924-9fba-77c0836bac20", - Controller: &isController, + APIVersion: "v1", + Kind: "Pod", + Name: "pod1", + UID: "7a09885b-b753-4924-9fba-77c0836bac20", + Controller: &isController, + BlockOwnerDeletion: &blockOwnerDeletion, }, }, },