Skip to content

Commit c38755d

Browse files
committed
Adjust main.go files
Signed-off-by: Stefan Büringer [email protected]
1 parent ea0dc4d commit c38755d

File tree

6 files changed

+218
-130
lines changed

6 files changed

+218
-130
lines changed

bootstrap/kubeadm/main.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ import (
4040
_ "k8s.io/component-base/logs/json/register"
4141
"k8s.io/klog/v2"
4242
ctrl "sigs.k8s.io/controller-runtime"
43+
"sigs.k8s.io/controller-runtime/pkg/cache"
4344
"sigs.k8s.io/controller-runtime/pkg/client"
4445
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
46+
"sigs.k8s.io/controller-runtime/pkg/config"
4547
"sigs.k8s.io/controller-runtime/pkg/controller"
48+
"sigs.k8s.io/controller-runtime/pkg/webhook"
4649

4750
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4851
bootstrapv1alpha3 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
@@ -164,15 +167,6 @@ func main() {
164167

165168
// klog.Background will automatically use the right logger.
166169
ctrl.SetLogger(klog.Background())
167-
if profilerAddress != "" {
168-
setupLog.Info(fmt.Sprintf("Profiler listening for requests at %s", profilerAddress))
169-
go func() {
170-
srv := http.Server{Addr: profilerAddress, ReadHeaderTimeout: 2 * time.Second}
171-
if err := srv.ListenAndServe(); err != nil {
172-
setupLog.Error(err, "problem running profiler server")
173-
}
174-
}()
175-
}
176170

177171
restConfig := ctrl.GetConfigOrDie()
178172
restConfig.QPS = restConfigQPS
@@ -185,6 +179,11 @@ func main() {
185179
os.Exit(1)
186180
}
187181

182+
var watchNamespaces []string
183+
if watchNamespace != "" {
184+
watchNamespaces = []string{watchNamespace}
185+
}
186+
188187
ctrlOptions := ctrl.Options{
189188
Scheme: scheme,
190189
MetricsBindAddress: metricsBindAddr,
@@ -194,21 +193,30 @@ func main() {
194193
RenewDeadline: &leaderElectionRenewDeadline,
195194
RetryPeriod: &leaderElectionRetryPeriod,
196195
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
197-
Namespace: watchNamespace,
198-
SyncPeriod: &syncPeriod,
199-
ClientDisableCacheFor: []client.Object{
200-
&corev1.ConfigMap{},
201-
&corev1.Secret{},
196+
HealthProbeBindAddress: healthAddr,
197+
PprofBindAddress: profilerAddress,
198+
Cache: cache.Options{
199+
Namespaces: watchNamespaces,
200+
SyncPeriod: &syncPeriod,
201+
},
202+
Client: client.Options{
203+
Cache: &client.CacheOptions{
204+
DisableFor: []client.Object{
205+
&corev1.ConfigMap{},
206+
&corev1.Secret{},
207+
},
208+
},
209+
},
210+
WebhookServer: &webhook.Server{
211+
Port: webhookPort,
212+
CertDir: webhookCertDir,
213+
TLSOpts: tlsOptionOverrides,
202214
},
203-
Port: webhookPort,
204-
HealthProbeBindAddress: healthAddr,
205-
CertDir: webhookCertDir,
206-
TLSOpts: tlsOptionOverrides,
207215
}
208216

209217
if feature.Gates.Enabled(feature.LazyRestmapper) {
210-
ctrlOptions.MapperProvider = func(c *rest.Config) (meta.RESTMapper, error) {
211-
return apiutil.NewDynamicRESTMapper(c, apiutil.WithExperimentalLazyMapper)
218+
ctrlOptions.MapperProvider = func(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) {
219+
return apiutil.NewDynamicRESTMapper(c, httpClient, apiutil.WithExperimentalLazyMapper)
212220
}
213221
}
214222

@@ -268,5 +276,5 @@ func setupWebhooks(mgr ctrl.Manager) {
268276
}
269277

270278
func concurrency(c int) controller.Options {
271-
return controller.Options{MaxConcurrentReconciles: c}
279+
return controller.Options{Controller: config.Controller{MaxConcurrentReconciles: c}}
272280
}

controlplane/kubeadm/main.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ import (
4242
_ "k8s.io/component-base/logs/json/register"
4343
"k8s.io/klog/v2"
4444
ctrl "sigs.k8s.io/controller-runtime"
45+
"sigs.k8s.io/controller-runtime/pkg/cache"
4546
"sigs.k8s.io/controller-runtime/pkg/client"
4647
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
48+
"sigs.k8s.io/controller-runtime/pkg/config"
4749
"sigs.k8s.io/controller-runtime/pkg/controller"
50+
"sigs.k8s.io/controller-runtime/pkg/webhook"
4851

4952
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
5053
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
@@ -173,16 +176,6 @@ func main() {
173176
// klog.Background will automatically use the right logger.
174177
ctrl.SetLogger(klog.Background())
175178

176-
if profilerAddress != "" {
177-
setupLog.Info(fmt.Sprintf("Profiler listening for requests at %s", profilerAddress))
178-
go func() {
179-
srv := http.Server{Addr: profilerAddress, ReadHeaderTimeout: 2 * time.Second}
180-
if err := srv.ListenAndServe(); err != nil {
181-
setupLog.Error(err, "problem running profiler server")
182-
}
183-
}()
184-
}
185-
186179
restConfig := ctrl.GetConfigOrDie()
187180
restConfig.QPS = restConfigQPS
188181
restConfig.Burst = restConfigBurst
@@ -194,6 +187,11 @@ func main() {
194187
os.Exit(1)
195188
}
196189

190+
var watchNamespaces []string
191+
if watchNamespace != "" {
192+
watchNamespaces = []string{watchNamespace}
193+
}
194+
197195
ctrlOptions := ctrl.Options{
198196
Scheme: scheme,
199197
MetricsBindAddress: metricsBindAddr,
@@ -203,21 +201,30 @@ func main() {
203201
RenewDeadline: &leaderElectionRenewDeadline,
204202
RetryPeriod: &leaderElectionRetryPeriod,
205203
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
206-
Namespace: watchNamespace,
207-
SyncPeriod: &syncPeriod,
208-
ClientDisableCacheFor: []client.Object{
209-
&corev1.ConfigMap{},
210-
&corev1.Secret{},
204+
HealthProbeBindAddress: healthAddr,
205+
PprofBindAddress: profilerAddress,
206+
Cache: cache.Options{
207+
Namespaces: watchNamespaces,
208+
SyncPeriod: &syncPeriod,
209+
},
210+
Client: client.Options{
211+
Cache: &client.CacheOptions{
212+
DisableFor: []client.Object{
213+
&corev1.ConfigMap{},
214+
&corev1.Secret{},
215+
},
216+
},
217+
},
218+
WebhookServer: &webhook.Server{
219+
Port: webhookPort,
220+
CertDir: webhookCertDir,
221+
TLSOpts: tlsOptionOverrides,
211222
},
212-
Port: webhookPort,
213-
HealthProbeBindAddress: healthAddr,
214-
CertDir: webhookCertDir,
215-
TLSOpts: tlsOptionOverrides,
216223
}
217224

218225
if feature.Gates.Enabled(feature.LazyRestmapper) {
219-
ctrlOptions.MapperProvider = func(c *rest.Config) (meta.RESTMapper, error) {
220-
return apiutil.NewDynamicRESTMapper(c, apiutil.WithExperimentalLazyMapper)
226+
ctrlOptions.MapperProvider = func(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) {
227+
return apiutil.NewDynamicRESTMapper(c, httpClient, apiutil.WithExperimentalLazyMapper)
221228
}
222229
}
223230

@@ -315,5 +322,5 @@ func setupWebhooks(mgr ctrl.Manager) {
315322
}
316323

317324
func concurrency(c int) controller.Options {
318-
return controller.Options{MaxConcurrentReconciles: c}
325+
return controller.Options{Controller: config.Controller{MaxConcurrentReconciles: c}}
319326
}

internal/test/envtest/environment.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"net"
23+
"net/http"
2324
"os"
2425
"path"
2526
"path/filepath"
@@ -49,6 +50,7 @@ import (
4950
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
5051
"sigs.k8s.io/controller-runtime/pkg/envtest"
5152
"sigs.k8s.io/controller-runtime/pkg/manager"
53+
"sigs.k8s.io/controller-runtime/pkg/webhook"
5254

5355
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
5456
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
@@ -251,14 +253,20 @@ func newEnvironment(uncachedObjs ...client.Object) *Environment {
251253
}
252254

253255
options := manager.Options{
254-
Scheme: scheme.Scheme,
255-
MetricsBindAddress: "0",
256-
CertDir: env.WebhookInstallOptions.LocalServingCertDir,
257-
Port: env.WebhookInstallOptions.LocalServingPort,
258-
ClientDisableCacheFor: objs,
259-
Host: host,
260-
MapperProvider: func(c *rest.Config) (meta.RESTMapper, error) {
261-
return apiutil.NewDynamicRESTMapper(c, apiutil.WithExperimentalLazyMapper)
256+
Scheme: scheme.Scheme,
257+
MetricsBindAddress: "0",
258+
Client: client.Options{
259+
Cache: &client.CacheOptions{
260+
DisableFor: objs,
261+
},
262+
},
263+
WebhookServer: &webhook.Server{
264+
Port: env.WebhookInstallOptions.LocalServingPort,
265+
CertDir: env.WebhookInstallOptions.LocalServingCertDir,
266+
Host: host,
267+
},
268+
MapperProvider: func(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) {
269+
return apiutil.NewDynamicRESTMapper(c, httpClient, apiutil.WithExperimentalLazyMapper)
262270
},
263271
}
264272

main.go

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"flag"
2323
"fmt"
2424
"net/http"
25-
_ "net/http/pprof"
2625
"os"
2726
"time"
2827

@@ -41,9 +40,12 @@ import (
4140
_ "k8s.io/component-base/logs/json/register"
4241
"k8s.io/klog/v2"
4342
ctrl "sigs.k8s.io/controller-runtime"
43+
"sigs.k8s.io/controller-runtime/pkg/cache"
4444
"sigs.k8s.io/controller-runtime/pkg/client"
4545
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
46+
"sigs.k8s.io/controller-runtime/pkg/config"
4647
"sigs.k8s.io/controller-runtime/pkg/controller"
48+
"sigs.k8s.io/controller-runtime/pkg/webhook"
4749

4850
clusterv1alpha3 "sigs.k8s.io/cluster-api/api/v1alpha3"
4951
clusterv1alpha4 "sigs.k8s.io/cluster-api/api/v1alpha4"
@@ -228,16 +230,6 @@ func main() {
228230
// klog.Background will automatically use the right logger.
229231
ctrl.SetLogger(klog.Background())
230232

231-
if profilerAddress != "" {
232-
setupLog.Info(fmt.Sprintf("Profiler listening for requests at %s", profilerAddress))
233-
go func() {
234-
srv := http.Server{Addr: profilerAddress, ReadHeaderTimeout: 2 * time.Second}
235-
if err := srv.ListenAndServe(); err != nil {
236-
setupLog.Error(err, "problem running profiler server")
237-
}
238-
}()
239-
}
240-
241233
restConfig := ctrl.GetConfigOrDie()
242234
restConfig.QPS = restConfigQPS
243235
restConfig.Burst = restConfigBurst
@@ -259,6 +251,11 @@ func main() {
259251
os.Exit(1)
260252
}
261253

254+
var watchNamespaces []string
255+
if watchNamespace != "" {
256+
watchNamespaces = []string{watchNamespace}
257+
}
258+
262259
ctrlOptions := ctrl.Options{
263260
Scheme: scheme,
264261
MetricsBindAddress: metricsBindAddr,
@@ -268,21 +265,30 @@ func main() {
268265
RenewDeadline: &leaderElectionRenewDeadline,
269266
RetryPeriod: &leaderElectionRetryPeriod,
270267
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
271-
Namespace: watchNamespace,
272-
SyncPeriod: &syncPeriod,
273-
ClientDisableCacheFor: []client.Object{
274-
&corev1.ConfigMap{},
275-
&corev1.Secret{},
268+
HealthProbeBindAddress: healthAddr,
269+
PprofBindAddress: profilerAddress,
270+
Cache: cache.Options{
271+
Namespaces: watchNamespaces,
272+
SyncPeriod: &syncPeriod,
273+
},
274+
Client: client.Options{
275+
Cache: &client.CacheOptions{
276+
DisableFor: []client.Object{
277+
&corev1.ConfigMap{},
278+
&corev1.Secret{},
279+
},
280+
},
281+
},
282+
WebhookServer: &webhook.Server{
283+
Port: webhookPort,
284+
CertDir: webhookCertDir,
285+
TLSOpts: tlsOptionOverrides,
276286
},
277-
Port: webhookPort,
278-
CertDir: webhookCertDir,
279-
HealthProbeBindAddress: healthAddr,
280-
TLSOpts: tlsOptionOverrides,
281287
}
282288

283289
if feature.Gates.Enabled(feature.LazyRestmapper) {
284-
ctrlOptions.MapperProvider = func(c *rest.Config) (meta.RESTMapper, error) {
285-
return apiutil.NewDynamicRESTMapper(c, apiutil.WithExperimentalLazyMapper)
290+
ctrlOptions.MapperProvider = func(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) {
291+
return apiutil.NewDynamicRESTMapper(c, httpClient, apiutil.WithExperimentalLazyMapper)
286292
}
287293
}
288294

@@ -362,16 +368,12 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
362368
}
363369

364370
if feature.Gates.Enabled(feature.ClusterTopology) {
365-
unstructuredCachingClient, err := client.NewDelegatingClient(
366-
client.NewDelegatingClientInput{
367-
// Use the default client for write operations.
368-
Client: mgr.GetClient(),
369-
// For read operations, use the same cache used by all the controllers but ensure
370-
// unstructured objects will be also cached (this does not happen with the default client).
371-
CacheReader: mgr.GetCache(),
372-
CacheUnstructured: true,
371+
unstructuredCachingClient, err := client.New(mgr.GetConfig(), client.Options{
372+
Cache: &client.CacheOptions{
373+
Reader: mgr.GetCache(),
374+
Unstructured: true,
373375
},
374-
)
376+
})
375377
if err != nil {
376378
setupLog.Error(err, "unable to create unstructured caching client", "controller", "ClusterTopology")
377379
os.Exit(1)
@@ -581,5 +583,5 @@ func setupWebhooks(mgr ctrl.Manager) {
581583
}
582584

583585
func concurrency(c int) controller.Options {
584-
return controller.Options{MaxConcurrentReconciles: c}
586+
return controller.Options{Controller: config.Controller{MaxConcurrentReconciles: c}}
585587
}

test/extension/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func main() {
123123
ctrl.SetLogger(klog.Background())
124124

125125
// Initialize the golang profiler server, if required.
126+
// TODO(sbueringer): remove this once we switched to using a manager.
126127
if profilerAddress != "" {
127128
setupLog.Info(fmt.Sprintf("Profiler listening for requests at %s", profilerAddress))
128129
go func() {

0 commit comments

Comments
 (0)