Skip to content

Commit

Permalink
fix bug for pod status in replicaSet (#4698)
Browse files Browse the repository at this point in the history
* fix bug for pod status in replicaSet

* golangci fix
  • Loading branch information
zehuaiWANG authored and k8s-ci-robot committed Jan 7, 2020
1 parent acb9f1f commit 53d5235
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/app/backend/resource/replicaset/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ import (
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
k8sClient "k8s.io/client-go/kubernetes"
)

// GetReplicaSetPods return list of pods targeting replica set.
func GetReplicaSetPods(client k8sClient.Interface, metricClient metricapi.MetricClient,
dsQuery *dataselect.DataSelectQuery, petSetName, namespace string) (*pod.PodList, error) {
log.Printf("Getting replication controller %s pods in namespace %s", petSetName, namespace)
dsQuery *dataselect.DataSelectQuery, replicaSetName, namespace string) (*pod.PodList, error) {
log.Printf("Getting replication controller %s pods in namespace %s", replicaSetName, namespace)

pods, err := getRawReplicaSetPods(client, petSetName, namespace)
pods, err := getRawReplicaSetPods(client, replicaSetName, namespace)
if err != nil {
return pod.EmptyPodList, err
}
Expand All @@ -51,14 +50,18 @@ func GetReplicaSetPods(client k8sClient.Interface, metricClient metricapi.Metric
return &podList, nil
}

func getRawReplicaSetPods(client k8sClient.Interface, petSetName, namespace string) ([]v1.Pod, error) {
rs, err := client.AppsV1().ReplicaSets(namespace).Get(petSetName, metaV1.GetOptions{})
func getRawReplicaSetPods(client k8sClient.Interface, replicaSetName, namespace string) ([]v1.Pod, error) {
rs, err := client.AppsV1().ReplicaSets(namespace).Get(replicaSetName, metaV1.GetOptions{})
if err != nil {
return nil, err
}

labelSelector := labels.SelectorFromSet(rs.Spec.Selector.MatchLabels)
channels := &common.ResourceChannels{
PodList: common.GetPodListChannel(client, common.NewSameNamespaceQuery(namespace), 1),
PodList: common.GetPodListChannelWithOptions(client, common.NewSameNamespaceQuery(namespace),
metaV1.ListOptions{
LabelSelector: labelSelector.String(),
}, 1),
}

podList := <-channels.PodList.List
Expand All @@ -75,15 +78,15 @@ func getReplicaSetPodInfo(client k8sClient.Interface, replicaSet *apps.ReplicaSe
PodList: common.GetPodListChannelWithOptions(client, common.NewSameNamespaceQuery(replicaSet.Namespace),
metaV1.ListOptions{
LabelSelector: labelSelector.String(),
FieldSelector: fields.Everything().String(),
}, 1),
}

pods := <-channels.PodList.List
podList := <-channels.PodList.List
if err := <-channels.PodList.Error; err != nil {
return nil, err
}

podInfo := common.GetPodInfo(replicaSet.Status.Replicas, replicaSet.Spec.Replicas, pods.Items)
filterPod := common.FilterPodsByControllerRef(replicaSet, podList.Items)
podInfo := common.GetPodInfo(replicaSet.Status.Replicas, replicaSet.Spec.Replicas, filterPod)
return &podInfo, nil
}

0 comments on commit 53d5235

Please sign in to comment.