GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(knative): handle multi-path KUBECONFIG entries · knative/func@245fbf2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pkg/knative
    • client.go
    • client_test.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pkg/knative/client.go‎

Copy file name to clipboardExpand all lines: pkg/knative/client.go
+13-3Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package knative
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67

78
clienteventingv1 "knative.dev/client/pkg/eventing/v1"
89
clientservingv1 "knative.dev/client/pkg/serving/v1"
@@ -60,9 +61,18 @@ func validateKubeconfigFile() error {
6061
return nil
6162
}
6263

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+
}
6575
}
6676

67-
return nil
77+
return fmt.Errorf("%w: kubeconfig file does not exist at path: %s", fn.ErrInvalidKubeconfig, kubeconfigPath)
6878
}
Collapse file

‎pkg/knative/client_test.go‎

Copy file name to clipboard
+79Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.