MCPcopy Index your code
hub / github.com/devspace-sh/devspace / forward

Method forward

pkg/devspace/server/port_forward.go:19–143  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

17const maxPort = 40000
18
19func (h *handler) forward(w http.ResponseWriter, r *http.Request) {
20 // Kube Context
21 kubeContext := h.ctx.KubeClient().CurrentContext()
22 ctx, ok := r.URL.Query()["context"]
23 if ok && len(ctx) == 1 && ctx[0] != "" {
24 kubeContext = ctx[0]
25 }
26
27 // Namespace
28 kubeNamespace := h.ctx.KubeClient().Namespace()
29 namespace, ok := r.URL.Query()["namespace"]
30 if ok && len(namespace) == 1 && namespace[0] != "" {
31 kubeNamespace = namespace[0]
32 }
33
34 name, ok := r.URL.Query()["name"]
35 if !ok || len(name) != 1 {
36 http.Error(w, "name is missing", http.StatusBadRequest)
37 return
38 }
39
40 targetPort, ok := r.URL.Query()["port"]
41 if !ok || len(targetPort) != 1 {
42 http.Error(w, "port is missing", http.StatusBadRequest)
43 return
44 }
45
46 key := fmt.Sprintf("%s/%s/%s:%s", kubeContext, kubeNamespace, name[0], targetPort[0])
47
48 // Check if exists
49 h.portsMutex.Lock()
50 defer h.portsMutex.Unlock()
51
52 // Create kubectl client
53 client, err := h.getClientFromCache(kubeContext, kubeNamespace)
54 if err != nil {
55 h.ctx.Log().Errorf("Error in %s: %v", r.URL.String(), err)
56 http.Error(w, err.Error(), http.StatusInternalServerError)
57 return
58 }
59
60 pod, err := client.KubeClient().CoreV1().Pods(kubeNamespace).Get(context.TODO(), name[0], metav1.GetOptions{})
61 if err != nil {
62 h.ctx.Log().Errorf("Error in %s: %v", r.URL.String(), err)
63 http.Error(w, err.Error(), http.StatusInternalServerError)
64 return
65 }
66
67 if h.ports[key] != nil {
68 // Check if the pod is the same
69 if h.ports[key].podUUID == string(pod.UID) {
70 _, _ = w.Write([]byte(strconv.Itoa(h.ports[key].portForwarderPort)))
71 return
72 }
73
74 close(h.ports[key].portForwarderStop)
75 delete(h.ports, key)
76 }

Callers

nothing calls this directly

Calls 15

getClientFromCacheMethod · 0.95
ForwardPortsMethod · 0.95
CloseMethod · 0.95
IsAvailableFunction · 0.92
NewPortForwarderFunction · 0.92
closeFunction · 0.85
LockMethod · 0.80
UnlockMethod · 0.80
CurrentContextMethod · 0.65
KubeClientMethod · 0.65
NamespaceMethod · 0.65
LogMethod · 0.65

Tested by

no test coverage detected