-
Notifications
You must be signed in to change notification settings - Fork 79
/
main.go
155 lines (131 loc) · 3.87 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
152
153
154
155
// k8s-tester-wordpress installs Kubernetes wordpress tester.
package main
import (
"fmt"
"os"
"github.com/aws/aws-k8s-tester/client"
"github.com/aws/aws-k8s-tester/k8s-tester/wordpress"
"github.com/aws/aws-k8s-tester/utils/log"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var rootCmd = &cobra.Command{
Use: "k8s-tester-wordpress",
Short: "Kubernetes wordpress tester",
SuggestFor: []string{"wordpress"},
}
func init() {
cobra.EnablePrefixMatching = true
}
var (
prompt bool
logLevel string
logOutputs []string
minimumNodes int
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", wordpress.DefaultMinimumNodes, "minimum number of Kubernetes nodes required for installing this addon")
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-wordpress failed %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
var (
partition string
region string
)
func newApply() *cobra.Command {
cmd := &cobra.Command{
Use: "apply",
Short: "Apply tests",
Run: createApplyFunc,
}
cmd.PersistentFlags().StringVar(&partition, "partition", "aws", "partition for AWS region")
cmd.PersistentFlags().StringVar(®ion, "region", "", "region for ELB resource")
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 := &wordpress.Config{
Prompt: prompt,
Logger: lg,
LogWriter: logWriter,
MinimumNodes: minimumNodes,
Client: cli,
Partition: partition,
Region: region,
}
ts := wordpress.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-wordpress 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 := &wordpress.Config{
Prompt: prompt,
Logger: lg,
LogWriter: logWriter,
Client: cli,
}
ts := wordpress.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-wordpress delete' success\n")
}