refactor: use slices.Equal to simplify code (#2036)

Signed-off-by: houpo-bob <houpocun@outlook.com>
This commit is contained in:
houpo-bob 2025-08-11 09:38:17 -04:00 committed by GitHub
parent ae2a394038
commit 2d736157ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,6 +22,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"slices"
"strings" "strings"
"testing" "testing"
@ -115,18 +116,6 @@ func newServiceConfigWithServiceVolumeMount(volumeMountSubPathValue string) kobj
} }
} }
func equalStringSlice(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for i := range s1 {
if s1[i] != s2[i] {
return false
}
}
return true
}
func equalEnv(kobjectEnvs []kobject.EnvVar, k8sEnvs []api.EnvVar) bool { func equalEnv(kobjectEnvs []kobject.EnvVar, k8sEnvs []api.EnvVar) bool {
if len(kobjectEnvs) != len(k8sEnvs) { if len(kobjectEnvs) != len(k8sEnvs) {
return false return false
@ -198,13 +187,13 @@ func checkPodTemplate(config kobject.ServiceConfig, template api.PodTemplateSpec
if !equalPorts(config.Port, container.Ports) { if !equalPorts(config.Port, container.Ports) {
return fmt.Errorf("Found different container ports: %#v vs. %#v", config.Port, container.Ports) return fmt.Errorf("Found different container ports: %#v vs. %#v", config.Port, container.Ports)
} }
if !equalStringSlice(config.Command, container.Command) { if !slices.Equal(config.Command, container.Command) {
return fmt.Errorf("Found different container cmd: %#v vs. %#v", config.Command, container.Command) return fmt.Errorf("Found different container cmd: %#v vs. %#v", config.Command, container.Command)
} }
if config.WorkingDir != container.WorkingDir { if config.WorkingDir != container.WorkingDir {
return fmt.Errorf("Found different container WorkingDir: %#v vs. %#v", config.WorkingDir, container.WorkingDir) return fmt.Errorf("Found different container WorkingDir: %#v vs. %#v", config.WorkingDir, container.WorkingDir)
} }
if !equalStringSlice(config.Args, container.Args) { if !slices.Equal(config.Args, container.Args) {
return fmt.Errorf("Found different container args: %#v vs. %#v", config.Args, container.Args) return fmt.Errorf("Found different container args: %#v vs. %#v", config.Args, container.Args)
} }
if len(template.Spec.Volumes) == 0 || len(template.Spec.Volumes[0].Name) == 0 || template.Spec.Volumes[0].VolumeSource.PersistentVolumeClaim == nil && template.Spec.Volumes[0].ConfigMap == nil { if len(template.Spec.Volumes) == 0 || len(template.Spec.Volumes[0].Name) == 0 || template.Spec.Volumes[0].VolumeSource.PersistentVolumeClaim == nil && template.Spec.Volumes[0].ConfigMap == nil {