-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (164 loc) · 5.46 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
package main
import (
"errors"
"fmt"
"net/http"
"os"
"regexp"
"runtime"
"strings"
"github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/webdevops/go-common/azuresdk/armclient"
"github.com/webdevops/go-common/azuresdk/azidentity"
"github.com/webdevops/go-common/azuresdk/prometheus/tracing"
"github.com/webdevops/azure-janitor/config"
"github.com/webdevops/azure-janitor/janitor"
)
const (
Author = "webdevops.io"
UserAgent = "azure-janitor/"
)
var (
argparser *flags.Parser
Opts config.Opts
AzureClient *armclient.ArmClient
// Git version information
gitCommit = "<unknown>"
gitTag = "<unknown>"
)
func main() {
initArgparser()
logger.Infof("starting azure-janitor v%s (%s; %s; by %v)", gitTag, gitCommit, runtime.Version(), Author)
logger.Info(string(Opts.GetJson()))
initSystem()
logger.Infof("init Azure connection")
initAzureConnection()
logger.Infof("init Janitor")
go func() {
j := janitor.Janitor{
Conf: Opts,
UserAgent: UserAgent + gitTag,
Logger: logger,
Azure: janitor.JanitorAzureConfig{
Client: AzureClient,
Subscription: Opts.Azure.Subscription,
},
}
j.Init()
j.Run()
}()
logger.Infof("starting http server on %s", Opts.Server.Bind)
startHttpServer()
}
// init argparser and parse/validate arguments
func initArgparser() {
argparser = flags.NewParser(&Opts, flags.Default)
_, err := argparser.Parse()
// check if there is an parse error
if err != nil {
var flagsErr *flags.Error
if ok := errors.As(err, &flagsErr); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Println()
argparser.WriteHelp(os.Stdout)
os.Exit(1)
}
}
initLogger()
// resourceGroup filter
Opts.Janitor.ResourceGroups.Filter = fmt.Sprintf(
"tagName eq '%s'",
strings.Replace(Opts.Janitor.Tag, "'", "\\'", -1),
)
// ResourceGroups: add additional filter
if Opts.Janitor.ResourceGroups.AdditionalFilter != nil {
Opts.Janitor.ResourceGroups.Filter = fmt.Sprintf(
"%s and %s",
Opts.Janitor.ResourceGroups.Filter,
*Opts.Janitor.ResourceGroups.AdditionalFilter,
)
}
// Resources: add additional filter
// resource (if we specify tagValue here we don't get the tag.. wtf?!)
Opts.Janitor.Resources.Filter = ""
if Opts.Janitor.Resources.AdditionalFilter != nil {
Opts.Janitor.Resources.Filter = *Opts.Janitor.Resources.AdditionalFilter
}
if Opts.Janitor.RoleAssignments.Enable {
if len(Opts.Janitor.RoleAssignments.RoleDefintionIds) == 0 {
logger.Fatal("roleAssignment janitor active but no roleDefinitionIds defined")
}
}
// RoleAssignments: add additional filter
if Opts.Janitor.RoleAssignments.AdditionalFilter != nil {
Opts.Janitor.RoleAssignments.Filter = *Opts.Janitor.RoleAssignments.AdditionalFilter
}
if !Opts.Janitor.ResourceGroups.Enable && !Opts.Janitor.Resources.Enable && !Opts.Janitor.Deployments.Enable && !Opts.Janitor.RoleAssignments.Enable {
logger.Fatal(`no janitor task (resources, resourcegroups, deployments, roleassignments) enabled, not starting`)
}
if Opts.Janitor.RoleAssignments.DescriptionTtl != nil {
Opts.Janitor.RoleAssignments.DescriptionTtlRegExp = regexp.MustCompile(*Opts.Janitor.RoleAssignments.DescriptionTtl)
}
for _, val := range Opts.Janitor.RoleAssignments.RoleDefintionIds {
val = strings.ToLower(val)
if !strings.Contains(val, "/providers/microsoft.authorization/roledefinitions/") {
logger.Fatal(`roleAssignment roleDefinition IDs MUST contain "/providers/Microsoft.Authorization/" for security reasons`)
}
}
checkForDeprecations()
}
func checkForDeprecations() {
deprecatedEnvVars := map[string]string{
`JANITOR_ENABLE_DEPLOYMENTS`: `use env "JANITOR_DEPLOYMENTS_ENABLE" instead`,
`JANITOR_DEPLOYMENT_TTL`: `use env "JANITOR_DEPLOYMENTS_TTL" instead`,
`JANITOR_DEPLOYMENT_LIMIT`: `use env "JANITOR_DEPLOYMENTS_LIMIT" instead`,
`JANITOR_ENABLE_RESOURCEGROUPS`: `use env "JANITOR_RESOURCEGROUPS_ENABLE" instead`,
`JANITOR_FILTER_RESOURCEGROUPS`: `use env "JANITOR_RESOURCEGROUPS_FILTER" instead`,
`JANITOR_ENABLE_RESOURCES`: `use env "JANITOR_RESOURCES_ENABLE" instead`,
`JANITOR_FILTER_RESOURCES`: `use env "JANITOR_RESOURCES_FILTER" instead`,
}
for envVar, solution := range deprecatedEnvVars {
if val := os.Getenv(envVar); val != "" {
logger.Panicf(`unsupported environment variable "%v" detected: %v`, envVar, solution)
}
}
}
func initAzureConnection() {
var err error
if Opts.Azure.Environment != nil {
if err := os.Setenv(azidentity.EnvAzureEnvironment, *Opts.Azure.Environment); err != nil {
logger.Warnf(`unable to set envvar "%s": %v`, azidentity.EnvAzureEnvironment, err.Error())
}
}
AzureClient, err = armclient.NewArmClientFromEnvironment(logger)
if err != nil {
logger.Fatal(err.Error())
}
AzureClient.SetUserAgent(UserAgent + gitTag)
}
// start and handle prometheus handler
func startHttpServer() {
mux := http.NewServeMux()
// healthz
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, "Ok"); err != nil {
logger.Error(err)
}
})
// readyz
mux.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, "Ok"); err != nil {
logger.Error(err)
}
})
mux.Handle("/metrics", tracing.RegisterAzureMetricAutoClean(promhttp.Handler()))
srv := &http.Server{
Addr: Opts.Server.Bind,
Handler: mux,
ReadTimeout: Opts.Server.ReadTimeout,
WriteTimeout: Opts.Server.WriteTimeout,
}
logger.Fatal(srv.ListenAndServe())
}