-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (54 loc) · 1.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"os"
"github.com/chen-keinan/k8s-component-info/pkg/k8s"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/spf13/cobra"
)
func main() {
Execute()
}
var report string
var format string
func init() {
rootCmd.Flags().StringVarP(&report, "report", "r", "", "--report cyclonedx")
rootCmd.Flags().StringVarP(&format, "format", "f", "json", "--format json | xml")
}
var rootCmd = &cobra.Command{
Use: "kbom",
Short: "kbom - a simple CLI to produce k8s bill of materials",
Long: "kbom - collect k8s core component , addon and nodes info and produce bil of materials",
RunE: func(cmd *cobra.Command, args []string) error {
cf := genericclioptions.NewConfigFlags(true)
rest.SetDefaultWarningHandler(rest.NoWarnings{})
clientConfig := cf.ToRawKubeConfigLoader()
rc, err := clientConfig.ClientConfig()
if err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(rc)
if err != nil {
return err
}
// collect nodes info
c := k8s.NewCluster(clientset, clientConfig)
clusterBom, err := c.CreatePkgBom()
if err != nil {
panic(err.Error())
}
err = k8s.WritePkgBomOutput(clusterBom, report, format)
if err != nil {
return err
}
return nil
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Whoops. There was an error while executing your CLI '%s'", err)
os.Exit(1)
}
}