Skip to content

Commit 21deeff

Browse files
authored
Merge pull request #104 from ntnn/multi-logging-improvement
✨ Improvements on the multi provider
2 parents e15a9cf + 299c481 commit 21deeff

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

providers/multi/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ limitations under the License.
2020
//
2121
// Each provider must be added with a unique name, which is used as
2222
// a prefix for clusters coming from that provider.
23+
//
24+
// When implementing providers that use the multi provider expose the
25+
// Options when constructing the multi provider to allow users to
26+
// configure the separator and logger suffix.
2327
package multi

providers/multi/provider.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,22 @@ var _ multicluster.Provider = &Provider{}
3838

3939
// Options defines the options for the provider.
4040
type Options struct {
41-
Separator string
41+
// Separator is the string is used when concatenating a provider
42+
// name and a cluster name.
43+
// Default: "#"
44+
// Example: "provider1#clusterA"
45+
Separator string
46+
// ChannelSize is the size of the internal channel used to
47+
// notify the provider to start newly added providers. Shouldn't
48+
// generally be needed unless many providers are added before
49+
// starting the manager and/or the multi provider.
50+
// Default: 10
4251
ChannelSize int
52+
// LoggerSuffix is an optional suffix to add to the logger name.
53+
// This can be useful to distinguish multiple multi providers
54+
// in the logs.
55+
// Default: ""
56+
LoggerSuffix string
4357
}
4458

4559
// Provider is a multicluster.Provider that manages multiple providers.
@@ -74,7 +88,11 @@ func New(opts Options) *Provider {
7488
p.opts.ChannelSize = 10
7589
}
7690

77-
p.log = log.Log.WithName("multi-provider")
91+
loggerName := "multi-provider"
92+
if p.opts.LoggerSuffix != "" {
93+
loggerName += "-" + p.opts.LoggerSuffix
94+
}
95+
p.log = log.Log.WithName(loggerName)
7896

7997
p.indexers = make([]index, 0)
8098
p.providers = make(map[string]multicluster.Provider)
@@ -242,17 +260,18 @@ func (p *Provider) RemoveProvider(providerName string) {
242260
}
243261

244262
// Get returns a cluster by name.
245-
func (p *Provider) Get(ctx context.Context, clusterName string) (cluster.Cluster, error) {
246-
providerName, clusterName := p.splitClusterName(clusterName)
247-
p.log.V(1).Info("getting cluster", "providerName", providerName, "name", clusterName)
263+
func (p *Provider) Get(ctx context.Context, input string) (cluster.Cluster, error) {
264+
providerName, clusterName := p.splitClusterName(input)
265+
log := p.log.WithValues("providerName", providerName, "clusterName", clusterName)
266+
log.V(1).Info("getting cluster")
248267

249268
p.lock.RLock()
250269
provider, ok := p.providers[providerName]
251270
p.lock.RUnlock()
252271

253272
if !ok {
254-
p.log.Error(multicluster.ErrClusterNotFound, "provider not found for provider name", "providerName", providerName)
255-
return nil, fmt.Errorf("provider not found %q: %w", providerName, multicluster.ErrClusterNotFound)
273+
log.Error(multicluster.ErrClusterNotFound, "provider not found")
274+
return nil, fmt.Errorf("provider not found %q (%q): %w", providerName, input, multicluster.ErrClusterNotFound)
256275
}
257276

258277
return provider.Get(ctx, clusterName)
@@ -273,7 +292,7 @@ func (p *Provider) IndexField(ctx context.Context, obj client.Object, field stri
273292
if err := provider.IndexField(ctx, obj, field, extractValue); err != nil {
274293
errs = errors.Join(
275294
errs,
276-
fmt.Errorf("failed to index field %q on cluster %q: %w", field, providerName, err),
295+
fmt.Errorf("failed to index field %q on provider %q: %w", field, providerName, err),
277296
)
278297
}
279298
}

0 commit comments

Comments
 (0)