Skip to content

Commit

Permalink
fix prometheus get result nil
Browse files Browse the repository at this point in the history
  • Loading branch information
liangzai006 committed Jun 20, 2023
1 parent a173230 commit b53ea8e
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 70 deletions.
4 changes: 2 additions & 2 deletions deploy/kubeeye/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.0.1
version: 0.0.2

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "v1.0.0-alpha.1"
appVersion: "v1.0.0-alpha.2"
62 changes: 62 additions & 0 deletions deploy/kubeeye/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "charts.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "charts.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "charts.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "charts.labels" -}}
helm.sh/chart: {{ include "charts.chart" . }}
{{ include "charts.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "charts.selectorLabels" -}}
app.kubernetes.io/name: {{ include "charts.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "charts.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "charts.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
2 changes: 1 addition & 1 deletion deploy/kubeeye/templates/kubeeye-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ data:
job:
AutoDelTime: 30
BackLimit: 5
Image: kubespheredev/kubeeye-job:v1.0.0-alpha.1
Image: kubespheredev/kubeeye-job:v1.0.0-alpha.2
ImagePullPolicy: Always
kind: ConfigMap
metadata:
Expand Down
48 changes: 1 addition & 47 deletions deploy/kubeeye/templates/role.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: kubeeye-controller-manager
namespace: kubeeye-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
Expand Down Expand Up @@ -210,44 +204,4 @@ rules:
resources:
- subjectaccessreviews
verbs:
- create
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: kubeeye-leader-election-rolebinding
namespace: kubeeye-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: kubeeye-leader-election-role
subjects:
- kind: ServiceAccount
name: kubeeye-controller-manager
namespace: kubeeye-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubeeye-manager-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubeeye-manager-role
subjects:
- kind: ServiceAccount
name: kubeeye-controller-manager
namespace: kubeeye-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubeeye-proxy-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubeeye-proxy-role
subjects:
- kind: ServiceAccount
name: kubeeye-controller-manager
namespace: kubeeye-system
- create
39 changes: 39 additions & 0 deletions deploy/kubeeye/templates/rolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: kubeeye-leader-election-rolebinding
namespace: kubeeye-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: kubeeye-leader-election-role
subjects:
- kind: ServiceAccount
name: kubeeye-controller-manager
namespace: kubeeye-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubeeye-manager-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubeeye-manager-role
subjects:
- kind: ServiceAccount
name: kubeeye-controller-manager
namespace: kubeeye-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubeeye-proxy-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubeeye-proxy-role
subjects:
- kind: ServiceAccount
name: kubeeye-controller-manager
namespace: kubeeye-system
7 changes: 7 additions & 0 deletions deploy/kubeeye/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "charts.fullname" . }}-controller-manager
namespace: kubeeye-system
{{- end }}
4 changes: 3 additions & 1 deletion deploy/kubeeye/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ image:
controller:
repository: kubespheredev/kubeeye-controller
pullPolicy: Always
tag: v1.0.0-alpha.1
tag: v1.0.0-alpha.2
imagePullSecrets: []

nameOverride: ""
fullnameOverride: ""

serviceAccount:
# Specifies whether a service account should be created
Expand Down
27 changes: 27 additions & 0 deletions examples/test/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package main

import (
"context"
"github.com/kubesphere/kubeeye/constant"
"github.com/kubesphere/kubeeye/pkg/inspect"
"github.com/kubesphere/kubeeye/pkg/kube"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func main() {

var kc kube.KubernetesClient
cluster, _ := kube.GetKubeConfigInCluster()
clients, _ := kc.K8SClients(cluster)
//get, _ := clients.ClientSet.CoreV1().ConfigMaps("kubeeye-system").Get(context.TODO(), "inspectplan-1687247535-prometheus", metav1.GetOptions{})

task, _ := clients.VersionClientSet.KubeeyeV1alpha2().InspectTasks("kubeeye-system").Get(context.TODO(), "inspectplan-1687247535", metav1.GetOptions{})

c, _ := client.New(cluster, client.Options{})

inspectInterface := inspect.RuleOperatorMap[constant.Prometheus]

runInspect, _ := inspectInterface.RunInspect(context.TODO(), task, clients, "inspectplan-1687247535-prometheus")

configMap := &corev1.ConfigMap{BinaryData: map[string][]byte{constant.Result: runInspect}}

inspectInterface.GetResult(context.TODO(), c, nil, configMap, task)

}
20 changes: 10 additions & 10 deletions pkg/inspect/file_change_Inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ func (o *fileChangeInspect) RunInspect(ctx context.Context, task *kubeeyev1alpha
fileResults = append(fileResults, resultItem)
}

if fileResults == nil && len(fileResults) == 0 {
return nil, nil
}

marshal, err := json.Marshal(fileResults)
if err != nil {
return nil, err
Expand All @@ -130,18 +126,22 @@ func (o *fileChangeInspect) RunInspect(ctx context.Context, task *kubeeyev1alpha
}

func (o *fileChangeInspect) GetResult(ctx context.Context, c client.Client, jobs *v1.Job, result *corev1.ConfigMap, task *kubeeyev1alpha2.InspectTask) error {

var fileChangeResult []kubeeyev1alpha2.FileChangeResultItem
jsonErr := json.Unmarshal(result.BinaryData[constant.Result], &fileChangeResult)
if jsonErr != nil {
klog.Error("failed to get result", jsonErr)
return jsonErr
}
if fileChangeResult == nil {
return nil
}
runNodeName := findJobRunNode(ctx, jobs, c)
var inspectResult kubeeyev1alpha2.InspectResult
err := c.Get(ctx, types.NamespacedName{
Namespace: task.Namespace,
Name: fmt.Sprintf("%s-nodeinfo", task.Name),
}, &inspectResult)
var fileChangeResult []kubeeyev1alpha2.FileChangeResultItem
jsonErr := json.Unmarshal(result.BinaryData[constant.Result], &fileChangeResult)
if jsonErr != nil {
klog.Error("failed to get result", jsonErr)
return err
}
if err != nil {
if kubeErr.IsNotFound(err) {
var ownerRefBol = true
Expand Down
5 changes: 2 additions & 3 deletions pkg/inspect/prometheus_Inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ func (o *prometheusInspect) RunInspect(ctx context.Context, task *kubeeyev1alpha

proRuleResult = append(proRuleResult, queryResultsMap)
}
if proRuleResult == nil && len(proRuleResult) == 0 {
return nil, nil
}

marshal, err := json.Marshal(proRuleResult)
if err != nil {
return nil, err
Expand All @@ -111,6 +109,7 @@ func (o *prometheusInspect) RunInspect(ctx context.Context, task *kubeeyev1alpha

func (o *prometheusInspect) GetResult(ctx context.Context, c client.Client, jobs *v1.Job, result *corev1.ConfigMap, task *kubeeyev1alpha2.InspectTask) error {
var prometheus [][]map[string]string

err := json.Unmarshal(result.BinaryData[constant.Result], &prometheus)
if err != nil {
return err
Expand Down
17 changes: 11 additions & 6 deletions pkg/inspect/systemd_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,23 @@ func (o *systemdInspect) RunInspect(ctx context.Context, task *kubeeyev1alpha2.I
}

func (o *systemdInspect) GetResult(ctx context.Context, c client.Client, jobs *v1.Job, result *corev1.ConfigMap, task *kubeeyev1alpha2.InspectTask) error {

var nodeInfoResult []kubeeyev1alpha2.NodeResultItem
jsonErr := json.Unmarshal(result.BinaryData[constant.Result], &nodeInfoResult)
if jsonErr != nil {
klog.Error("failed to get result", jsonErr)
return jsonErr
}

if nodeInfoResult == nil {
return nil
}
runNodeName := findJobRunNode(ctx, jobs, c)
var inspectResult kubeeyev1alpha2.InspectResult
err := c.Get(ctx, types.NamespacedName{
Namespace: task.Namespace,
Name: fmt.Sprintf("%s-nodeinfo", task.Name),
}, &inspectResult)
var nodeInfoResult []kubeeyev1alpha2.NodeResultItem
jsonErr := json.Unmarshal(result.BinaryData[constant.Result], &nodeInfoResult)
if jsonErr != nil {
klog.Error("failed to get result", jsonErr)
return err
}
if err != nil {
if kubeErr.IsNotFound(err) {
var ownerRefBol = true
Expand Down

0 comments on commit b53ea8e

Please sign in to comment.