forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-proxy-controller.go
217 lines (178 loc) · 6.03 KB
/
example-proxy-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
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
//go:build ignore
// +build ignore
package docs_demo
// package main
// all the import's we'll need for this controller
import (
"context"
"log"
"os"
"time"
"github.com/solo-io/k8s-utils/kubeutils"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/factory"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/kube"
core "github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
v1 "github.com/kgateway-dev/kgateway/projects/gloo/pkg/api/v1"
matchers "github.com/kgateway-dev/kgateway/projects/gloo/pkg/api/v1/core/matchers"
// import for GKE
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
func main() {
// root context for the whole thing
ctx := context.Background()
// initialize Gloo API clients
upstreamClient, proxyClient := initGlooClients(ctx)
// start a watch on upstreams. we'll use this as our trigger
// whenever upstreams are modified, we'll trigger our sync function
upstreamWatch, watchErrors, initError := upstreamClient.Watch("gloo-system",
clients.WatchOpts{Ctx: ctx})
must(initError)
// our "event loop". an event occurs whenever the list of upstreams has been updated
for {
select {
// if we error during watch, just exit
case err := <-watchErrors:
must(err)
// process a new upstream list
case newUpstreamList := <-upstreamWatch:
// we received a new list of upstreams from our watch,
resync(ctx, newUpstreamList, proxyClient)
}
}
}
// we received a new list of upstreams! regenerate the desired proxy
// and write it as a CRD to Kubernetes
func resync(ctx context.Context, upstreams v1.UpstreamList, client v1.ProxyClient) {
desiredProxy := makeDesiredProxy(upstreams)
// see if the proxy exists. if yes, update; if no, create
existingProxy, err := client.Read(
desiredProxy.Metadata.Namespace,
desiredProxy.Metadata.Name,
clients.ReadOpts{Ctx: ctx})
// proxy exists! this is an update, not a create
if err == nil {
// sleep for 1s as Gloo may be re-validating our proxy, which can cause resource version to change
time.Sleep(time.Second)
// ensure resource version is the latest
existingProxy, err = client.Read(
desiredProxy.Metadata.Namespace,
desiredProxy.Metadata.Name,
clients.ReadOpts{Ctx: ctx})
must(err)
// update the resource version on our desired proxy
desiredProxy.Metadata.ResourceVersion = existingProxy.Metadata.ResourceVersion
}
// write!
written, err := client.Write(desiredProxy,
clients.WriteOpts{Ctx: ctx, OverwriteExisting: true})
must(err)
log.Printf("wrote proxy object: %+v\n", written)
}
func initGlooClients(ctx context.Context) (v1.UpstreamClient, v1.ProxyClient) {
// root rest config
restConfig, err := kubeutils.GetConfig(
os.Getenv("KUBERNETES_MASTER_URL"),
os.Getenv("KUBECONFIG"))
must(err)
// wrapper for kubernetes shared informer factory
cache := kube.NewKubeCache(ctx)
// initialize the CRD client for Gloo Upstreams
upstreamClient, err := v1.NewUpstreamClient(&factory.KubeResourceClientFactory{
Crd: v1.UpstreamCrd,
Cfg: restConfig,
SharedCache: cache,
})
must(err)
// registering the client registers the type with the client cache
err = upstreamClient.Register()
must(err)
// initialize the CRD client for Gloo Proxies
proxyClient, err := v1.NewProxyClient(&factory.KubeResourceClientFactory{
Crd: v1.ProxyCrd,
Cfg: restConfig,
SharedCache: cache,
})
must(err)
// registering the client registers the type with the client cache
err = proxyClient.Register()
must(err)
return upstreamClient, proxyClient
}
// in this function we'll generate an opinionated
// proxy object with a routes for each of our upstreams
func makeDesiredProxy(upstreams v1.UpstreamList) *v1.Proxy {
// each virtual host represents the table of routes for a given
// domain or set of domains.
// in this example, we'll create one virtual host
// for each upstream.
var virtualHosts []*v1.VirtualHost
for _, upstream := range upstreams {
upstreamRef := upstream.Metadata.Ref()
// create a virtual host for each upstream
vHostForUpstream := &v1.VirtualHost{
// logical name of the virtual host, should be unique across vhosts
Name: upstream.Metadata.Name,
// the domain will be our "matcher".
// requests with the Host header equal to the upstream name
// will be routed to this upstream
Domains: []string{upstream.Metadata.Name},
// we'll create just one route designed to match any request
// and send it to the upstream for this domain
Routes: []*v1.Route{{
// use a basic catch-all matcher
Matchers: []*matchers.Matcher{
{
PathSpecifier: &matchers.Matcher_Prefix{
Prefix: "/",
},
},
},
// tell Gloo where to send the requests
Action: &v1.Route_RouteAction{
RouteAction: &v1.RouteAction{
Destination: &v1.RouteAction_Single{
// single destination
Single: &v1.Destination{
DestinationType: &v1.Destination_Upstream{
// a "reference" to the upstream, which is a Namespace/Name tuple
Upstream: &upstreamRef,
},
},
},
},
},
}},
}
virtualHosts = append(virtualHosts, vHostForUpstream)
}
desiredProxy := &v1.Proxy{
// metadata will be translated to Kubernetes ObjectMeta
Metadata: core.Metadata{Namespace: "gloo-system", Name: "my-cool-proxy"},
// we have the option of creating multiple listeners,
// but for the purpose of this example we'll just use one
Listeners: []*v1.Listener{{
// logical name for the listener
Name: "my-amazing-listener",
// instruct envoy to bind to all interfaces on port 8080
BindAddress: "::", BindPort: 8080,
// at this point you determine what type of listener
// to use. here we'll be using the HTTP Listener
// other listener types are currently unsupported,
// but future
ListenerType: &v1.Listener_HttpListener{
HttpListener: &v1.HttpListener{
// insert our list of virtual hosts here
VirtualHosts: virtualHosts,
},
}},
},
}
return desiredProxy
}
// make our lives easy
func must(err error) {
if err != nil {
panic(err)
}
}