forked from kiali/kiali
/
client.go
191 lines (166 loc) · 5.9 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package kubernetes
import (
"fmt"
"net"
"os"
"strings"
"k8s.io/api/apps/v1beta1"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
kube "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
kialiConfig "github.com/kiali/kiali/config"
)
const (
// These constants are tweaks to the k8s client I think once are set up they won't change so no need to put them on the config
// Default QPS and Burst are quite low and those are not designed for a backend that should perform several
// queries to build an inventory of entities from a k8s backend.
// Other k8s clients have increased these values to a similar values.
k8sQPS = 100
k8sBurst = 200
)
var (
emptyListOptions = meta_v1.ListOptions{}
emptyGetOptions = meta_v1.GetOptions{}
)
// IstioClientInterface for mocks (only mocked function are necessary here)
type IstioClientInterface interface {
GetService(namespace string, serviceName string) (*v1.Service, error)
GetServices(namespaceName string) (*ServiceList, error)
GetServiceDetails(namespace string, serviceName string) (*ServiceDetails, error)
GetServicePods(namespace string, serviceName string, serviceVersion string, selector string) (*v1.PodList, error)
GetIstioDetails(namespace string, serviceName string) (*IstioDetails, error)
}
// IstioClient is the client struct for Kubernetes and Istio APIs
// It hides the way it queries each API
type IstioClient struct {
IstioClientInterface
k8s *kube.Clientset
istioConfigApi *rest.RESTClient
istioNetworkingApi *rest.RESTClient
}
// 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 {
return rest.InClusterConfig()
}
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),
}, nil
}
// NewClient 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 NewClient() (*IstioClient, error) {
client := IstioClient{}
config, err := ConfigClient()
if err != nil {
return nil, err
}
config.QPS = k8sQPS
config.Burst = k8sBurst
k8s, err := kube.NewForConfig(config)
if err != nil {
return nil, err
}
client.k8s = k8s
// Istio is a CRD extension of Kubernetes API, so any custom type should be registered here.
// KnownTypes registers the Istio objects we use, as soon as we get more info we will increase the number of types.
types := runtime.NewScheme()
schemeBuilder := runtime.NewSchemeBuilder(
func(scheme *runtime.Scheme) error {
for _, kind := range istioKnownTypes {
scheme.AddKnownTypes(*kind.groupVersion, kind.object, kind.collection)
}
meta_v1.AddToGroupVersion(scheme, istioConfigGroupVersion)
meta_v1.AddToGroupVersion(scheme, istioNetworkingGroupVersion)
return nil
})
err = schemeBuilder.AddToScheme(types)
if err != nil {
return nil, err
}
// Istio needs another type as it queries a different K8S API.
istioConfig := rest.Config{
Host: config.Host,
APIPath: "/apis",
ContentConfig: rest.ContentConfig{
GroupVersion: &istioConfigGroupVersion,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: serializer.NewCodecFactory(types)},
ContentType: runtime.ContentTypeJSON,
},
BearerToken: config.BearerToken,
TLSClientConfig: config.TLSClientConfig,
QPS: config.QPS,
Burst: config.Burst,
}
istioConfigApi, err := rest.RESTClientFor(&istioConfig)
client.istioConfigApi = istioConfigApi
if err != nil {
return nil, err
}
istioNetworking := rest.Config{
Host: config.Host,
APIPath: "/apis",
ContentConfig: rest.ContentConfig{
GroupVersion: &istioNetworkingGroupVersion,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: serializer.NewCodecFactory(types)},
ContentType: runtime.ContentTypeJSON,
},
BearerToken: config.BearerToken,
TLSClientConfig: config.TLSClientConfig,
QPS: config.QPS,
Burst: config.Burst,
}
istioNetworkingApi, err := rest.RESTClientFor(&istioNetworking)
client.istioNetworkingApi = istioNetworkingApi
if err != nil {
return nil, err
}
return &client, nil
}
// FilterDeploymentsForService returns a subpart of deployments list where labels match the ones of the given service
func FilterDeploymentsForService(s *v1.Service, deployments *v1beta1.DeploymentList) *[]v1beta1.Deployment {
if s == nil || deployments == nil {
return nil
}
depls := make([]v1beta1.Deployment, len(deployments.Items))
i := 0
for _, depl := range deployments.Items {
if labelsMatch(depl.ObjectMeta.Labels, s.Spec.Selector) {
depls[i] = depl
i++
}
}
shrinked := depls[:i]
return &shrinked
}
func labelsMatch(serviceLabels, filterLabels map[string]string) bool {
labelMatch := true
for key, value := range filterLabels {
if serviceLabels[key] != value {
labelMatch = false
break
}
}
return labelMatch
}
func selectorToString(selector map[string]string) string {
querySelector := make([]string, 0, len(selector))
for label, name := range selector {
querySelector = append(querySelector, label+"="+name)
}
return strings.Join(querySelector, ",")
}