forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
253 lines (227 loc) · 9.07 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package kubernetes
import (
"fmt"
"net"
"os"
"k8s.io/api/apps/v1beta1"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"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"
"github.com/kiali/kiali/log"
)
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 {
GetNamespaces() (*v1.NamespaceList, error)
GetService(namespace string, serviceName string) (*v1.Service, error)
GetFullServices(namespace string) (*ServiceList, error)
GetNamespaceAppsDetails(namespace string) (NamespaceApps, error)
GetAppDetails(namespace, app string) (AppDetails, error)
GetServices(namespace string) (*v1.ServiceList, error)
GetServiceDetails(namespace string, serviceName string) (*ServiceDetails, error)
GetPods(namespace, labelSelector string) (*v1.PodList, error)
GetNamespacePods(namespace string) (*v1.PodList, error)
GetIstioDetails(namespace string, serviceName string) (*IstioDetails, error)
GetGateways(namespace string) ([]IstioObject, error)
GetGateway(namespace string, gateway string) (IstioObject, error)
GetServiceEntries(namespace string) ([]IstioObject, error)
GetServiceEntry(namespace string, serviceEntryName string) (IstioObject, error)
GetVirtualServices(namespace string, serviceName string) ([]IstioObject, error)
GetVirtualService(namespace string, virtualservice string) (IstioObject, error)
GetDestinationRules(namespace string, serviceName string) ([]IstioObject, error)
GetDestinationRule(namespace string, destinationrule string) (IstioObject, error)
GetIstioRules(namespace string) (*IstioRules, error)
GetIstioRuleDetails(namespace string, istiorule string) (*IstioRuleDetails, error)
GetQuotaSpecs(namespace string) ([]IstioObject, error)
GetQuotaSpec(namespace string, quotaSpecName string) (IstioObject, error)
GetQuotaSpecBindings(namespace string) ([]IstioObject, error)
GetQuotaSpecBinding(namespace string, quotaSpecBindingName string) (IstioObject, error)
GetDeployments(namespace string) (*v1beta1.DeploymentList, error)
GetDeployment(namespace string, deploymentName string) (*v1beta1.Deployment, error)
GetDeploymentDetails(namespace string, deploymentName string) (*DeploymentDetails, error)
GetDeploymentSelector(namespace string, deploymentName string) (string, 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
}
// FilterPodsForService returns a subpart of pod list filtered according service selector
func FilterPodsForService(s *v1.Service, allPods *v1.PodList) []v1.Pod {
if s == nil || allPods == nil {
return nil
}
serviceSelector := labels.Set(s.Spec.Selector).AsSelector()
pods := filterPodsForService(serviceSelector, allPods)
return pods
}
// FilterDeploymentsForService returns a subpart of deployments list filtered according to pods labels.
func FilterDeploymentsForService(s *v1.Service, servicePods []v1.Pod, allDepls *v1beta1.DeploymentList) []v1beta1.Deployment {
if s == nil || allDepls == nil {
return nil
}
serviceSelector := labels.Set(s.Spec.Selector).AsSelector()
var deployments []v1beta1.Deployment
for _, d := range allDepls.Items {
depSelector, err := meta_v1.LabelSelectorAsSelector(d.Spec.Selector)
if err != nil {
log.Errorf("Invalid label selector: %v", err)
}
added := false
// If it matches any of the pods, then it's "part" of the service
for _, pod := range servicePods {
// If a deployment with an empty selector creeps in, it should match nothing, not everything.
if !depSelector.Empty() && depSelector.Matches(labels.Set(pod.ObjectMeta.Labels)) {
deployments = append(deployments, d)
added = true
break
}
}
if !added {
// Maybe there's no pod (yet) for a deployment, but it still "belongs" to that service
// We can try to guess that by matching service selector with deployment labels and assume they would match.
// This is of course not guaranteed.
if !serviceSelector.Empty() && serviceSelector.Matches(labels.Set(d.ObjectMeta.Labels)) {
deployments = append(deployments, d)
}
}
}
return deployments
}
func filterPodsForService(selector labels.Selector, allPods *v1.PodList) []v1.Pod {
var pods []v1.Pod
for _, pod := range allPods.Items {
if selector.Matches(labels.Set(pod.ObjectMeta.Labels)) {
pods = append(pods, pod)
}
}
return pods
}
// filterPodsForEndpoints performs a second pass was selector may return too many data
// This case happens when a "nil" selector (such as one of default/kubernetes service) is used
func filterPodsForEndpoints(endpoints *v1.Endpoints, unfiltered *v1.PodList) []v1.Pod {
endpointPods := make(map[string]bool)
for _, subset := range endpoints.Subsets {
for _, address := range subset.Addresses {
if address.TargetRef != nil && address.TargetRef.Kind == "Pod" {
endpointPods[address.TargetRef.Name] = true
}
}
}
var pods []v1.Pod
for _, pod := range unfiltered.Items {
if _, ok := endpointPods[pod.Name]; ok {
pods = append(pods, pod)
}
}
return pods
}