From 90f5a449112586f464ad94280a0ff6422df424ad Mon Sep 17 00:00:00 2001 From: rick Date: Thu, 10 Jun 2021 21:01:38 +0800 Subject: [PATCH 1/2] Make ks-apiserver be easier to run locally with kube config Signed-off-by: rick --- pkg/simple/client/k8s/options.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/simple/client/k8s/options.go b/pkg/simple/client/k8s/options.go index c72699f82..642e5284d 100644 --- a/pkg/simple/client/k8s/options.go +++ b/pkg/simple/client/k8s/options.go @@ -18,6 +18,9 @@ package k8s import ( "os" + "path" + + "k8s.io/client-go/util/homedir" "github.com/spf13/pflag" @@ -44,12 +47,18 @@ type KubernetesOptions struct { } // NewKubernetesOptions returns a `zero` instance -func NewKubernetesOptions() *KubernetesOptions { - return &KubernetesOptions{ - KubeConfig: "", - QPS: 1e6, - Burst: 1e6, +func NewKubernetesOptions() (option *KubernetesOptions) { + option = &KubernetesOptions{ + QPS: 1e6, + Burst: 1e6, } + + // make it be easier for those who wants to run api-server locally + userHomeConfig := path.Join(homedir.HomeDir(), ".kube/config") + if _, err := os.Stat(userHomeConfig); !os.IsNotExist(err) { + option.KubeConfig = userHomeConfig + } + return } func (k *KubernetesOptions) Validate() []error { From 8e40702b75a1d316c1d1e465aaea173400cd704e Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 18 Jun 2021 11:28:10 +0800 Subject: [PATCH 2/2] try os/user.HomeDir when $HOME is unset when find kubeconfig file Signed-off-by: rick --- pkg/models/openpitrix/attachments.go | 1 + pkg/simple/client/k8s/options.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/models/openpitrix/attachments.go b/pkg/models/openpitrix/attachments.go index c9a0ef55d..ac2ac2018 100644 --- a/pkg/models/openpitrix/attachments.go +++ b/pkg/models/openpitrix/attachments.go @@ -15,6 +15,7 @@ package openpitrix import ( "bytes" + "github.com/go-openapi/strfmt" "k8s.io/klog" diff --git a/pkg/simple/client/k8s/options.go b/pkg/simple/client/k8s/options.go index 642e5284d..f4d97bd43 100644 --- a/pkg/simple/client/k8s/options.go +++ b/pkg/simple/client/k8s/options.go @@ -18,6 +18,7 @@ package k8s import ( "os" + "os/user" "path" "k8s.io/client-go/util/homedir" @@ -54,8 +55,16 @@ func NewKubernetesOptions() (option *KubernetesOptions) { } // make it be easier for those who wants to run api-server locally - userHomeConfig := path.Join(homedir.HomeDir(), ".kube/config") - if _, err := os.Stat(userHomeConfig); !os.IsNotExist(err) { + homePath := homedir.HomeDir() + if homePath == "" { + // try os/user.HomeDir when $HOME is unset. + if u, err := user.Current(); err == nil { + homePath = u.HomeDir + } + } + + userHomeConfig := path.Join(homePath, ".kube/config") + if _, err := os.Stat(userHomeConfig); err == nil { option.KubeConfig = userHomeConfig } return