|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package router |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "log/slog" |
| 21 | + "net" |
| 22 | + "time" |
| 23 | + |
| 24 | + "golang.org/x/sync/errgroup" |
| 25 | +) |
| 26 | + |
| 27 | +type dataplaneHealthCheck struct { |
| 28 | + url string |
| 29 | + expectedBody string |
| 30 | +} |
| 31 | + |
| 32 | +func (r atenetRouter) routeViaAuthority() bool { |
| 33 | + return r == atenetRouterAgentgateway |
| 34 | +} |
| 35 | + |
| 36 | +func (r atenetRouter) healthCheck() dataplaneHealthCheck { |
| 37 | + switch r { |
| 38 | + case atenetRouterEnvoy: |
| 39 | + return dataplaneHealthCheck{url: "http://127.0.0.1:9901/ready", expectedBody: "LIVE"} |
| 40 | + case atenetRouterAgentgateway: |
| 41 | + return dataplaneHealthCheck{url: "http://127.0.0.1:15021/healthz/ready", expectedBody: "ready"} |
| 42 | + default: |
| 43 | + return dataplaneHealthCheck{} |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (s *RouterServer) startDataplane(ctx context.Context, g *errgroup.Group, parkCfg ParkedRequestConfig) error { |
| 48 | + switch s.cfg.atenetRouter() { |
| 49 | + case atenetRouterEnvoy: |
| 50 | + s.startEnvoyDataplane(ctx, g, parkCfg) |
| 51 | + case atenetRouterAgentgateway: |
| 52 | + // Agentgateway receives all routing configuration from its static file. |
| 53 | + default: |
| 54 | + return fmt.Errorf("unsupported atenet router %q", s.cfg.atenetRouter()) |
| 55 | + } |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +func (s *RouterServer) startEnvoyDataplane(ctx context.Context, g *errgroup.Group, parkCfg ParkedRequestConfig) { |
| 60 | + xdsSrv := NewXdsServer(s.cfg.XdsPort) |
| 61 | + xdsSrv.SetConfig(s.cfg.HttpPort, s.cfg.ExtprocPort, s.cfg.ExtprocAddr) |
| 62 | + setOtlpCollector(ctx, xdsSrv, s.cfg.OtlpCollectorAddress) |
| 63 | + |
| 64 | + xdsSrv.SetExtProcMaxRequests(s.cfg.extProcMaxRequests()) |
| 65 | + if parkCfg.enabled() { |
| 66 | + // Envoy must keep a parked request open at least as long as the router |
| 67 | + // will hold it; add a margin so the router surfaces its own 503 first. |
| 68 | + xdsSrv.SetExtProcMessageTimeout(parkCfg.Budget + 5*time.Second) |
| 69 | + } |
| 70 | + |
| 71 | + xdsSrv.SetTlsConfig(s.cfg.HttpsPort, s.cfg.EnvoyCertPath) |
| 72 | + xdsSrv.SetUpstreamTls(s.cfg.UpstreamCredentialBundlePath, s.cfg.UpstreamTrustBundlePath, s.cfg.UpstreamSpiffePrefix) |
| 73 | + ctrl := NewController(s.k8sClient, s.clientset, s.cfg, xdsSrv, s.extprocSrv) |
| 74 | + |
| 75 | + // Envoy receives all routing configuration from the local xDS server. |
| 76 | + g.Go(func() error { |
| 77 | + slog.InfoContext(ctx, "Starting ActorTemplate controller") |
| 78 | + return ctrl.Start(ctx) |
| 79 | + }) |
| 80 | + g.Go(func() error { |
| 81 | + slog.InfoContext(ctx, "Starting Envoy xDS Server", slog.Int("port", s.cfg.XdsPort)) |
| 82 | + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.cfg.XdsPort)) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("failed to listen on port %d: %w", s.cfg.XdsPort, err) |
| 85 | + } |
| 86 | + defer lis.Close() |
| 87 | + |
| 88 | + return xdsSrv.Serve(ctx, lis) |
| 89 | + }) |
| 90 | +} |
0 commit comments