Skip to content

Commit

Permalink
sharded-test-server: add virtualWorkspacePort
Browse files Browse the repository at this point in the history
Avoid calculating the port in multiple places, and also use
JoinHostByPort instead of Sprintf
  • Loading branch information
Steven Hardy committed Jan 16, 2023
1 parent 445036b commit 57b01aa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 8 additions & 1 deletion cmd/sharded-test-server/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package main
import (
"context"
"fmt"
"net"
"os"
"path/filepath"
"strconv"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/authentication/user"
Expand Down Expand Up @@ -105,7 +107,8 @@ func newShard(ctx context.Context, n int, args []string, standaloneVW bool, serv
}

if standaloneVW {
args = append(args, fmt.Sprintf("--shard-virtual-workspace-url=https://%s:%d", hostIP, 7444+n))
args = append(args, fmt.Sprintf("--shard-virtual-workspace-url=https://%s",
net.JoinHostPort(hostIP, virtualWorkspacePort(n))))
}

return shard.NewShard(
Expand All @@ -116,6 +119,10 @@ func newShard(ctx context.Context, n int, args []string, standaloneVW bool, serv
), nil
}

func virtualWorkspacePort(n int) string {
return strconv.Itoa(7444 + n)
}

func embeddedEtcdClientPort(n int) int {
return 2380 + (n * 2) - 1
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/sharded-test-server/virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -134,7 +135,7 @@ func newVirtualWorkspace(ctx context.Context, index int, servingCA *crypto.CA, h
fmt.Sprintf("--client-ca-file=%s", clientCAFilePath),
fmt.Sprintf("--tls-private-key-file=%s", servingKeyFile),
fmt.Sprintf("--tls-cert-file=%s", servingCertFile),
fmt.Sprintf("--secure-port=%d", 7444+index),
fmt.Sprintf("--secure-port=%s", virtualWorkspacePort(index)),
)

return &VirtualWorkspace{
Expand Down Expand Up @@ -203,7 +204,7 @@ func (v *VirtualWorkspace) waitForReady(ctx context.Context) (<-chan error, erro
logger := klog.FromContext(ctx)
logger.WithValues("virtual-workspaces", v.index).Info("Waiting for virtual-workspaces /readyz to succeed")

vwHost := fmt.Sprintf("https://localhost:%d", 7444+v.index)
vwHost := fmt.Sprintf("https://%s", net.JoinHostPort("localhost", virtualWorkspacePort(v.index)))
kubeconfigPath := filepath.Join(v.workDirPath, fmt.Sprintf(".kcp-virtual-workspaces-%d/virtualworkspace.kubeconfig", v.index))

if err := wait.PollImmediateInfiniteWithContext(ctx, time.Millisecond*500, func(ctx context.Context) (bool, error) {
Expand Down

0 comments on commit 57b01aa

Please sign in to comment.