Skip to content

Commit

Permalink
Refactor on Alvaro's PR
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed Apr 19, 2024
1 parent 5117265 commit 414d92d
Show file tree
Hide file tree
Showing 35 changed files with 185 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,29 @@ func (r *KubeadmConfigReconciler) SetupWithManager(ctx context.Context, mgr ctrl
}

b := ctrl.NewControllerManagedBy(mgr).
Named("kubeadmConfig").
Add(builder.For(mgr,
&bootstrapv1.KubeadmConfig{},
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &bootstrapv1.KubeadmConfig{}),
)).
WithOptions(options).
Add(builder.Watches(mgr,
&clusterv1.Machine{},
handler.EnqueueRequestsFromObjectMap(r.MachineToBootstrapMapFunc),
handler.EnqueueRequestsFromTypedMapFunc(r.MachineToBootstrapMapFunc),
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &clusterv1.Machine{}),
))

if feature.Gates.Enabled(feature.MachinePool) {
b = b.Add(builder.Watches(mgr,
&expv1.MachinePool{},
handler.EnqueueRequestsFromObjectMap(r.MachinePoolToBootstrapMapFunc),
handler.EnqueueRequestsFromTypedMapFunc(r.MachinePoolToBootstrapMapFunc),
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &expv1.MachinePool{}),
))
}

b = b.Add(builder.Watches(mgr,
&clusterv1.Cluster{},
handler.EnqueueRequestsFromObjectMap(r.ClusterToKubeadmConfigs),
handler.EnqueueRequestsFromTypedMapFunc(r.ClusterToKubeadmConfigs),
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &clusterv1.Cluster{}),
predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)),
))
Expand Down
6 changes: 1 addition & 5 deletions controllers/external/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/go-logr/logr"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -54,12 +53,9 @@ func (o *ObjectTracker) Watch(log logr.Logger, obj client.Object, handler handle
return nil
}

u := &unstructured.Unstructured{}
u.SetGroupVersionKind(gvk)

