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@441773e

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
+22-1Lines changed: 22 additions & 1 deletion
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,7 +61,27 @@ func validateKubeconfigFile() error {
6061
return nil
6162
}
6263

63-
if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) {
64+
paths := filepath.SplitList(kubeconfigPath)
65+
foundExisting := false
66+
missingCount := 0
67+
for _, path := range paths {
68+
if path == "" {
69+
continue
70+
}
71+
if _, err := os.Stat(path); err == nil {
72+
foundExisting = true
73+
continue
74+
} else if os.IsNotExist(err) {
75+
missingCount++
76+
continue
77+
}
78+
return fmt.Errorf("%w: kubeconfig file is not accessible at path: %s", fn.ErrInvalidKubeconfig, path)
79+
}
80+
81+
if foundExisting {
82+
return nil
83+
}
84+
if missingCount > 0 || len(paths) > 0 {
6485
return fmt.Errorf("%w: kubeconfig file does not exist at path: %s", fn.ErrInvalidKubeconfig, kubeconfigPath)
6586
}
6687

Collapse file

‎pkg/knative/client_test.go‎

Copy file name to clipboard
+74Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 missing",
35+
kubeconfig: missingPath,
36+
wantErr: true,
37+
wantErrText: "kubeconfig file does not exist at path",
38+
},
39+
{
40+
name: "multiple with existing",
41+
kubeconfig: missingPath + listSep + existingPath,
42+
wantErr: false,
43+
},
44+
{
45+
name: "multiple all missing",
46+
kubeconfig: missingPath + listSep + filepath.Join(testDir, "missing2"),
47+
wantErr: true,
48+
wantErrText: "kubeconfig file does not exist at path",
49+
},
50+
}
51+
52+
for _, tc := range cases {
53+
t.Run(tc.name, func(t *testing.T) {
54+
t.Setenv("KUBECONFIG", tc.kubeconfig)
55+
56+
err := validateKubeconfigFile()
57+
if tc.wantErr {
58+
if err == nil {
59+
t.Fatalf("expected error")
60+
}
61+
if !errors.Is(err, fn.ErrInvalidKubeconfig) {
62+
t.Fatalf("expected ErrInvalidKubeconfig, got: %v", err)
63+
}
64+
if tc.wantErrText != "" && !strings.Contains(err.Error(), tc.wantErrText) {
65+
t.Fatalf("expected error to contain %q, got: %v", tc.wantErrText, err)
66+
}
67+
return
68+
}
69+
if err != nil {
70+
t.Fatalf("unexpected error: %v", err)
71+
}
72+
})
73+
}
74+
}

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.