Skip to content

Commit

Permalink
ClusterCacheTracker: improve error when workload cluster is not
Browse files Browse the repository at this point in the history
reachable

Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Jun 6, 2023
1 parent dc5f916 commit 5a15402
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions controllers/remote/cluster_cache_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,21 @@ func (t *ClusterCacheTracker) createClient(ctx context.Context, config *rest.Con
// Create a http client for the cluster.
httpClient, err := rest.HTTPClientFor(config)
if err != nil {
return nil, nil, errors.Wrapf(err, "error creating http client for remote cluster %q", cluster.String())
return nil, nil, errors.Wrapf(err, "error creating client for remote cluster %q: error creating http client", cluster.String())
}

// Create a mapper for it
mapper, err := apiutil.NewDynamicRESTMapper(config, httpClient)
if err != nil {
return nil, nil, errors.Wrapf(err, "error creating dynamic rest mapper for remote cluster %q", cluster.String())
return nil, nil, errors.Wrapf(err, "error creating client for remote cluster %q: error creating dynamic rest mapper", cluster.String())
}

// Verify if we can get a rest mapping from the workload cluster apiserver.
// Note: This also checks if the apiserver is up in general. We do this already here
// to avoid further effort creating a cache and a client and to produce a clearer error message.
_, err = mapper.RESTMapping(clusterv1.GroupVersion.WithKind("Cluster").GroupKind(), clusterv1.GroupVersion.Version)
if err != nil {
return nil, nil, errors.Wrapf(err, "error creating client for remote cluster %q: error getting rest mapping", cluster.String())
}

// Create the cache for the remote cluster
Expand All @@ -362,7 +370,7 @@ func (t *ClusterCacheTracker) createClient(ctx context.Context, config *rest.Con
}
remoteCache, err := cache.New(config, cacheOptions)
if err != nil {
return nil, nil, errors.Wrapf(err, "error creating cache for remote cluster %q", cluster.String())
return nil, nil, errors.Wrapf(err, "error creating client for remote cluster %q: error creating cache", cluster.String())
}

cacheCtx, cacheCtxCancel := context.WithCancel(ctx)
Expand All @@ -373,12 +381,6 @@ func (t *ClusterCacheTracker) createClient(ctx context.Context, config *rest.Con
cancelFunc: cacheCtxCancel,
}

for _, index := range indexes {
if err := cache.IndexField(ctx, index.Object, index.Field, index.ExtractValue); err != nil {
return nil, nil, errors.Wrapf(err, "error adding index for field %q to cache for remote cluster %q", index.Field, cluster.String())
}
}

// Create the client for the remote cluster
c, err := client.New(config, client.Options{
Scheme: t.scheme,
Expand All @@ -394,6 +396,12 @@ func (t *ClusterCacheTracker) createClient(ctx context.Context, config *rest.Con
return nil, nil, errors.Wrapf(err, "error creating client for remote cluster %q", cluster.String())
}

for _, index := range indexes {
if err := cache.IndexField(ctx, index.Object, index.Field, index.ExtractValue); err != nil {
return nil, nil, errors.Wrapf(err, "error creating client for remote cluster %q: error adding index for field %q to cache", cluster.String(), index.Field)
}
}

// Start the cache!!!
go cache.Start(cacheCtx) //nolint:errcheck

Expand Down

0 comments on commit 5a15402

Please sign in to comment.