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.
This commit is contained in:
Bowei Du
2026-06-12 16:53:19 -07:00
committed by Bowei Du
parent 27aa4eec85
commit ea86f9a4dc
28 changed files with 105 additions and 89 deletions
-6
View File
@@ -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
@@ -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{
@@ -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())
}
+61
View File
@@ -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
}
+2 -2
View File
@@ -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()
}
@@ -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{
@@ -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)
@@ -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()
})
}