-
Notifications
You must be signed in to change notification settings - Fork 88
/
port-forward.go
50 lines (41 loc) · 1.36 KB
/
port-forward.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package upload
import (
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
func StartPortForward(namespace string, kubeconfig string, stopCh <-chan struct{}) (<-chan error, error) {
podName, err := findKotsadm(namespace)
if err != nil {
return nil, errors.Wrap(err, "failed to find kotsadm pod")
}
// set up port forwarding to get to it
errChan, err := k8sutil.PortForward(kubeconfig, 3000, 3000, namespace, podName, false, stopCh)
if err != nil {
return nil, errors.Wrap(err, "failed to start port forwarding")
}
return errChan, nil
}
func findKotsadm(namespace string) (string, error) {
cfg, err := config.GetConfig()
if err != nil {
return "", errors.Wrap(err, "failed to get cluster config")
}
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
return "", errors.Wrap(err, "failed to create kubernetes clientset")
}
pods, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: "app=kotsadm-api"})
if err != nil {
return "", errors.Wrap(err, "failed to list pods")
}
for _, pod := range pods.Items {
if pod.Status.Phase == corev1.PodRunning {
return pod.Name, nil
}
}
return "", errors.New("unable to find kotsadm pod")
}