1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
- "fmt"
6
6
"log"
7
7
exec "os/exec"
8
8
@@ -11,70 +11,76 @@ import (
11
11
"github.com/intelops/kubviz/model"
12
12
"github.com/nats-io/nats.go"
13
13
"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"
15
16
)
16
17
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 {})
30
22
if err != nil {
31
- log .Printf ("Error executing command: %s " , err )
23
+ log .Println ("Error occurred while getting client set for kube-score: " , err )
32
24
return err
33
25
}
34
26
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
+ }
37
34
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...
39
45
err = json .Unmarshal ([]byte (out ), & report )
40
46
if err != nil {
41
47
log .Printf ("Error occurred while Unmarshalling json: %v" , err )
42
48
return err
43
49
}
44
50
45
51
publishKubescoreMetrics (report , js )
52
+ //err = publishKubescoreMetrics(uuid.New().String(), ns, out, js)
53
+ if err != nil {
54
+ return err
55
+ }
46
56
return nil
47
57
}
48
58
49
- func publishKubescoreMetrics (report []json_v2.ScoredObject , js nats.JetStreamContext ) {
59
+ func publishKubescoreMetrics (report []json_v2.ScoredObject , js nats.JetStreamContext ) error {
50
60
metrics := model.KubeScoreRecommendations {
51
61
ID : uuid .New ().String (),
52
62
ClusterName : ClusterName ,
53
63
Report : report ,
54
64
}
55
- metricsJson , err := json .Marshal (metrics )
65
+ metricsJson , _ := json .Marshal (metrics )
66
+ _ , err := js .Publish (constants .KUBESCORE_SUBJECT , metricsJson )
56
67
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
64
69
}
70
+ //log.Printf("Recommendations with ID:%s has been published\n", id)
71
+ log .Printf ("Recommendations :%#v" , report )
72
+ return nil
65
73
}
66
74
67
75
func executeCommand (command string ) (string , error ) {
68
76
cmd := exec .Command ("/bin/sh" , "-c" , command )
69
77
stdout , err := cmd .Output ()
70
78
71
79
if err != nil {
72
- log .Printf ("Error executing command: %s" , err )
73
- return "" , err
80
+ log .Println ("Execute Command Error" , err .Error ())
74
81
}
75
82
76
83
// Print the output
77
- log .Printf ("Command Output: %s" , string (stdout ))
78
-
84
+ log .Println (string (stdout ))
79
85
return string (stdout ), nil
80
86
}
0 commit comments