-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (97 loc) · 2.7 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"github.com/bespinian/livelint/internal/livelint"
tea "github.com/charmbracelet/bubbletea"
"github.com/urfave/cli/v2"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
var buildversion, builddate, githash string
var errNoHome = errors.New("error finding your HOME directory")
func main() {
kubeconfig := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
if kubeconfig == "" {
home := homedir.HomeDir()
if home == "" {
exitWithErr(errNoHome)
}
kubeconfig = filepath.Join(home, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName)
}
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
exitWithErr(fmt.Errorf("error building k8s config from flags: %w", err))
}
k8s, err := kubernetes.NewForConfig(config)
if err != nil {
exitWithErr(fmt.Errorf("error creating k8s client set: %w", err))
}
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
namespace, _, err := kubeConfig.Namespace()
if err != nil {
exitWithErr(fmt.Errorf("error getting namespace from k8s config: %w", err))
}
ui := livelint.NewBubbleTeaUI(tea.NewProgram(livelint.InitialModel()))
ll := livelint.New(k8s, config, ui)
app := &cli.App{
Name: "livelint",
Usage: "debug k8s workloads",
Version: buildversion,
Commands: []*cli.Command{
{
Name: "check",
Aliases: []string{"c"},
Usage: "checks a Kubernetes deployment for issues",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "namespace",
Aliases: []string{"n"},
Value: namespace,
Usage: "the source namespace",
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Value: false,
Usage: "if livelint should be verbose about what it's doing",
},
},
Action: func(c *cli.Context) error {
args := c.Args()
go func() {
defer ui.Quit()
runErr := ll.RunChecks(c.String("namespace"), args.Get(0), c.Bool("verbose"))
if runErr != nil {
ui.DisplayError(runErr)
}
}()
_, err = ui.Run()
if err != nil {
return fmt.Errorf("failed to start UI: %w", err)
}
return nil
},
},
},
Metadata: map[string]interface{}{
"build-date": builddate,
"git-hash": githash,
},
}
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func exitWithErr(err error) {
_, _ = fmt.Fprintf(os.Stderr, "failed to start livelint: %s", err)
os.Exit(1)
}