Skip to content

Commit

Permalink
优化事件 FieldSelector 的构造方法 (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Apr 27, 2024
1 parent 96a5541 commit 0ff1a51
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
30 changes: 22 additions & 8 deletions pkg/controller/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/caoyingjunz/pixiu/pkg/db"
"github.com/caoyingjunz/pixiu/pkg/db/model"
"github.com/caoyingjunz/pixiu/pkg/types"
"github.com/caoyingjunz/pixiu/pkg/util"
"github.com/caoyingjunz/pixiu/pkg/util/uuid"
)

Expand Down Expand Up @@ -312,14 +313,16 @@ func (c *cluster) GetEventList(ctx context.Context, cluster string, options type
options.Limit = 500
}
opt := metav1.ListOptions{Limit: options.Limit}
if !options.Namespaced {
opt.FieldSelector = c.makeFieldSelector(apitypes.UID(options.Uid), options.Name, options.Namespace, options.Kind)
fs := c.makeFieldSelector(apitypes.UID(options.Uid), options.Name, options.Namespace, options.Kind)
if len(fs) != 0 {
opt.FieldSelector = fs
}

clusterSet, err := c.GetClusterSetByName(ctx, cluster)
if err != nil {
return nil, err
}

return clusterSet.Client.CoreV1().Events(options.Namespace).List(ctx, opt)
}

Expand Down Expand Up @@ -543,13 +546,24 @@ func (c *cluster) GetKubernetesMeta(ctx context.Context, clusterName string) (*t
return &km, nil
}

// 构造事件的 FieldSelector, 如果参数为空则忽略
func (c *cluster) makeFieldSelector(uid apitypes.UID, name string, namespace string, kind string) string {
return strings.Join([]string{
"involvedObject.uid=" + string(uid),
"involvedObject.name=" + name,
"involvedObject.namespace=" + namespace,
"involvedObject.kind=" + kind,
}, ",")
eventFS := make([]string, 0)
// 追加对象的 uid
if util.IsEmptyS(string(uid)) {
eventFS = append(eventFS, "involvedObject.uid="+string(uid))
}
if util.IsEmptyS(name) {
eventFS = append(eventFS, "involvedObject.name="+name)
}
if util.IsEmptyS(namespace) {
eventFS = append(eventFS, "involvedObject.namespace="+namespace)
}
if util.IsEmptyS(kind) {
eventFS = append(eventFS, "involvedObject.kind="+kind)
}
// 构造 kubernetes 原生 FieldSelector 参数格式
return strings.Join(eventFS, ",")
}

func (c *cluster) parseKubernetesResource(nodeMetrics []v1beta1.NodeMetrics) types.Resources {
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ func ValidateStrongPassword(password string) bool {
func GenerateRequestID() string {
return fmt.Sprintf("%s-%06d", time.Now().Format("20060102150405"), rand.Intn(1000000))
}

func IsEmptyS(s string) bool {
return len(s) != 0
}

0 comments on commit 0ff1a51

Please sign in to comment.