-
Notifications
You must be signed in to change notification settings - Fork 589
/
main.go
146 lines (129 loc) · 3.54 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
// This script cleans up orphaned GKE clusters and Konnect runtime
// groups that were created by the e2e tests (caued by e.g. unexpected
// crash that didn't allow a test's teardown to be completed correctly).
// It's meant to be installed as a cronjob and run repeatedly throughout
// the day to catch any orphaned resources: however tests should be trying to
// delete the resources they create themselves.
//
// A cluster is considered orphaned when all conditions are satisfied:
// 1. Its name begins with a predefined prefix (`gke-e2e-`).
// 2. It was created more than 1h ago.
//
// A control plane is considered orphaned when all conditions are satisfied:
// 1. It has a label `created_in_tests` with value `true`.
// 2. It was created more than 1h ago.
//
// Usage: `go run ./hack/cleanup [mode]`
// Where `mode` is one of:
// - `all` (default): clean up both GKE clusters and Konnect control planes
// - `gke`: clean up only GKE clusters
// - `konnect`: clean up only Konnect control planes
package main
import (
"context"
"fmt"
"os"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/kong/kubernetes-testing-framework/pkg/clusters/types/gke"
"go.uber.org/zap"
)
const (
konnectAccessTokenVar = "TEST_KONG_KONNECT_ACCESS_TOKEN" //nolint:gosec
cleanupModeAll = "all"
cleanupModeGKE = "gke"
cleanupModeKonnect = "konnect"
)
var (
gkeCreds = os.Getenv(gke.GKECredsVar)
gkeProject = os.Getenv(gke.GKEProjectVar)
gkeLocation = os.Getenv(gke.GKELocationVar)
konnectAccessToken = os.Getenv(konnectAccessTokenVar)
)
func main() {
zaplog, err := zap.NewDevelopment()
if err != nil {
os.Exit(1)
}
log := zapr.NewLogger(zaplog)
mode, err := getCleanupMode()
if err != nil {
log.Error(err, "error getting cleanup mode")
os.Exit(1)
}
if err := validateVars(mode); err != nil {
log.Error(err, "error validating vars")
os.Exit(1)
}
cleanupFuncs := resolveCleanupFuncs(mode)
ctx := context.Background()
for _, f := range cleanupFuncs {
if err := f(ctx, log); err != nil {
log.Error(err, "error running cleanup function")
os.Exit(1)
}
}
}
func getCleanupMode() (string, error) {
if len(os.Args) < 2 {
return cleanupModeAll, nil
}
switch os.Args[1] {
case cleanupModeGKE:
case cleanupModeKonnect:
default:
return "", fmt.Errorf("invalid cleanup mode: %s", os.Args[1])
}
return os.Args[1], nil
}
func resolveCleanupFuncs(mode string) []func(context.Context, logr.Logger) error {
switch mode {
case cleanupModeGKE:
return []func(context.Context, logr.Logger) error{
cleanupGKEClusters,
}
case cleanupModeKonnect:
return []func(context.Context, logr.Logger) error{
cleanupKonnectControlPlanes,
}
default:
return []func(context.Context, logr.Logger) error{
cleanupGKEClusters,
cleanupKonnectControlPlanes,
}
}
}
func validateVars(mode string) error {
switch mode {
case cleanupModeGKE:
return validateGKEVars()
case cleanupModeKonnect:
return validateKonnectVars()
default:
if err := validateGKEVars(); err != nil {
return err
}
if err := validateKonnectVars(); err != nil {
return err
}
return nil
}
}
func validateKonnectVars() error {
return notEmpty(konnectAccessTokenVar, konnectAccessToken)
}
func validateGKEVars() error {
if err := notEmpty(gke.GKECredsVar, gkeCreds); err != nil {
return err
}
if err := notEmpty(gke.GKEProjectVar, gkeProject); err != nil {
return err
}
return notEmpty(gke.GKELocationVar, gkeLocation)
}
func notEmpty(name, value string) error {
if value == "" {
return fmt.Errorf("%s was empty", name)
}
return nil
}