From a501b624504b0b0a1cc392359c6a9779a330540c Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Fri, 17 Jan 2025 17:39:30 +0100 Subject: [PATCH] Check compose status only with compose provider When creating clients from profile, check the status of the compose project only when using the compose stack provider. --- internal/stack/clients.go | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/internal/stack/clients.go b/internal/stack/clients.go index b3915ec73..6db248752 100644 --- a/internal/stack/clients.go +++ b/internal/stack/clients.go @@ -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 @@ -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 @@ -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 +}