Skip to content

Commit a86ff98

Browse files
authored
feat(kubernetes): validate kubeconfig (#2426)
1 parent 4fe6332 commit a86ff98

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

cmd/installer/cli/install.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
"github.com/spf13/pflag"
5252
helmcli "helm.sh/helm/v3/pkg/cli"
5353
"k8s.io/cli-runtime/pkg/genericclioptions"
54+
"k8s.io/client-go/discovery"
5455
"k8s.io/client-go/metadata"
5556
"sigs.k8s.io/controller-runtime/pkg/client"
5657
)
@@ -529,10 +530,14 @@ func preRunInstallKubernetes(_ *cobra.Command, flags *InstallCmdFlags, _ kuberne
529530
return fmt.Errorf("failed to discover kubeconfig: %w", err)
530531
}
531532

532-
// If this is the default host, there was probably no kubeconfig discovered.
533-
// HACK: This is fragile but it is the best thing I could come up with
534-
if flags.kubernetesEnvSettings.KubeConfig == "" && restConfig.Host == "http://localhost:8080" {
535-
return fmt.Errorf("a kubeconfig is required when using kubernetes")
533+
// Check that we have a valid kubeconfig
534+
discoveryClient, err := discovery.NewDiscoveryClientForConfig(restConfig)
535+
if err != nil {
536+
return fmt.Errorf("failed to create discovery client: %w", err)
537+
}
538+
_, err = discoveryClient.ServerVersion()
539+
if err != nil {
540+
return fmt.Errorf("failed to connect to kubernetes api server: %w", err)
536541
}
537542

538543
flags.installConfig.kubernetesRESTClientGetterFactory = func(namespace string) genericclioptions.RESTClientGetter {

0 commit comments

Comments
 (0)