forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes_service.go
305 lines (273 loc) · 9.95 KB
/
kubernetes_service.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package kubernetes
import (
"k8s.io/api/apps/v1beta1"
"k8s.io/api/apps/v1beta2"
auth_v1 "k8s.io/api/authorization/v1"
batch_v1 "k8s.io/api/batch/v1"
batch_v1beta1 "k8s.io/api/batch/v1beta1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
osappsv1 "github.com/openshift/api/apps/v1"
osv1 "github.com/openshift/api/project/v1"
)
// GetNamespace fetches and returns the specified namespace definition
// from the cluster
func (in *IstioClient) GetNamespace(namespace string) (*v1.Namespace, error) {
ns, err := in.k8s.CoreV1().Namespaces().Get(namespace, emptyGetOptions)
if err != nil {
return &v1.Namespace{}, err
}
return ns, nil
}
// GetNamespaces returns a list of all namespaces of the cluster.
// It returns a list of all namespaces of the cluster.
// It returns an error on any problem.
func (in *IstioClient) GetNamespaces() ([]v1.Namespace, error) {
namespaces, err := in.k8s.CoreV1().Namespaces().List(emptyListOptions)
if err != nil {
return nil, err
}
return namespaces.Items, nil
}
// GetProject fetches and returns the definition of the project with
// the specified name by querying the cluster API. GetProject will fail
// if the underlying cluster is not Openshift.
func (in *IstioClient) GetProject(name string) (*osv1.Project, error) {
result := &osv1.Project{}
err := in.k8s.RESTClient().Get().Prefix("apis", "project.openshift.io", "v1", "projects", name).Do().Into(result)
if err != nil {
return nil, err
}
return result, nil
}
func (in *IstioClient) GetProjects() ([]osv1.Project, error) {
result := &osv1.ProjectList{}
err := in.k8s.RESTClient().Get().Prefix("apis", "project.openshift.io", "v1", "projects").Do().Into(result)
if err != nil {
return nil, err
}
return result.Items, nil
}
func (in *IstioClient) IsOpenShift() bool {
if in.isOpenShift == nil {
isOpenShift := false
_, err := in.k8s.RESTClient().Get().AbsPath("/version/openshift").Do().Raw()
if err == nil {
isOpenShift = true
}
in.isOpenShift = &isOpenShift
}
return *in.isOpenShift
}
// GetServices returns a list of services for a given namespace.
// If selectorLabels is defined the list of services is filtered for those that matches Services selector labels.
// It returns an error on any problem.
func (in *IstioClient) GetServices(namespace string, selectorLabels map[string]string) ([]v1.Service, error) {
var allServices []v1.Service
var err error
if in.k8sCache != nil {
allServices, err = in.k8sCache.GetServices(namespace)
} else {
if allServicesList, err := in.k8s.CoreV1().Services(namespace).List(emptyListOptions); err == nil {
allServices = allServicesList.Items
}
}
if err != nil {
return []v1.Service{}, err
}
if selectorLabels == nil {
return allServices, nil
}
var services []v1.Service
for _, svc := range allServices {
svcSelector := labels.Set(svc.Spec.Selector).AsSelector()
if svcSelector.Matches(labels.Set(selectorLabels)) {
services = append(services, svc)
}
}
return services, nil
}
// GetDeployment returns the definition of a specific deployment.
// It returns an error on any problem.
func (in *IstioClient) GetDeployment(namespace, deploymentName string) (*v1beta1.Deployment, error) {
if in.k8sCache != nil {
return in.k8sCache.GetDeployment(namespace, deploymentName)
}
return in.k8s.AppsV1beta1().Deployments(namespace).Get(deploymentName, emptyGetOptions)
}
// GetDeployments returns an array of deployments for a given namespace and a set of labels.
// It returns an error on any problem.
func (in *IstioClient) GetDeployments(namespace string) ([]v1beta1.Deployment, error) {
if in.k8sCache != nil {
return in.k8sCache.GetDeployments(namespace)
}
if depList, err := in.k8s.AppsV1beta1().Deployments(namespace).List(emptyListOptions); err == nil {
return depList.Items, nil
} else {
return []v1beta1.Deployment{}, err
}
}
// GetDeployment returns the definition of a specific deployment.
// It returns an error on any problem.
func (in *IstioClient) GetDeploymentConfig(namespace, deploymentconfigName string) (*osappsv1.DeploymentConfig, error) {
result := &osappsv1.DeploymentConfig{}
err := in.k8s.RESTClient().Get().Prefix("apis", "apps.openshift.io", "v1").Namespace(namespace).Resource("deploymentconfigs").SubResource(deploymentconfigName).Do().Into(result)
if err != nil {
return nil, err
}
return result, nil
}
// GetDeployments returns an array of deployments for a given namespace and a set of labels.
// An empty labelSelector will fetch all Deployments for a namespace.
// It returns an error on any problem.
func (in *IstioClient) GetDeploymentConfigs(namespace string) ([]osappsv1.DeploymentConfig, error) {
result := &osappsv1.DeploymentConfigList{}
err := in.k8s.RESTClient().Get().Prefix("apis", "apps.openshift.io", "v1").Namespace(namespace).Resource("deploymentconfigs").Do().Into(result)
if err != nil {
return nil, err
}
return result.Items, nil
}
func (in *IstioClient) GetReplicaSets(namespace string) ([]v1beta2.ReplicaSet, error) {
if in.k8sCache != nil {
return in.k8sCache.GetReplicaSets(namespace)
}
if rsList, err := in.k8s.AppsV1beta2().ReplicaSets(namespace).List(emptyListOptions); err == nil {
return rsList.Items, nil
} else {
return []v1beta2.ReplicaSet{}, err
}
}
func (in *IstioClient) GetStatefulSet(namespace string, statefulsetName string) (*v1beta2.StatefulSet, error) {
if in.k8sCache != nil {
return in.k8sCache.GetStatefulSet(namespace, statefulsetName)
}
return in.k8s.AppsV1beta2().StatefulSets(namespace).Get(statefulsetName, emptyGetOptions)
}
func (in *IstioClient) GetStatefulSets(namespace string) ([]v1beta2.StatefulSet, error) {
if in.k8sCache != nil {
return in.k8sCache.GetStatefulSets(namespace)
}
if ssList, err := in.k8s.AppsV1beta2().StatefulSets(namespace).List(emptyListOptions); err == nil {
return ssList.Items, nil
} else {
return []v1beta2.StatefulSet{}, err
}
}
func (in *IstioClient) GetReplicationControllers(namespace string) ([]v1.ReplicationController, error) {
if in.k8sCache != nil {
return in.k8sCache.GetReplicationControllers(namespace)
}
if rcList, err := in.k8s.CoreV1().ReplicationControllers(namespace).List(emptyListOptions); err == nil {
return rcList.Items, nil
} else {
return []v1.ReplicationController{}, err
}
}
// GetService returns the definition of a specific service.
// It returns an error on any problem.
func (in *IstioClient) GetService(namespace, serviceName string) (*v1.Service, error) {
if in.k8sCache != nil {
return in.k8sCache.GetService(namespace, serviceName)
}
return in.k8s.CoreV1().Services(namespace).Get(serviceName, emptyGetOptions)
}
// GetEndpoints return the list of endpoint of a specific service.
// It returns an error on any problem.
func (in *IstioClient) GetEndpoints(namespace, serviceName string) (*v1.Endpoints, error) {
if in.k8sCache != nil {
return in.k8sCache.GetEndpoints(namespace, serviceName)
}
return in.k8s.CoreV1().Endpoints(namespace).Get(serviceName, emptyGetOptions)
}
// GetPods returns the pods definitions for a given set of labels.
// An empty labelSelector will fetch all pods found per a namespace.
// It returns an error on any problem.
func (in *IstioClient) GetPods(namespace, labelSelector string) ([]v1.Pod, error) {
if in.k8sCache != nil {
pods, err := in.k8sCache.GetPods(namespace)
if err != nil {
return []v1.Pod{}, err
}
if labelSelector != "" {
selector, err := labels.Parse(labelSelector)
if err != nil {
return []v1.Pod{}, err
}
pods = FilterPodsForSelector(selector, pods)
}
return pods, nil
}
// An empty selector is ambiguous in the go client, could mean either "select all" or "select none"
// Here we assume empty == select all
// (see also https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors)
if pods, err := in.k8s.CoreV1().Pods(namespace).List(meta_v1.ListOptions{LabelSelector: labelSelector}); err == nil {
return pods.Items, nil
} else {
return []v1.Pod{}, err
}
}
func (in *IstioClient) GetCronJobs(namespace string) ([]batch_v1beta1.CronJob, error) {
if in.k8sCache != nil {
return in.k8sCache.GetCronJobs(namespace)
}
if cjList, err := in.k8s.BatchV1beta1().CronJobs(namespace).List(emptyListOptions); err == nil {
return cjList.Items, nil
} else {
return []batch_v1beta1.CronJob{}, err
}
}
func (in *IstioClient) GetJobs(namespace string) ([]batch_v1.Job, error) {
if in.k8sCache != nil {
return in.k8sCache.GetJobs(namespace)
}
if jList, err := in.k8s.BatchV1().Jobs(namespace).List(emptyListOptions); err == nil {
return jList.Items, nil
} else {
return []batch_v1.Job{}, err
}
}
// NewNotFound is a helper method to create a NotFound error similar as used by the kubernetes client.
// This method helps upper layers to send a explicit NotFound error without querying the backend.
func NewNotFound(name, group, resource string) error {
return errors.NewNotFound(schema.GroupResource{Group: group, Resource: resource}, name)
}
// GetSelfSubjectAccessReview provides information on Kiali permissions
func (in *IstioClient) GetSelfSubjectAccessReview(namespace, api, resourceType string, verbs []string) ([]*auth_v1.SelfSubjectAccessReview, error) {
calls := len(verbs)
ch := make(chan *auth_v1.SelfSubjectAccessReview, calls)
errChan := make(chan error)
for _, v := range verbs {
go func(verb string) {
res, err := in.k8s.AuthorizationV1().SelfSubjectAccessReviews().Create(&auth_v1.SelfSubjectAccessReview{
Spec: auth_v1.SelfSubjectAccessReviewSpec{
ResourceAttributes: &auth_v1.ResourceAttributes{
Namespace: namespace,
Verb: verb,
Group: api,
Resource: resourceType,
},
},
})
if err != nil {
errChan <- err
} else {
ch <- res
}
}(v)
}
var err error
result := []*auth_v1.SelfSubjectAccessReview{}
for count := 0; count < calls; count++ {
select {
case res := <-ch:
result = append(result, res)
case err = <-errChan:
// No op
}
}
return result, err
}