Skip to content

Commit 8d5a7cc

Browse files
committed
enhance error message on no current context
1 parent 6b75d0a commit 8d5a7cc

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

cmd/api-syncagent/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
golog "log"
2424
"slices"
25-
"strings"
2625

2726
"github.com/go-logr/zapr"
2827
"github.com/spf13/pflag"
@@ -32,6 +31,7 @@ import (
3231
"github.com/kcp-dev/api-syncagent/internal/controller/apiexport"
3332
"github.com/kcp-dev/api-syncagent/internal/controller/apiresourceschema"
3433
"github.com/kcp-dev/api-syncagent/internal/controller/syncmanager"
34+
"github.com/kcp-dev/api-syncagent/internal/kubeconfig"
3535
syncagentlog "github.com/kcp-dev/api-syncagent/internal/log"
3636
"github.com/kcp-dev/api-syncagent/internal/version"
3737
syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
@@ -108,8 +108,8 @@ func run(ctx context.Context, log *zap.SugaredLogger, opts *Options) error {
108108
}
109109

110110
// sanity check
111-
if !strings.Contains(kcpRestConfig.Host, "/clusters/") {
112-
return fmt.Errorf("kcp kubeconfig does not point to a specific workspace")
111+
if err := kubeconfig.Validate(kcpRestConfig); err != nil {
112+
return fmt.Errorf("failed to check kcp kubeconfig: %w", err)
113113
}
114114

115115
// We check if the APIExport/APIExportEndpointSlice exists and extract information we need to set up our kcpCluster.

internal/kubeconfig/check.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2025 The KCP Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kubeconfig
18+
19+
import (
20+
"errors"
21+
"strings"
22+
23+
"k8s.io/client-go/rest"
24+
)
25+
26+
func Validate(c *rest.Config) error {
27+
if c == nil {
28+
return errors.New("no kubeconfig was found. Have you set current-context?")
29+
}
30+
31+
if !strings.Contains(c.Host, "/clusters/") {
32+
return errors.New("kcp kubeconfig does not point to a specific workspace")
33+
}
34+
35+
return nil
36+
}

internal/kubeconfig/check_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2025 The KCP Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kubeconfig
18+
19+
import (
20+
"testing"
21+
22+
"k8s.io/client-go/rest"
23+
)
24+
25+
func TestValidate(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
kubeconfig *rest.Config
29+
expErr bool
30+
}{
31+
{
32+
name: "nil kubeconfig",
33+
kubeconfig: nil,
34+
expErr: true,
35+
},
36+
{
37+
name: "kubeconfig without workspace",
38+
kubeconfig: &rest.Config{
39+
Host: "https://example.com",
40+
},
41+
expErr: true,
42+
},
43+
{
44+
name: "valid kcp kubeconfig",
45+
kubeconfig: &rest.Config{
46+
Host: "https://example.com/clusters/foo",
47+
},
48+
expErr: false,
49+
},
50+
}
51+
52+
for _, testcase := range tests {
53+
t.Run(testcase.name, func(t *testing.T) {
54+
err := Validate(testcase.kubeconfig)
55+
if (err != nil) != testcase.expErr {
56+
t.Errorf("Expected error %v, got %v", testcase.expErr, err)
57+
}
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)