This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
controller.go
174 lines (147 loc) · 4.15 KB
/
controller.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
package app
import (
"context"
"fmt"
"reflect"
"sort"
riov1 "github.com/rancher/rio/pkg/apis/rio.cattle.io/v1"
"github.com/rancher/rio/pkg/constructors"
riov1controller "github.com/rancher/rio/pkg/generated/controllers/rio.cattle.io/v1"
"github.com/rancher/rio/pkg/indexes"
"github.com/rancher/rio/pkg/serviceports"
"github.com/rancher/rio/pkg/services"
"github.com/rancher/rio/types"
corev1controller "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/relatedresource"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
)
func Register(ctx context.Context, rContext *types.Context) error {
appSelector, err := labels.Parse("rio.cattle.io/app")
if err != nil {
return err
}
h := &handler{
serviceCache: rContext.Rio.Rio().V1().Service().Cache(),
services: rContext.Core.Core().V1().Service(),
appSelector: appSelector,
}
rContext.Rio.Rio().V1().Service().OnChange(ctx, "app", h.onRioServiceChange)
rContext.Core.Core().V1().Service().OnChange(ctx, "app", h.onServiceChange)
relatedresource.Watch(ctx, "app",
resolveAppService,
rContext.Core.Core().V1().Service(),
rContext.Rio.Rio().V1().Service())
return nil
}
func resolveAppService(_, _ string, obj runtime.Object) ([]relatedresource.Key, error) {
svc, ok := obj.(*riov1.Service)
if !ok {
return nil, nil
}
app, _ := services.AppAndVersion(svc)
return []relatedresource.Key{
{
Namespace: svc.Namespace,
Name: app,
},
}, nil
}
type handler struct {
serviceCache riov1controller.ServiceCache
services corev1controller.ServiceController
appSelector labels.Selector
}
func (h *handler) onRioServiceChange(key string, svc *riov1.Service) (*riov1.Service, error) {
if svc == nil {
return nil, nil
}
appName, _ := services.AppAndVersion(svc)
revisions, err := h.serviceCache.GetByIndex(indexes.ServiceByApp, fmt.Sprintf("%s/%s", svc.Namespace, appName))
if err != nil || len(revisions) == 0 {
return svc, err
}
existingSvc, err := h.services.Cache().Get(svc.Namespace, appName)
if err == nil {
ports := portsForService(revisions)
if !reflect.DeepEqual(existingSvc, ports) {
existingSvc.Spec.Ports = ports
if _, err := h.services.Update(existingSvc); err != nil {
return svc, err
}
return svc, nil
}
// Already Exists
return svc, nil
}
appService := newService(svc.Namespace, appName, revisions)
_, err = h.services.Create(appService)
if errors.IsAlreadyExists(err) {
// Already Exists
return svc, nil
}
return svc, err
}
func (h *handler) onServiceChange(key string, svc *corev1.Service) (*corev1.Service, error) {
if svc == nil {
return nil, nil
}
appName := svc.Labels["rio.cattle.io/app"]
if appName == "" {
return svc, nil
}
values, err := h.serviceCache.GetByIndex(indexes.ServiceByApp, fmt.Sprintf("%s/%s", svc.Namespace, appName))
if err != nil {
return svc, err
}
if len(values) == 0 {
return svc, h.services.Delete(svc.Namespace, svc.Name, nil)
}
return svc, nil
}
func newService(namespace, app string, serviceSet []*riov1.Service) *corev1.Service {
ports := portsForService(serviceSet)
return constructors.NewService(namespace, app, corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"rio.cattle.io/app": app,
},
},
Spec: corev1.ServiceSpec{
Ports: ports,
Selector: map[string]string{
"app": app,
},
Type: corev1.ServiceTypeClusterIP,
},
})
}
func portsForService(serviceSet []*riov1.Service) (result []corev1.ServicePort) {
ports := map[string]corev1.ServicePort{}
for _, rev := range serviceSet {
for _, port := range serviceports.ServiceNamedPorts(rev) {
ports[port.Name] = port
}
}
for _, port := range ports {
result = append(result, port)
}
sort.Slice(result, func(i, j int) bool {
return result[i].Name < result[j].Name
})
if len(result) == 0 {
return []corev1.ServicePort{
{
Name: "default",
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromInt(80),
Port: 80,
},
}
}
return
}