Skip to content

Commit

Permalink
Merge pull request #109344 from stbenjam/host-port-fixes
Browse files Browse the repository at this point in the history
Replace use of Sprintf with net.JoinHostPort
  • Loading branch information
k8s-ci-robot committed May 4, 2022
2 parents 6b988c5 + b351745 commit 6445efb
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 11 deletions.
4 changes: 3 additions & 1 deletion pkg/volume/portworx/portworx.go
Expand Up @@ -18,7 +18,9 @@ package portworx

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

"k8s.io/klog/v2"
"k8s.io/mount-utils"
Expand Down Expand Up @@ -71,7 +73,7 @@ func (plugin *portworxVolumePlugin) IsMigratedToCSI() bool {

func (plugin *portworxVolumePlugin) Init(host volume.VolumeHost) error {
client, err := volumeclient.NewDriverClient(
fmt.Sprintf("http://%s:%d", host.GetHostName(), osdMgmtDefaultPort),
fmt.Sprintf("http://%s", net.JoinHostPort(host.GetHostName(), strconv.Itoa(osdMgmtDefaultPort))),
pxdDriverName, osdDriverVersion, pxDriverName)
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion pkg/volume/portworx/portworx_util.go
Expand Up @@ -19,6 +19,8 @@ package portworx
import (
"context"
"fmt"
"net"
"strconv"

osdapi "github.com/libopenstorage/openstorage/api"
osdclient "github.com/libopenstorage/openstorage/api/client"
Expand All @@ -30,6 +32,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
volumehelpers "k8s.io/cloud-provider/volume/helpers"
"k8s.io/klog/v2"

api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/volume"
)
Expand Down Expand Up @@ -265,7 +268,7 @@ func isClientValid(client *osdclient.Client) (bool, error) {
}

func createDriverClient(hostname string, port int32) (*osdclient.Client, error) {
client, err := volumeclient.NewDriverClient(fmt.Sprintf("http://%s:%d", hostname, port),
client, err := volumeclient.NewDriverClient(fmt.Sprintf("http://%s", net.JoinHostPort(hostname, strconv.Itoa(int(port)))),
pxdDriverName, osdDriverVersion, pxDriverName)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/storage/vsphere/connection.go
Expand Up @@ -19,6 +19,7 @@ package vsphere
import (
"context"
"fmt"
"net"
neturl "net/url"
"sync"

Expand Down Expand Up @@ -72,7 +73,7 @@ func Connect(ctx context.Context, vs *VSphere) error {

// NewClient creates a new client for vSphere connection
func NewClient(ctx context.Context, vs *VSphere) (*govmomi.Client, error) {
url, err := neturl.Parse(fmt.Sprintf("https://%s:%s/sdk", vs.Config.Hostname, vs.Config.Port))
url, err := neturl.Parse(fmt.Sprintf("https://%s/sdk", net.JoinHostPort(vs.Config.Hostname, vs.Config.Port)))
if err != nil {
klog.Errorf("Failed to parse URL: %s. err: %+v", url, err)
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions test/e2e/upgrades/apps/cassandra.go
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"path/filepath"
"sync"
Expand All @@ -32,6 +33,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/apimachinery/pkg/util/wait"

"k8s.io/kubernetes/test/e2e/framework"
e2estatefulset "k8s.io/kubernetes/test/e2e/framework/statefulset"
e2etestfiles "k8s.io/kubernetes/test/e2e/framework/testfiles"
Expand Down Expand Up @@ -118,7 +120,7 @@ func (t *CassandraUpgradeTest) Setup(f *framework.Framework) {

// listUsers gets a list of users from the db via the tester service.
func (t *CassandraUpgradeTest) listUsers() ([]string, error) {
r, err := http.Get(fmt.Sprintf("http://%s:8080/list", t.ip))
r, err := http.Get(fmt.Sprintf("http://%s/list", net.JoinHostPort(t.ip, "8080")))
if err != nil {
return nil, err
}
Expand All @@ -140,7 +142,7 @@ func (t *CassandraUpgradeTest) listUsers() ([]string, error) {
// addUser adds a user to the db via the tester services.
func (t *CassandraUpgradeTest) addUser(name string) error {
val := map[string][]string{"name": {name}}
r, err := http.PostForm(fmt.Sprintf("http://%s:8080/add", t.ip), val)
r, err := http.PostForm(fmt.Sprintf("http://%s/add", net.JoinHostPort(t.ip, "8080")), val)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions test/e2e/upgrades/apps/etcd.go
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"path/filepath"
"sync"
Expand All @@ -32,6 +33,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/apimachinery/pkg/util/wait"

"k8s.io/kubernetes/test/e2e/framework"
e2estatefulset "k8s.io/kubernetes/test/e2e/framework/statefulset"
e2etestfiles "k8s.io/kubernetes/test/e2e/framework/testfiles"
Expand Down Expand Up @@ -112,7 +114,7 @@ func (t *EtcdUpgradeTest) Setup(f *framework.Framework) {
}

func (t *EtcdUpgradeTest) listUsers() ([]string, error) {
r, err := http.Get(fmt.Sprintf("http://%s:8080/list", t.ip))
r, err := http.Get(fmt.Sprintf("http://%s/list", net.JoinHostPort(t.ip, "8080")))
if err != nil {
return nil, err
}
Expand All @@ -133,7 +135,7 @@ func (t *EtcdUpgradeTest) listUsers() ([]string, error) {

func (t *EtcdUpgradeTest) addUser(name string) error {
val := map[string][]string{"name": {name}}
r, err := http.PostForm(fmt.Sprintf("http://%s:8080/add", t.ip), val)
r, err := http.PostForm(fmt.Sprintf("http://%s/add", net.JoinHostPort(t.ip, "8080")), val)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions test/e2e/upgrades/apps/mysql.go
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"path/filepath"
"strconv"
Expand All @@ -32,6 +33,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/apimachinery/pkg/util/wait"

"k8s.io/kubernetes/test/e2e/framework"
e2estatefulset "k8s.io/kubernetes/test/e2e/framework/statefulset"
e2etestfiles "k8s.io/kubernetes/test/e2e/framework/testfiles"
Expand Down Expand Up @@ -181,7 +183,7 @@ func (t *MySQLUpgradeTest) Teardown(f *framework.Framework) {
func (t *MySQLUpgradeTest) addName(name string) error {
val := map[string][]string{"name": {name}}
t.nextWrite++
r, err := http.PostForm(fmt.Sprintf("http://%s:8080/addName", t.ip), val)
r, err := http.PostForm(fmt.Sprintf("http://%s/addName", net.JoinHostPort(t.ip, "8080")), val)
if err != nil {
return err
}
Expand All @@ -199,7 +201,7 @@ func (t *MySQLUpgradeTest) addName(name string) error {
// countNames checks to make sure the values in testing.users are available, and returns
// the count of them.
func (t *MySQLUpgradeTest) countNames() (int, error) {
r, err := http.Get(fmt.Sprintf("http://%s:8080/countNames", t.ip))
r, err := http.Get(fmt.Sprintf("http://%s/countNames", net.JoinHostPort(t.ip, "8080")))
if err != nil {
return 0, err
}
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/windows/service.go
Expand Up @@ -18,9 +18,12 @@ package windows

import (
"fmt"
"net"
"strconv"

v1 "k8s.io/api/core/v1"
clientset "k8s.io/client-go/kubernetes"

"k8s.io/kubernetes/test/e2e/framework"
e2enode "k8s.io/kubernetes/test/e2e/framework/node"
e2eservice "k8s.io/kubernetes/test/e2e/framework/service"
Expand Down Expand Up @@ -77,7 +80,7 @@ var _ = SIGDescribe("Services", func() {
framework.ExpectEqual(testPod.Spec.NodeSelector["kubernetes.io/os"], "windows")

ginkgo.By(fmt.Sprintf("checking connectivity Pod to curl http://%s:%d", nodeIP, nodePort))
assertConsistentConnectivity(f, testPod.ObjectMeta.Name, windowsOS, windowsCheck(fmt.Sprintf("http://%s:%d", nodeIP, nodePort)))
assertConsistentConnectivity(f, testPod.ObjectMeta.Name, windowsOS, windowsCheck(fmt.Sprintf("http://%s", net.JoinHostPort(nodeIP, strconv.Itoa(nodePort)))))

})

Expand Down
4 changes: 3 additions & 1 deletion test/e2e_node/util.go
Expand Up @@ -23,9 +23,11 @@ import (
"flag"
"fmt"
"io"
"net"
"net/http"
"os/exec"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -83,7 +85,7 @@ func getNodeSummary() (*stats.Summary, error) {
if err != nil {
return nil, fmt.Errorf("failed to get current kubelet config")
}
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s:%d/stats/summary", kubeletConfig.Address, kubeletConfig.ReadOnlyPort), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/stats/summary", net.JoinHostPort(kubeletConfig.Address, strconv.Itoa(int(kubeletConfig.ReadOnlyPort)))), nil)
if err != nil {
return nil, fmt.Errorf("failed to build http request: %v", err)
}
Expand Down

0 comments on commit 6445efb

Please sign in to comment.