Skip to content

Commit

Permalink
add email message send
Browse files Browse the repository at this point in the history
  • Loading branch information
liangzai006 committed Oct 25, 2023
1 parent 4e58ea9 commit d7cbfca
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 128 deletions.
69 changes: 0 additions & 69 deletions .github/workflows/ci.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ jobs:
file: build/job/Dockerfile
platforms: linux/amd64
push: true
tags: jw008/kubeeye:latest
tags: jw008/kubeeye-job:latest
13 changes: 10 additions & 3 deletions chart/kubeeye/crds/inspectplan-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ spec:
one:
format: date-time
type: string
ruleGroup:
type: string
ruleNames:
items:
type: string
properties:
name:
type: string
nodeName:
type: string
nodeSelector:
additionalProperties:
type: string
type: object
type: object
type: array
schedule:
type: string
Expand Down
11 changes: 10 additions & 1 deletion chart/kubeeye/crds/inspecttask-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ spec:
type: string
ruleNames:
items:
type: string
properties:
name:
type: string
nodeName:
type: string
nodeSelector:
additionalProperties:
type: string
type: object
type: object
type: array
timeout:
type: string
Expand Down
7 changes: 6 additions & 1 deletion chart/kubeeye/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,16 @@ spec:
volumeMounts:
- mountPath: /hosts/result
name: inspect-result
- mountPath: /etc/localtime
name: localtime
securityContext:
runAsNonRoot: true
serviceAccountName: {{ include "kubeeye.fullname" . }}-controller-manager
terminationGracePeriodSeconds: 10
volumes:
- name: inspect-result
persistentVolumeClaim:
claimName: {{ include "kubeeye.fullname" . }}-inspect-result
claimName: {{ include "kubeeye.fullname" . }}-inspect-result
- hostPath:
path: /etc/localtime
name: localtime
18 changes: 14 additions & 4 deletions chart/kubeeye/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@ config:
requests:
cpu: 50m
memory: 256Mi
multiCluster:
member1:
image: kubespheredev/kubeeye-job:v1.0.0-beta.8
imagePullPolicy: Always
# multiCluster:
# member1:
# image: kubespheredev/kubeeye-job:v1.0.0-beta.8
# imagePullPolicy: Always
# message:
# enable: false
# mode: 发送模式(完成complete或异常abnormal) ,默认是异常
# email:
# address: 邮箱服务器地址
# port: 25
# fo: 发送人邮箱
# to:
# - 接受人邮箱
# secretKey: 存储用户名(username)密码(password)的secret的Name
controllerManager:
kubeRbacProxy:
args:
Expand Down
Empty file removed deploy/kubeeye_message_secret.yaml
Empty file.
3 changes: 0 additions & 3 deletions deploy/kubeeye_v1alpha2_inspectplan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ spec:
maxTasks: 10
ruleNames:
- name: inspect-rule-namespace
nodeSelector:
node-role.kubernetes.io/master: ""
- name: inspect-rule-systemd
nodeName: node1
5 changes: 2 additions & 3 deletions pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type MessageConfig struct {
type EmailConfig struct {
Address string `json:"address,omitempty"`
Port int32 `json:"port,omitempty"`
Fo string `json:"form,omitempty"`
Fo string `json:"fo,omitempty"`
To []string `json:"to,omitempty"`
SecretKey string `json:"secretKey,omitempty"`
}
Expand Down Expand Up @@ -98,9 +98,8 @@ func (j *JobConfig) DeepCopy() *JobConfig {
}

type MessageEvent struct {
Title string
Content []byte
Target string
Sender string
Timestamp time.Time
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/inspectplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (r *InspectPlanReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

if inspectPlan.Spec.Once != nil {
if !utils.IsEmptyString(inspectPlan.Status.LastTaskName) {
if !utils.IsEmptyValue(inspectPlan.Status.LastTaskName) {
return ctrl.Result{}, nil
}
if !inspectPlan.Spec.Once.After(time.Now()) {
Expand All @@ -139,7 +139,7 @@ func (r *InspectPlanReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

if inspectPlan.Spec.Schedule == nil {
if !utils.IsEmptyString(inspectPlan.Status.LastTaskName) {
if !utils.IsEmptyValue(inspectPlan.Status.LastTaskName) {
return ctrl.Result{}, nil
}
taskName, err := r.createInspectTask(inspectPlan, ctx)
Expand Down
57 changes: 33 additions & 24 deletions pkg/controllers/inspectresult_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
kubeeyev1alpha2 "github.com/kubesphere/kubeeye/apis/kubeeye/v1alpha2"
"github.com/kubesphere/kubeeye/pkg/conf"
"github.com/kubesphere/kubeeye/pkg/constant"
Expand Down Expand Up @@ -139,8 +140,7 @@ func (r *InspectResultReconciler) Reconcile(ctx context.Context, req ctrl.Reques
klog.Error("Failed to update inspect result status", err)
return ctrl.Result{}, err
}

r.SendMessage(ctx, result.Name)
go r.SendMessage(ctx, result)
return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -201,22 +201,20 @@ func totalResultLevel(data interface{}, mapLevel map[kubeeyev1alpha2.Level]*int)
}
for _, m := range maps {
_, exist := m["assert"]
if !exist {
continue
}
v, ok := m["level"]
if !ok {
mapLevel[kubeeyev1alpha2.DangerLevel] = Autoincrement(kubeeyev1alpha2.DangerLevel)
} else {
l := v.(string)
mapLevel[kubeeyev1alpha2.Level(l)] = Autoincrement(kubeeyev1alpha2.Level(l))
if exist {
v, ok := m["level"]
if !ok {
mapLevel[kubeeyev1alpha2.DangerLevel] = Autoincrement(kubeeyev1alpha2.DangerLevel)
} else {
l := v.(string)
mapLevel[kubeeyev1alpha2.Level(l)] = Autoincrement(kubeeyev1alpha2.Level(l))
}
}

}

}

func (r *InspectResultReconciler) SendMessage(ctx context.Context, name string) {
func (r *InspectResultReconciler) SendMessage(ctx context.Context, result *kubeeyev1alpha2.InspectResult) {

kc, err := kube.GetKubeEyeConfig(ctx, r.Client)
if err != nil {
klog.Error("GetKubeEyeConfig error", err)
Expand All @@ -226,12 +224,19 @@ func (r *InspectResultReconciler) SendMessage(ctx context.Context, name string)
return
}

n := GetIssuesNumber(result)
if kc.Message.Mode == "" || kc.Message.Mode == conf.AbnormalMode {
if n == 0 {
return
}
}
klog.Info("sending email")
htmlTemplate, err := template.GetInspectResultHtmlTemplate()
if err != nil {
klog.Error("GetInspectResultHtmlTemplate error", err)
return
}
err, m := output.HtmlOut(name)
err, m := output.HtmlOut(result.Name)
if err != nil {
klog.Error("get html render data error", err)
return
Expand All @@ -242,17 +247,21 @@ func (r *InspectResultReconciler) SendMessage(ctx context.Context, name string)
klog.Error("render html template error", err)
return
}
var messageHandler conf.EventHandler
switch kc.Message.Type {
case conf.EmailMessage:
messageHandler = message.NewEmailMessageOptions(&kc.Message.Email, r.Client)
default:
klog.Error("unable identify send message type")
return
}

messageHandler := message.NewEmailMessageOptions(&kc.Message.Email, r.Client)
dispatcher := message.RegisterHandler(messageHandler)
dispatcher.DispatchMessageEvent(&conf.MessageEvent{
Content: data.Bytes(),
Title: fmt.Sprintf("巡检完成,共发现%d个问题", n),
Timestamp: time.Now(),
Content: data.Bytes(),
})
}

func GetIssuesNumber(result *kubeeyev1alpha2.InspectResult) (n int) {
for _, l := range result.Status.Level {
if l != nil {
n += *l
}
}
return n
}
2 changes: 0 additions & 2 deletions pkg/message/alarm_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package message

import (
"bytes"
"fmt"
"github.com/kubesphere/kubeeye/pkg/conf"

"io"
Expand All @@ -19,7 +18,6 @@ func (h *AlarmMessageHandler) HandleMessageEvent(event *conf.MessageEvent) {
// 执行消息发送操作
// 例如,发送消息给目标

fmt.Printf("Message sent to %s by %s: %s\n", event.Target, event.Sender, event.Content)
resp, err := http.Post(h.RequestUrl, "application/json", bytes.NewReader(event.Content))
if err != nil {
klog.Error(err)
Expand Down
Loading

0 comments on commit d7cbfca

Please sign in to comment.