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..7e387ec8f9b50 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 { @@ -1271,3 +1269,4 @@ func (rcsw *RemoteClusterServiceWatcher) isRemoteDiscovery(l map[string]string) return remoteDiscoverySelector.Matches(labels.Set(l)) } +