@@ -17,8 +17,10 @@ limitations under the License.
17
17
package main
18
18
19
19
import (
20
+ "crypto/tls"
20
21
"flag"
21
22
"fmt"
23
+ "log"
22
24
"net/url"
23
25
"os"
24
26
"path/filepath"
@@ -31,10 +33,12 @@ import (
31
33
"k8s.io/client-go/metadata"
32
34
_ "k8s.io/client-go/plugin/pkg/client/auth"
33
35
ctrl "sigs.k8s.io/controller-runtime"
36
+ "sigs.k8s.io/controller-runtime/pkg/certwatcher"
34
37
"sigs.k8s.io/controller-runtime/pkg/healthz"
35
38
"sigs.k8s.io/controller-runtime/pkg/log/zap"
36
39
"sigs.k8s.io/controller-runtime/pkg/metrics"
37
40
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
41
+ crwebhook "sigs.k8s.io/controller-runtime/pkg/webhook"
38
42
39
43
"github.com/operator-framework/catalogd/api/core/v1alpha1"
40
44
corecontrollers "github.com/operator-framework/catalogd/internal/controllers/core"
@@ -45,6 +49,7 @@ import (
45
49
"github.com/operator-framework/catalogd/internal/source"
46
50
"github.com/operator-framework/catalogd/internal/storage"
47
51
"github.com/operator-framework/catalogd/internal/version"
52
+ "github.com/operator-framework/catalogd/internal/webhook"
48
53
)
49
54
50
55
var (
@@ -75,6 +80,7 @@ func main() {
75
80
gcInterval time.Duration
76
81
certFile string
77
82
keyFile string
83
+ webhookPort int
78
84
)
79
85
flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metric endpoint binds to." )
80
86
flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
@@ -90,6 +96,7 @@ func main() {
90
96
flag .DurationVar (& gcInterval , "gc-interval" , 12 * time .Hour , "interval in which garbage collection should be run against the catalog content cache" )
91
97
flag .StringVar (& certFile , "tls-cert" , "" , "The certificate file used for serving catalog contents over HTTPS. Requires tls-key." )
92
98
flag .StringVar (& keyFile , "tls-key" , "" , "The key file used for serving catalog contents over HTTPS. Requires tls-cert." )
99
+ flag .IntVar (& webhookPort , "webhook-server-port" , 9443 , "The port that the mutating webhook server serves at." )
93
100
opts := zap.Options {
94
101
Development : true ,
95
102
}
@@ -119,6 +126,23 @@ func main() {
119
126
externalAddr = protocol + externalAddr
120
127
121
128
cfg := ctrl .GetConfigOrDie ()
129
+
130
+ cw , err := certwatcher .New (certFile , keyFile )
131
+ if err != nil {
132
+ log .Fatalf ("Failed to initialize certificate watcher: %v" , err )
133
+ }
134
+
135
+ // Create webhook server and configure TLS
136
+ webhookServer := crwebhook .NewServer (crwebhook.Options {
137
+ Port : webhookPort ,
138
+ TLSOpts : []func (* tls.Config ){
139
+ func (cfg * tls.Config ) {
140
+ cfg .GetCertificate = cw .GetCertificate
141
+ },
142
+ },
143
+ })
144
+
145
+ // Create manager
122
146
mgr , err := ctrl .NewManager (cfg , ctrl.Options {
123
147
Scheme : scheme ,
124
148
Metrics : metricsserver.Options {
@@ -128,9 +152,17 @@ func main() {
128
152
HealthProbeBindAddress : probeAddr ,
129
153
LeaderElection : enableLeaderElection ,
130
154
LeaderElectionID : "catalogd-operator-lock" ,
155
+ WebhookServer : webhookServer ,
131
156
})
132
157
if err != nil {
133
- setupLog .Error (err , "unable to start manager" )
158
+ setupLog .Error (err , "unable to create manager" )
159
+ os .Exit (1 )
160
+ }
161
+
162
+ // Add the certificate watcher to the manager
163
+ err = mgr .Add (cw )
164
+ if err != nil {
165
+ setupLog .Error (err , "unable to add certificate watcher to manager" )
134
166
os .Exit (1 )
135
167
}
136
168
@@ -174,7 +206,7 @@ func main() {
174
206
LocalStorage : localStorage ,
175
207
}
176
208
177
- err = serverutil .AddCatalogServerToManager (mgr , catalogServerConfig )
209
+ err = serverutil .AddCatalogServerToManager (mgr , catalogServerConfig , cw )
178
210
if err != nil {
179
211
setupLog .Error (err , "unable to configure catalog server" )
180
212
os .Exit (1 )
@@ -217,7 +249,13 @@ func main() {
217
249
os .Exit (1 )
218
250
}
219
251
220
- setupLog .Info ("starting manager" )
252
+ // mutating webhook that labels ClusterCatalogs with name label
253
+ if err = (& webhook.ClusterCatalog {}).SetupWebhookWithManager (mgr ); err != nil {
254
+ setupLog .Error (err , "unable to create webhook" , "webhook" , "ClusterCatalog" )
255
+ os .Exit (1 )
256
+ }
257
+
258
+ setupLog .Info ("starting mutating webhook manager" )
221
259
if err := mgr .Start (ctx ); err != nil {
222
260
setupLog .Error (err , "problem running manager" )
223
261
os .Exit (1 )
0 commit comments