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
182 lines (154 loc) · 5.86 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
175
176
177
178
179
180
181
182
package service
import (
"context"
"fmt"
"github.com/rancher/rio/modules/istio/controllers/service/populate"
"github.com/rancher/rio/modules/istio/pkg/domains"
adminv1 "github.com/rancher/rio/pkg/apis/admin.rio.cattle.io/v1"
riov1 "github.com/rancher/rio/pkg/apis/rio.cattle.io/v1"
"github.com/rancher/rio/pkg/constants"
adminv1controller "github.com/rancher/rio/pkg/generated/controllers/admin.rio.cattle.io/v1"
riov1controller "github.com/rancher/rio/pkg/generated/controllers/rio.cattle.io/v1"
v1 "github.com/rancher/rio/pkg/generated/controllers/rio.cattle.io/v1"
services2 "github.com/rancher/rio/pkg/services"
"github.com/rancher/rio/pkg/stackobject"
"github.com/rancher/rio/types"
corev1controller "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/objectset"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
)
const (
serviceDomainUpdate = "service-domain-update"
appDomainHandler = "app-domain-update"
)
func Register(ctx context.Context, rContext *types.Context) error {
c := stackobject.NewGeneratingController(ctx, rContext, "routing-service", rContext.Rio.Rio().V1().Service())
c.Apply = c.Apply.WithStrictCaching().
WithCacheTypes(rContext.Networking.Networking().V1alpha3().DestinationRule(),
rContext.Networking.Networking().V1alpha3().VirtualService(),
rContext.Extensions.Extensions().V1beta1().Ingress())
sh := &serviceHandler{
systemNamespace: rContext.Namespace,
serviceClient: rContext.Rio.Rio().V1().Service(),
serviceCache: rContext.Rio.Rio().V1().Service().Cache(),
secretCache: rContext.Core.Core().V1().Secret().Cache(),
externalServiceCache: rContext.Rio.Rio().V1().ExternalService().Cache(),
clusterDomainCache: rContext.Global.Admin().V1().ClusterDomain().Cache(),
publicDomainCache: rContext.Global.Admin().V1().PublicDomain().Cache(),
}
rContext.Rio.Rio().V1().Service().OnChange(ctx, serviceDomainUpdate, riov1controller.UpdateServiceOnChange(rContext.Rio.Rio().V1().Service().Updater(), sh.syncDomain))
rContext.Rio.Rio().V1().App().OnChange(ctx, appDomainHandler, riov1controller.UpdateAppOnChange(rContext.Rio.Rio().V1().App().Updater(), sh.syncAppDomain))
c.Populator = sh.populate
return nil
}
type serviceHandler struct {
systemNamespace string
serviceClient v1.ServiceClient
serviceCache v1.ServiceCache
secretCache corev1controller.SecretCache
externalServiceCache v1.ExternalServiceCache
clusterDomainCache adminv1controller.ClusterDomainCache
publicDomainCache adminv1controller.PublicDomainCache
}
func (s *serviceHandler) populate(obj runtime.Object, namespace *corev1.Namespace, os *objectset.ObjectSet) error {
service := obj.(*riov1.Service)
if service.Spec.DisableServiceMesh {
return nil
}
clusterDomain, err := s.clusterDomainCache.Get(s.systemNamespace, constants.ClusterDomainName)
if err != nil {
return err
}
if err := populate.DestinationRulesAndVirtualServices(s.systemNamespace, clusterDomain, service, os); err != nil {
return err
}
return err
}
func (s *serviceHandler) syncDomain(key string, svc *riov1.Service) (*riov1.Service, error) {
if svc == nil {
return svc, nil
}
if svc.DeletionTimestamp != nil {
return svc, nil
}
clusterDomain, err := s.clusterDomainCache.Get(s.systemNamespace, constants.ClusterDomainName)
if err != nil {
return svc, err
}
updateDomain(svc, clusterDomain)
return svc, nil
}
func (s *serviceHandler) syncAppDomain(key string, obj *riov1.App) (*riov1.App, error) {
if obj == nil {
return obj, nil
}
if obj.DeletionTimestamp != nil {
return obj, nil
}
clusterDomain, err := s.clusterDomainCache.Get(s.systemNamespace, constants.ClusterDomainName)
if err != nil {
return obj, err
}
updateAppDomain(obj, clusterDomain)
return obj, nil
}
func updateAppDomain(app *riov1.App, clusterDomain *adminv1.ClusterDomain) {
public := true
for _, svc := range app.Spec.Revisions {
if !svc.Public {
public = false
break
}
}
protocol := "http"
if clusterDomain.Status.HTTPSSupported {
protocol = "https"
}
var endpoints []string
if public && clusterDomain.Status.ClusterDomain != "" {
endpoints = append(endpoints, fmt.Sprintf("%s://%s", protocol, domains.GetExternalDomain(app.Name, app.Namespace, clusterDomain.Status.ClusterDomain)))
}
for _, pd := range app.Status.PublicDomains {
endpoints = append(endpoints, fmt.Sprintf("%s://%s", protocol, pd))
}
for i, endpoint := range endpoints {
if protocol == "http" && constants.DefaultHTTPOpenPort != "80" {
endpoints[i] = fmt.Sprintf("%s:%s", endpoint, constants.DefaultHTTPOpenPort)
}
if protocol == "https" && constants.DefaultHTTPOpenPort != "443" {
endpoints[i] = fmt.Sprintf("%s:%s", endpoint, constants.DefaultHTTPSOpenPort)
}
}
app.Status.Endpoints = endpoints
}
func updateDomain(service *riov1.Service, clusterDomain *adminv1.ClusterDomain) {
public := false
for _, port := range service.Spec.Ports {
if !port.InternalOnly {
public = true
break
}
}
protocol := "http"
if clusterDomain.Status.HTTPSSupported {
protocol = "https"
}
var endpoints []string
if public && clusterDomain.Status.ClusterDomain != "" {
app, version := services2.AppAndVersion(service)
endpoints = append(endpoints, fmt.Sprintf("%s://%s", protocol, domains.GetExternalDomain(app+"-"+version, service.Namespace, clusterDomain.Status.ClusterDomain)))
}
for _, pd := range service.Status.PublicDomains {
endpoints = append(endpoints, fmt.Sprintf("%s://%s", protocol, pd))
}
for i, endpoint := range endpoints {
if protocol == "http" && constants.DefaultHTTPOpenPort != "80" {
endpoints[i] = fmt.Sprintf("%s:%s", endpoint, constants.DefaultHTTPOpenPort)
}
if protocol == "https" && constants.DefaultHTTPOpenPort != "443" {
endpoints[i] = fmt.Sprintf("%s:%s", endpoint, constants.DefaultHTTPSOpenPort)
}
}
service.Status.Endpoints = endpoints
}