This repository was archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
105 lines (96 loc) · 2.59 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
package main
import (
"flag"
"fmt"
"os"
"regexp"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
const (
// Default filename
defaultRulesFile string = "rules.yaml"
// Default TLS port
defaultTLSPort int = 8443
)
var (
// filePath String pointer of path to rules yaml file
filePath *string
// tls Enable TLS
tls *bool
// tlsCert Path to certificate
tlsCert *string
// tlsKey Path to key
tlsKey *string
// tlsPort TLS listening port
tlsPort *int
// r main rules struct to hold current ruleset
r rules
// version is used to output the version of the application
version string
// G default gin engine
g = gin.Default()
)
// Read flags from command line args and set defaults
func flags() {
// --file arg
filePath = flag.String("file", defaultRulesFile, "Path to yaml file with ruleset")
// --metrics arg
metrics := flag.Bool("metrics", str2bool(os.Getenv("METRICS")), "Enable prometheus endpoint at /metrics")
// --tls arg
tls = flag.Bool("tls", str2bool(os.Getenv("TLS_ENABLED")), "Enable TLS")
// --tls-cert arg
tlsCert = flag.String("tls-cert", os.Getenv("TLS_CERT"), "Path to TLS certificate")
// --tls-key arg
tlsKey = flag.String("tls-key", os.Getenv("TLS_KEY"), "Path to TLS key")
// --tls-port arg
tlsPort = flag.Int("tls-port", defaultTLSPort, "TLS listening port")
flag.Parse()
// Input file validation
if *filePath == "" {
flag.PrintDefaults()
log.Fatal("No file provided")
}
// Metrics flag validation
if *metrics {
// Create prometheus registry named "gin"
p := newRegistry("gin")
// Pass gin to inject prometheus middleware
p.Use(g)
}
}
// @title k8s-label-rules-webhook
// @description A kubernetes webhook to standardize labels on resources
// @contact.name GitHub
// @contact.url https://github.com/circa10a/k8s-label-rules-webhook/
// @license.name MIT
// @license.url https://github.com/circa10a/k8s-label-rules-webhook/blob/main/LICENSE
func main() {
// Output version of application
log.Infof("Version: %s", version)
// Validate command line arguments
flags()
// Instantiate map to cache regex compilations in
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Load initial rules into memory
err := r.load(*filePath)
if err != nil {
log.Error(err)
}
// Initialize paths and handlers in routes.go
routes(g)
// Listen via https if TLS enabled
if *tls {
err = g.RunTLS(fmt.Sprintf(":%d", *tlsPort), *tlsCert, *tlsKey)
if err != nil {
log.Fatal(err)
}
}
// Else listen on http
// Defaults to port 8080, can be overridden via PORT env var.
// Example: export PORT=3000
err = g.Run()
if err != nil {
log.Fatal(err)
}
}