-
Notifications
You must be signed in to change notification settings - Fork 347
/
runner.go
170 lines (145 loc) · 4.92 KB
/
runner.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
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package runner
import (
"context"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/gateway-api/apis/v1beta1"
"sigs.k8s.io/yaml"
"github.com/envoyproxy/gateway/api/config/v1alpha1"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
extension "github.com/envoyproxy/gateway/internal/extension/types"
"github.com/envoyproxy/gateway/internal/gatewayapi"
"github.com/envoyproxy/gateway/internal/message"
"github.com/envoyproxy/gateway/internal/provider/utils"
)
type Config struct {
config.Server
ProviderResources *message.ProviderResources
XdsIR *message.XdsIR
InfraIR *message.InfraIR
ExtensionManager extension.Manager
}
type Runner struct {
Config
}
func New(cfg *Config) *Runner {
return &Runner{Config: *cfg}
}
func (r *Runner) Name() string {
return string(v1alpha1.LogComponentGatewayApiRunner)
}
// Start starts the gateway-api translator runner
func (r *Runner) Start(ctx context.Context) error {
r.Logger = r.Logger.WithName(r.Name()).WithValues("runner", r.Name())
go r.subscribeAndTranslate(ctx)
r.Logger.Info("started")
return nil
}
func (r *Runner) subscribeAndTranslate(ctx context.Context) {
message.HandleSubscription(r.ProviderResources.GatewayAPIResources.Subscribe(ctx),
func(update message.Update[string, *gatewayapi.Resources]) {
val := update.Value
if update.Delete || val == nil {
return
}
// Translate and publish IRs.
t := &gatewayapi.Translator{
GatewayControllerName: r.Server.EnvoyGateway.Gateway.ControllerName,
GatewayClassName: v1beta1.ObjectName(update.Key),
GlobalRateLimitEnabled: r.EnvoyGateway.RateLimit != nil,
}
// If an extension is loaded, pass its supported groups/kinds to the translator
if r.EnvoyGateway.Extension != nil {
var extGKs []schema.GroupKind
for _, gvk := range r.EnvoyGateway.Extension.Resources {
extGKs = append(extGKs, schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind})
}
t.ExtensionGroupKinds = extGKs
}
// Translate to IR
result := t.Translate(val)
yamlXdsIR, _ := yaml.Marshal(&result.XdsIR)
r.Logger.WithValues("output", "xds-ir").Info(string(yamlXdsIR))
yamlInfraIR, _ := yaml.Marshal(&result.InfraIR)
r.Logger.WithValues("output", "infra-ir").Info(string(yamlInfraIR))
var curKeys, newKeys []string
// Get current IR keys
for key := range r.InfraIR.LoadAll() {
curKeys = append(curKeys, key)
}
// Publish the IRs.
// Also validate the ir before sending it.
for key, val := range result.InfraIR {
if err := val.Validate(); err != nil {
r.Logger.Error(err, "unable to validate infra ir, skipped sending it")
} else {
r.InfraIR.Store(key, val)
newKeys = append(newKeys, key)
}
}
for key, val := range result.XdsIR {
if err := val.Validate(); err != nil {
r.Logger.Error(err, "unable to validate xds ir, skipped sending it")
} else {
r.XdsIR.Store(key, val)
}
}
// Delete keys
// There is a 1:1 mapping between infra and xds IR keys
delKeys := getIRKeysToDelete(curKeys, newKeys)
for _, key := range delKeys {
r.InfraIR.Delete(key)
r.XdsIR.Delete(key)
}
// Update Status
for _, gateway := range result.Gateways {
key := utils.NamespacedName(gateway)
r.ProviderResources.GatewayStatuses.Store(key, &gateway.Status)
}
for _, httpRoute := range result.HTTPRoutes {
key := utils.NamespacedName(httpRoute)
r.ProviderResources.HTTPRouteStatuses.Store(key, &httpRoute.Status)
}
for _, grpcRoute := range result.GRPCRoutes {
key := utils.NamespacedName(grpcRoute)
r.ProviderResources.GRPCRouteStatuses.Store(key, &grpcRoute.Status)
}
for _, tlsRoute := range result.TLSRoutes {
key := utils.NamespacedName(tlsRoute)
r.ProviderResources.TLSRouteStatuses.Store(key, &tlsRoute.Status)
}
for _, tcpRoute := range result.TCPRoutes {
key := utils.NamespacedName(tcpRoute)
r.ProviderResources.TCPRouteStatuses.Store(key, &tcpRoute.Status)
}
for _, udpRoute := range result.UDPRoutes {
key := utils.NamespacedName(udpRoute)
r.ProviderResources.UDPRouteStatuses.Store(key, &udpRoute.Status)
}
},
)
r.Logger.Info("shutting down")
}
// getIRKeysToDelete returns the list of IR keys to delete
// based on the difference between the current keys and the
// new keys parameters passed to the function.
func getIRKeysToDelete(curKeys, newKeys []string) []string {
var delKeys []string
remaining := make(map[string]bool)
// Add all current keys to the remaining map
for _, key := range curKeys {
remaining[key] = true
}
// Delete newKeys from the remaining map
// to get keys that need to be deleted
for _, key := range newKeys {
delete(remaining, key)
}
for key := range remaining {
delKeys = append(delKeys, key)
}
return delKeys
}