79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
/*
|
|
* Copyright 2024 the KubeSphere Authors.
|
|
* Please refer to the LICENSE file in the root directory of the project.
|
|
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
|
|
*/
|
|
|
|
package k8s
|
|
|
|
import (
|
|
"os"
|
|
"os/user"
|
|
"path"
|
|
|
|
"github.com/spf13/pflag"
|
|
"k8s.io/client-go/util/homedir"
|
|
)
|
|
|
|
type Options struct {
|
|
// kubeconfig path, if not specified, will use
|
|
// in cluster way to create clientset
|
|
KubeConfig string `json:"kubeconfig" yaml:"kubeconfig"`
|
|
|
|
// kubernetes apiserver public address, used to generate kubeconfig
|
|
// for downloading, default to host defined in kubeconfig
|
|
// +optional
|
|
Master string `json:"master,omitempty" yaml:"master,omitempty"`
|
|
|
|
// kubernetes clientset qps
|
|
// +optional
|
|
QPS float32 `json:"qps,omitempty" yaml:"qps,omitempty"`
|
|
|
|
// kubernetes clientset burst
|
|
// +optional
|
|
Burst int `json:"burst,omitempty" yaml:"burst,omitempty"`
|
|
}
|
|
|
|
// NewKubernetesOptions returns a `zero` instance
|
|
func NewKubernetesOptions() (option *Options) {
|
|
option = &Options{
|
|
QPS: 1e6,
|
|
Burst: 1e6,
|
|
}
|
|
|
|
// make it be easier for those who wants to run api-server locally
|
|
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
|
|
}
|
|
|
|
func (k *Options) Validate() []error {
|
|
var errors []error
|
|
|
|
if k.KubeConfig != "" {
|
|
if _, err := os.Stat(k.KubeConfig); err != nil {
|
|
errors = append(errors, err)
|
|
}
|
|
}
|
|
return errors
|
|
}
|
|
|
|
func (k *Options) AddFlags(fs *pflag.FlagSet, c *Options) {
|
|
fs.StringVar(&k.KubeConfig, "kubeconfig", c.KubeConfig, ""+
|
|
"Path for kubernetes kubeconfig file, if left blank, will use "+
|
|
"in cluster way.")
|
|
|
|
fs.StringVar(&k.Master, "master", c.Master, ""+
|
|
"Used to generate kubeconfig for downloading, if not specified, will use host in kubeconfig.")
|
|
}
|