Skip to content

Commit

Permalink
chore: use net.JoinHostPort (#4599)
Browse files Browse the repository at this point in the history
* chore: use net.JoinHostPort

Signed-off-by: zirain <[email protected]>

* more fix

Signed-off-by: zirain <[email protected]>

* remove netutils.JoinHostPort

Signed-off-by: zirain <[email protected]>

---------

Signed-off-by: zirain <[email protected]>
  • Loading branch information
zirain authored Nov 1, 2024
1 parent db68027 commit 6e2587d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 18 deletions.
5 changes: 3 additions & 2 deletions api/v1alpha1/envoygateway_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
package v1alpha1

import (
"fmt"
"net"
"strconv"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
Expand Down Expand Up @@ -80,7 +81,7 @@ func (e *EnvoyGateway) GetEnvoyGatewayAdmin() *EnvoyGatewayAdmin {
func (e *EnvoyGateway) GetEnvoyGatewayAdminAddress() string {
address := e.GetEnvoyGatewayAdmin().Address
if address != nil {
return fmt.Sprintf("%s:%d", address.Host, address.Port)
return net.JoinHostPort(address.Host, strconv.Itoa(address.Port))
}

return ""
Expand Down
3 changes: 1 addition & 2 deletions examples/extension-server/cmd/extension-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package main

import (
"fmt"
"log/slog"
"net"
"os"
Expand Down Expand Up @@ -82,7 +81,7 @@ func startExtensionServer(cCtx *cli.Context) error {
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: level,
}))
address := fmt.Sprintf("%s:%d", cCtx.String("host"), cCtx.Int("port"))
address := net.JoinHostPort(cCtx.String("host"), cCtx.String("port"))
logger.Info("Starting the extension server", slog.String("host", address))
lis, err := net.Listen("tcp", address)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/extension/registry/extension_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"net"
"strconv"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -123,13 +124,13 @@ func getExtensionServerAddress(service *egv1a1.ExtensionService) string {
var serverAddr string
switch {
case service.FQDN != nil:
serverAddr = fmt.Sprintf("%s:%d", service.FQDN.Hostname, service.FQDN.Port)
serverAddr = net.JoinHostPort(service.FQDN.Hostname, strconv.Itoa(int(service.FQDN.Port)))
case service.IP != nil:
serverAddr = fmt.Sprintf("%s:%d", service.IP.Address, service.IP.Port)
serverAddr = net.JoinHostPort(service.IP.Address, strconv.Itoa(int(service.IP.Port)))
case service.Unix != nil:
serverAddr = fmt.Sprintf("unix://%s", service.Unix.Path)
case service.Host != "":
serverAddr = fmt.Sprintf("%s:%d", service.Host, service.Port)
serverAddr = net.JoinHostPort(service.Host, strconv.Itoa(int(service.Port)))
}
return serverAddr
}
Expand Down
11 changes: 6 additions & 5 deletions internal/gatewayapi/securitypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/netip"
"net/url"
Expand Down Expand Up @@ -921,16 +922,16 @@ func backendRefAuthority(resources *resource.Resources, backendRef *gwapiv1.Back
// TODO: exists multi FQDN endpoints?
for _, ep := range backend.Spec.Endpoints {
if ep.FQDN != nil {
return fmt.Sprintf("%s:%d", ep.FQDN.Hostname, ep.FQDN.Port)
return net.JoinHostPort(ep.FQDN.Hostname, strconv.Itoa(int(ep.FQDN.Port)))
}
}
}
}

return fmt.Sprintf("%s.%s:%d",
backendRef.Name,
backendNamespace,
*backendRef.Port)
return net.JoinHostPort(
fmt.Sprintf("%s.%s", backendRef.Name, backendNamespace),
strconv.Itoa(int(*backendRef.Port)),
)
}

func (t *Translator) buildAuthorization(policy *egv1a1.SecurityPolicy) (*ir.Authorization, error) {
Expand Down
4 changes: 3 additions & 1 deletion internal/kubernetes/port_forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ package kubernetes
import (
"fmt"
"io"
"net"
"net/http"
"os"
"strconv"

"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -134,5 +136,5 @@ func (f *localForwarder) WaitForStop() {
}

func (f *localForwarder) Address() string {
return fmt.Sprintf("%s:%d", netutil.DefaultLocalAddress, f.localPort)
return net.JoinHostPort(netutil.DefaultLocalAddress, strconv.Itoa(f.localPort))
}
8 changes: 5 additions & 3 deletions internal/xds/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
// Register embed
_ "embed"
"fmt"
"net"
"strconv"
"strings"
"text/template"

"k8s.io/apimachinery/pkg/util/sets"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/utils/net"
netutils "github.com/envoyproxy/gateway/internal/utils/net"
"github.com/envoyproxy/gateway/internal/utils/regex"
)

Expand Down Expand Up @@ -199,9 +201,9 @@ func GetRenderedBootstrapConfig(opts *RenderBootstrapConfigOptions) (string, err
host, port = *sink.OpenTelemetry.Host, uint32(sink.OpenTelemetry.Port)
}
if len(sink.OpenTelemetry.BackendRefs) > 0 {
host, port = net.BackendHostAndPort(sink.OpenTelemetry.BackendRefs[0].BackendObjectReference, "")
host, port = netutils.BackendHostAndPort(sink.OpenTelemetry.BackendRefs[0].BackendObjectReference, "")
}
addr := fmt.Sprintf("%s:%d", host, port)
addr := net.JoinHostPort(host, strconv.Itoa(int(port)))
if addresses.Has(addr) {
continue
}
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/tests/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package tests

import (
"context"
"fmt"
"net"
"testing"
"time"
Expand Down Expand Up @@ -495,7 +494,7 @@ var RateLimitMultipleListenersTest = suite.ConformanceTest{

gwPorts := []string{"80", "8080"}
for _, port := range gwPorts {
gwAddr = fmt.Sprintf("%s:%s", gwIP, port)
gwAddr = net.JoinHostPort(gwIP, port)

ratelimitHeader := make(map[string]string)
expectOkResp := http.ExpectedResponse{
Expand Down

0 comments on commit 6e2587d

Please sign in to comment.