-
Notifications
You must be signed in to change notification settings - Fork 151
/
dashboard.go
53 lines (43 loc) · 1.55 KB
/
dashboard.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
package watch
import (
"context"
"github.com/weaveworks/weave-gitops/pkg/logger"
"github.com/weaveworks/weave-gitops/pkg/run"
"github.com/weaveworks/weave-gitops/pkg/server"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// EnablePortForwardingForDashboard enables port forwarding for the GitOps Dashboard.
func EnablePortForwardingForDashboard(ctx context.Context, log logger.Logger, kubeClient client.Client, config *rest.Config, namespace string, podName string, dashboardPort string) (func(), error) {
specMap := &PortForwardSpec{
Namespace: namespace,
Name: podName,
Kind: "deployment",
HostPort: dashboardPort,
ContainerPort: server.DefaultPort,
}
// get pod from specMap
namespacedName := types.NamespacedName{Namespace: specMap.Namespace, Name: specMap.Name}
pod, err := run.GetPodFromResourceDescription(ctx, namespacedName, specMap.Kind, kubeClient)
if err != nil {
log.Failuref("Error getting pod from specMap: %v", err)
}
if pod != nil {
waitFwd := make(chan struct{}, 1)
readyChannel := make(chan struct{})
cancelPortFwd := func() {
close(waitFwd)
}
log.Actionf("Port forwarding to pod %s/%s ...", pod.Namespace, pod.Name)
go func() {
if err := ForwardPort(log.L(), pod, config, specMap, waitFwd, readyChannel); err != nil {
log.Failuref("Error forwarding port: %v", err)
}
}()
<-readyChannel
log.Successf("Port forwarding for dashboard is ready.")
return cancelPortFwd, nil
}
return nil, run.ErrDashboardPodNotFound
}