Skip to content

Commit 23205ab

Browse files
committed
kubescore added
1 parent cc14aa7 commit 23205ab

File tree

6 files changed

+59
-52
lines changed

6 files changed

+59
-52
lines changed

agent/kubviz/k8smetrics_agent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func main() {
130130
// //getK8sEvents(clientset)
131131
// err = runTrivyScans(config, js)
132132
// LogErr(err)
133-
err = RunKubeScore(config, js)
133+
err = RunKubeScore(clientset, js)
134134
LogErr(err)
135135
}
136136

@@ -321,7 +321,7 @@ func initScheduler(config *rest.Config, js nats.JetStreamContext, cfg config.Age
321321
}
322322
}
323323
if cfg.KubeScoreInterval != "" && cfg.KubeScoreInterval != "0" {
324-
sj, err := NewKubescoreJob(config, js, cfg.KubeScoreInterval)
324+
sj, err := NewKubescoreJob(clientset, js, cfg.KubeScoreInterval)
325325
if err != nil {
326326
log.Fatal("no time interval", err)
327327
}

agent/kubviz/kube_score.go

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
5-
"fmt"
66
"log"
77
exec "os/exec"
88

@@ -11,70 +11,76 @@ import (
1111
"github.com/intelops/kubviz/model"
1212
"github.com/nats-io/nats.go"
1313
"github.com/zegl/kube-score/renderer/json_v2"
14-
"k8s.io/client-go/rest"
14+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
"k8s.io/client-go/kubernetes"
1516
)
1617

17-
func RunKubeScore(config *rest.Config, js nats.JetStreamContext) error {
18-
// _, err := kubernetes.NewForConfig(config)
19-
// if err != nil {
20-
// log.Printf("Error creating Kubernetes clientset: %v", err)
21-
// return err
22-
// }
23-
//defer wg.Done()
24-
var report []json_v2.ScoredObject
25-
cmd := fmt.Sprintf(`kubectl api-resources --verbs=list --namespaced -o name | xargs -n1 -I{} sh -c "kubectl get {} --all-namespaces -oyaml && echo ---" | kube-score score - -o json`)
26-
log.Printf("Command: %s", cmd)
27-
28-
// Execute the command
29-
out, err := executeCommand(cmd)
18+
func RunKubeScore(clientset *kubernetes.Clientset, js nats.JetStreamContext) error {
19+
nsList, err := clientset.CoreV1().
20+
Namespaces().
21+
List(context.Background(), metav1.ListOptions{})
3022
if err != nil {
31-
log.Printf("Error executing command: %s", err)
23+
log.Println("Error occurred while getting client set for kube-score: ", err)
3224
return err
3325
}
3426

35-
// Log the output of the kubectl command
36-
log.Printf("kubectl Command Output: %s", out)
27+
log.Printf("Namespace size: %d", len(nsList.Items))
28+
for _, n := range nsList.Items {
29+
log.Printf("Publishing kube-score recommendations for namespace: %s\n", n.Name)
30+
publish(n.Name, js)
31+
}
32+
return nil
33+
}
3734

38-
// Continue with the rest of the code...
35+
func publish(ns string, js nats.JetStreamContext) error {
36+
var report []json_v2.ScoredObject
37+
cmd := "kubectl api-resources --verbs=list --namespaced -o name | xargs -n1 -I{} sh -c \"kubectl get {} -n " + ns + " -oyaml && echo ---\" | kube-score score - -o json"
38+
log.Printf("Command: %#v,", cmd)
39+
out, err := executeCommand(cmd)
40+
if err != nil {
41+
log.Println("Error occurred while running kube-score: ", err)
42+
return err
43+
}
44+
// // Continue with the rest of the code...
3945
err = json.Unmarshal([]byte(out), &report)
4046
if err != nil {
4147
log.Printf("Error occurred while Unmarshalling json: %v", err)
4248
return err
4349
}
4450

4551
publishKubescoreMetrics(report, js)
52+
//err = publishKubescoreMetrics(uuid.New().String(), ns, out, js)
53+
if err != nil {
54+
return err
55+
}
4656
return nil
4757
}
4858

49-
func publishKubescoreMetrics(report []json_v2.ScoredObject, js nats.JetStreamContext) {
59+
func publishKubescoreMetrics(report []json_v2.ScoredObject, js nats.JetStreamContext) error {
5060
metrics := model.KubeScoreRecommendations{
5161
ID: uuid.New().String(),
5262
ClusterName: ClusterName,
5363
Report: report,
5464
}
55-
metricsJson, err := json.Marshal(metrics)
65+
metricsJson, _ := json.Marshal(metrics)
66+
_, err := js.Publish(constants.KUBESCORE_SUBJECT, metricsJson)
5667
if err != nil {
57-
log.Printf("Error marshaling metrics to JSON: %v", err)
58-
return
59-
}
60-
_, err = js.Publish(constants.KUBESCORE_SUBJECT, metricsJson)
61-
if err != nil {
62-
log.Printf("error occures while publish %v", err)
63-
return
68+
return err
6469
}
70+
//log.Printf("Recommendations with ID:%s has been published\n", id)
71+
log.Printf("Recommendations :%#v", report)
72+
return nil
6573
}
6674

6775
func executeCommand(command string) (string, error) {
6876
cmd := exec.Command("/bin/sh", "-c", command)
6977
stdout, err := cmd.Output()
7078

7179
if err != nil {
72-
log.Printf("Error executing command: %s", err)
73-
return "", err
80+
log.Println("Execute Command Error", err.Error())
7481
}
7582

7683
// Print the output
77-
log.Printf("Command Output: %s", string(stdout))
78-
84+
log.Println(string(stdout))
7985
return string(stdout), nil
8086
}

agent/kubviz/scheduler_watch.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"github.com/nats-io/nats.go"
5+
"k8s.io/client-go/kubernetes"
56
"k8s.io/client-go/rest"
67
)
78

@@ -32,7 +33,7 @@ type KubePreUpgradeJob struct {
3233
frequency string
3334
}
3435
type KubescoreJob struct {
35-
config *rest.Config
36+
clientset *kubernetes.Clientset
3637
js nats.JetStreamContext
3738
frequency string
3839
}
@@ -87,9 +88,9 @@ func (j *KubePreUpgradeJob) Run() {
8788
LogErr(err)
8889
}
8990

90-
func NewKubescoreJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*KubescoreJob, error) {
91+
func NewKubescoreJob(clientset *kubernetes.Clientset, js nats.JetStreamContext, frequency string) (*KubescoreJob, error) {
9192
return &KubescoreJob{
92-
config: config,
93+
clientset: clientset,
9394
js: js,
9495
frequency: frequency,
9596
}, nil
@@ -100,7 +101,7 @@ func (v *KubescoreJob) CronSpec() string {
100101

101102
func (j *KubescoreJob) Run() {
102103
// Call the Kubescore function with the provided config and js
103-
err := RunKubeScore(j.config, j.js)
104+
err := RunKubeScore(j.clientset, j.js)
104105
LogErr(err)
105106
}
106107
func NewRakkessJob(config *rest.Config, js nats.JetStreamContext, frequency string) (*RakkessJob, error) {

client/pkg/clickhouse/db_client.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,6 @@ func (c *DBClient) InsertKubeScoreMetrics(metrics model.KubeScoreRecommendations
532532

533533
currentTime := time.Now().UTC()
534534

535-
// if _, err := stmt.Exec(
536-
// metrics.ID,
537-
// metrics.Namespace,
538-
// metrics.ClusterName,
539-
// metrics.Recommendations,
540-
// currentTime,
541-
// ); err != nil {
542-
// log.Fatal(err)
543-
// }
544535
for _, result := range metrics.Report {
545536
for _, check := range result.Checks {
546537
for _, comments := range check.Comments {

client/pkg/clickhouse/statements.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ const InsertDeletedApi DBStatement = "INSERT INTO DeletedAPIs (ClusterName, Obje
237237
const InsertKubvizEvent DBStatement = "INSERT INTO events (ClusterName, Id, EventTime, OpType, Name, Namespace, Kind, Message, Reason, Host, Event, FirstTime, LastTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
238238
const clickhouseExperimental DBStatement = `SET allow_experimental_object_type=1;`
239239
const containerGithubTable DBStatement = `CREATE table IF NOT EXISTS container_github(event JSON) ENGINE = MergeTree ORDER BY tuple();`
240-
const InsertKubeScore string = "INSERT INTO kubescore (id, namespace, cluster_name, recommendations, EventTime) VALUES (?, ?, ?, ?, ?)"
240+
const InsertKubeScore string = "INSERT INTO kubescore(id,clustername,object_name,kind,apiVersion,name,namespace,target_type,description,path,summary,file_name,file_row,EventTime) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
241241
const InsertTrivyVul string = "INSERT INTO trivy_vul (id, cluster_name, namespace, kind, name, vul_id, vul_vendor_ids, vul_pkg_id, vul_pkg_name, vul_pkg_path, vul_installed_version, vul_fixed_version, vul_title, vul_severity, vul_published_date, vul_last_modified_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?. ?)"
242242
const InsertTrivyImage string = "INSERT INTO trivyimage (id, cluster_name, artifact_name, vul_id, vul_pkg_id, vul_pkg_name, vul_installed_version, vul_fixed_version, vul_title, vul_severity, vul_published_date, vul_last_modified_date) VALUES ( ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
243243
const InsertTrivyMisconfig string = "INSERT INTO trivy_misconfig (id, cluster_name, namespace, kind, name, misconfig_id, misconfig_avdid, misconfig_type, misconfig_title, misconfig_desc, misconfig_msg, misconfig_query, misconfig_resolution, misconfig_severity, misconfig_status, EventTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

sql/000008_kubescore.up.sql

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
CREATE TABLE IF NOT EXISTS kubescore (
2-
id UUID,
3-
namespace String,
4-
cluster_name String,
5-
recommendations String,
2+
id UUID,
3+
clustername String,
4+
object_name String,
5+
kind String,
6+
apiVersion String,
7+
name String,
8+
namespace String,
9+
target_type String,
10+
description String,
11+
path String,
12+
summary String,
13+
file_name String,
14+
file_row BIGINT,
615
EventTime DateTime('UTC'),
716
ExpiryDate DateTime DEFAULT now() + INTERVAL {{.TTLValue}} {{.TTLUnit}}
817
) ENGINE = MergeTree()

0 commit comments

Comments
 (0)