Skip to content

Commit

Permalink
add inspection rule specified node
Browse files Browse the repository at this point in the history
  • Loading branch information
liangzai006 committed Oct 24, 2023
1 parent cbb4782 commit 4e58ea9
Show file tree
Hide file tree
Showing 29 changed files with 591 additions and 281 deletions.
17 changes: 8 additions & 9 deletions apis/kubeeye/v1alpha2/inspectplan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ type InspectPlanSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

Schedule *string `json:"schedule,omitempty"`
Suspend bool `json:"suspend,omitempty"`
Timeout string `json:"timeout,omitempty"`
RuleGroup string `json:"ruleGroup,omitempty"`
RuleNames []string `json:"ruleNames,omitempty"`
MaxTasks int `json:"maxTasks,omitempty"`
ClusterName []Cluster `json:"clusterName,omitempty"`
KubeConfig string `json:"kubeConfig,omitempty"`
Once *metav1.Time `json:"one,omitempty"`
Schedule *string `json:"schedule,omitempty"`
Suspend bool `json:"suspend,omitempty"`
Timeout string `json:"timeout,omitempty"`
RuleNames []InspectRuleNames `json:"ruleNames,omitempty"`
MaxTasks int `json:"maxTasks,omitempty"`
ClusterName []Cluster `json:"clusterName,omitempty"`
KubeConfig string `json:"kubeConfig,omitempty"`
Once *metav1.Time `json:"one,omitempty"`
}

