-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathmain.go
170 lines (146 loc) · 5.36 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
package main
import (
"flag"
"fmt"
"os"
"github.com/open-policy-agent/cert-controller/pkg/rotator"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"github.com/Azure/azure-workload-identity/pkg/logger"
"github.com/Azure/azure-workload-identity/pkg/metrics"
"github.com/Azure/azure-workload-identity/pkg/util"
"github.com/Azure/azure-workload-identity/pkg/version"
wh "github.com/Azure/azure-workload-identity/pkg/webhook"
)
var webhooks = []rotator.WebhookInfo{
{
Name: "azure-wi-webhook-mutating-webhook-configuration",
Type: rotator.Mutating,
},
}
const (
secretName = "azure-wi-webhook-server-cert" // #nosec
serviceName = "azure-wi-webhook-webhook-service"
caName = "azure-workload-identity-ca"
caOrganization = "azure-workload-identity"
)
var (
arcCluster bool
audience string
webhookCertDir string
tlsMinVersion string
healthAddr string
metricsAddr string
disableCertRotation bool
metricsBackend string
// DNSName is <service name>.<namespace>.svc
dnsName = fmt.Sprintf("%s.%s.svc", serviceName, util.GetNamespace())
scheme = runtime.NewScheme()
entryLog = log.Log.WithName("entrypoint")
)
func init() {
klog.InitFlags(nil)
_ = clientgoscheme.AddToScheme(scheme)
}
func main() {
logger := logger.New()
logger.AddFlags()
// TODO (aramase) once webhook is added as an arc extension, use extension
// util to check if running in arc cluster.
flag.BoolVar(&arcCluster, "arc-cluster", false, "Running on arc cluster")
flag.StringVar(&audience, "audience", "", "Audience for service account token")
flag.StringVar(&webhookCertDir, "webhook-cert-dir", "/certs", "Webhook certificates dir to use. Defaults to /certs")
flag.BoolVar(&disableCertRotation, "disable-cert-rotation", false, "disable automatic generation and rotation of webhook TLS certificates/keys")
flag.StringVar(&tlsMinVersion, "tls-min-version", "1.3", "Minimum TLS version")
flag.StringVar(&healthAddr, "health-addr", ":9440", "The address the health endpoint binds to")
flag.StringVar(&metricsAddr, "metrics-addr", ":8095", "The address the metrics endpoint binds to")
flag.StringVar(&metricsBackend, "metrics-backend", "prometheus", "Backend used for metrics")
flag.Parse()
log.SetLogger(logger.Get())
config := ctrl.GetConfigOrDie()
config.UserAgent = version.GetUserAgent("webhook")
// initialize metrics exporter before creating measurements
entryLog.Info("initializing metrics backend", "backend", metricsBackend)
if err := metrics.InitMetricsExporter(metricsBackend); err != nil {
klog.ErrorS(err, "failed to initialize metrics exporter")
os.Exit(1)
}
// log the user agent as it makes it easier to debug issues
entryLog.Info("setting up manager", "userAgent", config.UserAgent)
mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
LeaderElection: false,
HealthProbeBindAddress: healthAddr,
MetricsBindAddress: metricsAddr,
CertDir: webhookCertDir,
MapperProvider: func(c *rest.Config) (meta.RESTMapper, error) {
return apiutil.NewDynamicRESTMapper(c)
},
})
if err != nil {
entryLog.Error(err, "unable to set up controller manager")
os.Exit(1)
}
// Make sure certs are generated and valid if cert rotation is enabled.
setupFinished := make(chan struct{})
if !disableCertRotation {
entryLog.Info("setting up cert rotation")
if err := rotator.AddRotator(mgr, &rotator.CertRotator{
SecretKey: types.NamespacedName{
Namespace: util.GetNamespace(),
Name: secretName,
},
CertDir: webhookCertDir,
CAName: caName,
CAOrganization: caOrganization,
DNSName: dnsName,
IsReady: setupFinished,
Webhooks: webhooks,
}); err != nil {
entryLog.Error(err, "unable to set up cert rotation")
os.Exit(1)
}
} else {
close(setupFinished)
}
if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
entryLog.Error(err, "unable to create ready check")
os.Exit(1)
}
if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
entryLog.Error(err, "unable to create health check")
os.Exit(1)
}
go setupWebhook(mgr, setupFinished)
entryLog.Info("starting manager")
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
entryLog.Error(err, "unable to run manager")
os.Exit(1)
}
}
func setupWebhook(mgr manager.Manager, setupFinished chan struct{}) {
// Block until the setup (certificate generation) finishes.
<-setupFinished
// setup webhooks
entryLog.Info("setting up webhook server")
hookServer := mgr.GetWebhookServer()
hookServer.TLSMinVersion = tlsMinVersion
entryLog.Info("registering webhook to the webhook server")
podMutator, err := wh.NewPodMutator(mgr.GetClient(), mgr.GetAPIReader(), arcCluster, audience)
if err != nil {
entryLog.Error(err, "unable to set up pod mutator")
os.Exit(1)
}
hookServer.Register("/mutate-v1-pod", &webhook.Admission{Handler: podMutator})
}