This is a huge commit, it does following things:
1. refactor kubesphere dependency service client creation, we can disable dependency by config 2. dependencies can be configured by configuration file 3. refactor cmd package using cobra.Command, so we can use hypersphere to invoke command sepearately. Later we only need to build one image to contains all kubesphere core components. One command to rule them all! 4. live reloading configuration currently not implemented
This commit is contained in:
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
var (
|
||||
kubeConfigFile string
|
||||
k8sClient *kubernetes.Clientset
|
||||
k8sClientOnce sync.Once
|
||||
KubeConfig *rest.Config
|
||||
MasterURL string
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&kubeConfigFile, "kubeconfig-path", "", "path to kubeconfig file")
|
||||
flag.StringVar(&MasterURL, "master-url", "", "kube-apiserver url, only needed when out of cluster")
|
||||
}
|
||||
|
||||
func Client() *kubernetes.Clientset {
|
||||
|
||||
k8sClientOnce.Do(func() {
|
||||
|
||||
config, err := Config()
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
k8sClient = kubernetes.NewForConfigOrDie(config)
|
||||
|
||||
KubeConfig = config
|
||||
})
|
||||
|
||||
return k8sClient
|
||||
}
|
||||
|
||||
func Config() (kubeConfig *rest.Config, err error) {
|
||||
|
||||
if _, err = os.Stat(kubeConfigFile); err == nil {
|
||||
kubeConfig, err = clientcmd.BuildConfigFromFlags(MasterURL, kubeConfigFile)
|
||||
} else {
|
||||
kubeConfig, err = rest.InClusterConfig()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
kubeConfig.QPS = 1e6
|
||||
kubeConfig.Burst = 1e6
|
||||
|
||||
return kubeConfig, nil
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
ks "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
|
||||
)
|
||||
|
||||
var (
|
||||
ksClient *ks.Clientset
|
||||
ksClientOnce sync.Once
|
||||
)
|
||||
|
||||
func KsClient() *ks.Clientset {
|
||||
|
||||
ksClientOnce.Do(func() {
|
||||
|
||||
config, err := Config()
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
ksClient = ks.NewForConfigOrDie(config)
|
||||
})
|
||||
|
||||
return ksClient
|
||||
}
|
||||
100
pkg/simple/client/k8s/kubernetes.go
Normal file
100
pkg/simple/client/k8s/kubernetes.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
s2i "github.com/kubesphere/s2ioperator/pkg/client/clientset/versioned"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
|
||||
)
|
||||
|
||||
type KubernetesClient struct {
|
||||
// kubernetes client interface
|
||||
k8s *kubernetes.Clientset
|
||||
|
||||
// generated clientset
|
||||
ks *kubesphere.Clientset
|
||||
|
||||
s2i *s2i.Clientset
|
||||
|
||||
master string
|
||||
|
||||
config *rest.Config
|
||||
}
|
||||
|
||||
// NewKubernetesClient
|
||||
func NewKubernetesClientOrDie(options *KubernetesOptions) *KubernetesClient {
|
||||
config, err := clientcmd.BuildConfigFromFlags("", options.KubeConfig)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
config.QPS = options.QPS
|
||||
config.Burst = options.Burst
|
||||
|
||||
k := &KubernetesClient{
|
||||
k8s: kubernetes.NewForConfigOrDie(config),
|
||||
ks: kubesphere.NewForConfigOrDie(config),
|
||||
s2i: s2i.NewForConfigOrDie(config),
|
||||
master: config.Host,
|
||||
config: config,
|
||||
}
|
||||
|
||||
if options.Master != "" {
|
||||
k.master = options.Master
|
||||
}
|
||||
|
||||
return k
|
||||
}
|
||||
|
||||
func NewKubernetesClient(options *KubernetesOptions) (*KubernetesClient, error) {
|
||||
config, err := clientcmd.BuildConfigFromFlags("", options.KubeConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.QPS = options.QPS
|
||||
config.Burst = options.Burst
|
||||
|
||||
var k KubernetesClient
|
||||
k.k8s, err = kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
k.ks, err = kubesphere.NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
k.s2i, err = s2i.NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
k.master = options.Master
|
||||
k.config = config
|
||||
|
||||
return &k, nil
|
||||
}
|
||||
|
||||
func (k *KubernetesClient) Kubernetes() kubernetes.Interface {
|
||||
return k.k8s
|
||||
}
|
||||
|
||||
func (k *KubernetesClient) KubeSphere() kubesphere.Interface {
|
||||
return k.ks
|
||||
}
|
||||
|
||||
func (k *KubernetesClient) S2i() s2i.Interface {
|
||||
return k.s2i
|
||||
}
|
||||
|
||||
// master address used to generate kubeconfig for downloading
|
||||
func (k *KubernetesClient) Master() string {
|
||||
return k.master
|
||||
}
|
||||
|
||||
func (k *KubernetesClient) Config() *rest.Config {
|
||||
return k.config
|
||||
}
|
||||
59
pkg/simple/client/k8s/options.go
Normal file
59
pkg/simple/client/k8s/options.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"github.com/spf13/pflag"
|
||||
"kubesphere.io/kubesphere/pkg/utils/reflectutils"
|
||||
"os"
|
||||
)
|
||||
|
||||
type KubernetesOptions 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,omitemtpy" yaml:"qps,omitempty"`
|
||||
|
||||
// kubernetes clientset burst
|
||||
// +optional
|
||||
Burst int `json:"burst,omitempty" yaml:"burst,omitempty"`
|
||||
}
|
||||
|
||||
// NewKubernetesOptions returns a `zero` instance
|
||||
func NewKubernetesOptions() *KubernetesOptions {
|
||||
return &KubernetesOptions{
|
||||
KubeConfig: "",
|
||||
QPS: 1e6,
|
||||
Burst: 1e6,
|
||||
}
|
||||
}
|
||||
|
||||
func (k *KubernetesOptions) Validate() []error {
|
||||
errors := []error{}
|
||||
|
||||
if k.KubeConfig != "" {
|
||||
if _, err := os.Stat(k.KubeConfig); err != nil {
|
||||
errors = append(errors, err)
|
||||
}
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
func (k *KubernetesOptions) ApplyTo(options *KubernetesOptions) {
|
||||
reflectutils.Override(options, k)
|
||||
}
|
||||
|
||||
func (k *KubernetesOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
fs.StringVar(&k.KubeConfig, "kubeconfig", k.KubeConfig, ""+
|
||||
"Path for kubernetes kubeconfig file, if left blank, will use "+
|
||||
"in cluster way.")
|
||||
|
||||
fs.StringVar(&k.Master, "master", k.Master, ""+
|
||||
"Used to generate kubeconfig for downloading, if not specified, will use host in kubeconfig.")
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
s2i "github.com/kubesphere/s2ioperator/pkg/client/clientset/versioned"
|
||||
)
|
||||
|
||||
var (
|
||||
s2iClient *s2i.Clientset
|
||||
s2iClientOnce sync.Once
|
||||
)
|
||||
|
||||
func S2iClient() *s2i.Clientset {
|
||||
|
||||
s2iClientOnce.Do(func() {
|
||||
|
||||
config, err := Config()
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
s2iClient = s2i.NewForConfigOrDie(config)
|
||||
})
|
||||
|
||||
return s2iClient
|
||||
}
|
||||
Reference in New Issue
Block a user