Repository: spidernet-io/spiderpool
Pull request #1441 (2023-03-15 07:12:13)

[unit tests] pod mananger unit test
Signed-off-by: Icarus9913 [icaruswu66@qq.com](mailto:icaruswu66@qq.com)


Patch:
diff --git a/pkg/constant/k8s.go b/pkg/constant/k8s.go
index 16eb52eae..8ee1a3db3 100644
--- a/pkg/constant/k8s.go
+++ b/pkg/constant/k8s.go
@@ -30,6 +30,8 @@ const (
 	KindCronJob     = "CronJob"
 )
 
+var K8sKinds = []string{KindPod, KindDeployment, KindReplicaSet, KindDaemonSet, KindStatefulSet, KindJob, KindCronJob}
+
 const (
 	PodRunning     types.PodStatus = "Running"
 	PodTerminating types.PodStatus = "Terminating"
diff --git a/pkg/ipam/pool_selections.go b/pkg/ipam/pool_selections.go
index 2b13ba1fc..93f2576ba 100644
--- a/pkg/ipam/pool_selections.go
+++ b/pkg/ipam/pool_selections.go
@@ -146,7 +146,7 @@ func (i *ipam) getPoolFromSubnetAnno(ctx context.Context, pod *corev1.Pod, nic s
 		var err error
 		for j := 1; j <= i.config.OperationRetries; j++ {
 			// third-party controller applications
-			if !slices.Contains(k8sKinds, podController.Kind) {
+			if !slices.Contains(constant.K8sKinds, podController.Kind) {
 				pool, err = i.applyThirdControllerAutoPool(ctx, subnetName, poolName, podController, types.AutoPoolProperty{
 					DesiredIPNumber: poolIPNum,
 					IPVersion:       ipVersion,
diff --git a/pkg/ipam/utils.go b/pkg/ipam/utils.go
index 8775a68e8..b816773bd 100644
--- a/pkg/ipam/utils.go
+++ b/pkg/ipam/utils.go
@@ -25,9 +25,6 @@ import (
 	"github.com/spidernet-io/spiderpool/pkg/utils/convert"
 )
 
-var k8sKinds = []string{constant.KindPod, constant.KindDeployment, constant.KindReplicaSet, constant.KindDaemonSet,
-	constant.KindStatefulSet, constant.KindJob, constant.KindCronJob}
-
 func getCustomRoutes(pod *corev1.Pod) ([]*models.Route, error) {
 	anno, ok := pod.Annotations[constant.AnnoPodRoutes]
 	if !ok {
diff --git a/pkg/podmanager/pod_manager.go b/pkg/podmanager/pod_manager.go
index 0ae5ee974..6c7c65d88 100644
--- a/pkg/podmanager/pod_manager.go
+++ b/pkg/podmanager/pod_manager.go
@@ -85,7 +85,7 @@ func (pm *podManager) GetPodTopController(ctx context.Context, pod *corev1.Pod)
 	if podOwner == nil {
 		return types.PodTopController{
 			AppNamespacedName: types.AppNamespacedName{
-				APIVersion: podOwner.APIVersion,
+				APIVersion: pod.APIVersion,
 				Kind:       constant.KindPod,
 				Namespace:  pod.Namespace,
 				Name:       pod.Name,


Test patch:
diff --git a/pkg/podmanager/pod_manager_test.go b/pkg/podmanager/pod_manager_test.go
index ec735a941..437902e82 100644
--- a/pkg/podmanager/pod_manager_test.go
+++ b/pkg/podmanager/pod_manager_test.go
@@ -20,6 +20,7 @@ import (
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/runtime/schema"
 	"k8s.io/utils/pointer"
+	"k8s.io/utils/strings/slices"
 	"sigs.k8s.io/controller-runtime/pkg/client"
 	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
 
@@ -239,12 +240,13 @@ var _ = Describe("PodManager", Label("pod_manager_test"), func() {
 				err := kruiseapi.AddToScheme(scheme)
 				Expect(err).NotTo(HaveOccurred())
 
-				err = controllerutil.SetControllerReference(&kruisev1.CloneSet{}, podT, scheme)
+				cloneSet := &kruisev1.CloneSet{}
+				err = controllerutil.SetControllerReference(cloneSet, podT, scheme)
 				Expect(err).NotTo(HaveOccurred())
 
 				podTopController, err := podManager.GetPodTopController(ctx, podT)
 				Expect(err).NotTo(HaveOccurred())
-				Expect(podTopController.Kind).Should(Equal(constant.KindUnknown))
+				Expect(slices.Contains(constant.K8sKinds, podTopController.Kind)).To(BeFalse())
 			})
 
 			It("Pod with ReplicaSet controller", func() {
@@ -389,7 +391,7 @@ var _ = Describe("PodManager", Label("pod_manager_test"), func() {
 
 				podTopController, err := podManager.GetPodTopController(ctx, podT)
 				Expect(err).NotTo(HaveOccurred())
-				Expect(podTopController.Kind).Should(Equal(constant.KindUnknown))
+				Expect(slices.Contains(constant.K8sKinds, podTopController.Kind)).To(BeFalse())
 			})
 
 			It("Pod with Job controller", func() {
@@ -462,7 +464,7 @@ var _ = Describe("PodManager", Label("pod_manager_test"), func() {
 
 				podTopController, err := podManager.GetPodTopController(ctx, podT)
 				Expect(err).NotTo(HaveOccurred())
-				Expect(podTopController.Kind).Should(Equal(constant.KindUnknown))
+				Expect(slices.Contains(constant.K8sKinds, podTopController.Kind)).To(BeFalse())
 			})
 
 			It("Pod with CronJob controller", func() {