Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check compose status only with compose provider #2356

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 infinite recursion.
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
}