diff --git a/pkg/controllers/podgroup/pg_controller_handler.go b/pkg/controllers/podgroup/pg_controller_handler.go index f3c3b0c478..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,12 +110,11 @@ func newPGOwnerReferences(pod *v1.Pod) []metav1.OwnerReference { } } - isController := true - return []metav1.OwnerReference{{ - APIVersion: v1.SchemeGroupVersion.Version, - Kind: "Pod", - Controller: &isController, - Name: pod.Name, - UID: pod.UID, - }} + gvk := schema.GroupVersionKind{ + Group: v1.SchemeGroupVersion.Group, + Version: v1.SchemeGroupVersion.Version, + Kind: "Pod", + } + ref := metav1.NewControllerRef(pod, gvk) + return []metav1.OwnerReference{*ref} } 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, }, }, },