-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathportforward.go
39 lines (36 loc) · 1007 Bytes
/
portforward.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
package cmd
import (
"context"
"fmt"
"path/filepath"
dmskube "github.com/altinn/dotnet-monitor-sidecar-cli/pkg/kubernetes"
"github.com/altinn/dotnet-monitor-sidecar-cli/pkg/utils"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
// ForwardPort forwards port 52323 from host to a pod
func ForwardPort(ctx context.Context, kubeconfig string, namespace string, podname string) {
if home := homedir.HomeDir(); home != "" && kubeconfig == "" {
kubeconfig = filepath.Join(home, ".kube", "config")
}
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err.Error())
}
if namespace == "" {
namespace, err = utils.GetNamespaceFromCurrentContext()
if err != nil {
fmt.Printf("Error getting namespace: %v", err)
return
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
h := dmskube.Helper{
Client: clientset,
}
h.PortForward(ctx, namespace, podname)
}