Skip to content

Commit

Permalink
Check compose status only with compose provider
Browse files Browse the repository at this point in the history
When creating clients from profile, check the status of the compose
project only when using the compose stack provider.
  • Loading branch information
jsoriano committed Jan 17, 2025
1 parent 918cf21 commit 14acdd1
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions internal/stack/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ func NewElasticsearchClientFromProfile(profile *profile.Profile, customOptions .

elasticsearchHost, found := os.LookupEnv(ElasticsearchHostEnv)
if !found {
// Using backgound context on initial call to avoid context cancellation.
status, err := Status(context.Background(), Options{Profile: profile})
err := checkClientStackAvailability(profile)
if err != nil {
return nil, fmt.Errorf("failed to check status of stack in current profile: %w", err)
}
if len(status) == 0 {
return nil, ErrUnavailableStack
return nil, err
}

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

kibanaHost, found := os.LookupEnv(KibanaHostEnv)
if !found {
// Using background context on initial call to avoid context cancellation.
status, err := Status(context.Background(), Options{Profile: profile})
err := checkClientStackAvailability(profile)
if err != nil {
return nil, fmt.Errorf("failed to check status of stack in current profile: %w", err)
}
if len(status) == 0 {
return nil, ErrUnavailableStack
return nil, err
}

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

return caCertPath, nil
}

func checkClientStackAvailability(profile *profile.Profile) error {
config, err := LoadConfig(profile)
if err != nil {
return fmt.Errorf("cannot load stack configuration: %w", err)
}

// Checking it only with the compose provider because other providers need
// a client, and we fall in a recursion loop.
if config.Provider == ProviderCompose || config.Provider == "" {
// Using backgound context on initial call to avoid context cancellation.
status, err := Status(context.Background(), Options{Profile: profile})
if err != nil {
return fmt.Errorf("failed to check status of stack in current profile: %w", err)
}
if len(status) == 0 {
return ErrUnavailableStack
}
}

return nil
}

0 comments on commit 14acdd1

Please sign in to comment.