@@ -15,12 +15,12 @@ import (
1515 "strings"
1616 "syscall"
1717
18+ pb "github.com/envoyproxy/gateway/proto/extension"
19+ corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
1820 "github.com/urfave/cli/v2"
1921 "google.golang.org/grpc"
2022 "google.golang.org/grpc/codes"
2123 "google.golang.org/grpc/status"
22-
23- pb "github.com/envoyproxy/gateway/proto/extension"
2424)
2525
2626func main () {
@@ -54,16 +54,25 @@ func main() {
5454 DefaultText : "Info" ,
5555 Value : "Info" ,
5656 },
57+ & cli.StringFlag {
58+ Name : "suffix" ,
59+ Usage : "suffix appended to VirtualHost domains and used in response headers" ,
60+ DefaultText : "extserver" ,
61+ Value : "extserver" ,
62+ },
5763 },
5864 },
5965 },
6066 }
61- app .Run (os .Args )
67+ err := app .Run (os .Args )
68+ if err != nil {
69+ panic (err )
70+ }
6271}
6372
6473var grpcServer * grpc.Server
6574
66- func handleSignals (cCtx * cli.Context ) error {
75+ func handleSignals (_ * cli.Context ) error {
6776 c := make (chan os.Signal , 1 )
6877 signal .Notify (c , os .Interrupt , syscall .SIGQUIT )
6978 go func () {
@@ -94,57 +103,70 @@ func startExtensionServer(cCtx *cli.Context) error {
94103 var opts []grpc.ServerOption
95104 grpcServer = grpc .NewServer (opts ... )
96105 sig := make (chan int , 1 )
97- pb .RegisterEnvoyGatewayExtensionServer (grpcServer , New (logger , sig ))
106+ pb .RegisterEnvoyGatewayExtensionServer (grpcServer , New (logger , sig , cCtx . String ( "suffix" ) ))
98107 return grpcServer .Serve (lis )
99108}
100109
101110type Server struct {
102111 pb.UnimplementedEnvoyGatewayExtensionServer
103- sig chan int
104- log * slog.Logger
112+ sig chan int
113+ log * slog.Logger
114+ suffix string
105115}
106116
107- func New (logger * slog.Logger , sig chan int ) * Server {
117+ func New (logger * slog.Logger , sig chan int , suffix string ) * Server {
108118 return & Server {
109- log : logger ,
110- sig : sig ,
119+ log : logger ,
120+ sig : sig ,
121+ suffix : suffix ,
111122 }
112123}
113124
114- func (s * Server ) PostRouteModify (ctx context.Context , req * pb.PostRouteModifyRequest ) (* pb.PostRouteModifyResponse , error ) {
125+ func (s * Server ) PostRouteModify (_ context.Context , req * pb.PostRouteModifyRequest ) (* pb.PostRouteModifyResponse , error ) {
115126 s .log .Info ("PostRouteModify callback was invoked" )
116127
128+ if req .PostRouteContext != nil && len (req .PostRouteContext .ExtensionResources ) > 0 {
129+ s .log .Info ("PostRouteModify received extension resources, adding response header" ,
130+ slog .Int ("count" , len (req .PostRouteContext .ExtensionResources )))
131+ req .Route .ResponseHeadersToAdd = append (req .Route .ResponseHeadersToAdd , & corev3.HeaderValueOption {
132+ Header : & corev3.HeaderValue {
133+ Key : "x-ext-server" ,
134+ Value : s .suffix ,
135+ },
136+ })
137+ }
138+
117139 return & pb.PostRouteModifyResponse {
118140 Route : req .Route ,
119141 }, nil
120142}
121143
122- func (s * Server ) PostVirtualHostModify (ctx context.Context , req * pb.PostVirtualHostModifyRequest ) (* pb.PostVirtualHostModifyResponse , error ) {
144+ func (s * Server ) PostVirtualHostModify (_ context.Context , req * pb.PostVirtualHostModifyRequest ) (* pb.PostVirtualHostModifyResponse , error ) {
123145 s .log .Info ("PostVirtualHostModify callback was invoked" )
124146
125147 if strings .Contains (req .VirtualHost .Name , "fail" ) {
126148 s .log .Info ("PostVirtualHostModify returning unavailable error" )
127149 return nil , status .Error (codes .Unavailable , "Service is currently unavailable" )
128- } else {
129- s .log .Info ("PostVirtualHostModify sending response" )
130- if len (req .VirtualHost .Domains ) > 0 {
131- req .VirtualHost .Domains = append (req .VirtualHost .Domains , fmt .Sprintf ("%s.extserver" , req .VirtualHost .Domains [0 ]))
132- }
133- return & pb.PostVirtualHostModifyResponse {
134- VirtualHost : req .VirtualHost ,
135- }, nil
136150 }
151+
152+ s .log .Info ("PostVirtualHostModify sending response" )
153+ if len (req .VirtualHost .Domains ) > 0 {
154+ req .VirtualHost .Domains = append (req .VirtualHost .Domains , fmt .Sprintf ("%s.%s" , req .VirtualHost .Domains [0 ], s .suffix ))
155+ }
156+ return & pb.PostVirtualHostModifyResponse {
157+ VirtualHost : req .VirtualHost ,
158+ }, nil
137159}
138160
139- func (s * Server ) PostHTTPListenerModify (ctx context.Context , req * pb.PostHTTPListenerModifyRequest ) (* pb.PostHTTPListenerModifyResponse , error ) {
161+ func (s * Server ) PostHTTPListenerModify (_ context.Context , req * pb.PostHTTPListenerModifyRequest ) (* pb.PostHTTPListenerModifyResponse , error ) {
140162 s .log .Info ("postHTTPListenerModify callback was invoked" )
141163
142164 return & pb.PostHTTPListenerModifyResponse {
143165 Listener : req .Listener ,
144166 }, nil
145167}
146168
147- func (s * Server ) PostTranslateModify (ctx context.Context , req * pb.PostTranslateModifyRequest ) (* pb.PostTranslateModifyResponse , error ) {
169+ func (s * Server ) PostTranslateModify (_ context.Context , req * pb.PostTranslateModifyRequest ) (* pb.PostTranslateModifyResponse , error ) {
148170 s .log .Info ("PostTranslateModify callback was invoked" )
149171
150172 return & pb.PostTranslateModifyResponse {
0 commit comments