Skip to content

Commit

Permalink
Merge pull request horizoncd#199 from bysph/main
Browse files Browse the repository at this point in the history
fix: list pods by resourceTree instread of labelSelectors
  • Loading branch information
bysph authored Oct 23, 2023
2 parents 2e4b1ad + 19ec6ef commit 74694df
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
61 changes: 26 additions & 35 deletions pkg/cd/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -195,47 +202,31 @@ func (c *cd) GetResourceTree(ctx context.Context,
return nil, err
}

podsMap := make(map[string]*corev1.Pod)
c.traverseResourceTree(resourceTreeInArgo, func(node *ResourceTreeNode) bool {
ifContinue := false
workload.LoopAbilities(func(workload workload.Workload) bool {
if !workload.MatchGK(schema.GroupKind{Group: node.Group, Kind: node.Kind}) {
return true
}
gt := getter.New(workload)

var (
pods []corev1.Pod
err error
)
_ = c.informerFactories.GetDynamicFactory(params.RegionEntity.ID,
func(factory dynamicinformer.DynamicSharedInformerFactory) error {
pods, err = gt.ListPods(node.ResourceNode, factory)
return nil
})
if err != nil {
return true
}

for i := range pods {
podsMap[string(pods[i].UID)] = &pods[i]
}
ifContinue = false
return false
})
return ifContinue
})

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)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/workload/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions pkg/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down

0 comments on commit 74694df

Please sign in to comment.