log.Info(fmt.Sprintf("Adding watch on external object %q", gvk.String()))
err := o.Controller.Watch(
source.Kind(o.Cache, u).Prepare(handler, append(p, predicates.ResourceNotPaused(log, obj))...),
source.Kind(o.Cache, obj, handler, append(p, predicates.ResourceNotPaused(log, obj))...),
)
if err != nil {
o.m.Delete(key)
Expand Down
4 changes: 2 additions & 2 deletions controllers/remote/cluster_cache_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func (t *ClusterCacheTracker) deleteAccessor(_ context.Context, cluster client.O
// Watcher is a scoped-down interface from Controller that only knows how to watch.
type Watcher interface {
// Watch watches src for changes, sending events to eventHandler if they pass predicates.
Watch(src source.Source, eventHandler handler.EventHandler, predicates ...predicate.Predicate) error
Watch(src source.Source) error
}

// WatchInput specifies the parameters used to establish a new watch for a remote cluster.
Expand Down Expand Up @@ -585,7 +585,7 @@ func (t *ClusterCacheTracker) Watch(ctx context.Context, input WatchInput) error
}

// Need to create the watch
if err := input.Watcher.Watch(source.Kind(accessor.cache, input.Kind), input.EventHandler, input.Predicates...); err != nil {
if err := input.Watcher.Watch(source.Kind(accessor.cache, input.Kind, input.EventHandler, input.Predicates...)); err != nil {
return errors.Wrapf(err, "failed to add %s watch on cluster %s: failed to create watch", input.Kind, klog.KRef(input.Cluster.Namespace, input.Cluster.Name))
}

Expand Down
3 changes: 1 addition & 2 deletions controllers/remote/cluster_cache_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
Expand Down Expand Up @@ -88,7 +87,7 @@ func TestClusterCacheTracker(t *testing.T) {

watch, err := ctrl.NewControllerManagedBy(mgr).For(&clusterv1.MachineDeployment{}).Build(c)
g.Expect(err).ToNot(HaveOccurred())
w = &controller.ControllerAdapter{Controller: watch}
w = watch

mgrContext, mgrCancel = context.WithCancel(ctx)
t.Log("Starting the manager")
Expand Down
3 changes: 2 additions & 1 deletion controlplane/kubeadm/internal/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type KubeadmControlPlaneReconciler struct {

func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
c, err := ctrl.NewControllerManagedBy(mgr).
Named("kubeadmControlPlane").
Add(builder.For(mgr,
&controlplanev1.KubeadmControlPlane{},
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &controlplanev1.KubeadmControlPlane{}),
Expand All @@ -103,7 +104,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
WithOptions(options).
Add(builder.Watches(mgr,
&clusterv1.Cluster{},
handler.EnqueueRequestsFromObjectMap(r.ClusterToKubeadmControlPlane),
handler.EnqueueRequestsFromTypedMapFunc(r.ClusterToKubeadmControlPlane),
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &clusterv1.Cluster{}),
predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx)),
)).Build(r)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ type ClusterResourceSetReconciler struct {

func (r *ClusterResourceSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
err := ctrl.NewControllerManagedBy(mgr).
Named("clusterResourceSet").
Add(builder.For(mgr, &addonsv1.ClusterResourceSet{},
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &addonsv1.ClusterResourceSet{}),
)).
Add(builder.Watches(mgr,
&clusterv1.Cluster{},
handler.EnqueueRequestsFromObjectMap(r.clusterToClusterResourceSet),
handler.EnqueueRequestsFromTypedMapFunc(r.clusterToClusterResourceSet),
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &clusterv1.Cluster{}),
)).
WatchesMetadata(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ type ClusterResourceSetBindingReconciler struct {

func (r *ClusterResourceSetBindingReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
err := ctrl.NewControllerManagedBy(mgr).
Named("clusterResourceSetBinding").
For(&addonsv1.ClusterResourceSetBinding{}).
Add(builder.Watches(mgr,
&clusterv1.Cluster{},
handler.EnqueueRequestsFromObjectMap(r.clusterToClusterResourceSetBinding),
handler.EnqueueRequestsFromTypedMapFunc(r.clusterToClusterResourceSetBinding),
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &clusterv1.Cluster{}),
)).
WithOptions(options).
Expand Down
15 changes: 11 additions & 4 deletions exp/internal/controllers/machinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"fmt"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -87,13 +88,14 @@ func (r *MachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M
}

c, err := ctrl.NewControllerManagedBy(mgr).
Named("machinepool").
Add(builder.For(mgr,
&expv1.MachinePool{},
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &expv1.MachinePool{}))).
WithOptions(options).
Add(builder.Watches(mgr,
&clusterv1.Cluster{},
handler.EnqueueRequestsFromObjectMap(clusterToMachinePools),
handler.EnqueueRequestsFromTypedMapFunc(clusterToMachinePools),
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &clusterv1.Cluster{}),
Expand Down Expand Up @@ -331,13 +333,18 @@ func (r *MachinePoolReconciler) watchClusterNodes(ctx context.Context, cluster *
return r.Tracker.Watch(ctx, remote.WatchInput{
Name: "machinepool-watchNodes",
Cluster: util.ObjectKey(cluster),
Watcher: &controller.ControllerAdapter{Controller: r.controller},
Watcher: r.controller,
Kind: &corev1.Node{},
EventHandler: handler.EnqueueRequestsFromObjectMapFunc(r.nodeToMachinePool),
EventHandler: handler.EnqueueRequestsFromMapFunc(r.nodeToMachinePool),
})
}

func (r *MachinePoolReconciler) nodeToMachinePool(ctx context.Context, node *corev1.Node) []reconcile.Request {
func (r *MachinePoolReconciler) nodeToMachinePool(ctx context.Context, o client.Object) []reconcile.Request {
node, ok := o.(*corev1.Node)
if !ok {
panic(fmt.Sprintf("Expected a Node but got a %T", o))
}

var filters []client.ListOption
// Match by clusterName when the node has the annotation.
if clusterName, ok := node.GetAnnotations()[clusterv1.ClusterNameAnnotation]; ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Reconciler struct {

func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
err := ctrl.NewControllerManagedBy(mgr).
Named("extensionconfig").
Add(builder.For(mgr,
&runtimev1.ExtensionConfig{},
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue, &runtimev1.ExtensionConfig{}),
Expand Down
2 changes: 1 addition & 1 deletion exp/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func GetMachinePoolByLabels(ctx context.Context, c client.Client, namespace stri

// MachinePoolToInfrastructureMapFunc returns a handler.MapFunc that watches for
// MachinePool events and returns reconciliation requests for an infrastructure provider object.
func MachinePoolToInfrastructureMapFunc(gvk schema.GroupVersionKind, log logr.Logger) handler.ObjectMapFunc[*expv1.MachinePool] {
func MachinePoolToInfrastructureMapFunc(gvk schema.GroupVersionKind, log logr.Logger) handler.TypedMapFunc[*expv1.MachinePool] {
log = log.WithValues("machine-pool-to-infra-map-func", gvk.String())
return func(_ context.Context, m *expv1.MachinePool) []reconcile.Request {
log := log.WithValues("MachinePool", klog.KObj(m))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module sigs.k8s.io/cluster-api

go 1.22.0

replace sigs.k8s.io/controller-runtime => github.com/Danil-Grigorev/controller-runtime v0.6.1-0.20240417125124-8984b3049571
replace sigs.k8s.io/controller-runtime => github.com/Danil-Grigorev/controller-runtime v0.6.1-0.20240419093006-d4ca5bcd94b1

require (
github.com/MakeNowJust/heredoc v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOEl
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Danil-Grigorev/controller-runtime v0.6.1-0.20240417125124-8984b3049571 h1:a1Oaf+Zk1mbhUP0wVULBOLZ+b4MXLW6g/2kadPQg5yw=
github.com/Danil-Grigorev/controller-runtime v0.6.1-0.20240417125124-8984b3049571/go.mod h1:TLM3OvUJgcqHVBLVRlNylmfbOlOukMLFHtc6jo3EtIQ=
github.com/Danil-Grigorev/controller-runtime v0.6.1-0.20240419093006-d4ca5bcd94b1 h1:DUXbWHmgQDEW+dAeY6HrsYTh/FrpALIR0noA3vDVkG0=
github.com/Danil-Grigorev/controller-runtime v0.6.1-0.20240419093006-d4ca5bcd94b1/go.mod h1:TLM3OvUJgcqHVBLVRlNylmfbOlOukMLFHtc6jo3EtIQ=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
Expand Down
24 changes: 13 additions & 11 deletions hack/tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ replace sigs.k8s.io/cluster-api => ../../

replace sigs.k8s.io/cluster-api/test => ../../test

replace sigs.k8s.io/controller-runtime => github.com/Danil-Grigorev/controller-runtime v0.6.1-0.20240419093006-d4ca5bcd94b1

require (
cloud.google.com/go/storage v1.40.0
github.com/blang/semver/v4 v4.0.0
Expand All @@ -17,12 +19,12 @@ require (
github.com/valyala/fastjson v1.6.4
golang.org/x/oauth2 v0.19.0
google.golang.org/api v0.174.0
k8s.io/api v0.29.3
k8s.io/apiextensions-apiserver v0.29.3
k8s.io/apimachinery v0.29.3
k8s.io/client-go v0.29.3
k8s.io/klog/v2 v2.110.1
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00
k8s.io/api v0.30.0-rc.2
k8s.io/apiextensions-apiserver v0.30.0-rc.2
k8s.io/apimachinery v0.30.0-rc.2
k8s.io/client-go v0.30.0-rc.2
k8s.io/klog/v2 v2.120.1
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
sigs.k8s.io/cluster-api v0.0.0-00010101000000-000000000000
sigs.k8s.io/cluster-api/test v0.0.0-00010101000000-000000000000
Expand Down Expand Up @@ -85,7 +87,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/cel-go v0.17.7 // indirect
github.com/google/cel-go v0.17.8 // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/google/go-github/v53 v53.2.0 // indirect
github.com/google/go-github/v58 v58.0.0 // indirect
Expand Down Expand Up @@ -147,14 +149,14 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.17.0 // indirect
golang.org/x/tools v0.18.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240314234333-6e1732d8331c // indirect
Expand All @@ -166,9 +168,9 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiserver v0.29.3 // indirect
k8s.io/apiserver v0.30.0-rc.2 // indirect
k8s.io/cluster-bootstrap v0.29.3 // indirect
k8s.io/component-base v0.29.3 // indirect
k8s.io/component-base v0.30.0-rc.2 // indirect
k8s.io/release v0.16.6-0.20240222112346-71feb57b59a4
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
Expand Down
Loading

0 comments on commit 414d92d

Please sign in to comment.