From 0b3818835a06dde0f7778ac23fa40aae1f201056 Mon Sep 17 00:00:00 2001 From: Ryan VanGundy Date: Mon, 21 Jul 2025 17:35:35 -0400 Subject: [PATCH] fix(env): Don't throw an error when volumes not accessible We were throwing an error if we were unable to connect to a k8s cluster, and were not able to resolve a `.volumes/pvc-abcd` folder to its corresponding cluster volume. We now ignore errors, such that the volume env var would not be returned. --- pkg/env/kube_env.go | 5 ++++- pkg/env/kube_env_test.go | 21 ++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pkg/env/kube_env.go b/pkg/env/kube_env.go index 1dd8ee7eb..f2d50e844 100644 --- a/pkg/env/kube_env.go +++ b/pkg/env/kube_env.go @@ -50,6 +50,9 @@ func NewKubeEnvPrinter(injector di.Injector) *KubeEnvPrinter { // It checks for a project-specific volume directory and returns current variables // if it doesn't exist. If it does, it ensures each PVC directory has a corresponding // "PV_" environment variable, returning the map if all are accounted for. +// If not all volumes are accounted for, it attempts to query the Kubernetes API +// to create environment variables for matching PVCs. If the API is unavailable, +// it gracefully degrades by returning the basic environment variables without failing. func (e *KubeEnvPrinter) GetEnvVars() (map[string]string, error) { envVars := make(map[string]string) configRoot, err := e.configHandler.GetConfigRoot() @@ -111,7 +114,7 @@ func (e *KubeEnvPrinter) GetEnvVars() (map[string]string, error) { pvcs, err := queryPersistentVolumeClaims(kubeConfigPath) if err != nil { - return nil, fmt.Errorf("error querying persistent volume claims: %w", err) + return envVars, nil } if pvcs != nil && pvcs.Items != nil { diff --git a/pkg/env/kube_env_test.go b/pkg/env/kube_env_test.go index a8a57f6a3..fbf9169de 100644 --- a/pkg/env/kube_env_test.go +++ b/pkg/env/kube_env_test.go @@ -272,19 +272,22 @@ func TestKubeEnvPrinter_GetEnvVars(t *testing.T) { // When getting environment variables envVars, err := printer.GetEnvVars() - // Then an error should be returned - if err == nil { - t.Error("Expected error, got nil") + // Then no error should be returned (graceful degradation) + if err != nil { + t.Errorf("Expected no error due to graceful degradation, got %v", err) } - // And the error should mention querying PVCs - if !strings.Contains(err.Error(), "error querying persistent volume claims") { - t.Errorf("Expected error about querying PVCs, got %v", err) + // And envVars should not be nil (should contain basic kube config vars) + if envVars == nil { + t.Error("Expected non-nil envVars with basic kube config") } - // And envVars should be nil - if envVars != nil { - t.Errorf("Expected nil envVars, got %v", envVars) + // And basic kubernetes environment variables should be present + if _, exists := envVars["KUBECONFIG"]; !exists { + t.Error("Expected KUBECONFIG to be present in envVars") + } + if _, exists := envVars["KUBE_CONFIG_PATH"]; !exists { + t.Error("Expected KUBE_CONFIG_PATH to be present in envVars") } })