/
route.go
254 lines (217 loc) · 6.92 KB
/
route.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
// Copyright 2019 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package route
import (
"fmt"
"strings"
"github.com/maistra/ior/pkg/bootstrap"
"github.com/maistra/ior/pkg/util"
v1 "github.com/openshift/api/route/v1"
routev1 "github.com/openshift/client-go/route/clientset/versioned/typed/route/v1"
mcp "istio.io/api/mcp/v1alpha1"
networking "istio.io/api/networking/v1alpha3"
"istio.io/istio/pkg/kube"
"istio.io/pkg/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
maistraPrefix = "maistra.io/"
generatedByLabel = maistraPrefix + "generated-by"
generatedByValue = "ior"
originalHostAnnotation = maistraPrefix + "original-host"
gatewayNameLabel = maistraPrefix + "gateway-name"
gatewayNamespaceLabel = maistraPrefix + "gateway-namespace"
)
// GatewayInfo ...
type GatewayInfo struct {
Metadata *mcp.Metadata
Gateway *networking.Gateway
}
type syncedRoute struct {
route *v1.Route
valid bool
}
// Route ...
type Route struct {
args *bootstrap.Args
client *routev1.RouteV1Client
ig *IngressGateway
routes map[string]*syncedRoute
}
// New ...
func New(args *bootstrap.Args) (*Route, error) {
ig, err := NewIG()
if err != nil {
return nil, err
}
r := &Route{args: args, ig: ig}
err = r.initClient()
if err != nil {
return nil, err
}
err = r.initRoutes()
if err != nil {
return nil, err
}
return r, nil
}
func getHost(route v1.Route) string {
if host := route.ObjectMeta.Annotations[originalHostAnnotation]; host != "" {
return host
}
return route.Spec.Host
}
func (r *Route) initRoutes() error {
routes, err := r.client.Routes(r.args.Namespace).List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", generatedByLabel, generatedByValue),
})
if err != nil {
return fmt.Errorf("error getting routes: %v", err)
}
r.routes = make(map[string]*syncedRoute, len(routes.Items))
for _, route := range routes.Items {
localRoute := route
r.routes[getHost(localRoute)] = &syncedRoute{
route: &localRoute,
}
}
return nil
}
// DumpRoutes ...
func (r *Route) DumpRoutes() {
out := fmt.Sprintf("%d item(ns)\n", len(r.routes))
for host, route := range r.routes {
out += fmt.Sprintf("%s: %s/%s\n", host, route.route.ObjectMeta.Namespace, route.route.ObjectMeta.Name)
}
log.Debugf("Current state: %s\n", out)
}
// Sync ...
func (r *Route) Sync(gatewaysInfo []GatewayInfo) error {
for _, sRoute := range r.routes {
sRoute.valid = false
}
for _, gatewayInfo := range gatewaysInfo {
for _, server := range gatewayInfo.Gateway.Servers {
for _, host := range server.GetHosts() {
_, ok := r.routes[host]
if ok {
r.editRoute(gatewayInfo.Metadata, host)
} else {
r.createRoute(gatewayInfo.Metadata, gatewayInfo.Gateway, host, server)
}
}
}
}
for _, sRoute := range r.routes {
if !sRoute.valid {
r.deleteRoute(sRoute.route)
}
}
return nil
}
func (r *Route) editRoute(metadata *mcp.Metadata, host string) {
log.Debugf("Editing route for hostname %s", host)
r.routes[host].valid = true
}
func (r *Route) deleteRoute(route *v1.Route) {
var immediate int64
host := getHost(*route)
log.Debugf("Deleting route %s (hostname: %s)", route.ObjectMeta.Name, host)
err := r.client.Routes(r.args.Namespace).Delete(route.ObjectMeta.Name, &metav1.DeleteOptions{GracePeriodSeconds: &immediate})
delete(r.routes, getHost(*route))
if err == nil {
log.Infof("Deleted route %s/%s (hostname: %s)", route.ObjectMeta.Namespace, route.ObjectMeta.Name, host)
} else {
log.Errorf("Error deleting route %s: %s", route.ObjectMeta.Name, err)
}
}
func (r *Route) createRoute(metadata *mcp.Metadata, gateway *networking.Gateway, originalHost string, server *networking.Server) {
var wildcard = v1.WildcardPolicyNone
actualHost := originalHost
log.Debugf("Creating route for hostname %s", originalHost)
if originalHost == "*" {
log.Infof("Gateway %s: Wildcard * is not supported at the moment. Letting OpenShift create the hostname instead.", metadata.Name)
actualHost = ""
} else if strings.HasPrefix(originalHost, "*.") {
// Wildcards are not enabled by default in OCP 3.x.
// See https://docs.openshift.com/container-platform/3.11/install_config/router/default_haproxy_router.html#using-wildcard-routes
wildcard = v1.WildcardPolicySubdomain
actualHost = "wildcard." + strings.TrimPrefix(originalHost, "*.")
}
var tlsConfig *v1.TLSConfig
targetPort := "http2"
if server.Tls != nil {
tlsConfig = &v1.TLSConfig{Termination: v1.TLSTerminationPassthrough}
if server.Tls.HttpsRedirect {
tlsConfig.InsecureEdgeTerminationPolicy = v1.InsecureEdgeTerminationPolicyRedirect
}
targetPort = "https"
}
gatewayNamespace, gatewayName := util.ExtractNameNamespace(metadata.Name)
// FIXME: Should we look for ingress gateway pod/service in all mesh members instead of just in the control plane namespace?
serviceNamespace, serviceName, err := r.ig.FindService([]string{r.args.Namespace}, gateway)
if err != nil {
log.Errorf("Error creating a route for host %s (gateway %s): %s", originalHost, metadata.Name, err)
return
}
nr, err := r.client.Routes(serviceNamespace).Create(&v1.Route{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-%s-", gatewayNamespace, gatewayName),
Labels: map[string]string{
generatedByLabel: generatedByValue,
gatewayNamespaceLabel: gatewayNamespace,
gatewayNameLabel: gatewayName,
},
Annotations: map[string]string{
originalHostAnnotation: originalHost,
},
},
Spec: v1.RouteSpec{
Host: actualHost,
Port: &v1.RoutePort{
TargetPort: intstr.IntOrString{
Type: intstr.String,
StrVal: targetPort,
},
},
To: v1.RouteTargetReference{
Name: serviceName,
},
TLS: tlsConfig,
WildcardPolicy: wildcard,
},
})
if err != nil {
log.Errorf("Error creating a route for host %s (gateway %s): %s", originalHost, metadata.Name, err)
return
}
log.Infof("Created route %s/%s for hostname %s (gateway %s)", nr.ObjectMeta.Namespace, nr.ObjectMeta.Name, nr.Spec.Host, metadata.Name)
r.routes[originalHost] = &syncedRoute{
route: nr,
valid: true,
}
}
func (r *Route) initClient() error {
config, err := kube.BuildClientConfig("", "")
if err != nil {
return fmt.Errorf("error creating a Kubernetes client: %v", err)
}
client, err := routev1.NewForConfig(config)
if err != nil {
return fmt.Errorf("error creating an OpenShift route client: %v", err)
}
r.client = client
return nil
}