-
Notifications
You must be signed in to change notification settings - Fork 114
/
service.go
199 lines (179 loc) · 5.63 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
/*
* // Copyright (c) 2018 Cisco and/or its affiliates.
* //
* // 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 processor
import (
"net"
"github.com/ligato/cn-infra/logging"
epmodel "github.com/contiv/vpp/plugins/ksr/model/endpoints"
podmodel "github.com/contiv/vpp/plugins/ksr/model/pod"
svcmodel "github.com/contiv/vpp/plugins/ksr/model/service"
"github.com/contiv/vpp/plugins/service/renderer"
)
// Service is used to combine data from the service model with the endpoints.
type Service struct {
sp *ServiceProcessor
meta *svcmodel.Service
endpoints *epmodel.Endpoints
contivSvc *renderer.ContivService
localBackends []podmodel.ID
refreshed bool
}
// NewService is a constructor for Service.
func NewService(sp *ServiceProcessor) *Service {
return &Service{
sp: sp,
localBackends: []podmodel.ID{},
}
}
// SetMetadata initializes or changes metadata for the service.
func (s *Service) SetMetadata(meta *svcmodel.Service) {
s.meta = meta
s.refreshed = false
}
// SetEndpoints initializes or changes endpoints for the service.
func (s *Service) SetEndpoints(endpoints *epmodel.Endpoints) {
s.endpoints = endpoints
s.refreshed = false
}
// GetContivService returns the service data represented as ContivService.
// Returns nil if there are not enough available data.
func (s *Service) GetContivService() *renderer.ContivService {
if !s.refreshed {
s.Refresh()
}
return s.contivSvc
}
// GetLocalBackends returns the set of IDs od all local backends of this service.
// Returns empty array if there are not enough available data.
func (s *Service) GetLocalBackends() []podmodel.ID {
if !s.refreshed {
s.Refresh()
}
return s.localBackends
}
// Refresh combines metadata with endpoints to get ContivService representation
// and the list of local backends.
func (s *Service) Refresh() {
if s.meta == nil || s.endpoints == nil {
s.contivSvc = nil
s.localBackends = []podmodel.ID{}
s.refreshed = true
return
}
s.contivSvc = renderer.NewContivService()
s.localBackends = []podmodel.ID{}
s.contivSvc.ID = svcmodel.GetID(s.meta)
if s.meta.ExternalTrafficPolicy == "Local" {
s.contivSvc.TrafficPolicy = renderer.NodeLocal
} else {
s.contivSvc.TrafficPolicy = renderer.ClusterWide
}
// Collect all IP addresses on which the service should be exposed.
if s.meta.ClusterIp != "" && s.meta.ClusterIp != "None" {
clusterIP := net.ParseIP(s.meta.ClusterIp)
if clusterIP != nil {
s.contivSvc.ExternalIPs.Add(clusterIP)
} else {
s.sp.Log.WithFields(logging.Fields{
"service": s.contivSvc.ID,
"clusterIP": clusterIP,
}).Warn("Failed to parse clusterIP")
}
}
for _, externalIPStr := range s.meta.ExternalIps {
externalIP := net.ParseIP(externalIPStr)
if externalIP != nil {
s.contivSvc.ExternalIPs.Add(externalIP)
} else {
s.sp.Log.WithFields(logging.Fields{
"service": s.contivSvc.ID,
"externalIP": externalIPStr,
}).Warn("Failed to parse external IP")
}
}
if s.meta.ServiceType == "LoadBalancer" {
for _, lbIngressIPStr := range s.meta.LbIngressIps {
lbIngressIP := net.ParseIP(lbIngressIPStr)
if lbIngressIP != nil {
s.contivSvc.ExternalIPs.Add(lbIngressIP)
} else {
s.sp.Log.WithFields(logging.Fields{
"service": s.contivSvc.ID,
"LBIngressIP": lbIngressIPStr,
}).Warn("Failed to parse LB Ingress IP")
}
}
}
// Fill up the map of service ports.
for _, port := range s.meta.Port {
sp := &renderer.ServicePort{
Port: uint16(port.GetPort()),
NodePort: uint16(port.GetNodePort()),
}
if port.GetProtocol() == "TCP" {
sp.Protocol = renderer.TCP
} else {
sp.Protocol = renderer.UDP
}
s.contivSvc.Ports[port.Name] = sp
}
// Fill up the map of service backends.
for port := range s.contivSvc.Ports {
s.contivSvc.Backends[port] = []*renderer.ServiceBackend{}
}
for _, epSubSet := range s.endpoints.GetEndpointSubsets() {
epPorts := epSubSet.GetPorts()
epAddrs := epSubSet.GetAddresses()
for _, epAddr := range epAddrs {
var local bool
var hostNetwork bool
epIP := net.ParseIP(epAddr.GetIp())
if epIP == nil {
s.sp.Log.WithFields(logging.Fields{
"service": s.contivSvc.ID,
"endpointIP": epAddr.GetIp(),
}).Warn("Failed to parse endpoint IP")
continue
}
if epAddr.GetNodeName() == "" || epAddr.GetNodeName() == s.sp.ServiceLabel.GetAgentLabel() {
local = true
}
if !s.sp.Contiv.GetPodSubnet().Contains(epIP) {
hostNetwork = true
}
for _, epPort := range epPorts {
port := epPort.GetName()
if _, exposedPort := s.contivSvc.Ports[port]; exposedPort {
sb := &renderer.ServiceBackend{}
sb.IP = epIP
sb.Port = uint16(epPort.GetPort())
sb.Local = local
sb.HostNetwork = hostNetwork
s.contivSvc.Backends[port] = append(s.contivSvc.Backends[port], sb)
}
}
if local {
// Get target pod and add it to the set of local backends.
targetPod := epAddr.GetTargetRef()
if targetPod.GetKind() == "Pod" {
s.localBackends = append(s.localBackends,
podmodel.ID{Name: targetPod.GetName(), Namespace: targetPod.GetNamespace()})
}
}
}
}
s.refreshed = true
}