-
Notifications
You must be signed in to change notification settings - Fork 56
/
validate_kube.go
121 lines (94 loc) · 3.01 KB
/
validate_kube.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
117
118
119
120
121
package main
import (
"context"
"flag"
"fmt"
"path/filepath"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"github.com/sourcegraph/src-cli/internal/validate/kube"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
func init() {
usage := `'src validate kube' is a tool that validates a Kubernetes based Sourcegraph deployment
Examples:
Run default deployment validation:
$ src validate kube
Specify Kubernetes namespace:
$ src validate kube --namespace sourcegraph
Specify the kubeconfig file())) location:
$ src validate kube --kubeconfig ~/.kube/config
Suppress output (useful for CI/CD pipelines)
$ src validate kube --quiet
Validate EKS cluster:
$ src validate kube --eks
Validate GKE cluster:
$ src validate kube --gke
Validate AKS cluster:
$ src validate kube --aks
`
flagSet := flag.NewFlagSet("kube", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src validate %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
kubeConfig *string
namespace = flagSet.String("namespace", "", "(optional) specify the kubernetes namespace to use")
quiet = flagSet.Bool("quiet", false, "(optional) suppress output and return exit status only")
eks = flagSet.Bool("eks", false, "(optional) validate EKS cluster")
gke = flagSet.Bool("gke", false, "(optional) validate GKE cluster")
aks = flagSet.Bool("aks", false, "(optional) validate AKS cluster")
)
if home := homedir.HomeDir(); home != "" {
kubeConfig = flagSet.String(
"kubeconfig",
filepath.Join(home, ".kube", "config"),
"(optional) absolute path to the kubeconfig file",
)
} else {
kubeConfig = flagSet.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
handler := func(args []string) error {
ctx := context.Background()
if err := flagSet.Parse(args); err != nil {
return err
}
// use the current context in kubeConfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
if err != nil {
return errors.Wrap(err, "failed to load kubernetes config")
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Wrap(err, "failed to create kubernetes client")
}
// parse through flag options
var options []kube.Option
if *namespace != "" {
options = append(options, kube.WithNamespace(*namespace))
}
if *quiet {
options = append(options, kube.Quiet())
}
if *eks {
options = append(options, kube.GenerateAWSClients(ctx))
}
if *gke {
options = append(options, kube.Gke())
}
if *aks {
options = append(options, kube.Aks())
}
return kube.Validate(context.Background(), clientSet, config, options...)
}
validateCommands = append(validateCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}