Skip to content

Commit 91fef18

Browse files
committed
remove unnecessary previous value merging
1 parent 66dc96e commit 91fef18

File tree

3 files changed

+74
-104
lines changed

3 files changed

+74
-104
lines changed

internal/configsource/source.go

+41-42
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,49 @@ type Factory interface {
9292
// Factories maps the type of a ConfigSource to the respective factory object.
9393
type Factories map[component.Type]Factory
9494

95-
// BuildConfigSourcesAndResolve inspects the given confmap.Conf and resolves all config sources referenced
96-
// in the configuration, returning a confmap.Conf in which all env vars and config sources on
95+
// BuildConfigSourcesFromConf inspects the given confmap.Conf and builds all config sources referenced
96+
// in the configuration intended to be used with ResolveWithConfigSources().
97+
func BuildConfigSourcesFromConf(ctx context.Context, confToFurtherResolve *confmap.Conf, logger *zap.Logger, factories Factories, confmapProviders map[string]confmap.Provider) (map[string]ConfigSource, *confmap.Conf, error) {
98+
configSourceSettings, confWithoutSettings, err := SettingsFromConf(ctx, confToFurtherResolve, factories, confmapProviders)
99+
if err != nil {
100+
return nil, nil, fmt.Errorf("failed to parse settings from conf: %w", err)
101+
}
102+
103+
configSources, err := BuildConfigSources(context.Background(), configSourceSettings, logger, factories)
104+
if err != nil {
105+
return nil, nil, fmt.Errorf("failed to build config sources: %w", err)
106+
}
107+
return configSources, confWithoutSettings, nil
108+
}
109+
110+
func BuildConfigSources(ctx context.Context, configSourcesSettings map[string]Settings, logger *zap.Logger, factories Factories) (map[string]ConfigSource, error) {
111+
cfgSources := make(map[string]ConfigSource, len(configSourcesSettings))
112+
for fullName, cfgSrcSettings := range configSourcesSettings {
113+
// If we have the setting we also have the factory.
114+
factory, ok := factories[cfgSrcSettings.ID().Type()]
115+
if !ok {
116+
return nil, fmt.Errorf("unknown %s config source type for %s", cfgSrcSettings.ID().Type(), fullName)
117+
}
118+
119+
cfgSrc, err := factory.CreateConfigSource(ctx, cfgSrcSettings, logger.With(zap.String("config_source", fullName)))
120+
if err != nil {
121+
return nil, fmt.Errorf("failed to create config source %s: %w", fullName, err)
122+
}
123+
124+
if cfgSrc == nil {
125+
return nil, fmt.Errorf("factory for %q produced a nil extension", fullName)
126+
}
127+
128+
cfgSources[fullName] = cfgSrc
129+
}
130+
131+
return cfgSources, nil
132+
}
133+
134+
// ResolveWithConfigSources returns a confmap.Conf in which all env vars and config sources on
97135
// the given input config map are resolved to actual literal values of the env vars or config sources.
98136
//
99-
// 1. BuildConfigSourcesAndResolve to inject the data from config sources into a configuration;
137+
// 1. ResolveWithConfigSources to inject the data from config sources into a configuration;
100138
// 2. Wait for an update on "watcher" func.
101139
// 3. Close the confmap.Retrieved instance;
102140
//
@@ -202,45 +240,6 @@ type Factories map[component.Type]Factory
202240
// results.
203241
//
204242
// For an overview about the internals of the Manager refer to the package README.md.
205-
206-
func BuildConfigSourcesAndSettings(ctx context.Context, confToFurtherResolve *confmap.Conf, logger *zap.Logger, factories Factories, confmapProviders map[string]confmap.Provider) (map[string]ConfigSource, *confmap.Conf, error) {
207-
configSourceSettings, confWithoutSettings, err := SettingsFromConf(ctx, confToFurtherResolve, factories, confmapProviders)
208-
if err != nil {
209-
return nil, nil, fmt.Errorf("failed to parse settings from conf: %w", err)
210-
}
211-
212-
configSources, err := BuildConfigSources(context.Background(), configSourceSettings, logger, factories)
213-
if err != nil {
214-
return nil, nil, fmt.Errorf("failed to build config sources: %w", err)
215-
}
216-
return configSources, confWithoutSettings, nil
217-
}
218-
219-
// BuildConfigSources builds the ConfigSource objects according to the given ConfigSettings.
220-
func BuildConfigSources(ctx context.Context, configSourcesSettings map[string]Settings, logger *zap.Logger, factories Factories) (map[string]ConfigSource, error) {
221-
cfgSources := make(map[string]ConfigSource, len(configSourcesSettings))
222-
for fullName, cfgSrcSettings := range configSourcesSettings {
223-
// If we have the setting we also have the factory.
224-
factory, ok := factories[cfgSrcSettings.ID().Type()]
225-
if !ok {
226-
return nil, fmt.Errorf("unknown %s config source type for %s", cfgSrcSettings.ID().Type(), fullName)
227-
}
228-
229-
cfgSrc, err := factory.CreateConfigSource(ctx, cfgSrcSettings, logger.With(zap.String("config_source", fullName)))
230-
if err != nil {
231-
return nil, fmt.Errorf("failed to create config source %s: %w", fullName, err)
232-
}
233-
234-
if cfgSrc == nil {
235-
return nil, fmt.Errorf("factory for %q produced a nil extension", fullName)
236-
}
237-
238-
cfgSources[fullName] = cfgSrc
239-
}
240-
241-
return cfgSources, nil
242-
}
243-
244243
func ResolveWithConfigSources(ctx context.Context, configSources map[string]ConfigSource, confmapProviders map[string]confmap.Provider, conf *confmap.Conf, watcher confmap.WatcherFunc) (*confmap.Conf, confmap.CloseFunc, error) {
245244
resolved := map[string]any{}
246245
var closeFuncs []confmap.CloseFunc

internal/configsource/source_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
var errValueUpdated = errors.New("configuration must retrieve the updated value")
3434

3535
func BuildConfigSourcesAndResolve(ctx context.Context, confToFurtherResolve *confmap.Conf, logger *zap.Logger, factories Factories, watcher confmap.WatcherFunc) (*confmap.Conf, confmap.CloseFunc, error) {
36-
cfgSources, conf, err := BuildConfigSourcesAndSettings(ctx, confToFurtherResolve, logger, factories, nil)
36+
cfgSources, conf, err := BuildConfigSourcesFromConf(ctx, confToFurtherResolve, logger, factories, nil)
3737
if err != nil {
3838
return nil, nil, err
3939
}

internal/confmapprovider/configsource/provider.go

+32-61
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ type ProviderWrapper struct {
7979
var _ confmap.Provider = (*wrappedProvider)(nil)
8080

8181
type wrappedProvider struct {
82-
wrapper *ProviderWrapper
83-
provider confmap.Provider
84-
lastRetrieved *confmap.Retrieved
82+
wrapper *ProviderWrapper
83+
provider confmap.Provider
8584
}
8685

8786
func (w *wrappedProvider) Retrieve(ctx context.Context, uri string, watcher confmap.WatcherFunc) (*confmap.Retrieved, error) {
@@ -102,93 +101,65 @@ func (w *wrappedProvider) Shutdown(ctx context.Context) error {
102101
return w.provider.Shutdown(ctx)
103102
}
104103

105-
func (c *ProviderWrapper) Provider(provider confmap.Provider) confmap.Provider {
106-
for _, h := range c.hooks {
104+
func (pw *ProviderWrapper) Provider(provider confmap.Provider) confmap.Provider {
105+
for _, h := range pw.hooks {
107106
h.OnNew()
108107
}
109-
c.providersLock.Lock()
110-
defer c.providersLock.Unlock()
111-
c.providers[provider.Scheme()] = provider
112-
return &wrappedProvider{provider: provider, wrapper: c, lastRetrieved: &confmap.Retrieved{}}
108+
pw.providersLock.Lock()
109+
defer pw.providersLock.Unlock()
110+
pw.providers[provider.Scheme()] = provider
111+
return &wrappedProvider{provider: provider, wrapper: pw}
113112
}
114113

115-
// ResolveForWrapped will retrieve from the wrappedProvider provider and merge the result w/ a previous retrieved instance (if any) as latestConf.
116-
// It will then "configsource.BuildConfigSourcesAndResolve(latestConf)" and return the resolved map as a confmap.Retrieved w/ resolve closer and the wrappedProvider provider closer.
117-
func (c *ProviderWrapper) ResolveForWrapped(ctx context.Context, uri string, onChange confmap.WatcherFunc, w *wrappedProvider) (*confmap.Retrieved, error) {
118-
provider := w.provider
119-
retrieved := &confmap.Retrieved{}
120-
121-
var tmpRetrieved *confmap.Retrieved
122-
var err error
123-
// Here we are getting the value directly from the provider, which
124-
// is what the core's resolver intends (invokes this parent method).
125-
if tmpRetrieved, err = provider.Retrieve(ctx, uri, onChange); err != nil {
114+
// ResolveForWrapped will retrieve from the wrappedProvider and, if possible, resolve all config source directives with their resolved values.
115+
// If the wrappedProvider's retrieved value is only valid AsRaw() (scalar/array) then that will be returned without further evaluation.
116+
func (pw *ProviderWrapper) ResolveForWrapped(ctx context.Context, uri string, onChange confmap.WatcherFunc, w *wrappedProvider) (*confmap.Retrieved, error) {
117+
retrieved, err := w.provider.Retrieve(ctx, uri, onChange)
118+
if err != nil {
126119
return nil, fmt.Errorf("configsource provider failed retrieving: %w", err)
127-
} else if tmpRetrieved != nil {
128-
retrieved = tmpRetrieved
129120
}
130121

131-
var previousConf *confmap.Conf
132-
var rawRetrieved *confmap.Retrieved
133-
if previousConf, err = w.lastRetrieved.AsConf(); err != nil {
134-
return nil, fmt.Errorf("configsource provider failed updated retrieval: %w", err)
135-
} else if previousConf != nil {
136-
// Need to merge config maps that we've encountered so far
137-
if latestConf, e := retrieved.AsConf(); e != nil {
138-
// raw fallback
139-
if raw, ee := retrieved.AsRaw(); ee == nil {
140-
if rawRetrieved, ee = confmap.NewRetrieved(raw); ee != nil {
141-
return nil, fmt.Errorf("failed resolving wrappedProvider retrieve: %w", e)
142-
}
122+
conf, err := retrieved.AsConf()
123+
if err != nil {
124+
// raw fallback attempt, or return AsConf() error
125+
if raw, e := retrieved.AsRaw(); e == nil {
126+
if rawRetrieved, ee := confmap.NewRetrieved(raw); ee == nil {
127+
// raw confmap.Retrieved values shouldn't be further processed so return here
128+
return rawRetrieved, nil
143129
}
144-
} else if e = latestConf.Merge(previousConf); e != nil {
145-
return nil, fmt.Errorf("failed merging previous wrappedProvider retrieve: %w", e)
146-
} else if tmpRetrieved, e = confmap.NewRetrieved(latestConf.ToStringMap()); e != nil {
147-
return nil, err
148-
} else if tmpRetrieved != nil {
149-
retrieved = tmpRetrieved
150130
}
131+
return nil, fmt.Errorf("failed retrieving from wrappedProvider: %w", err)
151132
}
152133

153-
// raw confmap.Retrieved values cannot be coerced AsConf() so we return here to not update lastRetrieved
154-
// or attempt subsequent config source value resolution.
155-
if rawRetrieved != nil {
156-
return rawRetrieved, nil
157-
}
158-
159-
w.lastRetrieved = retrieved
160-
161-
latestConf, err := w.lastRetrieved.AsConf()
162-
if err != nil {
163-
return nil, err
164-
} else if latestConf == nil {
165-
return nil, fmt.Errorf("latest retrieved confmap.Conf is nil")
134+
if conf == nil {
135+
return nil, fmt.Errorf("retrieved confmap.Conf is unexpectedly nil")
166136
}
167137

168-
scheme, stringMap := provider.Scheme(), latestConf.ToStringMap()
169-
for _, h := range c.hooks {
138+
scheme, stringMap := w.provider.Scheme(), conf.ToStringMap()
139+
for _, h := range pw.hooks {
170140
h.OnRetrieve(scheme, stringMap)
171141
}
172142

173-
c.providersLock.Lock()
143+
// copy providers map for downstream resolution
144+
pw.providersLock.Lock()
174145
providers := map[string]confmap.Provider{}
175-
for s, p := range c.providers {
146+
for s, p := range pw.providers {
176147
providers[s] = p
177148
}
178-
c.providersLock.Unlock()
179-
configSources, conf, err := configsource.BuildConfigSourcesAndSettings(ctx, latestConf, c.logger, c.factories, providers)
149+
pw.providersLock.Unlock()
150+
configSources, confToResolve, err := configsource.BuildConfigSourcesFromConf(ctx, conf, pw.logger, pw.factories, providers)
180151
if err != nil {
181152
return nil, fmt.Errorf("failed resolving latestConf: %w", err)
182153
}
183154

184-
resolved, closeFunc, err := configsource.ResolveWithConfigSources(ctx, configSources, providers, conf, onChange)
155+
resolved, closeFunc, err := configsource.ResolveWithConfigSources(ctx, configSources, providers, confToResolve, onChange)
185156
if err != nil {
186157
return nil, fmt.Errorf("failed resolving with config sources: %w", err)
187158
}
188159

189160
return confmap.NewRetrieved(
190161
resolved.ToStringMap(), confmap.WithRetrievedClose(
191-
configsource.MergeCloseFuncs([]confmap.CloseFunc{closeFunc, w.lastRetrieved.Close}),
162+
configsource.MergeCloseFuncs([]confmap.CloseFunc{closeFunc, retrieved.Close}),
192163
),
193164
)
194165
}

0 commit comments

Comments
 (0)