GeezerVison is showing github.com — fix(knative): handle multi-path KUBECONFIG entries · knative/func@245fbf2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pkg/knative/client.go +13-3Lines changed: 13 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ package knative | 3 | 3 | import ( | 4 | 4 | "fmt" | 5 | 5 | "os" | | 6 | + "path/filepath" | 6 | 7 |
| 7 | 8 | clienteventingv1 "knative.dev/client/pkg/eventing/v1" | 8 | 9 | clientservingv1 "knative.dev/client/pkg/serving/v1" | @@ -60,9 +61,18 @@ func validateKubeconfigFile() error { | 60 | 61 | return nil | 61 | 62 | } | 62 | 63 |
| 63 | | - if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) { | 64 | | - return fmt.Errorf("%w: kubeconfig file does not exist at path: %s", fn.ErrInvalidKubeconfig, kubeconfigPath) | | 64 | + for _, path := range filepath.SplitList(kubeconfigPath) { | | 65 | + if path == "" { | | 66 | + continue | | 67 | + } | | 68 | + _, statErr := os.Stat(path) | | 69 | + if statErr == nil { | | 70 | + return nil | | 71 | + } | | 72 | + if !os.IsNotExist(statErr) { | | 73 | + return fmt.Errorf("%w: kubeconfig file is not accessible at path: %s", fn.ErrInvalidKubeconfig, path) | | 74 | + } | 65 | 75 | } | 66 | 76 |
| 67 | | - return nil | | 77 | + return fmt.Errorf("%w: kubeconfig file does not exist at path: %s", fn.ErrInvalidKubeconfig, kubeconfigPath) | 68 | 78 | } |
Collapse file Copy file name to clipboard +79Lines changed: 79 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package knative | | 2 | + | | 3 | +import ( | | 4 | + "errors" | | 5 | + "os" | | 6 | + "path/filepath" | | 7 | + "strings" | | 8 | + "testing" | | 9 | + | | 10 | + fn "knative.dev/func/pkg/functions" | | 11 | +) | | 12 | + | | 13 | +func TestValidateKubeconfigFile(t *testing.T) { | | 14 | + testDir := t.TempDir() | | 15 | + existingPath := filepath.Join(testDir, "kubeconfig") | | 16 | + if err := os.WriteFile(existingPath, []byte("apiVersion: v1\n"), 0o600); err != nil { | | 17 | + t.Fatalf("write kubeconfig: %v", err) | | 18 | + } | | 19 | + missingPath := filepath.Join(testDir, "missing") | | 20 | + listSep := string(filepath.ListSeparator) | | 21 | + | | 22 | + cases := []struct { | | 23 | + name string | | 24 | + kubeconfig string | | 25 | + wantErr bool | | 26 | + wantErrText string | | 27 | + }{ | | 28 | + { | | 29 | + name: "empty env", | | 30 | + kubeconfig: "", | | 31 | + wantErr: false, | | 32 | + }, | | 33 | + { | | 34 | + name: "single existing", | | 35 | + kubeconfig: existingPath, | | 36 | + wantErr: false, | | 37 | + }, | | 38 | + { | | 39 | + name: "single missing", | | 40 | + kubeconfig: missingPath, | | 41 | + wantErr: true, | | 42 | + wantErrText: "kubeconfig file does not exist at path", | | 43 | + }, | | 44 | + { | | 45 | + name: "multiple with existing", | | 46 | + kubeconfig: missingPath + listSep + existingPath, | | 47 | + wantErr: false, | | 48 | + }, | | 49 | + { | | 50 | + name: "multiple all missing", | | 51 | + kubeconfig: missingPath + listSep + filepath.Join(testDir, "missing2"), | | 52 | + wantErr: true, | | 53 | + wantErrText: "kubeconfig file does not exist at path", | | 54 | + }, | | 55 | + } | | 56 | + | | 57 | + for _, tc := range cases { | | 58 | + t.Run(tc.name, func(t *testing.T) { | | 59 | + t.Setenv("KUBECONFIG", tc.kubeconfig) | | 60 | + | | 61 | + err := validateKubeconfigFile() | | 62 | + if tc.wantErr { | | 63 | + if err == nil { | | 64 | + t.Fatalf("expected error") | | 65 | + } | | 66 | + if !errors.Is(err, fn.ErrInvalidKubeconfig) { | | 67 | + t.Fatalf("expected ErrInvalidKubeconfig, got: %v", err) | | 68 | + } | | 69 | + if tc.wantErrText != "" && !strings.Contains(err.Error(), tc.wantErrText) { | | 70 | + t.Fatalf("expected error to contain %q, got: %v", tc.wantErrText, err) | | 71 | + } | | 72 | + return | | 73 | + } | | 74 | + if err != nil { | | 75 | + t.Fatalf("unexpected error: %v", err) | | 76 | + } | | 77 | + }) | | 78 | + } | | 79 | +} |
|