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

fix(ipv6): support ipv6 address in grpc config #7035

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions internal/pkg/agent/configuration/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package configuration

import (
"fmt"
"net"
"os"
"strconv"

Expand Down Expand Up @@ -71,7 +71,7 @@ func OverrideDefaultContainerGRPCPort(cfg *GRPCConfig) {

// String returns the composed listen address for the GRPC.
func (cfg *GRPCConfig) String() string {
return fmt.Sprintf("%s:%d", cfg.Address, cfg.Port)
return net.JoinHostPort(cfg.Address, strconv.Itoa(int(cfg.Port)))
}

// IsLocal returns true if port value is less than 0
Expand Down
37 changes: 37 additions & 0 deletions internal/pkg/agent/configuration/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,40 @@ func TestOverrideDefaultGRPCPort(t *testing.T) {
})
}
}

func TestGRPCAddr(t *testing.T) {
testcases := []struct {
name string
addr string
port uint16
expected string
}{{
name: "ipv4",
addr: "127.0.0.1",
expected: "127.0.0.1:0",
}, {
name: "ipv4+port",
addr: "127.0.0.1",
port: 1,
expected: "127.0.0.1:1",
}, {
name: "ipv6",
addr: "::1",
expected: "[::1]:0",
}, {
name: "ipv6+port",
addr: "::1",
port: 1,
expected: "[::1]:1",
}}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
cfg := GRPCConfig{
Address: tc.addr,
Port: tc.port,
}
assert.Equal(t, tc.expected, cfg.String())
})
}
}
7 changes: 4 additions & 3 deletions pkg/component/runtime/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net"
"net/url"
"path"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -988,9 +989,9 @@ func (m *Manager) getListenAddr() string {
if m.isLocal {
return m.listenAddr
}
addr := strings.SplitN(m.listenAddr, ":", 2)
if len(addr) == 2 && addr[1] == "0" {
return fmt.Sprintf("%s:%d", addr[0], m.listenPort)
host, port, err := net.SplitHostPort(m.listenAddr)
if err == nil && port == "0" {
return net.JoinHostPort(host, strconv.Itoa(m.listenPort))
}
return m.listenAddr
}
Expand Down
Loading