Skip to content

Commit

Permalink
Refactor the kubernetes event result (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Feb 25, 2024
1 parent e89952d commit 784585c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 39 deletions.
30 changes: 7 additions & 23 deletions pkg/controller/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
16 changes: 0 additions & 16 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 784585c

Please sign in to comment.