Skip to content

Commit

Permalink
Add reconcile.ClusterAwareRequest and matching EventHandlers
Browse files Browse the repository at this point in the history
On-behalf-of: SAP <[email protected]>
Signed-off-by: Marvin Beckers <[email protected]>
  • Loading branch information
embik committed Jan 8, 2025
1 parent d65ae80 commit b604946
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 341 deletions.
15 changes: 0 additions & 15 deletions examples/fleet-namespace/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,12 @@ package main
import (
"context"

"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

type clusterRequest struct {
reconcile.Request
ClusterName string
}

// String returns the general purpose string representation.
func (cr *clusterRequest) String() string {
if cr.ClusterName == "" {
return cr.NamespacedName.String()
}
return "cluster://" + cr.ClusterName + string(types.Separator) + cr.NamespacedName.String()
}

type NamespacedCluster struct {
clusterName string
cluster.Cluster
Expand Down
124 changes: 0 additions & 124 deletions examples/fleet-namespace/eventhandler.go

This file was deleted.

8 changes: 4 additions & 4 deletions examples/fleet-namespace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ func main() {
os.Exit(1)
}

if err := builder.TypedControllerManagedBy[clusterRequest](mgr).
if err := builder.TypedControllerManagedBy[reconcile.ClusterAwareRequest](mgr).
Named("fleet-ns-configmap-controller").
Watches(&corev1.ConfigMap{}, &EnqueueClusterRequestForObject{}).
Complete(reconcile.TypedFunc[clusterRequest](
func(ctx context.Context, req clusterRequest) (ctrl.Result, error) {
For(&corev1.ConfigMap{}).
Complete(reconcile.TypedFunc[reconcile.ClusterAwareRequest](
func(ctx context.Context, req reconcile.ClusterAwareRequest) (ctrl.Result, error) {
log := log.FromContext(ctx).WithValues("cluster", req.ClusterName)

cl, err := mgr.GetCluster(ctx, req.ClusterName)
Expand Down
9 changes: 9 additions & 0 deletions examples/fleet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# examples/fleet

The `fleet` example is a basic implementation of a multi-cluster-capable controller. As a cluster provider it uses `kind`: Every local kind cluster with a `fleet-` name prefix will be picked up as dynamic cluster if multi-cluster support is enabled in `fleet`.

`fleet` can switch between multi-cluster mode and single-cluster mode via the `--enable-cluster-provider` flag (defaults to `true`) to demonstrate the seamless switch of using the same code base as a "normal" single-cluster controller and as a multi-cluster controller as easy as plugging in a (third-party) cluster provider. To run `fleet` against the current `KUBECONFIG` in single-cluster mode, just run:

```sh
$ USE_EXISTING_CLUSTER=true go run . --enable-cluster-provider=false
```
35 changes: 0 additions & 35 deletions examples/fleet/cluster.go

This file was deleted.

125 changes: 0 additions & 125 deletions examples/fleet/eventhandler.go

This file was deleted.

Loading

0 comments on commit b604946

Please sign in to comment.