From 43fe71c4531abcc127f154ffe17117eaaec4d173 Mon Sep 17 00:00:00 2001 From: Alejandro Pedraza Date: Fri, 5 Jan 2024 09:52:09 -0500 Subject: [PATCH] Call remoteAPI.UnregisterGauges() inside NewRemoteClusterServiceWatcher instead --- multicluster/cmd/service-mirror/main.go | 3 ++- multicluster/service-mirror/cluster_watcher.go | 16 +++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/multicluster/cmd/service-mirror/main.go b/multicluster/cmd/service-mirror/main.go index 8e34b40d63083..614df04192828 100644 --- a/multicluster/cmd/service-mirror/main.go +++ b/multicluster/cmd/service-mirror/main.go @@ -303,7 +303,7 @@ func restartClusterWatcher( if err != nil { return fmt.Errorf("unable to parse kube config: %w", err) } - clusterWatcher, err = servicemirror.NewRemoteClusterServiceWatcher( + cw, err := servicemirror.NewRemoteClusterServiceWatcher( ctx, namespace, controllerK8sAPI, @@ -317,6 +317,7 @@ func restartClusterWatcher( if err != nil { return fmt.Errorf("unable to create cluster watcher: %w", err) } + clusterWatcher = cw err = clusterWatcher.Start(ctx) if err != nil { return fmt.Errorf("failed to start cluster watcher: %w", err) diff --git a/multicluster/service-mirror/cluster_watcher.go b/multicluster/service-mirror/cluster_watcher.go index 13c766f645999..2b52aea6a08f6 100644 --- a/multicluster/service-mirror/cluster_watcher.go +++ b/multicluster/service-mirror/cluster_watcher.go @@ -173,6 +173,11 @@ func NewRemoteClusterServiceWatcher( if err != nil { return nil, fmt.Errorf("cannot initialize api for target cluster %s: %w", clusterName, err) } + _, err = remoteAPI.Client.Discovery().ServerVersion() + if err != nil { + remoteAPI.UnregisterGauges() + return nil, fmt.Errorf("cannot connect to api for target cluster %s: %w", clusterName, err) + } // Create k8s event recorder eventBroadcaster := record.NewBroadcaster() @@ -184,7 +189,7 @@ func NewRemoteClusterServiceWatcher( }) stopper := make(chan struct{}) - rcsw := RemoteClusterServiceWatcher{ + return &RemoteClusterServiceWatcher{ serviceMirrorNamespace: serviceMirrorNamespace, link: link, remoteAPIClient: remoteAPI, @@ -203,14 +208,7 @@ func NewRemoteClusterServiceWatcher( headlessServicesEnabled: enableHeadlessSvc, // always instantiate the gatewayAlive=true to prevent unexpected service fail fast gatewayAlive: true, - } - - _, err = remoteAPI.Client.Discovery().ServerVersion() - if err != nil { - return &rcsw, fmt.Errorf("cannot connect to api for target cluster %s: %w", clusterName, err) - } - - return &rcsw, nil + }, nil } func (rcsw *RemoteClusterServiceWatcher) mirroredResourceName(remoteName string) string {