Skip to content

Commit e699f04

Browse files
authored
Merge pull request #4931 from Danil-Grigorev/decouple-kubeadm
🌱 Decouple MachinePool reconciler from Kubeadm
2 parents da763bd + c1472a8 commit e699f04

File tree

5 files changed

+41
-29
lines changed

5 files changed

+41
-29
lines changed

exp/controllers/azuremachinepool_controller.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ package controllers
1818

1919
import (
2020
"context"
21+
"reflect"
2122
"time"
2223

2324
"github.com/pkg/errors"
2425
corev1 "k8s.io/api/core/v1"
2526
apierrors "k8s.io/apimachinery/pkg/api/errors"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2628
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
2730
kerrors "k8s.io/apimachinery/pkg/util/errors"
2831
"k8s.io/client-go/tools/record"
2932
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
@@ -60,6 +63,7 @@ type (
6063
Timeouts reconciler.Timeouts
6164
WatchFilterValue string
6265
createAzureMachinePoolService azureMachinePoolServiceCreator
66+
BootstrapConfigGVK schema.GroupVersionKind
6367
}
6468

6569
// annotationReaderWriter provides an interface to read and write annotations.
@@ -72,12 +76,20 @@ type (
7276
type azureMachinePoolServiceCreator func(machinePoolScope *scope.MachinePoolScope) (*azureMachinePoolService, error)
7377

7478
// NewAzureMachinePoolReconciler returns a new AzureMachinePoolReconciler instance.
75-
func NewAzureMachinePoolReconciler(client client.Client, recorder record.EventRecorder, timeouts reconciler.Timeouts, watchFilterValue string) *AzureMachinePoolReconciler {
79+
func NewAzureMachinePoolReconciler(client client.Client, recorder record.EventRecorder, timeouts reconciler.Timeouts, watchFilterValue, bootstrapConfigGVK string) *AzureMachinePoolReconciler {
80+
gvk := schema.FromAPIVersionAndKind(kubeadmv1.GroupVersion.String(), reflect.TypeOf((*kubeadmv1.KubeadmConfig)(nil)).Elem().Name())
81+
userGVK, _ := schema.ParseKindArg(bootstrapConfigGVK)
82+
83+
if userGVK != nil {
84+
gvk = *userGVK
85+
}
86+
7687
ampr := &AzureMachinePoolReconciler{
77-
Client: client,
78-
Recorder: recorder,
79-
Timeouts: timeouts,
80-
WatchFilterValue: watchFilterValue,
88+
Client: client,
89+
Recorder: recorder,
90+
Timeouts: timeouts,
91+
WatchFilterValue: watchFilterValue,
92+
BootstrapConfigGVK: gvk,
8193
}
8294

8395
ampr.createAzureMachinePoolService = newAzureMachinePoolService
@@ -108,6 +120,8 @@ func (ampr *AzureMachinePoolReconciler) SetupWithManager(ctx context.Context, mg
108120
return errors.Wrapf(err, "failed to create AzureManagedCluster to AzureMachinePools mapper")
109121
}
110122

123+
config := &metav1.PartialObjectMetadata{}
124+
config.SetGroupVersionKind(ampr.BootstrapConfigGVK)
111125
c, err := ctrl.NewControllerManagedBy(mgr).
112126
WithOptions(options.Options).
113127
For(&infrav1exp.AzureMachinePool{}).
@@ -127,10 +141,10 @@ func (ampr *AzureMachinePoolReconciler) SetupWithManager(ctx context.Context, mg
127141
&infrav1.AzureManagedControlPlane{},
128142
handler.EnqueueRequestsFromMapFunc(azureManagedControlPlaneMapper),
129143
).
130-
// watch for changes in KubeadmConfig to sync bootstrap token
144+
// watch for changes in KubeadmConfig (or any BootstrapConfig) to sync bootstrap token
131145
Watches(
132-
&kubeadmv1.KubeadmConfig{},
133-
handler.EnqueueRequestsFromMapFunc(KubeadmConfigToInfrastructureMapFunc(ctx, ampr.Client, log)),
146+
config,
147+
handler.EnqueueRequestsFromMapFunc(BootstrapConfigToInfrastructureMapFunc(ctx, ampr.Client, log)),
134148
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
135149
).
136150
Build(r)

exp/controllers/azuremachinepool_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var _ = Describe("AzureMachinePoolReconciler", func() {
4444
Context("Reconcile an AzureMachinePool", func() {
4545
It("should not error with minimal set up", func() {
4646
reconciler := NewAzureMachinePoolReconciler(testEnv, testEnv.GetEventRecorderFor("azuremachinepool-reconciler"),
47-
reconciler.Timeouts{}, "")
47+
reconciler.Timeouts{}, "", "")
4848
By("Calling reconcile")
4949
instance := &infrav1exp.AzureMachinePool{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}}
5050
result, err := reconciler.Reconcile(context.Background(), ctrl.Request{
@@ -79,7 +79,7 @@ func TestAzureMachinePoolReconcilePaused(t *testing.T) {
7979

8080
recorder := record.NewFakeRecorder(1)
8181

82-
reconciler := NewAzureMachinePoolReconciler(c, recorder, reconciler.Timeouts{}, "")
82+
reconciler := NewAzureMachinePoolReconciler(c, recorder, reconciler.Timeouts{}, "", "")
8383
name := test.RandomName("paused", 10)
8484
namespace := "default"
8585

exp/controllers/helpers.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
3333
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
3434
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
35-
kubeadmv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
3635
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
3736
"sigs.k8s.io/cluster-api/util"
3837
ctrl "sigs.k8s.io/controller-runtime"
@@ -372,28 +371,22 @@ func MachinePoolMachineHasStateOrVersionChange(logger logr.Logger) predicate.Fun
372371
}
373372
}
374373

375-
// KubeadmConfigToInfrastructureMapFunc returns a handler.ToRequestsFunc that watches for KubeadmConfig events and returns.
376-
func KubeadmConfigToInfrastructureMapFunc(ctx context.Context, c client.Client, log logr.Logger) handler.MapFunc {
374+
// BootstrapConfigToInfrastructureMapFunc returns a handler.ToRequestsFunc that watches for <Bootstrap>Config events and returns.
375+
func BootstrapConfigToInfrastructureMapFunc(ctx context.Context, c client.Client, log logr.Logger) handler.MapFunc {
377376
return func(ctx context.Context, o client.Object) []reconcile.Request {
378377
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultMappingTimeout)
379378
defer cancel()
380379

381-
kc, ok := o.(*kubeadmv1.KubeadmConfig)
382-
if !ok {
383-
log.V(4).Info("attempt to map incorrect type", "type", fmt.Sprintf("%T", o))
384-
return nil
385-
}
386-
387380
mpKey := client.ObjectKey{
388-
Namespace: kc.Namespace,
389-
Name: kc.Name,
381+
Namespace: o.GetNamespace(),
382+
Name: o.GetName(),
390383
}
391384

392385
// fetch MachinePool to get reference
393386
mp := &expv1.MachinePool{}
394387
if err := c.Get(ctx, mpKey, mp); err != nil {
395388
if !apierrors.IsNotFound(err) {
396-
log.Error(err, "failed to fetch MachinePool for KubeadmConfig")
389+
log.Error(err, "failed to fetch MachinePool to validate Bootstrap.ConfigRef")
397390
}
398391
return []reconcile.Request{}
399392
}
@@ -404,8 +397,8 @@ func KubeadmConfigToInfrastructureMapFunc(ctx context.Context, c client.Client,
404397
return []reconcile.Request{}
405398
}
406399
sameKind := ref.Kind != o.GetObjectKind().GroupVersionKind().Kind
407-
sameName := ref.Name == kc.Name
408-
sameNamespace := ref.Namespace == kc.Namespace
400+
sameName := ref.Name == o.GetName()
401+
sameNamespace := ref.Namespace == o.GetNamespace()
409402
if !sameKind || !sameName || !sameNamespace {
410403
log.V(4).Info("Bootstrap.ConfigRef does not match",
411404
"sameKind", sameKind,
@@ -417,10 +410,7 @@ func KubeadmConfigToInfrastructureMapFunc(ctx context.Context, c client.Client,
417410
return []reconcile.Request{}
418411
}
419412

420-
key := client.ObjectKey{
421-
Namespace: kc.Namespace,
422-
Name: kc.Name,
423-
}
413+
key := client.ObjectKeyFromObject(o)
424414
log.V(4).Info("adding KubeadmConfig to watch", "key", key)
425415

426416
return []reconcile.Request{

exp/controllers/suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var _ = BeforeSuite(func() {
5454
ctx = log.IntoContext(ctx, logr.New(testEnv.Log))
5555

5656
Expect(NewAzureMachinePoolReconciler(testEnv, testEnv.GetEventRecorderFor("azuremachinepool-reconciler"),
57-
reconciler.Timeouts{}, "").SetupWithManager(ctx, testEnv.Manager, controllers.Options{Options: controller.Options{MaxConcurrentReconciles: 1}})).To(Succeed())
57+
reconciler.Timeouts{}, "", "").SetupWithManager(ctx, testEnv.Manager, controllers.Options{Options: controller.Options{MaxConcurrentReconciles: 1}})).To(Succeed())
5858

5959
Expect(NewAzureMachinePoolMachineController(testEnv, testEnv.GetEventRecorderFor("azuremachinepoolmachine-reconciler"),
6060
reconciler.Timeouts{}, "").SetupWithManager(ctx, testEnv.Manager, controllers.Options{Options: controller.Options{MaxConcurrentReconciles: 1}})).To(Succeed())

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ var (
105105
azureMachineConcurrency int
106106
azureMachinePoolConcurrency int
107107
azureMachinePoolMachineConcurrency int
108+
azureBootrapConfigGVK string
108109
debouncingTimer time.Duration
109110
syncPeriod time.Duration
110111
healthAddr string
@@ -253,6 +254,12 @@ func InitFlags(fs *pflag.FlagSet) {
253254
"Enable tracing to the opentelemetry-collector service in the same namespace.",
254255
)
255256

257+
fs.StringVar(&azureBootrapConfigGVK,
258+
"bootstrap-config-gvk",
259+
"",
260+
"Provide fully qualified GVK string to override default kubeadm config watch source, in the form of Kind.version.group (default: KubeadmConfig.v1beta1.bootstrap.cluster.x-k8s.io)",
261+
)
262+
256263
flags.AddDiagnosticsOptions(fs, &diagnosticsOptions)
257264

258265
feature.MutableGates.AddFlag(fs)
@@ -426,6 +433,7 @@ func registerControllers(ctx context.Context, mgr manager.Manager) {
426433
mgr.GetEventRecorderFor("azuremachinepool-reconciler"),
427434
timeouts,
428435
watchFilterValue,
436+
azureBootrapConfigGVK,
429437
).SetupWithManager(ctx, mgr, controllers.Options{Options: controller.Options{MaxConcurrentReconciles: azureMachinePoolConcurrency}, Cache: mpCache}); err != nil {
430438
setupLog.Error(err, "unable to create controller", "controller", "AzureMachinePool")
431439
os.Exit(1)

0 commit comments

Comments
 (0)