From c9884a61f8efa10857519dc0fd9788ac231ca4b7 Mon Sep 17 00:00:00 2001 From: gzb2025 Date: Tue, 18 Nov 2025 22:53:05 +0800 Subject: [PATCH 1/6] fix:104 unsafe integer conversion in instance_utils.go Signed-off-by: gzb2025 --- .../instance/utils/instance_utils.go | 23 ++- .../instance/utils/instance_utils_test.go | 176 ++++++++++++++++++ 2 files changed, 196 insertions(+), 3 deletions(-) create mode 100644 pkg/reconciler/instance/utils/instance_utils_test.go diff --git a/pkg/reconciler/instance/utils/instance_utils.go b/pkg/reconciler/instance/utils/instance_utils.go index 7e1a1dfa..b7413beb 100644 --- a/pkg/reconciler/instance/utils/instance_utils.go +++ b/pkg/reconciler/instance/utils/instance_utils.go @@ -136,12 +136,29 @@ func GetPodComponentName(pod *v1.Pod) string { func GetPodComponentID(pod *v1.Pod) int32 { componentId := pod.Labels[v1alpha1.InstanceComponentIDKey] if len(componentId) != 0 { - id, _ := strconv.Atoi(componentId) + id, err := strconv.ParseInt(componentId, 10, 32) + if err != nil { + return 0 + } return int32(id) } list := strings.Split(pod.Name, "-") - componentId = list[len(list)-1] - id, _ := strconv.Atoi(componentId) + lastIdx := len(list) - 1 + for lastIdx >= 0 && list[lastIdx] == "" { + lastIdx-- + } + if lastIdx < 0 { + return 0 + } + if lastIdx > 0 && list[lastIdx-1] == "" { + componentId = "-" + list[lastIdx] + } else { + componentId = list[lastIdx] + } + id, err := strconv.ParseInt(componentId, 10, 32) + if err != nil { + return 0 + } return int32(id) } diff --git a/pkg/reconciler/instance/utils/instance_utils_test.go b/pkg/reconciler/instance/utils/instance_utils_test.go new file mode 100644 index 00000000..14d9489b --- /dev/null +++ b/pkg/reconciler/instance/utils/instance_utils_test.go @@ -0,0 +1,176 @@ +package utils + +import ( + "math" + "testing" + + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/rbgs/api/workloads/v1alpha1" +) + +// TestGetPodComponentID 测试修复后的GetPodComponentID函数 +func TestGetPodComponentID(t *testing.T) { + // 测试用例:覆盖标签存在/不存在、合法值、边界值、超出范围、非数字等场景 + tests := []struct { + name string // 测试用例名称 + pod *corev1.Pod // 输入的Pod对象 + expected int32 // 预期返回的ComponentID + }{ + // 场景1:通过标签获取ID(正常情况) + { + name: "valid id from label (positive)", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + v1alpha1.InstanceComponentIDKey: "123", + }, + }, + }, + expected: 123, + }, + { + name: "valid id from label (negative)", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + v1alpha1.InstanceComponentIDKey: "-456", + }, + }, + }, + expected: -456, + }, + + // 场景2:标签存在但值不合法(非数字、超出范围) + { + name: "invalid non-numeric id in label", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + v1alpha1.InstanceComponentIDKey: "abc", + }, + }, + }, + expected: 0, // 错误时返回默认值0 + }, + { + name: "id in label exceeds int32 max", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + v1alpha1.InstanceComponentIDKey: "2147483648", // 比MaxInt32大1 + }, + }, + }, + expected: 0, + }, + { + name: "id in label less than int32 min", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + v1alpha1.InstanceComponentIDKey: "-2147483649", // 比MinInt32小1 + }, + }, + }, + expected: 0, + }, + + // 场景3:标签不存在,从Pod名称解析ID(名称格式:前缀-组件名-ID) + { + name: "valid id from pod name (positive)", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "instance-web-789", // 最后一段为ID + Labels: map[string]string{}, // 无ComponentID标签 + }, + }, + expected: 789, + }, + { + name: "valid id from pod name (negative)", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "instance-db--987", // 最后一段为负ID + Labels: map[string]string{}, + }, + }, + expected: -987, + }, + + // 场景4:标签不存在,名称解析不合法 + { + name: "invalid non-numeric id in pod name", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "instance-cache-xyz", // 最后一段非数字 + Labels: map[string]string{}, + }, + }, + expected: 0, + }, + { + name: "id in pod name exceeds int32 max", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "instance-store-2147483648", + Labels: map[string]string{}, + }, + }, + expected: 0, + }, + { + name: "id in pod name less than int32 min", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "instance-queue--2147483649", + Labels: map[string]string{}, + }, + }, + expected: 0, + }, + + // 场景5:边界值测试(int32最大/最小值) + { + name: "int32 max value in label", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + v1alpha1.InstanceComponentIDKey: "2147483647", // math.MaxInt32 + }, + }, + }, + expected: math.MaxInt32, + }, + { + name: "int32 min value in label", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + v1alpha1.InstanceComponentIDKey: "-2147483648", // math.MinInt32 + }, + }, + }, + expected: math.MinInt32, + }, + { + name: "int32 max value in pod name", + pod: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "instance-max-2147483647", + Labels: map[string]string{}, + }, + }, + expected: math.MaxInt32, + }, + } + + // 执行测试用例 + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := GetPodComponentID(tt.pod) + assert.Equal(t, tt.expected, result, "测试用例[%s]失败", tt.name) + }) + } +} From ed239e96c4bc317be321ff3f742f3063603ed580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A2=93=E5=8D=9A?= <140300626+gzb2025@users.noreply.github.com> Date: Wed, 19 Nov 2025 12:56:33 +0800 Subject: [PATCH 2/6] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../instance/utils/instance_utils_test.go | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/reconciler/instance/utils/instance_utils_test.go b/pkg/reconciler/instance/utils/instance_utils_test.go index 14d9489b..5e5931fe 100644 --- a/pkg/reconciler/instance/utils/instance_utils_test.go +++ b/pkg/reconciler/instance/utils/instance_utils_test.go @@ -10,15 +10,15 @@ import ( "sigs.k8s.io/rbgs/api/workloads/v1alpha1" ) -// TestGetPodComponentID 测试修复后的GetPodComponentID函数 +// TestGetPodComponentID tests the GetPodComponentID function after the fix func TestGetPodComponentID(t *testing.T) { - // 测试用例:覆盖标签存在/不存在、合法值、边界值、超出范围、非数字等场景 + // Test cases: covers scenarios with labels present/absent, valid values, boundary values, out-of-range, and non-numeric cases tests := []struct { - name string // 测试用例名称 - pod *corev1.Pod // 输入的Pod对象 - expected int32 // 预期返回的ComponentID + name string // Test case name + pod *corev1.Pod // Input Pod object + expected int32 // Expected returned ComponentID }{ - // 场景1:通过标签获取ID(正常情况) + // Scenario 1: Get ID from label (normal case) { name: "valid id from label (positive)", pod: &corev1.Pod{ @@ -42,7 +42,7 @@ func TestGetPodComponentID(t *testing.T) { expected: -456, }, - // 场景2:标签存在但值不合法(非数字、超出范围) + // Scenario 2: Label exists but value is invalid (non-numeric, out of range) { name: "invalid non-numeric id in label", pod: &corev1.Pod{ @@ -52,7 +52,7 @@ func TestGetPodComponentID(t *testing.T) { }, }, }, - expected: 0, // 错误时返回默认值0 + expected: 0, // Return default value 0 on error }, { name: "id in label exceeds int32 max", @@ -70,14 +70,14 @@ func TestGetPodComponentID(t *testing.T) { pod: &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ - v1alpha1.InstanceComponentIDKey: "-2147483649", // 比MinInt32小1 + v1alpha1.InstanceComponentIDKey: "-2147483649", // 1 less than MinInt32 }, }, }, expected: 0, }, - // 场景3:标签不存在,从Pod名称解析ID(名称格式:前缀-组件名-ID) + // Scenario 3: Label doesn't exist, parse ID from Pod name (name format: prefix-componentName-ID) { name: "valid id from pod name (positive)", pod: &corev1.Pod{ @@ -92,19 +92,19 @@ func TestGetPodComponentID(t *testing.T) { name: "valid id from pod name (negative)", pod: &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Name: "instance-db--987", // 最后一段为负ID + Name: "instance-db--987", // Last segment is negative ID Labels: map[string]string{}, }, }, expected: -987, }, - // 场景4:标签不存在,名称解析不合法 + // Scenario 4: Label doesn't exist, name parsing is invalid { name: "invalid non-numeric id in pod name", pod: &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Name: "instance-cache-xyz", // 最后一段非数字 + Name: "instance-cache-xyz", // Last segment is non-numeric Labels: map[string]string{}, }, }, @@ -131,7 +131,7 @@ func TestGetPodComponentID(t *testing.T) { expected: 0, }, - // 场景5:边界值测试(int32最大/最小值) + // Scenario 5: Boundary value tests (int32 max/min values) { name: "int32 max value in label", pod: &corev1.Pod{ @@ -166,7 +166,7 @@ func TestGetPodComponentID(t *testing.T) { }, } - // 执行测试用例 + // Execute test cases for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := GetPodComponentID(tt.pod) From 83add91319b1876f7a43f8db00fbc92dd7fdfd3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A2=93=E5=8D=9A?= <140300626+gzb2025@users.noreply.github.com> Date: Thu, 20 Nov 2025 14:26:05 +0800 Subject: [PATCH 3/6] Update pkg/reconciler/instance/utils/instance_utils_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- pkg/reconciler/instance/utils/instance_utils_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/reconciler/instance/utils/instance_utils_test.go b/pkg/reconciler/instance/utils/instance_utils_test.go index 5e5931fe..8c2eed3e 100644 --- a/pkg/reconciler/instance/utils/instance_utils_test.go +++ b/pkg/reconciler/instance/utils/instance_utils_test.go @@ -82,7 +82,7 @@ func TestGetPodComponentID(t *testing.T) { name: "valid id from pod name (positive)", pod: &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Name: "instance-web-789", // 最后一段为ID + Name: "instance-web-789", // last segment is the ID Labels: map[string]string{}, // 无ComponentID标签 }, }, From b67f0346ca7e251a4ec4550effb05229330007c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A2=93=E5=8D=9A?= <140300626+gzb2025@users.noreply.github.com> Date: Thu, 20 Nov 2025 14:26:17 +0800 Subject: [PATCH 4/6] Update pkg/reconciler/instance/utils/instance_utils_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- pkg/reconciler/instance/utils/instance_utils_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/reconciler/instance/utils/instance_utils_test.go b/pkg/reconciler/instance/utils/instance_utils_test.go index 8c2eed3e..34773263 100644 --- a/pkg/reconciler/instance/utils/instance_utils_test.go +++ b/pkg/reconciler/instance/utils/instance_utils_test.go @@ -170,7 +170,7 @@ func TestGetPodComponentID(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := GetPodComponentID(tt.pod) - assert.Equal(t, tt.expected, result, "测试用例[%s]失败", tt.name) + assert.Equal(t, tt.expected, result, "Test case [%s] failed", tt.name) }) } } From 1b17a9e856b35d6d2628f1723c3c69b456217876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A2=93=E5=8D=9A?= <140300626+gzb2025@users.noreply.github.com> Date: Thu, 20 Nov 2025 14:26:32 +0800 Subject: [PATCH 5/6] Update pkg/reconciler/instance/utils/instance_utils_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- pkg/reconciler/instance/utils/instance_utils_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/reconciler/instance/utils/instance_utils_test.go b/pkg/reconciler/instance/utils/instance_utils_test.go index 34773263..5e9a3391 100644 --- a/pkg/reconciler/instance/utils/instance_utils_test.go +++ b/pkg/reconciler/instance/utils/instance_utils_test.go @@ -83,7 +83,7 @@ func TestGetPodComponentID(t *testing.T) { pod: &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: "instance-web-789", // last segment is the ID - Labels: map[string]string{}, // 无ComponentID标签 + Labels: map[string]string{}, // no ComponentID label }, }, expected: 789, From 19d90aaccb5d760b47102705160f3603dad56b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A2=93=E5=8D=9A?= <140300626+gzb2025@users.noreply.github.com> Date: Thu, 20 Nov 2025 17:14:08 +0800 Subject: [PATCH 6/6] Update comment for label exceeding int32 max --- pkg/reconciler/instance/utils/instance_utils_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/reconciler/instance/utils/instance_utils_test.go b/pkg/reconciler/instance/utils/instance_utils_test.go index 5e9a3391..4b73f95c 100644 --- a/pkg/reconciler/instance/utils/instance_utils_test.go +++ b/pkg/reconciler/instance/utils/instance_utils_test.go @@ -59,7 +59,7 @@ func TestGetPodComponentID(t *testing.T) { pod: &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ - v1alpha1.InstanceComponentIDKey: "2147483648", // 比MaxInt32大1 + v1alpha1.InstanceComponentIDKey: "2147483648", // 1 greater than MaxInt32 }, }, },