From 89abaffa40987e33ac37d474582cdd698c5d1791 Mon Sep 17 00:00:00 2001 From: Zhen Zhao <70508195+JameKeal@users.noreply.github.com> Date: Mon, 19 Jun 2023 13:58:12 +0800 Subject: [PATCH] improve yurthub get pods for cloud node (#1514) --- cmd/yurthub/app/config/config.go | 10 ++++++++- pkg/yurthub/server/server.go | 35 +++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/cmd/yurthub/app/config/config.go b/cmd/yurthub/app/config/config.go index 88f35cfa93a..85a64ab435a 100644 --- a/cmd/yurthub/app/config/config.go +++ b/cmd/yurthub/app/config/config.go @@ -302,12 +302,20 @@ func registerInformers(options *options.YurtHubOptions, if tenantNs != "" { newSecretInformer := func(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { - return coreinformers.NewFilteredSecretInformer(client, tenantNs, resyncPeriod, nil, nil) } informerFactory.InformerFor(&corev1.Secret{}, newSecretInformer) } + if workingMode == util.WorkingModeCloud { + newPodInformer := func(client kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { + listOptions := func(ops *metav1.ListOptions) { + ops.FieldSelector = fields.Set{"spec.nodeName": options.NodeName}.String() + } + return coreinformers.NewFilteredPodInformer(client, "", resyncPeriod, nil, listOptions) + } + informerFactory.InformerFor(&corev1.Pod{}, newPodInformer) + } } // isServiceTopologyFilterEnabled is used to verify the service topology filter should be enabled or not. diff --git a/pkg/yurthub/server/server.go b/pkg/yurthub/server/server.go index fead050d390..4b7189ac4b6 100644 --- a/pkg/yurthub/server/server.go +++ b/pkg/yurthub/server/server.go @@ -22,12 +22,17 @@ import ( "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus/promhttp" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/client-go/informers" + "k8s.io/klog/v2" "github.com/openyurtio/openyurt/cmd/yurthub/app/config" "github.com/openyurtio/openyurt/pkg/profile" "github.com/openyurtio/openyurt/pkg/yurthub/certificate" "github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/rest" ota "github.com/openyurtio/openyurt/pkg/yurthub/otaupdate" + otautil "github.com/openyurtio/openyurt/pkg/yurthub/otaupdate/util" "github.com/openyurtio/openyurt/pkg/yurthub/util" ) @@ -89,7 +94,11 @@ func registerHandlers(c *mux.Router, cfg *config.YurtHubConfiguration, rest *res c.Handle("/metrics", promhttp.Handler()) // register handler for ota upgrade - c.Handle("/pods", ota.GetPods(cfg.StorageWrapper)).Methods("GET") + if cfg.WorkingMode == util.WorkingModeEdge { + c.Handle("/pods", ota.GetPods(cfg.StorageWrapper)).Methods("GET") + } else { + c.Handle("/pods", getPodList(cfg.SharedFactory, cfg.NodeName)).Methods("GET") + } c.Handle("/openyurt.io/v1/namespaces/{ns}/pods/{podname}/upgrade", ota.HealthyCheck(rest, cfg.NodeName, ota.UpdatePod)).Methods("POST") } @@ -112,3 +121,27 @@ func readyz(certificateMgr certificate.YurtCertificateManager) http.Handler { } }) } + +func getPodList(sharedFactory informers.SharedInformerFactory, nodeName string) http.Handler { + + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + podLister := sharedFactory.Core().V1().Pods().Lister() + podList, err := podLister.List(labels.Everything()) + if err != nil { + klog.Errorf("get pods key failed, %v", err) + otautil.WriteErr(w, "Get pods key failed", http.StatusInternalServerError) + return + } + pl := new(corev1.PodList) + for i := range podList { + pl.Items = append(pl.Items, *podList[i]) + } + + data, err := otautil.EncodePods(pl) + if err != nil { + klog.Errorf("Encode pod list failed, %v", err) + otautil.WriteErr(w, "Encode pod list failed", http.StatusInternalServerError) + } + otautil.WriteJSONResponse(w, data) + }) +}