-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add kubernetes event monitor
- Loading branch information
Showing
17 changed files
with
696 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package process | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
v1 "k8s.io/api/core/v1" | ||
"strings" | ||
"watchAlert/pkg/ctx" | ||
) | ||
|
||
type KubernetesEvent struct { | ||
ctx *ctx.Context | ||
event v1.Event | ||
} | ||
|
||
func KubernetesAlertEvent(ctx *ctx.Context, event v1.Event) KubernetesEvent { | ||
return KubernetesEvent{ctx: ctx, event: event} | ||
} | ||
|
||
func (a KubernetesEvent) GetFingerprint() string { | ||
h := md5.New() | ||
streamString := a.event.Namespace + "-" + a.event.Reason | ||
h.Write([]byte(streamString)) | ||
fingerprint := hex.EncodeToString(h.Sum(nil)) | ||
return fingerprint | ||
} | ||
|
||
func (a KubernetesEvent) GetMetrics() map[string]interface{} { | ||
return map[string]interface{}{ | ||
"namespace": a.event.Namespace, | ||
"resource": a.event.Reason, | ||
"podName": a.event.InvolvedObject.Name, | ||
} | ||
} | ||
|
||
// EvalKubeEvent 评估 Kubernetes 事件 | ||
type EvalKubeEvent struct { | ||
Reason string | ||
Filter []string | ||
} | ||
|
||
// FilterKubeEvent 过滤资源 | ||
func FilterKubeEvent(event *v1.EventList, filter []string) *v1.EventList { | ||
if filter == nil { | ||
return event | ||
} | ||
|
||
warningEvents := &v1.EventList{} | ||
for _, event := range event.Items { | ||
var found bool | ||
for _, f := range filter { | ||
if strings.Contains(event.InvolvedObject.Name, f) { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
warningEvents.Items = append(warningEvents.Items, event) | ||
} | ||
} | ||
|
||
return warningEvents | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"watchAlert/internal/middleware" | ||
"watchAlert/internal/models" | ||
"watchAlert/internal/types" | ||
) | ||
|
||
type KubernetesTypesController struct{} | ||
|
||
func (ktc KubernetesTypesController) API(gin *gin.RouterGroup) { | ||
k8s := gin.Group("kubernetes") | ||
k8s.Use( | ||
middleware.Auth(), | ||
middleware.Permission(), | ||
middleware.ParseTenant(), | ||
) | ||
{ | ||
k8s.GET("getResourceList", ktc.getResourceList) | ||
k8s.GET("getReasonList", ktc.getReasonList) | ||
} | ||
} | ||
|
||
func (ktc KubernetesTypesController) getResourceList(ctx *gin.Context) { | ||
Service(ctx, func() (interface{}, interface{}) { | ||
return types.EventResourceTypeList, nil | ||
}) | ||
} | ||
|
||
func (ktc KubernetesTypesController) getReasonList(ctx *gin.Context) { | ||
r := new(models.RequestEventTypes) | ||
BindQuery(ctx, r) | ||
|
||
Service(ctx, func() (interface{}, interface{}) { | ||
return types.EventReasonLMapping[r.Resource], nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.