forked from CATechnologiesTest/kubeiql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
293 lines (247 loc) · 6.48 KB
/
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
// Copyright (c) 2018 CA. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
// "fmt"
)
// Services expose functionality to the outside world
type service struct {
Metadata metadata
Spec serviceSpec
Selected []*resource
}
type serviceSpec struct {
ClusterIP *string
ExternalIPs *[]string
ExternalName *string
ExternalTrafficPolicy *string
HealthCheckNodePort *int32
LoadBalancerIP *string
LoadBalancerSourceRanges *[]string
Ports *[]servicePort
PublishNotReadyAddresses *bool
Selector []label
SessionAffinity *string
SessionAffinityConfig *sessionAffinityConfig
Type string
}
type sessionAffinityConfig struct {
ClientIP clientIPConfig
}
type clientIPConfig struct {
TimeoutSeconds int32
}
type servicePort struct {
Name *string
NodePort *int32
Port int32
Protocol *string
TargetPortString *string
TargetPortInt *int32
}
type serviceResolver struct {
ctx context.Context
s service
}
type serviceSpecResolver struct {
ctx context.Context
s serviceSpec
}
type sessionAffinityConfigResolver struct {
ctx context.Context
s sessionAffinityConfig
}
type clientIPConfigResolver struct {
ctx context.Context
s clientIPConfig
}
type servicePortResolver struct {
ctx context.Context
s servicePort
}
// Translate unmarshalled json into a deployment object
func mapToService(
ctx context.Context,
jsonObj JsonObject) service {
ns := getNamespace(jsonObj)
meta := mapToMetadata(ctx, ns, mapItem(jsonObj, "metadata"))
return service{
meta,
extractServiceSpec(ctx, jsonObj),
extractSelected(ctx, ns, jsonObj)}
}
func extractSelected(ctx context.Context, ns string, jsonObj JsonObject) []*resource {
return []*resource{}
}
func extractServiceSpec(ctx context.Context, jsonObj JsonObject) serviceSpec {
return serviceSpec{
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
[]label{},
nil,
nil,
"ClientIP"}
}
// Service implementations
func (r *serviceResolver) Kind() string {
return ServiceKind
}
func (r *serviceResolver) Metadata() metadataResolver {
return metadataResolver{r.ctx, r.s.Metadata}
}
func (r *serviceResolver) Spec() serviceSpecResolver {
return serviceSpecResolver{r.ctx, r.s.Spec}
}
func (r *serviceResolver) Owner() *resourceResolver {
return &resourceResolver{r.ctx, &serviceResolver{r.ctx, r.s}}
}
func (r *serviceResolver) RootOwner() *resourceResolver {
return &resourceResolver{r.ctx, &serviceResolver{r.ctx, r.s}}
}
func (r *serviceResolver) Selected() []*resourceResolver {
return getSelectedResources(r)
}
func getSelectedResources(r *serviceResolver) []*resourceResolver {
ns := (*r).s.Metadata.Namespace
ls := (*r).s.Spec.Selector
objs := getAllK8sObjsOfKindInNamespace(r.ctx, PodKind, *ns,
func(jo JsonObject) bool {
labels := getMatchLabels(jo)
for _, label := range ls {
seenMatch := false
for k, v := range labels {
if k == label.Name {
seenMatch = true
if v != label.Value {
return false
}
}
}
if !seenMatch {
return false
}
}
return true
})
results := make([]*resourceResolver, len(objs))
for idx, val := range objs {
results[idx] = &resourceResolver{r.ctx, val}
}
return results
}
func getMatchLabels(jo JsonObject) JsonObject {
kind := getKind(jo)
switch kind {
case PodKind:
return getMetadataField(jo, "labels").(JsonObject)
}
return JsonObject{}
}
// Service Spec implementations
func (r serviceSpecResolver) ClusterIP() *string {
return r.s.ClusterIP
}
func (r serviceSpecResolver) ExternalIPs() *[]string {
return r.s.ExternalIPs
}
func (r serviceSpecResolver) ExternalName() *string {
return r.s.ExternalName
}
func (r serviceSpecResolver) ExternalTrafficPolicy() *string {
return r.s.ExternalTrafficPolicy
}
func (r serviceSpecResolver) HealthCheckNodePort() *int32 {
return r.s.HealthCheckNodePort
}
func (r serviceSpecResolver) LoadBalancerIP() *string {
return r.s.LoadBalancerIP
}
func (r serviceSpecResolver) LoadBalancerSourceRanges() *[]string {
return r.s.LoadBalancerSourceRanges
}
func (r serviceSpecResolver) Ports() *[]servicePortResolver {
s := r.s.Ports
if s == nil || len(*s) == 0 {
res := make([]servicePortResolver, 0)
return &res
}
resolvers := make([]servicePortResolver, len(*s))
for idx, val := range *s {
resolvers[idx] = servicePortResolver{r.ctx, val}
}
return &resolvers
}
func (r serviceSpecResolver) PublishNotReadyAddresses() *bool {
return r.s.PublishNotReadyAddresses
}
func (r serviceSpecResolver) Selector() []labelResolver {
s := r.s.Selector
if len(s) == 0 {
res := make([]labelResolver, 0)
return res
}
resolvers := make([]labelResolver, len(s))
for idx, val := range s {
l := val
resolvers[idx] = labelResolver{r.ctx, &l}
}
return resolvers
}
func (r serviceSpecResolver) SessionAffinity() *string {
return r.s.SessionAffinity
}
func (r serviceSpecResolver) SessionAffinityConfig() *sessionAffinityConfigResolver {
if r.s.SessionAffinityConfig == nil {
return nil
}
return &sessionAffinityConfigResolver{r.ctx, *r.s.SessionAffinityConfig}
}
func (r serviceSpecResolver) Type() string {
return r.s.Type
}
// Service port implementations
func (r servicePortResolver) Name() *string {
return r.s.Name
}
func (r servicePortResolver) NodePort() *int32 {
return r.s.NodePort
}
func (r servicePortResolver) Port() int32 {
return r.s.Port
}
func (r servicePortResolver) Protocol() *string {
return r.s.Protocol
}
func (r servicePortResolver) TargetPortString() *string {
return r.s.TargetPortString
}
func (r servicePortResolver) TargetPortInt() *int32 {
return r.s.TargetPortInt
}
// SessionAffinityConfig implementations
func (r *sessionAffinityConfigResolver) ClientIP() clientIPConfigResolver {
return clientIPConfigResolver{r.ctx, r.s.ClientIP}
}
// ClientIPConfig implementations
func (r clientIPConfigResolver) TimeoutSeconds() int32 {
return r.s.TimeoutSeconds
}