forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workload.go
308 lines (273 loc) · 8.32 KB
/
workload.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
306
307
308
package workload
import (
"context"
"encoding/json"
"reflect"
"strings"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/validation"
)
// This controller is responsible for monitoring workloads and
// creating services for them
// a) when rancher ports annotation is present, create service based on annotation ports
// b) when annotation is missing, create a headless service
const (
creatorIDAnnotation = "field.cattle.io/creatorId"
)
type Controller struct {
workloadController CommonController
serviceLister v1.ServiceLister
services v1.ServiceInterface
}
func Register(ctx context.Context, workload *config.UserOnlyContext) {
c := &Controller{
serviceLister: workload.Core.Services("").Controller().Lister(),
services: workload.Core.Services(""),
}
c.workloadController = NewWorkloadController(workload, c.CreateService)
}
func getName() string {
return "workloadServiceGenerationController"
}
func (c *Controller) CreateService(key string, w *Workload) error {
// do not create service for job, cronJob and for workload owned by controller (ReplicaSet)
if strings.EqualFold(w.Kind, "job") || strings.EqualFold(w.Kind, "cronJob") {
return nil
}
for _, o := range w.OwnerReferences {
if o.Controller != nil && *o.Controller {
return nil
}
}
if _, ok := w.Annotations[creatorIDAnnotation]; !ok {
return nil
}
if errs := validation.IsDNS1123Subdomain(w.Name); len(errs) != 0 {
logrus.Debugf("Not creating service for workload [%s]: dns name is invalid", w.Name)
return nil
}
return c.CreateServiceForWorkload(w)
}
func (c *Controller) serviceExistsForWorkload(workload *Workload, service *Service) (*corev1.Service, error) {
s, err := c.serviceLister.Get(workload.Namespace, service.Name)
if err != nil {
if apierrors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
if s.DeletionTimestamp != nil {
return nil, nil
}
return s, nil
}
func (c *Controller) CreateServiceForWorkload(workload *Workload) error {
services := map[corev1.ServiceType]Service{}
if _, ok := workload.TemplateSpec.Annotations[PortsAnnotation]; ok {
svcs, err := generateServicesFromPortsAnnotation(workload)
if err != nil {
return err
}
for _, service := range svcs {
services[service.Type] = service
}
}
// always create cluster ip service, if missing in ports
if _, ok := services[ClusterIPServiceType]; !ok {
service := generateClusterIPServiceFromContainers(workload)
services[service.Type] = *service
}
// 1. Create new services
for _, toCreate := range services {
existing, err := c.serviceExistsForWorkload(workload, &toCreate)
if err != nil {
return err
}
recreate := false
// to handle clusterIP to headless service updates
// as clusterIP field is immutable
if existing != nil && toCreate.Type == ClusterIPServiceType {
clusterIPNew := toCreate.ClusterIP
custerIPOld := existing.Spec.ClusterIP
if clusterIPNew != custerIPOld && (clusterIPNew == "None" || custerIPOld == "None") {
err = c.services.DeleteNamespaced(existing.Namespace, existing.Name, &metav1.DeleteOptions{})
if err != nil {
return err
}
recreate = true
}
}
if existing == nil || recreate {
if err := c.createService(toCreate, workload); err != nil {
return err
}
} else {
// check if the port of the same type
if existing.Spec.Type != toCreate.Type {
logrus.Warnf("Service [%s/%s] already exists but with diff type. Expected type [%s], actual type [%v]", existing.Name, existing.Namespace, toCreate.Type, existing.Spec.Type)
return nil
}
isOwner := false
for _, ref := range existing.OwnerReferences {
if reflect.DeepEqual(ref.UID, workload.UUID) {
isOwner = true
break
}
}
if !isOwner {
logrus.Warnf("Service [%s/%s] already exists but with diff owner", existing.Name, existing.Namespace)
return nil
}
if arePortsEqual(toCreate.ServicePorts, existing.Spec.Ports) {
continue
}
if err := c.updateService(toCreate, existing); err != nil {
return err
}
}
}
// 2. Cleanup services that are no longer needed
existingSvcs, err := c.getServicesOwnedByWorkload(workload)
if err != nil {
return err
}
var toRemove []*corev1.Service
for _, existingSvc := range existingSvcs {
toCreate, ok := services[existingSvc.Spec.Type]
if ok && toCreate.Name == existingSvc.Name {
continue
}
toRemove = append(toRemove, existingSvc)
}
for _, svc := range toRemove {
logrus.Infof("Deleting [%s/%s] service of type [%s] for workload [%s/%s]", svc.Namespace, svc.Name, svc.Spec.Type,
workload.Namespace, workload.Name)
if err := c.services.DeleteNamespaced(svc.Namespace, svc.Name, &metav1.DeleteOptions{}); err != nil {
return err
}
}
return nil
}
func (c *Controller) updateService(toUpdate Service, existing *corev1.Service) error {
existingPortNameToPort := map[string]corev1.ServicePort{}
for _, p := range existing.Spec.Ports {
existingPortNameToPort[p.Name] = p
}
var portsToUpdate []corev1.ServicePort
for _, p := range toUpdate.ServicePorts {
if val, ok := existingPortNameToPort[p.Name]; ok {
if val.Port == p.Port {
if p.NodePort != val.NodePort {
if p.NodePort == 0 {
// random port handling to avoid infinite updates
p.NodePort = val.NodePort
}
}
}
}
portsToUpdate = append(portsToUpdate, p)
}
existing.Spec.Ports = portsToUpdate
if existing.Spec.Type == ClusterIPServiceType {
existing.Spec.ClusterIP = toUpdate.ClusterIP
}
logrus.Infof("Updating [%s/%s] service with ports [%v]", existing.Namespace, existing.Name, portsToUpdate)
_, err := c.services.Update(existing)
if err != nil {
return err
}
return nil
}
func (c *Controller) getServicesOwnedByWorkload(workload *Workload) ([]*corev1.Service, error) {
var toReturn []*corev1.Service
services, err := c.serviceLister.List(workload.Namespace, labels.NewSelector())
if err != nil {
return toReturn, err
}
for _, svc := range services {
if _, ok := svc.Annotations[WorkloaAnnotationdPortBasedService]; ok {
for _, o := range svc.OwnerReferences {
if o.UID == workload.UUID {
toReturn = append(toReturn, svc)
break
}
}
}
}
return toReturn, nil
}
func (c *Controller) createService(toCreate Service, workload *Workload) error {
controller := true
ownerRef := metav1.OwnerReference{
Name: workload.Name,
APIVersion: workload.APIVersion,
UID: workload.UUID,
Kind: workload.Kind,
Controller: &controller,
}
serviceAnnotations := map[string]string{}
workloadAnnotationValue, err := workloadAnnotationToString(workload.Key)
if err != nil {
return err
}
serviceAnnotations[WorkloadAnnotation] = workloadAnnotationValue
serviceAnnotations[WorkloadAnnotatioNoop] = "true"
serviceAnnotations[WorkloaAnnotationdPortBasedService] = "true"
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
OwnerReferences: []metav1.OwnerReference{ownerRef},
Namespace: workload.Namespace,
Name: toCreate.Name,
Annotations: serviceAnnotations,
},
Spec: corev1.ServiceSpec{
ClusterIP: toCreate.ClusterIP,
Type: toCreate.Type,
Ports: toCreate.ServicePorts,
Selector: workload.SelectorLabels,
},
}
logrus.Infof("Creating [%s/%s] service of type [%s] with ports [%v] for workload %s", service.Namespace, service.Name,
service.Spec.Type, toCreate.ServicePorts, workload.Key)
_, err = c.services.Create(service)
if err != nil {
if apierrors.IsAlreadyExists(err) {
return nil
}
return err
}
return nil
}
func arePortsEqual(one []corev1.ServicePort, two []corev1.ServicePort) bool {
if len(one) != len(two) {
return false
}
for _, o := range one {
found := false
for _, t := range two {
nodePortsEqual := (o.NodePort == 0 || t.NodePort == 0) || (o.NodePort == t.NodePort)
if o.TargetPort == t.TargetPort && o.Protocol == t.Protocol && o.Port == t.Port && nodePortsEqual {
found = true
break
}
}
if !found {
return false
}
}
return true
}
func workloadAnnotationToString(workloadID string) (string, error) {
ws := []string{workloadID}
b, err := json.Marshal(ws)
if err != nil {
return "", err
}
return string(b), nil
}