diff --git a/pkg/cd/cd.go b/pkg/cd/cd.go index f6b9c0bfa..eab9650fc 100644 --- a/pkg/cd/cd.go +++ b/pkg/cd/cd.go @@ -56,6 +56,13 @@ const ( RolloutPodTemplateHash = "rollouts-pod-template-hash" ) +var ( + GKPod = schema.GroupKind{ + Group: "", + Kind: "Pod", + } +) + const ( // PodLifeCycleSchedule specifies whether pod has been scheduled PodLifeCycleSchedule = "PodSchedule" @@ -227,15 +234,30 @@ func (c *cd) GetResourceTree(ctx context.Context, }) resourceTree := make([]ResourceNode, 0, len(resourceTreeInArgo.Nodes)) + pd, err := workload.GetAbility(GKPod) + if err != nil { + return nil, err + } + gt := getter.New(pd) for _, node := range resourceTreeInArgo.Nodes { n := ResourceNode{ResourceNode: node} if n.Kind == "Pod" { - if podDetail, ok := podsMap[n.UID]; ok { - t := Compact(*podDetail) - n.PodDetail = &t - } else { + var podDetail corev1.Pod + err = c.informerFactories.GetDynamicFactory(params.RegionEntity.ID, + func(factory dynamicinformer.DynamicSharedInformerFactory) error { + pods, err := gt.ListPods(&node, factory) + if err != nil { + return err + } + podDetail = pods[0] + return nil + }) + if err != nil { + log.Errorf(ctx, "failed to get pod detail: %v", err) continue } + t := Compact(podDetail) + n.PodDetail = &t } resourceTree = append(resourceTree, n) } diff --git a/pkg/workload/pod/pod.go b/pkg/workload/pod/pod.go index acc7d5edf..14a18bca6 100644 --- a/pkg/workload/pod/pod.go +++ b/pkg/workload/pod/pod.go @@ -47,14 +47,16 @@ func (*pod) MatchGK(gk schema.GroupKind) bool { func (*pod) getPod(node *v1alpha1.ResourceNode, factory dynamicinformer.DynamicSharedInformerFactory) (*corev1.Pod, error) { - instance, err := factory.ForResource(GVRPod).Lister().ByNamespace(node.Namespace).Get(node.Name) + obj, err := factory.ForResource(GVRPod).Lister().ByNamespace(node.Namespace).Get(node.Name) if err != nil { return nil, perror.Wrapf( herrors.NewErrGetFailed(herrors.ResourceInK8S, "failed to get deployment in k8s"), "failed to get deployment in k8s: deployment = %s, err = %v", node.Name, err) } - return instance.(*corev1.Pod), nil + pods := workload.ObjIntoPod(obj) + + return &pods[0], nil } func (p *pod) ListPods(node *v1alpha1.ResourceNode, diff --git a/pkg/workload/workload.go b/pkg/workload/workload.go index 82460223a..67c668aee 100644 --- a/pkg/workload/workload.go +++ b/pkg/workload/workload.go @@ -15,7 +15,10 @@ package workload import ( + "fmt" + "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" + herrors "github.com/horizoncd/horizon/core/errors" "github.com/horizoncd/horizon/pkg/regioninformers" "github.com/horizoncd/horizon/pkg/util/kube" corev1 "k8s.io/api/core/v1" @@ -39,6 +42,16 @@ func Register(ability Workload, gvrs ...schema.GroupVersionResource) { Resources = append(Resources, gvrsUnderResource...) } +func GetAbility(gk schema.GroupKind) (Workload, error) { + for _, ability := range abilities { + if ability.MatchGK(gk) { + return ability, nil + } + } + return nil, herrors.NewErrGetFailed(herrors.StepInWorkload, + fmt.Sprintf("ability does not exist, gk = %v", gk)) +} + type Handler func(workload Workload) bool func LoopAbilities(handlers ...Handler) {