/
main.go
160 lines (137 loc) · 4.27 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
156
157
158
159
160
// k8s-tester-cloudwatch-agent installs Kubernetes cloudwatch-agent tester.
package main
import (
"fmt"
"os"
"github.com/aws/aws-k8s-tester/client"
cloudwatch_agent "github.com/aws/aws-k8s-tester/k8s-tester/cloudwatch-agent"
"github.com/aws/aws-k8s-tester/utils/log"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var rootCmd = &cobra.Command{
Use: "k8s-tester-cloudwatch-agent",
Short: "Kubernetes cloudwatch-agent tester",
SuggestFor: []string{"cloudwatch-agent"},
}
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", cloudwatch_agent.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-cloudwatch-agent failed %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
var (
region string
clusterName string
)
func newApply() *cobra.Command {
cmd := &cobra.Command{
Use: "apply",
Short: "Apply tests",
Run: createApplyFunc,
}
cmd.PersistentFlags().StringVar(®ion, "region", "", "region")
cmd.PersistentFlags().StringVar(&clusterName, "cluster-name", "", "cluster name")
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))
}
// TODO: notify stopc
cfg := &cloudwatch_agent.Config{
Prompt: prompt,
Stopc: make(chan struct{}),
Logger: lg,
LogWriter: logWriter,
MinimumNodes: minimumNodes,
Namespace: namespace,
Client: cli,
Region: region,
ClusterName: clusterName,
}
ts := cloudwatch_agent.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-cloudwatch-agent 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 := &cloudwatch_agent.Config{
Prompt: prompt,
Logger: lg,
LogWriter: logWriter,
Namespace: namespace,
Client: cli,
}
ts := cloudwatch_agent.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-cloudwatch-agent delete' success\n")
}