Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
151 lines (127 loc) · 5.98 KB
/
Copy pathmain.go
File metadata and controls
151 lines (127 loc) · 5.98 KB
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
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 pull-based replication (replicate-from) of all objects, even if the source does not allow it with the replication-allowed annotations; does not affect push-based replication (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.StringVar(&f.ExcludeNamespaces, "exclude-namespaces", "", "Comma-separated list of regex patterns for namespaces to exclude from replication (a namespace is excluded if any part of its name matches); the replicator never writes to excluded namespaces")
flag.StringVar(&f.PushAllowedSourceNs, "push-allowed-source-namespaces", "", "Comma-separated list of regex patterns (matching the whole name) for namespaces whose objects may be pushed into all other namespaces with the replicate-to/replicate-to-matching annotations. Other namespaces can only push into namespaces that accept them through the "+common.AllowPushFrom+" annotation (CAUTION: anyone who may create objects in a matching namespace can write to every namespace with the replicator's permissions)")
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)
excludePatterns := strings.Split(f.ExcludeNamespaces, ",")
filter, err := common.NewNamespaceFilter(excludePatterns)
if err != nil {
log.WithError(err).Fatal("invalid value for -exclude-namespaces")
}
pushPolicy, err := common.NewPushPolicy(strings.Split(f.PushAllowedSourceNs, ","))
if err != nil {
log.WithError(err).Fatal("invalid value for -push-allowed-source-namespaces")
}
if len(pushPolicy.AllowedSourceNamespaces) == 0 {
log.Infof("no namespace is allowed to push into all namespaces (-push-allowed-source-namespaces); push replication is only performed into namespaces with the %s annotation", common.AllowPushFrom)
} else {
log.Infof("namespaces matching %v may push into all namespaces", pushPolicy.AllowedSourceNamespaces)
}
if f.ReplicateSecrets {
secretRepl := secret.NewReplicator(client, f.ResyncPeriod, f.AllowAll, f.SyncByContent, filter, pushPolicy)
go secretRepl.Run()
enabledReplicators = append(enabledReplicators, secretRepl)
}
if f.ReplicateConfigMaps {
configMapRepl := configmap.NewReplicator(client, f.ResyncPeriod, f.AllowAll, f.SyncByContent, filter, pushPolicy)
go configMapRepl.Run()
enabledReplicators = append(enabledReplicators, configMapRepl)
}
if f.ReplicateRoles {
roleRepl := role.NewReplicator(client, f.ResyncPeriod, f.AllowAll, filter, pushPolicy)
go roleRepl.Run()
enabledReplicators = append(enabledReplicators, roleRepl)
}
if f.ReplicateRoleBindings {
roleBindingRepl := rolebinding.NewReplicator(client, f.ResyncPeriod, f.AllowAll, filter, pushPolicy)
go roleBindingRepl.Run()
enabledReplicators = append(enabledReplicators, roleBindingRepl)
}
if f.ReplicateServiceAccounts {
serviceAccountRepl := serviceaccount.NewReplicator(client, f.ResyncPeriod, f.AllowAll, filter, pushPolicy)
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)
}
}