-
Notifications
You must be signed in to change notification settings - Fork 474
/
client.go
162 lines (140 loc) · 4.78 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package kubernetes
import (
"context"
"encoding/base64"
"errors"
"fmt"
"net"
"os"
"strings"
istio "istio.io/client-go/pkg/clientset/versioned"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"
kube "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd/api"
kialiConfig "github.com/kiali/kiali/config"
"github.com/kiali/kiali/log"
)
const RemoteSecretData = "/kiali-remote-secret/kiali"
var (
emptyGetOptions = meta_v1.GetOptions{}
emptyListOptions = meta_v1.ListOptions{}
)
type PodLogs struct {
Logs string `json:"logs,omitempty"`
}
// ClientInterface for mocks (only mocked function are necessary here)
type ClientInterface interface {
GetServerVersion() (*version.Info, error)
GetToken() string
GetAuthInfo() *api.AuthInfo
IsOpenShift() bool
K8SClientInterface
IstioClientInterface
OSClientInterface
}
// K8SClient is the client struct for Kubernetes and Istio APIs
// It hides the way it queries each API
type K8SClient struct {
ClientInterface
token string
k8s *kube.Clientset
istioClientset *istio.Clientset
// Used in REST queries after bump to client-go v0.20.x
ctx context.Context
// isOpenShift private variable will check if kiali is deployed under an OpenShift cluster or not
// It is represented as a pointer to include the initialization phase.
// See kubernetes_service.go#IsOpenShift() for more details.
isOpenShift *bool
}
// GetK8sApi returns the clientset referencing all K8s rest clients
func (client *K8SClient) GetK8sApi() *kube.Clientset {
return client.k8s
}
// GetToken returns the BearerToken used from the config
func (client *K8SClient) GetToken() string {
return client.token
}
// Point the k8s client to a remote cluster's API server
func UseRemoteCreds(remoteSecret *RemoteSecret) (*rest.Config, error) {
caData := remoteSecret.Clusters[0].Cluster.CertificateAuthorityData
rootCaDecoded, err := base64.StdEncoding.DecodeString(caData)
if err != nil {
return nil, err
}
// Basically implement rest.InClusterConfig() with the remote creds
tlsClientConfig := rest.TLSClientConfig{
CAData: []byte(rootCaDecoded),
}
serverParse := strings.Split(remoteSecret.Clusters[0].Cluster.Server, ":")
if len(serverParse) != 3 && len(serverParse) != 2 {
return nil, errors.New("Invalid remote API server URL")
}
host := strings.TrimPrefix(serverParse[1], "//")
port := "443"
if len(serverParse) == 3 {
port = serverParse[2]
}
if !strings.EqualFold(serverParse[0], "https") {
return nil, errors.New("Only HTTPS protocol is allowed in remote API server URL")
}
// There's no need to add the BearerToken because it's ignored later on
return &rest.Config{
Host: "https://" + net.JoinHostPort(host, port),
TLSClientConfig: tlsClientConfig,
}, nil
}
// ConfigClient return a client with the correct configuration
// Returns configuration if Kiali is in Cluster when InCluster is true
// Returns configuration if Kiali is not int Cluster when InCluster is false
// It returns an error on any problem
func ConfigClient() (*rest.Config, error) {
if kialiConfig.Get().InCluster {
var incluster *rest.Config
var err error
if remoteSecret, readErr := GetRemoteSecret(RemoteSecretData); readErr == nil {
incluster, err = UseRemoteCreds(remoteSecret)
} else {
incluster, err = rest.InClusterConfig()
}
if err != nil {
return nil, err
}
incluster.QPS = kialiConfig.Get().KubernetesConfig.QPS
incluster.Burst = kialiConfig.Get().KubernetesConfig.Burst
return incluster, nil
}
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
if len(host) == 0 || len(port) == 0 {
return nil, fmt.Errorf("unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined")
}
return &rest.Config{
// TODO: switch to using cluster DNS.
Host: "http://" + net.JoinHostPort(host, port),
QPS: kialiConfig.Get().KubernetesConfig.QPS,
Burst: kialiConfig.Get().KubernetesConfig.Burst,
}, nil
}
// NewClientFromConfig creates a new client to the Kubernetes and Istio APIs.
// It takes the assumption that Istio is deployed into the cluster.
// It hides the access to Kubernetes/Openshift credentials.
// It hides the low level use of the API of Kubernetes and Istio, it should be considered as an implementation detail.
// It returns an error on any problem.
func NewClientFromConfig(config *rest.Config) (*K8SClient, error) {
client := K8SClient{
token: config.BearerToken,
}
log.Debugf("Rest perf config QPS: %f Burst: %d", config.QPS, config.Burst)
k8s, err := kube.NewForConfig(config)
if err != nil {
return nil, err
}
client.k8s = k8s
client.istioClientset, err = istio.NewForConfig(config)
if err != nil {
return nil, err
}
client.ctx = context.Background()
return &client, nil
}