Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extproc: allow configuring an UDS listen address #149

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions cmd/extproc/mainlib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand All @@ -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")
mathetake marked this conversation as resolved.
Show resolved Hide resolved
logLevel = flag.String("logLevel", defaultLogLevel, "log level")
)

// Main is a main function for the external processor exposed
Expand All @@ -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")
Expand All @@ -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)
}
Expand All @@ -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
}
27 changes: 27 additions & 0 deletions cmd/extproc/mainlib/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
Loading