'golang CLI running gcloud command gets stuck running command
I am creating a Go CLI that will run a gcloud command (specifically in this instance the port forwarding command, which if you go into a Kubernetes cluster -> Services & Ingress. the command looks like this:
gcloud container clusters get-credentials <k8_name> --zone <zone> --project <project> \
&& kubectl port-forward --namespace <ns> $(kubectl get pod --namespace <ns> --selector="app=<app_name>" --output jsonpath='{.items[0].metadata.name}') <remote_port>:<local_port>
here is the code responsible for running this command
for scanner.Scan() {
answer := scanner.Text()
if answer != "y" {
os.Exit(1)
}
c := fmt.Sprintf(`gcloud container clusters get-credentials <k8_cluster> --zone <zone> --project <project> && \
kubectl port-forward --namespace <ns> $(kubectl get pod --namespace <ns> --selector="app=%s" --output jsonpath='{.items[0].metadata.name}') %d:%d`, app, localPort, remotePort)
cmd := exec.Command("bash", "-c", c)
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
if err = cmd.Start(); err != nil {
log.Fatal(err)
}
defer cmd.Wait()
go io.Copy(os.Stdout, stderr)
if err != nil {
fmt.Println("err: ", err)
os.Exit(1)
}
}
what I see in the terminal is the following:
is the information above correct? (y/n): y
Fetching cluster endpoint and auth data.
kubeconfig entry generated for <k8_cluster>.
and it freezes there.
what I should see is:
Fetching cluster endpoint and auth data.
kubeconfig entry generated for <k8_cluster>.
Forwarding from 127.0.0.1:8080 -> 61727
Forwarding from [::1]:8080 -> 61727
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
