-
Notifications
You must be signed in to change notification settings - Fork 106
/
main.go
133 lines (111 loc) · 4.24 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
package main
import (
"flag"
"net/http"
"strings"
"time"
"github.com/mittwald/kubernetes-replicator/replicate/common"
"github.com/mittwald/kubernetes-replicator/replicate/configmap"
"github.com/mittwald/kubernetes-replicator/replicate/role"
"github.com/mittwald/kubernetes-replicator/replicate/rolebinding"
"github.com/mittwald/kubernetes-replicator/replicate/secret"
"github.com/mittwald/kubernetes-replicator/replicate/serviceaccount"
log "github.com/sirupsen/logrus"
"github.com/mittwald/kubernetes-replicator/liveness"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
var f flags
func init() {
var err error
flag.StringVar(&f.Kubeconfig, "kubeconfig", "", "path to Kubernetes config file")
flag.StringVar(&f.ResyncPeriodS, "resync-period", "30m", "resynchronization period")
flag.StringVar(&f.StatusAddr, "status-addr", ":9102", "listen address for status and monitoring server")
flag.StringVar(&f.LogLevel, "log-level", "info", "Log level (trace, debug, info, warn, error)")
flag.StringVar(&f.LogFormat, "log-format", "plain", "Log format (plain, json)")
flag.BoolVar(&f.AllowAll, "allow-all", false, "allow replication of all secrets (CAUTION: only use when you know what you're doing)")
flag.BoolVar(&f.ReplicateSecrets, "replicate-secrets", true, "Enable replication of secrets")
flag.BoolVar(&f.ReplicateConfigMaps, "replicate-configmaps", true, "Enable replication of config maps")
flag.BoolVar(&f.ReplicateRoles, "replicate-roles", true, "Enable replication of roles")
flag.BoolVar(&f.ReplicateRoleBindings, "replicate-role-bindings", true, "Enable replication of role bindings")
flag.BoolVar(&f.ReplicateServiceAccounts, "replicate-service-accounts", true, "Enable replication of service accounts")
flag.BoolVar(&f.SyncByContent, "sync-by-content", false, "Always compare the contents of source and target resources and force them to be the same")
flag.Parse()
switch strings.ToUpper(strings.TrimSpace(f.LogLevel)) {
case "TRACE":
log.SetLevel(log.TraceLevel)
case "DEBUG":
log.SetLevel(log.DebugLevel)
case "WARN", "WARNING":
log.SetLevel(log.WarnLevel)
case "ERROR":
log.SetLevel(log.ErrorLevel)
case "FATAL":
log.SetLevel(log.FatalLevel)
case "PANIC":
log.SetLevel(log.PanicLevel)
default:
log.SetLevel(log.InfoLevel)
}
if strings.ToUpper(strings.TrimSpace(f.LogFormat)) == "JSON" {
log.SetFormatter(&log.JSONFormatter{})
}
f.ResyncPeriod, err = time.ParseDuration(f.ResyncPeriodS)
if err != nil {
panic(err)
}
log.Debugf("using flag values %#v", f)
}
func main() {
var config *rest.Config
var err error
var client kubernetes.Interface
var enabledReplicators []common.Replicator
if f.Kubeconfig == "" {
log.Info("using in-cluster configuration")
config, err = rest.InClusterConfig()
} else {
log.Infof("using configuration from '%s'", f.Kubeconfig)
config, err = clientcmd.BuildConfigFromFlags("", f.Kubeconfig)
}
if err != nil {
panic(err)
}
client = kubernetes.NewForConfigOrDie(config)
if f.ReplicateSecrets {
secretRepl := secret.NewReplicator(client, f.ResyncPeriod, f.AllowAll, f.SyncByContent)
go secretRepl.Run()
enabledReplicators = append(enabledReplicators, secretRepl)
}
if f.ReplicateConfigMaps {
configMapRepl := configmap.NewReplicator(client, f.ResyncPeriod, f.AllowAll, f.SyncByContent)
go configMapRepl.Run()
enabledReplicators = append(enabledReplicators, configMapRepl)
}
if f.ReplicateRoles {
roleRepl := role.NewReplicator(client, f.ResyncPeriod, f.AllowAll)
go roleRepl.Run()
enabledReplicators = append(enabledReplicators, roleRepl)
}
if f.ReplicateRoleBindings {
roleBindingRepl := rolebinding.NewReplicator(client, f.ResyncPeriod, f.AllowAll)
go roleBindingRepl.Run()
enabledReplicators = append(enabledReplicators, roleBindingRepl)
}
if f.ReplicateServiceAccounts {
serviceAccountRepl := serviceaccount.NewReplicator(client, f.ResyncPeriod, f.AllowAll)
go serviceAccountRepl.Run()
enabledReplicators = append(enabledReplicators, serviceAccountRepl)
}
h := liveness.Handler{
Replicators: enabledReplicators,
}
log.Infof("starting liveness monitor at %s", f.StatusAddr)
http.Handle("/healthz", &h)
http.Handle("/readyz", &h)
err = http.ListenAndServe(f.StatusAddr, nil)
if err != nil {
log.Fatal(err)
}
}