/
main.go
151 lines (130 loc) · 4.05 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// k8s-tester-csi-ebs installs Kubernetes csi-ebs tester.
package main
import (
"fmt"
"os"
"github.com/aws/aws-k8s-tester/client"
csi_ebs "github.com/aws/aws-k8s-tester/k8s-tester/csi-ebs"
"github.com/aws/aws-k8s-tester/utils/log"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var rootCmd = &cobra.Command{
Use: "k8s-tester-csi-ebs",
Short: "Kubernetes CSI EBS tester",
SuggestFor: []string{"csi-ebs"},
}
func init() {
cobra.EnablePrefixMatching = true
}
var (
prompt bool
logLevel string
logOutputs []string
minimumNodes int
namespace string
kubectlDownloadURL string
kubectlPath string
kubeconfigPath string
)
func init() {
rootCmd.PersistentFlags().BoolVar(&prompt, "prompt", true, "'true' to enable prompt mode")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", log.DefaultLogLevel, "Logging level")
rootCmd.PersistentFlags().StringSliceVar(&logOutputs, "log-outputs", []string{"stderr"}, "Additional logger outputs")
rootCmd.PersistentFlags().IntVar(&minimumNodes, "minimum-nodes", csi_ebs.DefaultMinimumNodes, "minimum number of Kubernetes nodes required for installing this addon")
rootCmd.PersistentFlags().StringVar(&namespace, "namespace", "test-namespace", "'true' to auto-generate path for create config/cluster, overwrites existing --path value")
rootCmd.PersistentFlags().StringVar(&kubectlDownloadURL, "kubectl-download-url", client.DefaultKubectlDownloadURL(), "kubectl download URL")
rootCmd.PersistentFlags().StringVar(&kubectlPath, "kubectl-path", client.DefaultKubectlPath(), "kubectl path")
rootCmd.PersistentFlags().StringVar(&kubeconfigPath, "kubeconfig-path", "", "KUBECONFIG path")
rootCmd.AddCommand(
newApply(),
newDelete(),
)
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "k8s-tester-csi-ebs failed %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
var helmChartRepoURL string
func newApply() *cobra.Command {
cmd := &cobra.Command{
Use: "apply",
Short: "Apply tests",
Run: createApplyFunc,
}
cmd.PersistentFlags().StringVar(&helmChartRepoURL, "helm-chart-repo-url", csi_ebs.DefaultHelmChartRepoURL, "helm chart repo URL")
return cmd
}
func createApplyFunc(cmd *cobra.Command, args []string) {
lg, logWriter, _, err := log.NewWithStderrWriter(logLevel, logOutputs)
if err != nil {
panic(err)
}
_ = zap.ReplaceGlobals(lg)
cli, err := client.New(&client.Config{
Logger: lg,
KubectlDownloadURL: kubectlDownloadURL,
KubectlPath: kubectlPath,
KubeconfigPath: kubeconfigPath,
})
if err != nil {
lg.Panic("failed to create client", zap.Error(err))
}
cfg := &csi_ebs.Config{
Prompt: prompt,
Logger: lg,
LogWriter: logWriter,
MinimumNodes: minimumNodes,
HelmChartRepoURL: helmChartRepoURL,
Namespace: namespace,
Client: cli,
}
ts := csi_ebs.New(cfg)
if err := ts.Apply(); err != nil {
fmt.Fprintf(os.Stderr, "failed to apply (%v)\n", err)
os.Exit(1)
}
fmt.Printf("\n*********************************\n")
fmt.Printf("'k8s-tester-csi-ebs apply' success\n")
}
func newDelete() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete resources",
Run: createDeleteFunc,
}
return cmd
}
func createDeleteFunc(cmd *cobra.Command, args []string) {
lg, logWriter, _, err := log.NewWithStderrWriter(logLevel, logOutputs)
if err != nil {
panic(err)
}
_ = zap.ReplaceGlobals(lg)
cli, err := client.New(&client.Config{
Logger: lg,
KubectlDownloadURL: kubectlDownloadURL,
KubectlPath: kubectlPath,
KubeconfigPath: kubeconfigPath,
})
if err != nil {
lg.Panic("failed to create client", zap.Error(err))
}
cfg := &csi_ebs.Config{
Prompt: prompt,
Logger: lg,
LogWriter: logWriter,
Namespace: namespace,
Client: cli,
}
ts := csi_ebs.New(cfg)
if err := ts.Delete(); err != nil {
fmt.Fprintf(os.Stderr, "failed to delete (%v)\n", err)
os.Exit(1)
}
fmt.Printf("\n*********************************\n")
fmt.Printf("'k8s-tester-csi-ebs delete' success\n")
}