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

import (
Z
zhangmin 已提交
4
	snapshotclient "github.com/kubernetes-csi/external-snapshotter/v2/pkg/client/clientset/versioned"
5
	applicationclientset "github.com/kubernetes-sigs/application/pkg/client/clientset/versioned"
6
	istioclient "istio.io/client-go/pkg/clientset/versioned"
7
	"k8s.io/client-go/discovery"
8 9 10 11
	"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 已提交
12
	"strings"
13 14
)

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

type kubernetesClient struct {
27
	// kubernetes client interface
28
	k8s kubernetes.Interface
29

30 31 32
	// discovery client
	discoveryClient *discovery.DiscoveryClient

33
	// generated clientset
34
	ks kubesphere.Interface
35

36
	application applicationclientset.Interface
37

38
	istio istioclient.Interface
39

Z
zhangmin 已提交
40 41
	snapshot snapshotclient.Interface

42 43 44 45 46
	master string

	config *rest.Config
}

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

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

Z
zryfish 已提交
57
	k := &kubernetesClient{
58 59 60
		k8s:             kubernetes.NewForConfigOrDie(config),
		discoveryClient: discovery.NewDiscoveryClientForConfigOrDie(config),
		ks:              kubesphere.NewForConfigOrDie(config),
61
		istio:           istioclient.NewForConfigOrDie(config),
62
		application:     applicationclientset.NewForConfigOrDie(config),
Z
zhangmin 已提交
63
		snapshot:        snapshotclient.NewForConfigOrDie(config),
64 65
		master:          config.Host,
		config:          config,
66 67 68 69 70
	}

	if options.Master != "" {
		k.master = options.Master
	}
R
runzexia 已提交
71 72 73 74 75 76
	// 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
	}
77 78 79
	return k
}

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

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

Z
zryfish 已提交
90
	var k kubernetesClient
91 92 93 94 95 96 97 98 99 100
	k.k8s, err = kubernetes.NewForConfig(config)
	if err != nil {
		return nil, err
	}

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

101 102 103 104 105
	k.application, err = applicationclientset.NewForConfig(config)
	if err != nil {
		return nil, err
	}

H
hongming 已提交
106 107 108 109 110 111
	k.istio, err = istioclient.NewForConfig(config)

	if err != nil {
		return nil, err
	}

Z
zhangmin 已提交
112 113 114 115 116
	k.snapshot, err = snapshotclient.NewForConfig(config)
	if err != nil {
		return nil, err
	}

117 118 119 120 121 122
	k.master = options.Master
	k.config = config

	return &k, nil
}

Z
zryfish 已提交
123
func (k *kubernetesClient) Kubernetes() kubernetes.Interface {
124 125 126
	return k.k8s
}

Z
zryfish 已提交
127
func (k *kubernetesClient) Discovery() discovery.DiscoveryInterface {
128 129 130
	return k.discoveryClient
}

Z
zryfish 已提交
131
func (k *kubernetesClient) KubeSphere() kubesphere.Interface {
132 133 134
	return k.ks
}

Z
zryfish 已提交
135
func (k *kubernetesClient) Application() applicationclientset.Interface {
136 137 138
	return k.application
}

139 140 141 142
func (k *kubernetesClient) Istio() istioclient.Interface {
	return k.istio
}

Z
zhangmin 已提交
143 144 145 146
func (k *kubernetesClient) Snapshot() snapshotclient.Interface {
	return k.snapshot
}

147
// master address used to generate kubeconfig for downloading
Z
zryfish 已提交
148
func (k *kubernetesClient) Master() string {
149 150 151
	return k.master
}

Z
zryfish 已提交
152
func (k *kubernetesClient) Config() *rest.Config {
153 154
	return k.config
}