Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve yurthub get pods for cloud node #1514

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}