-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
231 lines (204 loc) · 6.53 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"os"
"time"
str2duration "github.com/xhit/go-str2duration/v2"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var verboseFlag = cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "enables debugging log level",
}
var versionFlag = cli.BoolFlag{
Name: "version",
Aliases: []string{"V"},
Usage: "print only the version",
}
var k8sContextFlag = cli.StringFlag{
Name: "context",
Aliases: []string{"c"},
DefaultText: "aks-test",
Usage: "Context name in your KUBECONFIG",
Required: true,
Value: "aks-test",
}
var ageFlag = cli.StringFlag{
Name: "age",
Aliases: []string{"a"},
DefaultText: "30d",
Usage: "The max allowed age of an image. For example 1w2d6h3ns (1 week 2 days 6 hours and 3 nanoseconds).",
Required: true,
Value: "30d",
}
var filterNamespaceAnnotationFlag = cli.StringFlag{
Name: "filter",
Aliases: []string{"f"},
DefaultText: "type=workload",
Usage: "Filter on namespaces containing the annotation and value. Without this filter all namespaces are checked",
Required: false,
Value: "",
}
var emailNamespaceAnnotationFlag = cli.StringFlag{
Name: "email",
//Aliases: []string{"e"},
DefaultText: "email",
Usage: "The annotation key on the namespaces containing an email address to contact if there are outdated images used in this namespace",
Required: false,
Value: "",
}
var resultFileNameFlag = cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
DefaultText: "result.json",
Usage: "The name of the file to write the result",
Required: false,
Value: "result.json",
}
var resultFileFormatFlag = cli.StringFlag{
Name: "format",
DefaultText: "json",
Usage: "The format of the results. Allowed values are \"json\" or \"csv\"",
Required: false,
Value: "json",
}
var resultFormatGroupByEmailFlag = cli.BoolFlag{
Name: "groupByEmail",
Usage: "groups the findings by email in the json output format (only applicable to the json output)",
}
var sendEmailUserFlag = cli.BoolFlag{
Name: "sendEmail",
Usage: "when set, send email notification with findings to email recipients found on the namespace metadata configured by the --email flag",
}
var emailUserContentPrefixFilePathFlag = cli.StringFlag{
Name: "emailUserContentPrefixFilePath",
Usage: "Path to a file containing html code to be inserted before the reult table in the user facing email notification",
Required: false,
}
var emailUserContentSuffixFilePathFlag = cli.StringFlag{
Name: "emailUserContentSuffixFilePath",
Usage: "Path to a file containing html code to be inserted after the reult table in the user facing email notification",
Required: false,
}
var sendAdminEmailFlag = cli.BoolFlag{
Name: "sendAdminEmail",
Usage: "when set, send Admin email notification with findings to email recipients found on the namespace metadata configured by the --email flag",
}
var emailAdminAdress = cli.StringFlag{
Name: "emailAdminAddress",
Usage: "Email-Adress to send to the Admin report",
Required: false,
}
var emailAdminContentPrefixFilePathFlag = cli.StringFlag{
Name: "emailAdminContentPrefixFilePath",
Usage: "Path to a file containing html code to be inserted before the reult table in the admin email notification",
Required: false,
}
var emailAdminContentSuffixFilePathFlag = cli.StringFlag{
Name: "emailAdminContentSuffixFilePath",
Usage: "Path to a file containing html code to be inserted after the reult table in the admin email notification",
Required: false,
}
var smtpSenderAddressFlag = cli.StringFlag{
Name: "smtpSenderAddress",
Usage: "The sender email address to use",
Required: false,
}
var smtpUsernameFlag = cli.StringFlag{
Name: "smtpUsername",
Usage: "Username for authenticating with the SMTP server",
Required: false,
}
var smtpPasswordFlag = cli.StringFlag{
Name: "smtpPassword",
Usage: "Password for authenticating with the SMTP server",
Required: false,
}
var smtpServerAddressFlag = cli.StringFlag{
Name: "smtpAddress",
Usage: "Adress of the SMTP server (expected format is host:port)",
Required: false,
}
func main() {
app := &cli.App{
UseShortOptionHandling: true,
Flags: []cli.Flag{
&verboseFlag,
&versionFlag,
},
Before: func(ctx *cli.Context) error {
verboseFlagValue := ctx.Bool(verboseFlag.Name)
if verboseFlagValue {
log.SetLevel(log.DebugLevel)
}
return nil
},
Commands: []*cli.Command{
{
Name: "find",
Usage: "do it!",
UsageText: "find - does the finding",
//Description: "no really, there is a lot of dooing to be done",
//ArgsUsage: "[arrgh]",
Flags: []cli.Flag{
&k8sContextFlag,
&ageFlag,
&filterNamespaceAnnotationFlag,
&emailNamespaceAnnotationFlag,
&resultFileNameFlag,
&resultFileFormatFlag,
&resultFormatGroupByEmailFlag,
&sendEmailUserFlag,
&emailUserContentPrefixFilePathFlag,
&emailUserContentSuffixFilePathFlag,
&sendAdminEmailFlag,
&emailAdminContentPrefixFilePathFlag,
&emailAdminContentSuffixFilePathFlag,
&emailAdminAdress,
&smtpSenderAddressFlag,
&smtpUsernameFlag,
&smtpPasswordFlag,
&smtpServerAddressFlag,
},
Action: func(c *cli.Context) error {
return findOutdatedImages(c)
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
/**
* Template method
*/
func findOutdatedImages(ctx *cli.Context) error {
images := make(map[string]ImageData)
namespaces := make(map[string]*NotificationData)
// Preprare:
var k8sclient = getK8sClient(ctx)
allowedAge, err := str2duration.ParseDuration(ctx.String(ageFlag.Name))
if err != nil {
log.Errorf("Cannot parse allowed age from '%s' for flag '--%s'", ctx.String(ageFlag.Name), ageFlag.Name)
}
oldestAllowedTimestamp := time.Now().Add(-allowedAge)
//0. Step: Get all namespaces with relevant data and filter them
getNamespaces(&namespaces, ctx, k8sclient)
//1. Step: Get all (filtered) container images running in the cluster
getImages(&images, &namespaces, ctx, k8sclient)
//2. Step: Query Registry for Build-Timestamp of each image
queryTimestamps(&images)
//3. Step: filter Images which are outdated
filterOutdatedImages(&images, oldestAllowedTimestamp)
log.Debugf("Found %d outdated images", len(images))
//4. Step: Output results
outputJsonResult(&images, ctx)
outputCsvResult(&images, ctx)
//5. Step: Send notifications
sendEmailAdminNotification(&images, ctx)
sendEmailUserNotifications(&images, ctx)
return nil
}