-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrappers.go
64 lines (56 loc) · 1.45 KB
/
wrappers.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package k8s
import (
"github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func OpenPortForwardPodWrapper(
clientset kubernetes.Interface,
restConfig *rest.Config,
podName string,
namespace string,
podPort int,
podLocalPort int,
stopChannel chan struct{},
) error {
// readyCh communicate when the port forward is ready to get traffic
readyCh := make(chan struct{})
portForwardRequest := PortForwardAPodRequest{
RestConfig: restConfig,
Pod: v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: namespace,
},
},
PodPort: podPort,
LocalPort: podLocalPort,
StopCh: stopChannel,
ReadyCh: readyCh,
}
// Check to see if the port is already used
err := CheckForExistingPortForwards(podLocalPort)
if err != nil {
log.Error().Msgf("unable to start port forward for pod %s in namespace %s: %s", podName, namespace, err)
return err
}
go func() {
err := PortForwardPodWithRetry(clientset, portForwardRequest)
if err != nil {
log.Error().Err(err).Msg(err.Error())
}
}()
select {
case <-stopChannel:
log.Info().Msg("leaving...")
close(stopChannel)
close(readyCh)
break
case <-readyCh:
log.Info().Msg("port forwarding is ready to get traffic")
}
log.Info().Msgf("pod %q at namespace %q has port-forward accepting local connections at port %d", podName, namespace, podLocalPort)
return nil
}