From b9773de2367efcee93a649ab348741ee4d02d638 Mon Sep 17 00:00:00 2001 From: "x.zhou" Date: Mon, 9 Dec 2024 10:12:30 +0800 Subject: [PATCH] fix --- pkg/dataprotection/restore/builder.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/pkg/dataprotection/restore/builder.go b/pkg/dataprotection/restore/builder.go index e3de0a1374a..ac250ae11d6 100644 --- a/pkg/dataprotection/restore/builder.go +++ b/pkg/dataprotection/restore/builder.go @@ -253,11 +253,22 @@ func (r *restoreJobBuilder) addTargetPodAndCredentialEnv(pod *corev1.Pod, env = pod.Spec.Containers[0].Env r.envFrom = pod.Spec.Containers[0].EnvFrom } - env = append(env, corev1.EnvVar{Name: dptypes.DPDBHost, Value: intctrlutil.BuildPodHostDNS(pod)}) - if connectionCredential == nil { - if portEnv, err := utils.GetDPDBPortEnv(pod, target.ContainerPort); err == nil { + addDBHostEnv := func() { + env = append(env, corev1.EnvVar{Name: dptypes.DPDBHost, Value: intctrlutil.BuildPodHostDNS(pod)}) + } + addDBPortEnv := func() { + portEnv, err := utils.GetDPDBPortEnv(pod, target.ContainerPort) + if err != nil { + // fallback to use the first port of the pod + portEnv, _ = utils.GetDPDBPortEnv(pod, nil) + } + if portEnv != nil { env = append(env, *portEnv) } + } + if connectionCredential == nil { + addDBHostEnv() + addDBPortEnv() } else { appendEnvFromSecret := func(envName, keyName string) { if keyName == "" { @@ -277,11 +288,12 @@ func (r *restoreJobBuilder) addTargetPodAndCredentialEnv(pod *corev1.Pod, if connectionCredential.PortKey != "" { appendEnvFromSecret(dptypes.DPDBPort, connectionCredential.PortKey) } else { - portEnv, _ := utils.GetDPDBPortEnv(pod, nil) - env = append(env, *portEnv) + addDBPortEnv() } if connectionCredential.HostKey != "" { appendEnvFromSecret(dptypes.DPDBHost, connectionCredential.HostKey) + } else { + addDBHostEnv() } } r.env = utils.MergeEnv(r.env, env)