Skip to content

Commit 1865191

Browse files
authored
Check compose status only with compose provider (#2356)
When creating clients from profile, check the status of the compose project only when using the compose stack provider.
1 parent ca5f984 commit 1865191

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

internal/stack/clients.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,9 @@ func NewElasticsearchClientFromProfile(profile *profile.Profile, customOptions .
4747

4848
elasticsearchHost, found := os.LookupEnv(ElasticsearchHostEnv)
4949
if !found {
50-
// Using backgound context on initial call to avoid context cancellation.
51-
status, err := Status(context.Background(), Options{Profile: profile})
50+
err := checkClientStackAvailability(profile)
5251
if err != nil {
53-
return nil, fmt.Errorf("failed to check status of stack in current profile: %w", err)
54-
}
55-
if len(status) == 0 {
56-
return nil, ErrUnavailableStack
52+
return nil, err
5753
}
5854

5955
elasticsearchHost = profileConfig.ElasticsearchHostPort
@@ -118,13 +114,9 @@ func NewKibanaClientFromProfile(profile *profile.Profile, customOptions ...kiban
118114

119115
kibanaHost, found := os.LookupEnv(KibanaHostEnv)
120116
if !found {
121-
// Using background context on initial call to avoid context cancellation.
122-
status, err := Status(context.Background(), Options{Profile: profile})
117+
err := checkClientStackAvailability(profile)
123118
if err != nil {
124-
return nil, fmt.Errorf("failed to check status of stack in current profile: %w", err)
125-
}
126-
if len(status) == 0 {
127-
return nil, ErrUnavailableStack
119+
return nil, err
128120
}
129121

130122
kibanaHost = profileConfig.KibanaHostPort
@@ -177,3 +169,25 @@ func FindCACertificate(profile *profile.Profile) (string, error) {
177169

178170
return caCertPath, nil
179171
}
172+
173+
func checkClientStackAvailability(profile *profile.Profile) error {
174+
config, err := LoadConfig(profile)
175+
if err != nil {
176+
return fmt.Errorf("cannot load stack configuration: %w", err)
177+
}
178+
179+
// Checking it only with the compose provider because other providers need
180+
// a client, and we fall in infinite recursion.
181+
if config.Provider == ProviderCompose || config.Provider == "" {
182+
// Using backgound context on initial call to avoid context cancellation.
183+
status, err := Status(context.Background(), Options{Profile: profile})
184+
if err != nil {
185+
return fmt.Errorf("failed to check status of stack in current profile: %w", err)
186+
}
187+
if len(status) == 0 {
188+
return ErrUnavailableStack
189+
}
190+
}
191+
192+
return nil
193+
}

0 commit comments

Comments
 (0)