forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingress.go
149 lines (137 loc) · 4.09 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
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"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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(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("", "", portStr)
if _, ok := state[key]; ok {
serviceToKey[serviceName] = key
serviceToPort[serviceName] = portStr
}
}
for serviceName, portStr := range serviceToPort {
workloadIDsStr := state[serviceToKey[serviceName]]
b, err := json.Marshal(strings.Split(workloadIDsStr, "/"))
if err != nil {
return err
}
workloadIDs := string(b)
existing, err := c.serviceLister.Get(obj.Namespace, serviceName)
if err != nil && !apierrors.IsNotFound(err) {
return err
}
if existing == nil {
controller := true
ownerRef := metav1.OwnerReference{
Name: serviceName,
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{
ClusterIP: "None",
Type: "ClusterIP",
Ports: servicePorts,
},
}
logrus.Infof("Creating headless service %s for ingress %s, port %s", serviceName, key, portStr)
if _, err := c.services.Create(service); err != nil {
return err
}
} else {
ingressManaged := false
for _, o := range existing.OwnerReferences {
if o.UID == obj.UID {
ingressManaged = true
break
}
}
if ingressManaged {
// TODO - fix so the update is done only when workload ids are changed
toUpdate := existing.DeepCopy()
if toUpdate.Annotations == nil {
toUpdate.Annotations = map[string]string{}
}
toUpdate.Annotations[util.WorkloadAnnotation] = workloadIDs
if _, err := c.services.Update(toUpdate); err != nil {
return err
}
}
}
}
return nil
}