From ea86f9a4dcdff2b43bc62348a57e24738d333ef4 Mon Sep 17 00:00:00 2001 From: Bowei Du Date: Fri, 12 Jun 2026 16:08:35 -0700 Subject: [PATCH] Flatten the deep dir structure in atenet I just noticed that the directory structure is pretty deep. I want to make this less deep before we get too far into the project: * cmd/atenet/internal/app << this directory level is not needed. get rid of it. * cmd/atenet/internal/router/* move everything but the cobra flags to internal/router * cmd/atenet/internal/* just has the cobra flags definitions This is moving files -- no logic change. --- .golangci.yaml | 6 -- .../internal/{app/dns/cmd.go => dns.go} | 7 +- cmd/atenet/internal/{app => }/root.go | 8 +- cmd/atenet/internal/router.go | 61 +++++++++++ cmd/atenet/main.go | 4 +- .../app => internal}/router/README.md | 0 .../app => internal}/router/atstore.go | 0 .../app => internal}/router/atstore_file.go | 0 .../app => internal}/router/atstore_test.go | 0 .../app => internal}/router/controller.go | 0 .../app => internal}/router/dashboard.html | 0 .../app => internal}/router/envoyrunner.go | 0 .../app => internal}/router/errors.go | 0 .../app => internal}/router/errors_test.go | 0 .../app => internal}/router/extproc.go | 0 .../app => internal}/router/extproc_in.go | 0 .../router/extproc_in_test.go | 0 .../app => internal}/router/extproc_out.go | 3 +- .../app => internal}/router/extproc_test.go | 0 .../app => internal}/router/health.go | 0 .../app => internal}/router/metrics.go | 0 .../app => internal}/router/resumer.go | 0 .../app => internal}/router/resumer_test.go | 0 .../app => internal}/router/router.go | 101 ++++++------------ .../app => internal}/router/status.go | 4 +- .../app => internal}/router/status_test.go | 0 .../internal/app => internal}/router/xds.go | 0 .../app => internal}/router/xds_test.go | 0 28 files changed, 105 insertions(+), 89 deletions(-) rename cmd/atenet/internal/{app/dns/cmd.go => dns.go} (98%) rename cmd/atenet/internal/{app => }/root.go (82%) create mode 100644 cmd/atenet/internal/router.go rename {cmd/atenet/internal/app => internal}/router/README.md (100%) rename {cmd/atenet/internal/app => internal}/router/atstore.go (100%) rename {cmd/atenet/internal/app => internal}/router/atstore_file.go (100%) rename {cmd/atenet/internal/app => internal}/router/atstore_test.go (100%) rename {cmd/atenet/internal/app => internal}/router/controller.go (100%) rename {cmd/atenet/internal/app => internal}/router/dashboard.html (100%) rename {cmd/atenet/internal/app => internal}/router/envoyrunner.go (100%) rename {cmd/atenet/internal/app => internal}/router/errors.go (100%) rename {cmd/atenet/internal/app => internal}/router/errors_test.go (100%) rename {cmd/atenet/internal/app => internal}/router/extproc.go (100%) rename {cmd/atenet/internal/app => internal}/router/extproc_in.go (100%) rename {cmd/atenet/internal/app => internal}/router/extproc_in_test.go (100%) rename {cmd/atenet/internal/app => internal}/router/extproc_out.go (93%) rename {cmd/atenet/internal/app => internal}/router/extproc_test.go (100%) rename {cmd/atenet/internal/app => internal}/router/health.go (100%) rename {cmd/atenet/internal/app => internal}/router/metrics.go (100%) rename {cmd/atenet/internal/app => internal}/router/resumer.go (100%) rename {cmd/atenet/internal/app => internal}/router/resumer_test.go (100%) rename {cmd/atenet/internal/app => internal}/router/router.go (69%) rename {cmd/atenet/internal/app => internal}/router/status.go (99%) rename {cmd/atenet/internal/app => internal}/router/status_test.go (100%) rename {cmd/atenet/internal/app => internal}/router/xds.go (100%) rename {cmd/atenet/internal/app => internal}/router/xds_test.go (100%) diff --git a/.golangci.yaml b/.golangci.yaml index ab63bf4ee..a8ff17b72 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -100,12 +100,6 @@ linters: # json.Encoder.Encode in HTTP-handler write paths is fire-and-forget. - linters: [errcheck] text: 'Error return value of `\(\*encoding/json\.Encoder\)\.Encode` is not checked' - # ST1019: duplicate import aliases in extproc_out.go are pending a - # separate cleanup that will collide with an in-flight PR; suppress - # in this file for now. - - path: '^cmd/atenet/internal/app/router/extproc_out\.go$' - linters: [staticcheck] - text: '^ST1019' issues: max-issues-per-linter: 0 diff --git a/cmd/atenet/internal/app/dns/cmd.go b/cmd/atenet/internal/dns.go similarity index 98% rename from cmd/atenet/internal/app/dns/cmd.go rename to cmd/atenet/internal/dns.go index a8a68c440..253d64df8 100644 --- a/cmd/atenet/internal/app/dns/cmd.go +++ b/cmd/atenet/internal/dns.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package dns +package internal import ( "context" @@ -24,11 +24,12 @@ import ( "syscall" "time" - "github.com/agent-substrate/substrate/internal/dns" "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/config" + + "github.com/agent-substrate/substrate/internal/dns" ) type DnsConfig struct { @@ -38,7 +39,7 @@ type DnsConfig struct { CorefilePath string } -func NewCmd() *cobra.Command { +func NewDnsCmd() *cobra.Command { var cfg DnsConfig cmd := &cobra.Command{ diff --git a/cmd/atenet/internal/app/root.go b/cmd/atenet/internal/root.go similarity index 82% rename from cmd/atenet/internal/app/root.go rename to cmd/atenet/internal/root.go index 1483fca40..7c7e66806 100644 --- a/cmd/atenet/internal/app/root.go +++ b/cmd/atenet/internal/root.go @@ -12,14 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -package app +package internal import ( "fmt" "os" - "github.com/agent-substrate/substrate/cmd/atenet/internal/app/dns" - "github.com/agent-substrate/substrate/cmd/atenet/internal/app/router" "github.com/agent-substrate/substrate/internal/version" "github.com/spf13/cobra" ) @@ -39,6 +37,6 @@ func Execute() { } func init() { - rootCmd.AddCommand(router.NewCmd()) - rootCmd.AddCommand(dns.NewCmd()) + rootCmd.AddCommand(NewRouterCmd()) + rootCmd.AddCommand(NewDnsCmd()) } diff --git a/cmd/atenet/internal/router.go b/cmd/atenet/internal/router.go new file mode 100644 index 000000000..5c743b07d --- /dev/null +++ b/cmd/atenet/internal/router.go @@ -0,0 +1,61 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package internal + +import ( + "fmt" + "time" + + "github.com/spf13/cobra" + + "github.com/agent-substrate/substrate/internal/router" +) + +func NewRouterCmd() *cobra.Command { + var cfg router.RouterConfig + + cmd := &cobra.Command{ + Use: "router", + Short: "Router components including xDS server and Envoy ExtProc gateway processing server", + RunE: func(cmd *cobra.Command, args []string) error { + srv, err := router.NewRouterServer(cfg) + if err != nil { + return fmt.Errorf("failed to create router server: %w", err) + } + srv.Cmd = cmd + + return srv.Run(cmd.Context()) + }, + } + + cmd.Flags().StringVar(&cfg.LogLevel, "log-level", "info", "Log level: debug, info, warn, error") + cmd.Flags().StringVar(&cfg.MetricsAddr, "metrics-listen-addr", ":9090", "Address and port the prometheus metrics server should listen on.") + cmd.Flags().BoolVar(&cfg.Standalone, "standalone", false, "Run in standalone mode, bypassing creation of managed deployment and services in Kubernetes cluster") + cmd.Flags().StringVar(&cfg.Namespace, "namespace", "default", "Target operations namespace") + cmd.Flags().StringVar(&cfg.Kubeconfig, "kubeconfig", "", "Absolute path to the kubeconfig configuration file") + cmd.Flags().StringVar(&cfg.AteapiAddr, "ateapi-address", "api.ate-system.svc:443", "gRPC host address of the cluster ateapi Control instance") + cmd.Flags().IntVar(&cfg.HttpPort, "port-http", 8080, "TCP port for workload traffic entering through the Envoy Router") + cmd.Flags().IntVar(&cfg.XdsPort, "port-xds", 18000, "TCP port listening for the xDS dynamic Envoy connections") + cmd.Flags().IntVar(&cfg.ExtprocPort, "port-extproc", 50051, "Listen port for the Envoy dynamic External Processing (ext_proc) server") + cmd.Flags().StringVar(&cfg.ExtprocAddr, "extproc-address", "127.0.0.1", "Host IP or address of the Envoy External Processing (ext_proc) server") + cmd.Flags().StringVar(&cfg.EnvoyImage, "envoy-image", "envoyproxy/envoy:v1.30-latest", "Image URI used for dynamically launched router instances") + cmd.Flags().StringVar(&cfg.TemplatesFile, "actor-templates-file", "", "Path to offline YAML configuration file listing ActorTemplates") + cmd.Flags().IntVar(&cfg.StatusPort, "status-port", 4040, "Port to serve /statusz on (set <= 0 to disable serving status)") + cmd.Flags().DurationVar(&cfg.HealthInterval, "health-interval", 1*time.Second, "Interval for checking health of dependent services") + cmd.Flags().IntVar(&cfg.HttpsPort, "port-https", 8443, "TCP port for HTTPS workload traffic entering through the Envoy Router") + cmd.Flags().StringVar(&cfg.EnvoyCertPath, "envoy-cert-path", "", "Path to the Envoy certificate file (if empty, a self-signed cert will be generated for testing)") + + return cmd +} diff --git a/cmd/atenet/main.go b/cmd/atenet/main.go index 1bdcbc91b..097f9617f 100644 --- a/cmd/atenet/main.go +++ b/cmd/atenet/main.go @@ -14,8 +14,8 @@ package main -import "github.com/agent-substrate/substrate/cmd/atenet/internal/app" +import "github.com/agent-substrate/substrate/cmd/atenet/internal" func main() { - app.Execute() + internal.Execute() } diff --git a/cmd/atenet/internal/app/router/README.md b/internal/router/README.md similarity index 100% rename from cmd/atenet/internal/app/router/README.md rename to internal/router/README.md diff --git a/cmd/atenet/internal/app/router/atstore.go b/internal/router/atstore.go similarity index 100% rename from cmd/atenet/internal/app/router/atstore.go rename to internal/router/atstore.go diff --git a/cmd/atenet/internal/app/router/atstore_file.go b/internal/router/atstore_file.go similarity index 100% rename from cmd/atenet/internal/app/router/atstore_file.go rename to internal/router/atstore_file.go diff --git a/cmd/atenet/internal/app/router/atstore_test.go b/internal/router/atstore_test.go similarity index 100% rename from cmd/atenet/internal/app/router/atstore_test.go rename to internal/router/atstore_test.go diff --git a/cmd/atenet/internal/app/router/controller.go b/internal/router/controller.go similarity index 100% rename from cmd/atenet/internal/app/router/controller.go rename to internal/router/controller.go diff --git a/cmd/atenet/internal/app/router/dashboard.html b/internal/router/dashboard.html similarity index 100% rename from cmd/atenet/internal/app/router/dashboard.html rename to internal/router/dashboard.html diff --git a/cmd/atenet/internal/app/router/envoyrunner.go b/internal/router/envoyrunner.go similarity index 100% rename from cmd/atenet/internal/app/router/envoyrunner.go rename to internal/router/envoyrunner.go diff --git a/cmd/atenet/internal/app/router/errors.go b/internal/router/errors.go similarity index 100% rename from cmd/atenet/internal/app/router/errors.go rename to internal/router/errors.go diff --git a/cmd/atenet/internal/app/router/errors_test.go b/internal/router/errors_test.go similarity index 100% rename from cmd/atenet/internal/app/router/errors_test.go rename to internal/router/errors_test.go diff --git a/cmd/atenet/internal/app/router/extproc.go b/internal/router/extproc.go similarity index 100% rename from cmd/atenet/internal/app/router/extproc.go rename to internal/router/extproc.go diff --git a/cmd/atenet/internal/app/router/extproc_in.go b/internal/router/extproc_in.go similarity index 100% rename from cmd/atenet/internal/app/router/extproc_in.go rename to internal/router/extproc_in.go diff --git a/cmd/atenet/internal/app/router/extproc_in_test.go b/internal/router/extproc_in_test.go similarity index 100% rename from cmd/atenet/internal/app/router/extproc_in_test.go rename to internal/router/extproc_in_test.go diff --git a/cmd/atenet/internal/app/router/extproc_out.go b/internal/router/extproc_out.go similarity index 93% rename from cmd/atenet/internal/app/router/extproc_out.go rename to internal/router/extproc_out.go index df9b056cc..98d21d93e 100644 --- a/cmd/atenet/internal/app/router/extproc_out.go +++ b/internal/router/extproc_out.go @@ -17,7 +17,6 @@ package router import ( corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" extproc "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" - extprocv3 "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" envoy_type "github.com/envoyproxy/go-control-plane/envoy/type/v3" ) @@ -33,7 +32,7 @@ type reqError struct { func (e *reqError) Error() string { return e.msg } func (e *reqError) Unwrap() error { return e.cause } -func addAuthorityMutation(auth string, mut *extprocv3.HeaderMutation) { +func addAuthorityMutation(auth string, mut *extproc.HeaderMutation) { mut.SetHeaders = append(mut.SetHeaders, &corev3.HeaderValueOption{ Header: &corev3.HeaderValue{ diff --git a/cmd/atenet/internal/app/router/extproc_test.go b/internal/router/extproc_test.go similarity index 100% rename from cmd/atenet/internal/app/router/extproc_test.go rename to internal/router/extproc_test.go diff --git a/cmd/atenet/internal/app/router/health.go b/internal/router/health.go similarity index 100% rename from cmd/atenet/internal/app/router/health.go rename to internal/router/health.go diff --git a/cmd/atenet/internal/app/router/metrics.go b/internal/router/metrics.go similarity index 100% rename from cmd/atenet/internal/app/router/metrics.go rename to internal/router/metrics.go diff --git a/cmd/atenet/internal/app/router/resumer.go b/internal/router/resumer.go similarity index 100% rename from cmd/atenet/internal/app/router/resumer.go rename to internal/router/resumer.go diff --git a/cmd/atenet/internal/app/router/resumer_test.go b/internal/router/resumer_test.go similarity index 100% rename from cmd/atenet/internal/app/router/resumer_test.go rename to internal/router/resumer_test.go diff --git a/cmd/atenet/internal/app/router/router.go b/internal/router/router.go similarity index 69% rename from cmd/atenet/internal/app/router/router.go rename to internal/router/router.go index 93bf5c2ac..797ce36c0 100644 --- a/cmd/atenet/internal/app/router/router.go +++ b/internal/router/router.go @@ -84,7 +84,7 @@ type RouterConfig struct { type RouterServer struct { cfg RouterConfig - cmd *cobra.Command + Cmd *cobra.Command k8sClient client.Client clientset kubernetes.Interface apiClient ateapipb.ControlClient @@ -93,74 +93,6 @@ type RouterServer struct { atStore atStore } -func NewCmd() *cobra.Command { - var cfg RouterConfig - - cmd := &cobra.Command{ - Use: "router", - Short: "Router components including xDS server and Envoy ExtProc gateway processing server", - RunE: func(cmd *cobra.Command, args []string) error { - ctx, cancel := context.WithCancel(cmd.Context()) - defer cancel() - - var level slog.Level - switch strings.ToLower(cfg.LogLevel) { - case "debug": - level = slog.LevelDebug - case "warn": - level = slog.LevelWarn - case "error": - level = slog.LevelError - default: - level = slog.LevelInfo - } - slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level}))) - - mp, err := serverboot.InitMetrics(ctx, routerServiceName) - if err != nil { - return fmt.Errorf("failed to initialize metrics: %w", err) - } - defer serverboot.ShutdownProvider("MeterProvider", mp.Shutdown) - - go serverboot.StartMetricsServer(ctx, serverboot.MetricsServerOptions{Addr: cfg.MetricsAddr}) - - sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) - go func() { - <-sigChan - cancel() - }() - - srv, err := NewRouterServer(cfg) - if err != nil { - return fmt.Errorf("failed to create router server: %w", err) - } - srv.cmd = cmd - - return srv.Run(ctx) - }, - } - - cmd.Flags().StringVar(&cfg.LogLevel, "log-level", "info", "Log level: debug, info, warn, error") - cmd.Flags().StringVar(&cfg.MetricsAddr, "metrics-listen-addr", ":9090", "Address and port the prometheus metrics server should listen on.") - cmd.Flags().BoolVar(&cfg.Standalone, "standalone", false, "Run in standalone mode, bypassing creation of managed deployment and services in Kubernetes cluster") - cmd.Flags().StringVar(&cfg.Namespace, "namespace", "default", "Target operations namespace") - cmd.Flags().StringVar(&cfg.Kubeconfig, "kubeconfig", "", "Absolute path to the kubeconfig configuration file") - cmd.Flags().StringVar(&cfg.AteapiAddr, "ateapi-address", "api.ate-system.svc:443", "gRPC host address of the cluster ateapi Control instance") - cmd.Flags().IntVar(&cfg.HttpPort, "port-http", 8080, "TCP port for workload traffic entering through the Envoy Router") - cmd.Flags().IntVar(&cfg.XdsPort, "port-xds", 18000, "TCP port listening for the xDS dynamic Envoy connections") - cmd.Flags().IntVar(&cfg.ExtprocPort, "port-extproc", 50051, "Listen port for the Envoy dynamic External Processing (ext_proc) server") - cmd.Flags().StringVar(&cfg.ExtprocAddr, "extproc-address", "127.0.0.1", "Host IP or address of the Envoy External Processing (ext_proc) server") - cmd.Flags().StringVar(&cfg.EnvoyImage, "envoy-image", "envoyproxy/envoy:v1.30-latest", "Image URI used for dynamically launched router instances") - cmd.Flags().StringVar(&cfg.TemplatesFile, "actor-templates-file", "", "Path to offline YAML configuration file listing ActorTemplates") - cmd.Flags().IntVar(&cfg.StatusPort, "status-port", 4040, "Port to serve /statusz on (set <= 0 to disable serving status)") - cmd.Flags().DurationVar(&cfg.HealthInterval, "health-interval", 1*time.Second, "Interval for checking health of dependent services") - cmd.Flags().IntVar(&cfg.HttpsPort, "port-https", 8443, "TCP port for HTTPS workload traffic entering through the Envoy Router") - cmd.Flags().StringVar(&cfg.EnvoyCertPath, "envoy-cert-path", "", "Path to the Envoy certificate file (if empty, a self-signed cert will be generated for testing)") - - return cmd -} - func NewRouterServer(cfg RouterConfig) (*RouterServer, error) { var k8sClient client.Client var clientset kubernetes.Interface @@ -218,6 +150,37 @@ func NewRouterServer(cfg RouterConfig) (*RouterServer, error) { } func (s *RouterServer) Run(ctx context.Context) error { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) + go func() { + <-sigChan + cancel() + }() + + var level slog.Level + switch strings.ToLower(s.cfg.LogLevel) { + case "debug": + level = slog.LevelDebug + case "warn": + level = slog.LevelWarn + case "error": + level = slog.LevelError + default: + level = slog.LevelInfo + } + slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level}))) + + mp, err := serverboot.InitMetrics(ctx, routerServiceName) + if err != nil { + return fmt.Errorf("failed to initialize metrics: %w", err) + } + defer serverboot.ShutdownProvider("MeterProvider", mp.Shutdown) + + go serverboot.StartMetricsServer(ctx, serverboot.MetricsServerOptions{Addr: s.cfg.MetricsAddr}) + slog.InfoContext(ctx, "Starting substrate router subsystem", slog.Bool("standalone", s.cfg.Standalone)) g, ctx := errgroup.WithContext(ctx) diff --git a/cmd/atenet/internal/app/router/status.go b/internal/router/status.go similarity index 99% rename from cmd/atenet/internal/app/router/status.go rename to internal/router/status.go index 377554298..3d5f2dc74 100644 --- a/cmd/atenet/internal/app/router/status.go +++ b/internal/router/status.go @@ -188,8 +188,8 @@ func (s *RouterServer) handleStatusz(w http.ResponseWriter, req *http.Request) { argsStr := strings.Join(os.Args, " ") flagsMap := make(map[string]string) - if s.cmd != nil { - s.cmd.Flags().VisitAll(func(f *pflag.Flag) { + if s.Cmd != nil { + s.Cmd.Flags().VisitAll(func(f *pflag.Flag) { flagsMap[f.Name] = f.Value.String() }) } diff --git a/cmd/atenet/internal/app/router/status_test.go b/internal/router/status_test.go similarity index 100% rename from cmd/atenet/internal/app/router/status_test.go rename to internal/router/status_test.go diff --git a/cmd/atenet/internal/app/router/xds.go b/internal/router/xds.go similarity index 100% rename from cmd/atenet/internal/app/router/xds.go rename to internal/router/xds.go diff --git a/cmd/atenet/internal/app/router/xds_test.go b/internal/router/xds_test.go similarity index 100% rename from cmd/atenet/internal/app/router/xds_test.go rename to internal/router/xds_test.go