forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingress.go
175 lines (163 loc) · 4.96 KB
/
ingress.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
package ingress
import (
"context"
"encoding/json"
"strconv"
"strings"
"github.com/rancher/norman/types/convert"
util "github.com/rancher/rancher/pkg/controllers/user/workload"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
)
//TODO fix the workload services cleanup
// This controller is responsible for monitoring ingress and
// creating services for them if the service is missing
// Creation would only happen if the service reference was put by Rancher API based on
// ingress.backend.workloadId. This information is stored in state field in the annotation
type Controller struct {
serviceLister v1.ServiceLister
services v1.ServiceInterface
}
func Register(ctx context.Context, workload *config.UserOnlyContext) {
c := &Controller{
services: workload.Core.Services(""),
serviceLister: workload.Core.Services("").Controller().Lister(),
}
workload.Extensions.Ingresses("").AddHandler("ingressWorkloadController", c.sync)
}
func (c *Controller) sync(key string, obj *v1beta1.Ingress) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
state := GetIngressState(obj)
if state == nil {
return nil
}
serviceToPort := make(map[string]string)
serviceToKey := make(map[string]string)
for _, r := range obj.Spec.Rules {
host := r.Host
for _, b := range r.HTTP.Paths {
path := b.Path
port := b.Backend.ServicePort.IntVal
key := GetStateKey(obj.Name, obj.Namespace, host, path, convert.ToString(port))
if _, ok := state[key]; ok {
serviceToKey[b.Backend.ServiceName] = key
serviceToPort[b.Backend.ServiceName] = convert.ToString(port)
}
}
}
if obj.Spec.Backend != nil {
serviceName := obj.Spec.Backend.ServiceName
portStr := convert.ToString(obj.Spec.Backend.ServicePort.IntVal)
key := GetStateKey(obj.Name, obj.Namespace, "", "", portStr)
if _, ok := state[key]; ok {
serviceToKey[serviceName] = key
serviceToPort[serviceName] = portStr
}
}
ingressServices := map[string]*corev1.Service{}
services, err := c.serviceLister.List(obj.Namespace, labels.NewSelector())
if err != nil {
return err
}
for _, service := range services {
//mark the service which related to ingress
if _, ok := serviceToKey[service.Name]; ok {
ingressServices[service.Name] = service
continue
}
//mark the service which own by ingress but not related to ingress
for i, owners := 0, service.GetOwnerReferences(); owners != nil && i < len(owners); i++ {
if owners[i].UID == obj.UID {
ingressServices[service.Name] = service
break
}
}
}
for serviceName, portStr := range serviceToPort {
workloadIDsStr := state[serviceToKey[serviceName]]
workloadIDs := ""
if workloadIDsStr != "" {
b, err := json.Marshal(strings.Split(workloadIDsStr, "/"))
if err != nil {
return err
}
workloadIDs = string(b)
}
existing := ingressServices[serviceName]
if existing == nil {
controller := true
ownerRef := metav1.OwnerReference{
Name: obj.Name,
APIVersion: "v1beta1/extensions",
UID: obj.UID,
Kind: "Ingress",
Controller: &controller,
}
port, err := strconv.ParseInt(portStr, 10, 64)
if err != nil {
return err
}
servicePorts := []corev1.ServicePort{
{
Port: int32(port),
TargetPort: intstr.Parse(portStr),
Protocol: "TCP",
},
}
annotations := make(map[string]string)
annotations[util.WorkloadAnnotation] = workloadIDs
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
OwnerReferences: []metav1.OwnerReference{ownerRef},
Namespace: obj.Namespace,
Annotations: annotations,
},
Spec: corev1.ServiceSpec{
Type: "NodePort",
Ports: servicePorts,
},
}
logrus.Infof("Creating NodePort service %s for ingress %s, port %s", serviceName, key, portStr)
if _, err := c.services.Create(service); err != nil {
return err
}
} else {
if existing.Annotations == nil {
existing.Annotations = map[string]string{}
}
if existing.Annotations[util.WorkloadAnnotation] != workloadIDs && workloadIDs != "" {
toUpdate := existing.DeepCopy()
toUpdate.Annotations[util.WorkloadAnnotation] = workloadIDs
if _, err := c.services.Update(toUpdate); err != nil {
return err
}
}
delete(ingressServices, serviceName)
}
}
for serviceName, service := range ingressServices {
//only delete those service owned by ingress
shouldDelete := false
for i, owners := 0, service.GetOwnerReferences(); owners != nil && i < len(owners); i++ {
if owners[i].UID == obj.UID {
shouldDelete = true
break
}
}
if shouldDelete {
if err := c.services.DeleteNamespaced(obj.Namespace, serviceName, &metav1.DeleteOptions{}); err != nil {
return err
}
}
}
return nil
}