Skip to content

Commit

Permalink
fix: list pods by resource tree instread of labelSelectors
Browse files Browse the repository at this point in the history
Signed-off-by: sph <[email protected]>
  • Loading branch information
bysph committed Oct 7, 2023
1 parent d09b910 commit 1f15a91
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
30 changes: 26 additions & 4 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 @@ -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)
}
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 1f15a91

Please sign in to comment.