Skip to content

Commit 133feac

Browse files
committed
Use strongly type IR when sending data to remote infra server
Signed-off-by: Zachary Nixon <nixozach@amazon.com>
1 parent 4d7bb2b commit 133feac

16 files changed

Lines changed: 3725 additions & 364 deletions

File tree

examples/remote-infra/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ The provider implements the four RPCs defined in
2424
| `CreateOrUpdateRateLimitInfra` | No-op. Rate limiting must be provisioned out of band, see below |
2525
| `DeleteRateLimitInfra` | No-op |
2626

27-
The IR is delivered as JSON in the request's `ir_bytes` field. The example
28-
ignores fields it doesn't understand, which is the same forward-compatibility
29-
posture you'll want in your own provider.
27+
The IR is delivered as a structured `Infra` protobuf message in the request's
28+
`infra` field. The example maps only the subset of fields it understands onto
29+
its own pared-down types (see `synthesizer/ir.go`) and ignores the rest, which
30+
is the same forward-compatibility posture you'll want in your own provider.
3031

3132
## Layout
3233

examples/remote-infra/main.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ package main
1313

1414
import (
1515
"context"
16-
"encoding/json"
1716
"errors"
1817
"flag"
1918
"fmt"
@@ -22,11 +21,12 @@ import (
2221
"os"
2322
"path/filepath"
2423

25-
"github.com/envoyproxy/gateway-remote-infra/pb"
26-
"github.com/envoyproxy/gateway-remote-infra/synthesizer"
2724
"google.golang.org/grpc"
2825
"sigs.k8s.io/controller-runtime/pkg/client"
2926
clicfg "sigs.k8s.io/controller-runtime/pkg/client/config"
27+
28+
"github.com/envoyproxy/gateway-remote-infra/pb"
29+
"github.com/envoyproxy/gateway-remote-infra/synthesizer"
3030
)
3131

3232
var (
@@ -43,27 +43,64 @@ type basicServer struct {
4343
}
4444

4545
func (bs *basicServer) CreateOrUpdateProxyInfra(ctx context.Context, req *pb.CreateOrUpdateProxyInfraRequest) (*pb.CreateOrUpdateProxyInfraResponse, error) {
46-
ir := new(synthesizer.Infra{})
47-
if err := json.Unmarshal(req.GetIrBytes(), ir); err != nil {
48-
return nil, fmt.Errorf("failed to unmarshal IR: %w", err)
49-
}
46+
ir := infraFromProto(req.GetInfra())
5047

5148
fmt.Printf("Creating proxy infra [%v]\n", ir)
5249
err := bs.synth.CreateOrUpdate(ctx, ir)
5350
return new(pb.CreateOrUpdateProxyInfraResponse{}), err
5451
}
5552

5653
func (bs *basicServer) DeleteProxyInfra(ctx context.Context, req *pb.DeleteProxyInfraRequest) (*pb.DeleteProxyInfraResponse, error) {
57-
ir := new(synthesizer.Infra{})
58-
if err := json.Unmarshal(req.GetIrBytes(), ir); err != nil {
59-
return nil, fmt.Errorf("failed to unmarshal IR: %w", err)
60-
}
54+
ir := infraFromProto(req.GetInfra())
6155

6256
fmt.Printf("Deleting proxy infra [%v]\n", ir)
6357
err := bs.synth.Delete(ctx, ir)
6458
return new(pb.DeleteProxyInfraResponse{}), err
6559
}
6660

61+
// infraFromProto maps the structured proto IR onto the pared-down
62+
// synthesizer.Infra used by this example. Fields the synthesizer does not
63+
// consume (proxy config, addresses, resolved metric sinks, and the owner
64+
// reference within metadata) are intentionally dropped, demonstrating the
65+
// forward-compatible posture a provider should adopt: read only what you
66+
// understand.
67+
func infraFromProto(in *pb.Infra) *synthesizer.Infra {
68+
if in == nil || in.GetProxy() == nil {
69+
return new(synthesizer.Infra{})
70+
}
71+
72+
p := in.GetProxy()
73+
proxy := &synthesizer.ProxyInfra{
74+
Name: p.GetName(),
75+
Namespace: p.GetNamespace(),
76+
}
77+
78+
if md := p.GetMetadata(); md != nil {
79+
proxy.Metadata = &synthesizer.InfraMetadata{
80+
Annotations: md.GetAnnotations(),
81+
Labels: md.GetLabels(),
82+
}
83+
}
84+
85+
for _, l := range p.GetListeners() {
86+
if l == nil {
87+
continue
88+
}
89+
listener := &synthesizer.ProxyListener{Name: l.GetName()}
90+
for _, port := range l.GetPorts() {
91+
listener.Ports = append(listener.Ports, synthesizer.ListenerPort{
92+
Name: port.GetName(),
93+
Protocol: port.GetProtocol(),
94+
ServicePort: port.GetServicePort(),
95+
ContainerPort: port.GetContainerPort(),
96+
})
97+
}
98+
proxy.Listeners = append(proxy.Listeners, listener)
99+
}
100+
101+
return &synthesizer.Infra{Proxy: proxy}
102+
}
103+
67104
func (bs *basicServer) CreateOrUpdateRateLimitInfra(_ context.Context, _ *pb.CreateOrUpdateRateLimitInfraRequest) (*pb.CreateOrUpdateRateLimitInfraResponse, error) {
68105
return new(pb.CreateOrUpdateRateLimitInfraResponse{}), nil
69106
}

0 commit comments

Comments
 (0)