Skip to content

Commit

Permalink
more fix
Browse files Browse the repository at this point in the history
Signed-off-by: zirain <zirain2009@gmail.com>
  • Loading branch information
zirain committed Oct 31, 2024
1 parent 5f065f5 commit c1c0a04
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 14 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))
}
5 changes: 5 additions & 0 deletions internal/utils/net/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package net
import (
"fmt"
"net"
"strconv"
)

const (
Expand All @@ -22,3 +23,7 @@ func LocalAvailablePort() (int, error) {

return l.Addr().(*net.TCPAddr).Port, l.Close()
}

func JoinHostPort(host string, port int) string {
return net.JoinHostPort(host, strconv.Itoa(port))
}
2 changes: 1 addition & 1 deletion internal/xds/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func GetRenderedBootstrapConfig(opts *RenderBootstrapConfigOptions) (string, err
if len(sink.OpenTelemetry.BackendRefs) > 0 {
host, port = net.BackendHostAndPort(sink.OpenTelemetry.BackendRefs[0].BackendObjectReference, "")
}
addr := fmt.Sprintf("%s:%d", host, port)
addr := net.JoinHostPort(host, int(port))
if addresses.Has(addr) {
continue
}
Expand Down

0 comments on commit c1c0a04

Please sign in to comment.