diff --git a/cmd/extproc/mainlib/main.go b/cmd/extproc/mainlib/main.go index 3c82b7104..f2b85c2d9 100644 --- a/cmd/extproc/mainlib/main.go +++ b/cmd/extproc/mainlib/main.go @@ -8,6 +8,7 @@ import ( "net" "os" "os/signal" + "strings" "syscall" "time" @@ -19,12 +20,16 @@ import ( "github.com/envoyproxy/ai-gateway/internal/version" ) +const ( + defaultAddress = ":1063" + defaultLogLevel = "info" +) + var ( configPath = flag.String("configPath", "", "path to the configuration file. "+ "The file must be in YAML format specified in extprocconfig.Config type. The configuration file is watched for changes.") - // TODO: unix domain socket support. - extProcPort = flag.String("extProcPort", ":1063", "gRPC port for the external processor") - logLevel = flag.String("logLevel", "info", "log level") + extProcAddr = flag.String("extProcAddr", defaultAddress, "gRPC address for the external processor") + logLevel = flag.String("logLevel", defaultLogLevel, "log level") ) // Main is a main function for the external processor exposed @@ -40,7 +45,9 @@ func Main() { Level: level, })) - l.Info("starting external processor", slog.String("version", version.Version)) + l.Info("starting external processor", + slog.String("version", version.Version), + slog.String("address", *extProcAddr)) if *configPath == "" { log.Fatal("configPath must be provided") @@ -54,8 +61,7 @@ func Main() { cancel() }() - // TODO: unix domain socket support. - lis, err := net.Listen("tcp", *extProcPort) + lis, err := net.Listen(listenAddress(*extProcAddr)) if err != nil { log.Fatalf("failed to listen: %v", err) } @@ -78,3 +84,14 @@ func Main() { }() _ = s.Serve(lis) } + +// listenAddress returns the network and address for the given address flag. +func listenAddress(addrFlag string) (string, string) { + if addrFlag == "" { + return "tcp", defaultAddress + } + if strings.HasPrefix(addrFlag, "unix://") { + return "unix", strings.TrimPrefix(addrFlag, "unix://") + } + return "tcp", addrFlag +} diff --git a/cmd/extproc/mainlib/main_test.go b/cmd/extproc/mainlib/main_test.go new file mode 100644 index 000000000..71c3446dc --- /dev/null +++ b/cmd/extproc/mainlib/main_test.go @@ -0,0 +1,27 @@ +package mainlib + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestListenAddress(t *testing.T) { + tests := []struct { + addr string + wantNetwork string + wantAddress string + }{ + {"", "tcp", defaultAddress}, + {":8080", "tcp", ":8080"}, + {"unix:///var/run/ai-gateway/extproc.sock", "unix", "/var/run/ai-gateway/extproc.sock"}, + } + + for _, tt := range tests { + t.Run(tt.addr, func(t *testing.T) { + network, address := listenAddress(tt.addr) + assert.Equal(t, tt.wantNetwork, network) + assert.Equal(t, tt.wantAddress, address) + }) + } +}