type TaskNames struct {
Expand Down
3 changes: 1 addition & 2 deletions apis/kubeeye/v1alpha2/inspectrule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ type OpaRule struct {
}
type PrometheusRule struct {
RuleItemBases `json:",inline"`
Endpoint string `json:"endpoint,omitempty"`
SpecialRule *string `json:"specialRule,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
}

type FileChangeRule struct {
Expand Down
14 changes: 10 additions & 4 deletions apis/kubeeye/v1alpha2/inspecttask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ type InspectTaskSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

ClusterName []Cluster `json:"clusterName,omitempty"`
RuleNames []string `json:"ruleNames,omitempty"`
Timeout string `json:"timeout,omitempty"`
InspectPolicy Policy `json:"inspectPolicy,omitempty"`
ClusterName []Cluster `json:"clusterName,omitempty"`
RuleNames []InspectRuleNames `json:"ruleNames,omitempty"`
Timeout string `json:"timeout,omitempty"`
InspectPolicy Policy `json:"inspectPolicy,omitempty"`
}

// InspectTaskStatus defines the observed state of InspectTask
Expand All @@ -48,6 +48,12 @@ type InspectTaskStatus struct {
InspectRuleType []string `json:"inspectRuleType,omitempty" yaml:"inspectRuleType"`
}

type InspectRuleNames struct {
Name string `json:"name,omitempty"`
NodeName string `json:"nodeName,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
}

type JobPhase struct {
JobName string `json:"jobName,omitempty"`
Phase Phase `json:"phase,omitempty"`
Expand Down
34 changes: 30 additions & 4 deletions apis/kubeeye/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions config/crd/bases/kubeeye.kubesphere.io_inspectplans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,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 config/crd/bases/kubeeye.kubesphere.io_inspecttasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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
Empty file.
6 changes: 5 additions & 1 deletion deploy/kubeeye_v1alpha2_inspectplan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ metadata:
spec:
maxTasks: 10
ruleNames:
- inspect-rule-namespace
- name: inspect-rule-namespace
nodeSelector:
node-role.kubernetes.io/master: ""
- name: inspect-rule-systemd
nodeName: node1
5 changes: 4 additions & 1 deletion deploy/rule/kubeeye_v1alpha2_systemd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ spec:
rule: docker == "active"
- name: etcd
rule: etcd == "active"
nodeSelector:
node-role.kubernetes.io/master: ""
- name: kubelet
rule: kubelet == "active"
rule: kubelet == "active"
nodeName: master
30 changes: 29 additions & 1 deletion pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package conf

import (
corev1 "k8s.io/api/core/v1"
"time"
)

const (
Expand Down Expand Up @@ -40,12 +41,28 @@ type MessageType string

const (
AlarmMessage MessageType = "alarm"
EmailMessage MessageType = "email"
)

type Mode string

const (
CompleteMode Mode = "complete"
AbnormalMode Mode = "abnormal"
)

type MessageConfig struct {
Enable bool `json:"enable,omitempty"`
Type MessageType `json:"type,omitempty"`
Url string `json:"url,omitempty"`
Mode Mode `json:"mode,omitempty"`
Email EmailConfig `json:"email,omitempty"`
}
type EmailConfig struct {
Address string `json:"address,omitempty"`
Port int32 `json:"port,omitempty"`
Fo string `json:"form,omitempty"`
To []string `json:"to,omitempty"`
SecretKey string `json:"secretKey,omitempty"`
}

type JobConfig struct {
Expand Down Expand Up @@ -79,3 +96,14 @@ func (j *JobConfig) DeepCopy() *JobConfig {
*j2 = *j
return j2
}

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

type EventHandler interface {
HandleMessageEvent(event *MessageEvent)
}
34 changes: 17 additions & 17 deletions pkg/controllers/inspectplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,39 +198,39 @@ func nextScheduledTimeDuration(sched cron.Schedule, now *metav1.Time) *time.Dura
return &nextTime
}

func (r *InspectPlanReconciler) createInspectTask(inspectPlan *kubeeyev1alpha2.InspectPlan, ctx context.Context) (string, error) {
func (r *InspectPlanReconciler) createInspectTask(plan *kubeeyev1alpha2.InspectPlan, ctx context.Context) (string, error) {
ownerController := true
r.removeTask(ctx, inspectPlan)
r.removeTask(ctx, plan)
inspectTask := kubeeyev1alpha2.InspectTask{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s", inspectPlan.Name, time.Now().Format("20060102-15-04")),
Labels: map[string]string{constant.LabelPlanName: inspectPlan.Name},
Name: fmt.Sprintf("%s-%s", plan.Name, time.Now().Format("20060102-15-04")),
Labels: map[string]string{constant.LabelPlanName: plan.Name},
Annotations: map[string]string{constant.AnnotationInspectType: func() string {
if inspectPlan.Spec.Schedule == nil {
if plan.Spec.Schedule == nil {
return string(kubeeyev1alpha2.InspectTypeInstant)
}
return string(kubeeyev1alpha2.InspectTypeTiming)
}()},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: inspectPlan.APIVersion,
Kind: inspectPlan.Kind,
Name: inspectPlan.Name,
UID: inspectPlan.UID,
APIVersion: plan.APIVersion,
Kind: plan.Kind,
Name: plan.Name,
UID: plan.UID,
Controller: &ownerController,
BlockOwnerDeletion: &ownerController,
}},
},
Spec: kubeeyev1alpha2.InspectTaskSpec{
RuleNames: inspectPlan.Spec.RuleNames,
ClusterName: inspectPlan.Spec.ClusterName,
RuleNames: plan.Spec.RuleNames,
ClusterName: plan.Spec.ClusterName,
Timeout: func() string {
if inspectPlan.Spec.Timeout == "" {
if plan.Spec.Timeout == "" {
return "10m"
}
return inspectPlan.Spec.Timeout
return plan.Spec.Timeout
}(),
InspectPolicy: func() kubeeyev1alpha2.Policy {
if inspectPlan.Spec.Once == nil && inspectPlan.Spec.Schedule != nil {
if plan.Spec.Once == nil && plan.Spec.Schedule != nil {
return kubeeyev1alpha2.CyclePolicy
}
return kubeeyev1alpha2.SinglePolicy
Expand Down Expand Up @@ -320,7 +320,7 @@ func ConvertTaskStatus(tasks []kubeeyev1alpha2.InspectTask) (taskStatus []kubeey
func (r *InspectPlanReconciler) updateAddRuleReferNum(ctx context.Context, plan *kubeeyev1alpha2.InspectPlan) {

for _, v := range plan.Spec.RuleNames {
rule, err := r.K8sClient.VersionClientSet.KubeeyeV1alpha2().InspectRules().Get(ctx, v, metav1.GetOptions{})
rule, err := r.K8sClient.VersionClientSet.KubeeyeV1alpha2().InspectRules().Get(ctx, v.Name, metav1.GetOptions{})
if err != nil {
klog.Error(err, "Failed to get inspectRules")
continue
Expand All @@ -344,7 +344,7 @@ func (r *InspectPlanReconciler) updateAddRuleReferNum(ctx context.Context, plan
klog.Error(err, "Failed to update inspectRules")
continue
}
plan.Labels = utils.MergeMap(plan.Labels, map[string]string{fmt.Sprintf("%s/%s", "kubeeye.kubesphere.io", v): v})
plan.Labels = utils.MergeMap(plan.Labels, map[string]string{fmt.Sprintf("%s/%s", "kubeeye.kubesphere.io", v.Name): v.Name})

}

Expand All @@ -353,7 +353,7 @@ func (r *InspectPlanReconciler) updateAddRuleReferNum(ctx context.Context, plan
func (r *InspectPlanReconciler) updateSubRuleReferNum(ctx context.Context, plan *kubeeyev1alpha2.InspectPlan) {

for _, v := range plan.Spec.RuleNames {
rule, err := r.K8sClient.VersionClientSet.KubeeyeV1alpha2().InspectRules().Get(ctx, v, metav1.GetOptions{})
rule, err := r.K8sClient.VersionClientSet.KubeeyeV1alpha2().InspectRules().Get(ctx, v.Name, metav1.GetOptions{})
if err != nil {
klog.Error(err, "Failed to get inspectRules")
continue
Expand Down
24 changes: 11 additions & 13 deletions pkg/controllers/inspectresult_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"context"
"encoding/json"
kubeeyev1alpha2 "github.com/kubesphere/kubeeye/apis/kubeeye/v1alpha2"
"github.com/kubesphere/kubeeye/pkg/conf"
"github.com/kubesphere/kubeeye/pkg/constant"
"github.com/kubesphere/kubeeye/pkg/kube"
"github.com/kubesphere/kubeeye/pkg/message"
"github.com/kubesphere/kubeeye/pkg/message/conf"
"github.com/kubesphere/kubeeye/pkg/output"
"github.com/kubesphere/kubeeye/pkg/template"
"github.com/kubesphere/kubeeye/pkg/utils"
Expand Down Expand Up @@ -188,7 +188,7 @@ func (r *InspectResultReconciler) CountLevelNum(resultName string) (map[kubeeyev
}
func totalResultLevel(data interface{}, mapLevel map[kubeeyev1alpha2.Level]*int) {

maps, err := utils.StructToMap(data)
maps, err := utils.ArrayStructToArrayMap(data)
if err != nil {
return
}
Expand Down Expand Up @@ -242,19 +242,17 @@ func (r *InspectResultReconciler) SendMessage(ctx context.Context, name string)
klog.Error("render html template error", err)
return
}

if kc.Message.Url == "" {
klog.Error("message request url is empty")
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.AlarmMessageHandler{
RequestUrl: kc.Message.Url,
}
event := &conf.MessageEvent{
Content: data.String(),
}

dispatcher := message.RegisterHandler(messageHandler)
dispatcher.DispatchMessageEvent(event)
dispatcher.DispatchMessageEvent(&conf.MessageEvent{
Content: data.Bytes(),
})
}
2 changes: 1 addition & 1 deletion pkg/controllers/inspectrules_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (r *InspectRulesReconciler) SetupWithManager(mgr ctrl.Manager) error {

func ComputeLevel(data interface{}, mapLevel map[kubeeyev1alpha2.Level]*int) {

maps, err := utils.StructToMap(data)
maps, err := utils.ArrayStructToArrayMap(data)
if err != nil {
return
}
Expand Down
Loading

0 comments on commit 4e58ea9

Please sign in to comment.