From 9ce51b8dffa283d4f97faeab25751b0814407214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20=C3=81lvarez?= Date: Fri, 24 May 2024 16:25:31 +0200 Subject: [PATCH 1/4] Fleet example changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Iván Álvarez --- examples/fleet/main.go | 16 ++++++++++------ pkg/manager/internal.go | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/examples/fleet/main.go b/examples/fleet/main.go index 4dd366cfd8..195cedc718 100644 --- a/examples/fleet/main.go +++ b/examples/fleet/main.go @@ -68,8 +68,9 @@ func main() { // Setup a Manager, note that this not yet engages clusters, only makes them available. entryLog.Info("Setting up manager") provider := &KindClusterProvider{ - log: log.Log.WithName("kind-cluster-provider"), - clusters: map[string]cluster.Cluster{}, + log: log.Log.WithName("kind-cluster-provider"), + clusters: map[string]cluster.Cluster{}, + cancelFns: map[string]context.CancelFunc{}, } mgr, err := manager.New( cfg, @@ -85,18 +86,18 @@ func main() { func(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx) - cluster, err := mgr.GetCluster(ctx, req.ClusterName) + cl, err := mgr.GetCluster(ctx, req.ClusterName) if err != nil { return reconcile.Result{}, err } - client := cluster.GetClient() + client := cl.GetClient() // Retrieve the pod from the cluster. pod := &corev1.Pod{} if err := client.Get(ctx, req.NamespacedName, pod); err != nil { return reconcile.Result{}, err } - log.Info("Reconciling pod", "name", pod.Name, "uuid", pod.UID) + log.Info(fmt.Sprintf("Retrieved pod %s:>%s/%s", cl.Name(), pod.Namespace, pod.Name)) // Print any annotations that start with fleet. for k, v := range pod.Labels { @@ -185,7 +186,10 @@ func (k *KindClusterProvider) Run(ctx context.Context, mgr manager.Manager) erro k.log.Info("failed to create rest config", "error", err) return false, nil // keep going } - cl, err := cluster.New(cfg, k.Options...) + // Copy provider options and append the cluster name + clOptions := append(k.Options, cluster.WithName(clusterName)) + + cl, err := cluster.New(cfg, clOptions...) if err != nil { k.log.Info("failed to create cluster", "error", err) return false, nil // keep going diff --git a/pkg/manager/internal.go b/pkg/manager/internal.go index 40e5172f9e..51cd6ca1d7 100644 --- a/pkg/manager/internal.go +++ b/pkg/manager/internal.go @@ -619,9 +619,9 @@ func (cm *controllerManager) Engage(ctx context.Context, cl cluster.Cluster) err // be reentrant via noop cm.engagedClustersLock.RLock() if _, ok := cm.engagedClusters[cl.Name()]; ok { - cm.engagedClustersLock.RUnlock() return nil } + cm.engagedClustersLock.RUnlock() // add early because any engaged runnable could access it cm.engagedClustersLock.Lock() From 86639c0a0b6b920388842802ab1f4b7ac7866a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20=C3=81lvarez?= Date: Mon, 27 May 2024 08:33:38 +0200 Subject: [PATCH 2/4] Update examples/fleet/main.go Co-authored-by: Dr. Stefan Schimanski --- examples/fleet/main.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/fleet/main.go b/examples/fleet/main.go index 195cedc718..ea97cb6ca8 100644 --- a/examples/fleet/main.go +++ b/examples/fleet/main.go @@ -186,9 +186,7 @@ func (k *KindClusterProvider) Run(ctx context.Context, mgr manager.Manager) erro k.log.Info("failed to create rest config", "error", err) return false, nil // keep going } - // Copy provider options and append the cluster name - clOptions := append(k.Options, cluster.WithName(clusterName)) - + clOptions := append([]cluster.Option{cluster.WithName(clusterName)}, k.Options...) cl, err := cluster.New(cfg, clOptions...) if err != nil { k.log.Info("failed to create cluster", "error", err) From 2f0deff72aad35ab890274200ed65520588313c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20=C3=81lvarez?= Date: Mon, 27 May 2024 08:34:56 +0200 Subject: [PATCH 3/4] PR suggested changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Iván Álvarez --- examples/fleet/main.go | 2 +- pkg/manager/internal.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/fleet/main.go b/examples/fleet/main.go index 195cedc718..d256e6c7d6 100644 --- a/examples/fleet/main.go +++ b/examples/fleet/main.go @@ -97,7 +97,7 @@ func main() { if err := client.Get(ctx, req.NamespacedName, pod); err != nil { return reconcile.Result{}, err } - log.Info(fmt.Sprintf("Retrieved pod %s:>%s/%s", cl.Name(), pod.Namespace, pod.Name)) + log.Info("Reconciling pod", "ns", pod.GetNamespace(), "name", pod.Name, "uuid", pod.UID) // Print any annotations that start with fleet. for k, v := range pod.Labels { diff --git a/pkg/manager/internal.go b/pkg/manager/internal.go index 51cd6ca1d7..75facc7e85 100644 --- a/pkg/manager/internal.go +++ b/pkg/manager/internal.go @@ -619,6 +619,7 @@ func (cm *controllerManager) Engage(ctx context.Context, cl cluster.Cluster) err // be reentrant via noop cm.engagedClustersLock.RLock() if _, ok := cm.engagedClusters[cl.Name()]; ok { + cm.engagedClustersLock.RUnlock() return nil } cm.engagedClustersLock.RUnlock() From a9d3c0043edda9a190c11ae0f938bceb3f0c39a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20=C3=81lvarez?= Date: Mon, 27 May 2024 08:50:01 +0200 Subject: [PATCH 4/4] Fix fleet example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Iván Álvarez --- examples/fleet/main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/fleet/main.go b/examples/fleet/main.go index 40d8a2debe..ee107ddf01 100644 --- a/examples/fleet/main.go +++ b/examples/fleet/main.go @@ -97,7 +97,7 @@ func main() { if err := client.Get(ctx, req.NamespacedName, pod); err != nil { return reconcile.Result{}, err } - log.Info("Reconciling pod", "ns", pod.GetNamespace(), "name", pod.Name, "uuid", pod.UID) + log.Info("Reconciling pod", "cluster", cl.Name(), "ns", pod.GetNamespace(), "name", pod.Name, "uuid", pod.UID) // Print any annotations that start with fleet. for k, v := range pod.Labels { @@ -171,6 +171,7 @@ func (k *KindClusterProvider) Run(ctx context.Context, mgr manager.Manager) erro } k.lock.RLock() if _, ok := k.clusters[clusterName]; ok { + k.lock.RUnlock() continue } k.lock.RUnlock() @@ -247,6 +248,8 @@ func (k *KindClusterProvider) Run(ctx context.Context, mgr manager.Manager) erro delete(k.clusters, name) delete(k.cancelFns, name) k.lock.Unlock() + + k.log.Info("Cluster removed", "cluster", name) } }