kubernetes.go 3.3 KB
Newer Older
1 2 3
package k8s

import (
4
	applicationclientset "github.com/kubernetes-sigs/application/pkg/client/clientset/versioned"
5
	istioclient "istio.io/client-go/pkg/clientset/versioned"
6
	"k8s.io/client-go/discovery"
7 8 9 10
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
	kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
R
runzexia 已提交
11
	"strings"
12 13
)

Z
zryfish 已提交
14 15 16
type Client interface {
	Kubernetes() kubernetes.Interface
	KubeSphere() kubesphere.Interface
17
	Istio() istioclient.Interface
Z
zryfish 已提交
18 19 20 21 22 23 24
	Application() applicationclientset.Interface
	Discovery() discovery.DiscoveryInterface
	Master() string
	Config() *rest.Config
}

type kubernetesClient struct {
25
	// kubernetes client interface
26
	k8s kubernetes.Interface
27

28 29 30
	// discovery client
	discoveryClient *discovery.DiscoveryClient

31
	// generated clientset
32
	ks kubesphere.Interface
33

34
	application applicationclientset.Interface
35

36
	istio istioclient.Interface
37

38 39 40 41 42
	master string

	config *rest.Config
}

J
Jeff 已提交
43
// NewKubernetesClientOrDie creates KubernetesClient and panic if there is an error
Z
zryfish 已提交
44
func NewKubernetesClientOrDie(options *KubernetesOptions) Client {
45 46 47 48 49 50 51 52
	config, err := clientcmd.BuildConfigFromFlags("", options.KubeConfig)
	if err != nil {
		panic(err)
	}

	config.QPS = options.QPS
	config.Burst = options.Burst

Z
zryfish 已提交
53
	k := &kubernetesClient{
54 55 56
		k8s:             kubernetes.NewForConfigOrDie(config),
		discoveryClient: discovery.NewDiscoveryClientForConfigOrDie(config),
		ks:              kubesphere.NewForConfigOrDie(config),
57
		istio:           istioclient.NewForConfigOrDie(config),
58
		application:     applicationclientset.NewForConfigOrDie(config),
59 60
		master:          config.Host,
		config:          config,
61 62 63 64 65
	}

	if options.Master != "" {
		k.master = options.Master
	}
R
runzexia 已提交
66 67 68 69 70 71
	// The https prefix is automatically added when using sa.
	// But it will not be set automatically when reading from kubeconfig
	// which may cause some problems in the client of other languages.
	if !strings.HasPrefix(k.master, "http://") && !strings.HasPrefix(k.master, "https://") {
		k.master = "https://" + k.master
	}
72 73 74
	return k
}

J
Jeff 已提交
75
// NewKubernetesClient creates a KubernetesClient
Z
zryfish 已提交
76
func NewKubernetesClient(options *KubernetesOptions) (Client, error) {
77 78 79 80 81 82 83 84
	config, err := clientcmd.BuildConfigFromFlags("", options.KubeConfig)
	if err != nil {
		return nil, err
	}

	config.QPS = options.QPS
	config.Burst = options.Burst

Z
zryfish 已提交
85
	var k kubernetesClient
86 87 88 89 90 91 92 93 94 95
	k.k8s, err = kubernetes.NewForConfig(config)
	if err != nil {
		return nil, err
	}

	k.ks, err = kubesphere.NewForConfig(config)
	if err != nil {
		return nil, err
	}

96 97 98 99 100
	k.application, err = applicationclientset.NewForConfig(config)
	if err != nil {
		return nil, err
	}

101 102 103 104 105 106
	k.master = options.Master
	k.config = config

	return &k, nil
}

Z
zryfish 已提交
107
func (k *kubernetesClient) Kubernetes() kubernetes.Interface {
108 109 110
	return k.k8s
}

Z
zryfish 已提交
111
func (k *kubernetesClient) Discovery() discovery.DiscoveryInterface {
112 113 114
	return k.discoveryClient
}

Z
zryfish 已提交
115
func (k *kubernetesClient) KubeSphere() kubesphere.Interface {
116 117 118
	return k.ks
}

Z
zryfish 已提交
119
func (k *kubernetesClient) Application() applicationclientset.Interface {
120 121 122
	return k.application
}

123 124 125 126
func (k *kubernetesClient) Istio() istioclient.Interface {
	return k.istio
}

127
// master address used to generate kubeconfig for downloading
Z
zryfish 已提交
128
func (k *kubernetesClient) Master() string {
129 130 131
	return k.master
}

Z
zryfish 已提交
132
func (k *kubernetesClient) Config() *rest.Config {
133 134
	return k.config
}