Skip to content

Commit

Permalink
improve yurthub get pods for cloud node (#1514)
Browse files Browse the repository at this point in the history
  • Loading branch information
JameKeal authored Jun 19, 2023
1 parent b2d7d49 commit 89abaff
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
10 changes: 9 additions & 1 deletion cmd/yurthub/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 34 additions & 1 deletion pkg/yurthub/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
})
}

0 comments on commit 89abaff

Please # to comment.