This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
gloo.go
154 lines (140 loc) · 3.78 KB
/
gloo.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
package vsfactory
import (
"net"
"github.com/rancher/wrangler/pkg/name"
soloapiv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
solov1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1/kube/apis/gateway.solo.io/v1"
gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/core/matchers"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/headers"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/retries"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func newGlooVirtualService(namespace, name string, hosts []string, targets ...target) *solov1.VirtualService {
vs := &solov1.VirtualService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: soloapiv1.VirtualService{
VirtualHost: &soloapiv1.VirtualHost{
Domains: hosts,
Routes: []*soloapiv1.Route{
{
Matchers: []*matchers.Matcher{
{
PathSpecifier: &matchers.Matcher_Prefix{
Prefix: "/",
},
},
},
},
},
},
},
}
vs.Spec.VirtualHost.Routes[0].Action = newRouteAction(targets...)
vs.Spec.VirtualHost.Routes[0].Options = newRoutePlugin(targets...)
return vs
}
func tlsCopy(hostname, tlsNamespace, tlsName string, vs *solov1.VirtualService) *solov1.VirtualService {
vsTLS := vs.DeepCopy()
vsTLS.Name = name.SafeConcatName(vsTLS.Name, "tls", tlsName)
host, _, err := net.SplitHostPort(hostname)
if err != nil {
host = hostname
}
vsTLS.Spec.SslConfig = &gloov1.SslConfig{
SslSecrets: &gloov1.SslConfig_SecretRef{
SecretRef: &core.ResourceRef{
Name: tlsName,
Namespace: tlsNamespace,
},
},
SniDomains: []string{
host,
},
}
vsTLS.Spec.VirtualHost.Domains = []string{hostname}
return vsTLS
}
func newRouteAction(targets ...target) *soloapiv1.Route_RouteAction {
if len(targets) == 0 {
return nil
}
if len(targets) == 1 {
return single(targets[0])
}
var dest []*gloov1.WeightedDestination
for _, target := range targets {
dest = append(dest, &gloov1.WeightedDestination{
Destination: destination(target),
Weight: uint32(target.Weight),
})
}
return &soloapiv1.Route_RouteAction{
RouteAction: &gloov1.RouteAction{
Destination: &gloov1.RouteAction_Multi{
Multi: &gloov1.MultiDestination{
Destinations: dest,
},
},
},
}
}
func destination(target target) *gloov1.Destination {
return &gloov1.Destination{
DestinationType: &gloov1.Destination_Kube{
Kube: &gloov1.KubernetesServiceDestination{
Ref: core.ResourceRef{
Name: target.Name,
Namespace: target.Namespace,
},
Port: uint32(target.Port),
},
},
}
}
func single(target target) *soloapiv1.Route_RouteAction {
return &soloapiv1.Route_RouteAction{
RouteAction: &gloov1.RouteAction{
Destination: &gloov1.RouteAction_Single{
Single: destination(target),
},
},
}
}
func newRoutePlugin(targets ...target) *v1.RouteOptions {
for _, t := range targets {
// use envoy to retry the connection if the service is being scaled up and increase envoy timeout
if t.ScaleIsZero {
return &v1.RouteOptions{
Timeout: &overallTimeout,
Retries: &retries.RetryPolicy{
RetryOn: "5xx",
NumRetries: scaleFromZeroAttempts,
PerTryTimeout: &perTryTimeout,
},
HeaderManipulation: &headers.HeaderManipulation{
RequestHeadersToAdd: []*headers.HeaderValueOption{
{
Header: &headers.HeaderValue{
Key: rioNameHeader,
Value: t.OriginalTarget.Name,
},
},
{
Header: &headers.HeaderValue{
Key: rioNamespaceHeader,
Value: t.OriginalTarget.Namespace,
},
},
},
},
}
}
}
return nil
}