From 784585c681e48d7819fb16182ffb0ec3c1b06b72 Mon Sep 17 00:00:00 2001 From: caoyingjunz Date: Sun, 25 Feb 2024 18:24:12 +0800 Subject: [PATCH] Refactor the kubernetes event result (#362) --- pkg/controller/cluster/cluster.go | 30 +++++++----------------------- pkg/types/types.go | 16 ---------------- 2 files changed, 7 insertions(+), 39 deletions(-) diff --git a/pkg/controller/cluster/cluster.go b/pkg/controller/cluster/cluster.go index 036ea87c..37ae988b 100644 --- a/pkg/controller/cluster/cluster.go +++ b/pkg/controller/cluster/cluster.go @@ -21,7 +21,6 @@ import ( "fmt" "net/http" "regexp" - "sort" "strconv" "strings" "sync" @@ -62,7 +61,7 @@ type Interface interface { Ping(ctx context.Context, kubeConfig string) error // AggregateEvents 聚合指定资源的 events - AggregateEvents(ctx context.Context, cluster string, namespace string, name string, kind string) ([]types.Event, error) + AggregateEvents(ctx context.Context, cluster string, namespace string, name string, kind string) (*v1.EventList, error) // WsHandler pod 的 webShell WsHandler(ctx context.Context, webShellOptions *types.WebShellOptions, w http.ResponseWriter, r *http.Request) error @@ -271,7 +270,7 @@ func (c *cluster) WsHandler(ctx context.Context, opt *types.WebShellOptions, w h } // AggregateEvents 聚合 k8s 资源的所有 events,比如 kind 为 deployment 时,则聚合 deployment,所属 rs 以及 pod 的事件 -func (c *cluster) AggregateEvents(ctx context.Context, cluster string, namespace string, name string, kind string) ([]types.Event, error) { +func (c *cluster) AggregateEvents(ctx context.Context, cluster string, namespace string, name string, kind string) (*v1.EventList, error) { clusterSet, err := c.GetClusterSetByName(ctx, cluster) if err != nil { return nil, err @@ -358,20 +357,16 @@ func (c *cluster) AggregateEvents(ctx context.Context, cluster string, namespace default: } - var allEvents types.EventList + eventList := &v1.EventList{Items: []v1.Event{}} for i := 0; i < diff; i++ { - eventList := <-eventCh - if eventList == nil { + es := <-eventCh + if es == nil { continue } - for _, eve := range eventList.Items { - allEvents = append(allEvents, c.parseOriginEvent(eve)) - } + eventList.Items = append(eventList.Items, es.Items...) } - // 按发生事件排序 - sort.Sort(allEvents) - return allEvents, nil + return eventList, nil } // GetKubeObjectByLabel @@ -491,17 +486,6 @@ func (c *cluster) GetKubernetesMeta(ctx context.Context, clusterName string) (*t return &km, nil } -func (c *cluster) parseOriginEvent(eve v1.Event) types.Event { - return types.Event{ - Type: eve.Type, - Reason: eve.Reason, - Message: eve.Message, - LastTimestamp: eve.LastTimestamp, - ObjectName: eve.InvolvedObject.Name, - Kind: eve.InvolvedObject.Kind, - } -} - func (c *cluster) makeFieldSelector(uid apitypes.UID, name string, namespace string, kind string) string { return strings.Join([]string{ "involvedObject.uid=" + string(uid), diff --git a/pkg/types/types.go b/pkg/types/types.go index 7ee8fcb3..b409755e 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -23,7 +23,6 @@ import ( "github.com/gorilla/websocket" appv1 "k8s.io/api/apps/v1" "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/remotecommand" ) @@ -112,21 +111,6 @@ type TimeSpec struct { GmtModified interface{} `json:"gmt_modified,omitempty"` } -type Event struct { - Type string `json:"type"` - Reason string `json:"reason"` - ObjectName string `json:"objectName"` - Kind string `json:"kind"` - Message string `json:"message"` - LastTimestamp metav1.Time `json:"lastTimestamp,omitempty"` -} - -type EventList []Event - -func (e EventList) Len() int { return len(e) } -func (e EventList) Less(i, j int) bool { return e[i].LastTimestamp.After(e[j].LastTimestamp.Time) } -func (e EventList) Swap(i, j int) { e[i], e[j] = e[j], e[i] } - type KubeObject struct { lock sync.RWMutex