/
service.go
195 lines (178 loc) · 5.73 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
package v1
import (
"database/sql"
"github.com/icinga/icinga-kubernetes/pkg/database"
"github.com/icinga/icinga-kubernetes/pkg/strcase"
"github.com/icinga/icinga-kubernetes/pkg/types"
kcorev1 "k8s.io/api/core/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)
type Service struct {
Meta
Id types.Binary
Type string
ClusterIP string
ClusterIPs string
ExternalIPs string
SessionAffinity string
ExternalName string
ExternalTrafficPolicy sql.NullString
HealthCheckNodePort int32
PublishNotReadyAddresses types.Bool
IpFamilies string
IpFamilyPolicy string
AllocateLoadBalancerNodePorts types.Bool
LoadBalancerClass string
InternalTrafficPolicy string
Selectors []Selector `db:"-"`
ServiceSelectors []ServiceSelector `db:"-"`
Ports []ServicePort `db:"-"`
Conditions []ServiceCondition `db:"-"`
Labels []Label `db:"-"`
ServiceLabels []ServiceLabel `db:"-"`
}
type ServiceSelector struct {
ServiceId types.Binary
SelectorId types.Binary
}
type ServicePort struct {
ServiceId types.Binary
Name string
Protocol string
AppProtocol string
Port int32
TargetPort string
NodePort int32
}
type ServiceCondition struct {
ServiceId types.Binary
Type string
Status string
ObservedGeneration int64
LastTransition types.UnixMilli
Reason string
Message string
}
type ServiceLabel struct {
ServiceId types.Binary
LabelId types.Binary
}
func NewService() Resource {
return &Service{}
}
func (s *Service) Obtain(k8s kmetav1.Object) {
s.ObtainMeta(k8s)
service := k8s.(*kcorev1.Service)
var externalTrafficPolicy sql.NullString
if service.Spec.ExternalTrafficPolicy != "" {
externalTrafficPolicy.String = strcase.Snake(string(service.Spec.ExternalTrafficPolicy))
externalTrafficPolicy.Valid = true
}
var ipFamilyPolicy string
if service.Spec.IPFamilyPolicy != nil {
ipFamilyPolicy = strcase.Snake(string(*service.Spec.IPFamilyPolicy))
}
var allocateLoadBalancerNodePorts bool
if service.Spec.AllocateLoadBalancerNodePorts != nil {
allocateLoadBalancerNodePorts = *service.Spec.AllocateLoadBalancerNodePorts
}
var loadBalancerClass string
if service.Spec.LoadBalancerClass != nil {
loadBalancerClass = *service.Spec.LoadBalancerClass
}
var internalTrafficPolicy string
if service.Spec.InternalTrafficPolicy == nil {
internalTrafficPolicy = "cluster"
} else {
internalTrafficPolicy = strcase.Snake(string(*service.Spec.InternalTrafficPolicy))
}
s.Id = types.Checksum(service.Namespace + "/" + service.Name)
s.Type = strcase.Snake(string(service.Spec.Type))
s.ClusterIP = service.Spec.ClusterIP
for _, clusterIP := range service.Spec.ClusterIPs {
s.ClusterIPs = clusterIP
}
for _, externalIPs := range service.Spec.ExternalIPs {
s.ExternalIPs = externalIPs
}
s.SessionAffinity = strcase.Snake(string(service.Spec.SessionAffinity))
s.ExternalName = service.Spec.ExternalName
s.ExternalTrafficPolicy = externalTrafficPolicy
s.HealthCheckNodePort = service.Spec.HealthCheckNodePort
s.PublishNotReadyAddresses = types.Bool{
Bool: service.Spec.PublishNotReadyAddresses,
Valid: true,
}
for _, ipFamily := range service.Spec.IPFamilies {
s.IpFamilies = string(ipFamily)
}
s.IpFamilyPolicy = ipFamilyPolicy
s.AllocateLoadBalancerNodePorts = types.Bool{
Bool: allocateLoadBalancerNodePorts,
Valid: true,
}
s.LoadBalancerClass = loadBalancerClass
s.InternalTrafficPolicy = internalTrafficPolicy
for selectorName, selectorValue := range service.Spec.Selector {
selectorId := types.Checksum(strings.ToLower(selectorName + ":" + selectorValue))
s.Selectors = append(s.Selectors, Selector{
Id: selectorId,
Name: selectorName,
Value: selectorValue,
})
s.ServiceSelectors = append(s.ServiceSelectors, ServiceSelector{
ServiceId: s.Id,
SelectorId: selectorId,
})
}
for _, port := range service.Spec.Ports {
var appProtocol string
if port.AppProtocol != nil {
appProtocol = *port.AppProtocol
}
s.Ports = append(s.Ports, ServicePort{
ServiceId: s.Id,
Name: port.Name,
Protocol: string(port.Protocol),
AppProtocol: appProtocol,
Port: port.Port,
TargetPort: port.TargetPort.String(),
NodePort: port.NodePort,
})
}
for _, condition := range service.Status.Conditions {
s.Conditions = append(s.Conditions, ServiceCondition{
ServiceId: s.Id,
Type: condition.Type,
Status: strcase.Snake(string(condition.Status)),
ObservedGeneration: condition.ObservedGeneration,
LastTransition: types.UnixMilli(condition.LastTransitionTime.Time),
Reason: condition.Reason,
Message: condition.Message,
})
}
for labelName, labelValue := range service.Labels {
labelId := types.Checksum(strings.ToLower(labelName + ":" + labelValue))
s.Labels = append(s.Labels, Label{
Id: labelId,
Name: labelName,
Value: labelValue,
})
s.ServiceLabels = append(s.ServiceLabels, ServiceLabel{
ServiceId: s.Id,
LabelId: labelId,
})
}
}
func (s *Service) Relations() []database.Relation {
fk := database.WithForeignKey("service_id")
return []database.Relation{
database.HasMany(s.Conditions, fk),
database.HasMany(s.Ports, fk),
database.HasMany(s.Labels, database.WithoutCascadeDelete()),
database.HasMany(s.ServiceLabels, fk),
database.HasMany(s.Selectors, database.WithoutCascadeDelete()),
database.HasMany(s.ServiceSelectors, fk),
}
}