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

[v0.21] fix: create background proxy container (#2374) #2375

Merged
merged 1 commit into from
Jan 3, 2025
Merged
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
68 changes: 61 additions & 7 deletions pkg/cli/localkubernetes/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package localkubernetes
import (
"context"
"fmt"
"net"
"os"
"os/exec"
"runtime"
"slices"
"strconv"
"strings"
"sync"
"time"

"github.com/loft-sh/log"
Expand Down Expand Up @@ -146,17 +151,45 @@ func CreateBackgroundProxyContainer(ctx context.Context, vClusterName, vClusterN
// check if the background proxy container for this vcluster is running and then remove it.
_ = CleanupBackgroundProxy(proxyName, log)

// get ips to try to connect to
dockerMachineIPs := []string{
"127.0.0.1",
}
if runtime.GOOS != "linux" {
output, err := exec.Command("docker", "run", "--network=host", "--rm", "alpine", "sh", "-c", "ifconfig | grep 'inet addr:' | sed 's/.*inet addr:\\([^ ]*\\).*/\\1/'").CombinedOutput()
if err != nil {
log.Warnf("Couldn't find network config for docker machine: %s %v", string(output), err)
}
for _, ip := range strings.Split(string(output), "\n") {
ip = strings.TrimSpace(ip)
if net.ParseIP(ip) != nil && !slices.Contains(dockerMachineIPs, ip) {
dockerMachineIPs = append(dockerMachineIPs, ip)
}
}

// since the vCluster certificate is only signed for 127.0.0.1 we need to make sure we use insecure here
if len(dockerMachineIPs) > 1 {
for k := range vRawConfig.Clusters {
vRawConfig.Clusters[k].CertificateAuthority = ""
vRawConfig.Clusters[k].CertificateAuthorityData = nil
vRawConfig.Clusters[k].InsecureSkipTLSVerify = true
}
}
}

// build the command
cmd := exec.Command(
"docker",
"run",
"--rm",
"-d",
"-v", fmt.Sprintf("%v:%v", kubeConfigPath, "/kube-config"),
fmt.Sprintf("--name=%s", proxyName),
"--network=host",
"bitnami/kubectl:1.29",
"port-forward",
"svc/"+vClusterName,
"--address=0.0.0.0",
strconv.Itoa(localPort)+":443",
"--kubeconfig", "/kube-config",
"-n", vClusterNamespace,
Expand All @@ -166,16 +199,37 @@ func CreateBackgroundProxyContainer(ctx context.Context, vClusterName, vClusterN
if err != nil {
return "", errors.Errorf("error starting background proxy: %s %v", string(out), err)
}
server := fmt.Sprintf("https://127.0.0.1:%v", localPort)
waitErr := wait.PollUntilContextTimeout(ctx, time.Second, time.Second*7, true, func(ctx context.Context) (bool, error) {
err = testConnectionWithServer(ctx, vRawConfig, server)
if err != nil {
return false, nil

server := ""
serverMutex := sync.Mutex{}
waitErr := wait.PollUntilContextTimeout(ctx, time.Second, time.Second*10, true, func(ctx context.Context) (bool, error) {
// try all ips in parallel
waitGroup := sync.WaitGroup{}
for _, ip := range dockerMachineIPs {
waitGroup.Add(1)
go func() {
defer waitGroup.Done()

testServer := fmt.Sprintf("https://%s:%v", ip, localPort)
err := testConnectionWithServer(ctx, vRawConfig, testServer)
if err == nil {
serverMutex.Lock()
if server == "" {
server = testServer
}
serverMutex.Unlock()
} else {
log.Debugf("Attempted to connect to %s: %v", testServer, err)
}
}()
}
return true, nil

// wait until we are done
waitGroup.Wait()
return server != "", nil
})
if waitErr != nil {
return "", fmt.Errorf("test connection: %w %w", waitErr, err)
return "", errors.New("test connection for background proxy failed")
}

return server, nil
Expand Down
Loading