From 4f3665f8a602a86ba3ceff6b27be935349f78f54 Mon Sep 17 00:00:00 2001 From: Andreas Kupries Date: Thu, 26 Jan 2023 15:55:17 +0100 Subject: [PATCH] dept: refactor common parts of proxy setup for exec and port-forward chore: update callers --- internal/api/v1/application/exec.go | 36 ++-------------- internal/api/v1/application/portforward.go | 34 ++------------- internal/api/v1/application/proxy.go | 49 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 63 deletions(-) create mode 100644 internal/api/v1/application/proxy.go diff --git a/internal/api/v1/application/exec.go b/internal/api/v1/application/exec.go index 118b3c5edd..3ce8366309 100644 --- a/internal/api/v1/application/exec.go +++ b/internal/api/v1/application/exec.go @@ -13,17 +13,13 @@ package application import ( "net/http" - "net/http/httputil" - "time" "github.com/epinio/epinio/helpers/kubernetes" "github.com/epinio/epinio/internal/application" apierror "github.com/epinio/epinio/pkg/api/core/v1/errors" "github.com/gin-gonic/gin" v1 "k8s.io/api/core/v1" - thekubernetes "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" - "k8s.io/client-go/rest" ) func (hc Controller) Exec(c *gin.Context) apierror.APIErrors { @@ -37,11 +33,6 @@ func (hc Controller) Exec(c *gin.Context) apierror.APIErrors { return apierror.InternalError(err) } - clientSetHTTP1, err := kubernetes.GetHTTP1Client(ctx) - if err != nil { - return apierror.InternalError(err) - } - app, err := application.Lookup(ctx, cluster, namespace, appName) if err != nil { return apierror.InternalError(err) @@ -87,41 +78,22 @@ func (hc Controller) Exec(c *gin.Context) apierror.APIErrors { return apierror.InternalError(err) } - proxyRequest(c.Writer, c.Request, podToConnect, namespace, appData.Name, clientSetHTTP1) - - return nil -} - -func proxyRequest(rw http.ResponseWriter, req *http.Request, podName, namespace, container string, client thekubernetes.Interface) { // https://github.com/kubernetes/kubectl/blob/2acffc93b61e483bd26020df72b9aef64541bd56/pkg/cmd/exec/exec.go#L352 - attachURL := client.CoreV1().RESTClient(). + attachURL := cluster.Kubectl.CoreV1().RESTClient(). Post(). Namespace(namespace). Resource("pods"). - Name(podName). + Name(podToConnect). SubResource("exec"). VersionedParams(&v1.PodExecOptions{ Stdin: true, Stdout: true, Stderr: true, TTY: true, - Container: container, + Container: appData.Name, // https://github.com/rancher/dashboard/blob/37f40d7213ff32096bfefd02de77be6a0e7f40ab/components/nav/WindowManager/ContainerShell.vue#L22 Command: []string{"/bin/sh", "-c", "TERM=xterm-256color; export TERM; exec /bin/bash"}, }, scheme.ParameterCodec).URL() - httpClient := client.CoreV1().RESTClient().(*rest.RESTClient).Client - p := httputil.ReverseProxy{ - Director: func(req *http.Request) { - req.URL = attachURL - req.Host = attachURL.Host - // let kube authentication work - delete(req.Header, "Cookie") - delete(req.Header, "Authorization") - }, - Transport: httpClient.Transport, - FlushInterval: time.Millisecond * 100, - } - - p.ServeHTTP(rw, req) + return runProxy(ctx, c.Writer, c.Request, attachURL) } diff --git a/internal/api/v1/application/portforward.go b/internal/api/v1/application/portforward.go index 6f6db6d261..b07da2a27e 100644 --- a/internal/api/v1/application/portforward.go +++ b/internal/api/v1/application/portforward.go @@ -13,15 +13,11 @@ package application import ( "net/http" - "net/http/httputil" - "time" "github.com/epinio/epinio/helpers/kubernetes" "github.com/epinio/epinio/internal/application" apierror "github.com/epinio/epinio/pkg/api/core/v1/errors" "github.com/gin-gonic/gin" - thekubernetes "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" ) func (hc Controller) PortForward(c *gin.Context) apierror.APIErrors { @@ -35,11 +31,6 @@ func (hc Controller) PortForward(c *gin.Context) apierror.APIErrors { return apierror.InternalError(err) } - clientSetHTTP1, err := kubernetes.GetHTTP1Client(ctx) - if err != nil { - return apierror.InternalError(err) - } - app, err := application.Lookup(ctx, cluster, namespace, appName) if err != nil { return apierror.InternalError(err) @@ -80,33 +71,14 @@ func (hc Controller) PortForward(c *gin.Context) apierror.APIErrors { podToConnect = podNames[0] } - forwardRequest(c.Writer, c.Request, podToConnect, namespace, clientSetHTTP1) - - return nil -} - -func forwardRequest(rw http.ResponseWriter, req *http.Request, podName, namespace string, client thekubernetes.Interface) { // https://github.com/kubernetes/kubectl/blob/2acffc93b61e483bd26020df72b9aef64541bd56/pkg/cmd/portforward/portforward.go#L409 - forwardURL := client.CoreV1().RESTClient(). + forwardURL := cluster.Kubectl.CoreV1().RESTClient(). Post(). Resource("pods"). Namespace(namespace). - Name(podName). + Name(podToConnect). SubResource("portforward"). URL() - httpClient := client.CoreV1().RESTClient().(*rest.RESTClient).Client - p := httputil.ReverseProxy{ - Director: func(req *http.Request) { - req.URL = forwardURL - req.Host = forwardURL.Host - // let kube authentication work - delete(req.Header, "Cookie") - delete(req.Header, "Authorization") - }, - Transport: httpClient.Transport, - FlushInterval: time.Millisecond * 100, - } - - p.ServeHTTP(rw, req) + return runProxy(ctx, c.Writer, c.Request, forwardURL) } diff --git a/internal/api/v1/application/proxy.go b/internal/api/v1/application/proxy.go new file mode 100644 index 0000000000..7698613b66 --- /dev/null +++ b/internal/api/v1/application/proxy.go @@ -0,0 +1,49 @@ +// Copyright © 2021 - 2023 SUSE LLC +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package application + +import ( + "context" + "net/http" + "net/http/httputil" + "net/url" + "time" + + "github.com/epinio/epinio/helpers/kubernetes" + apierror "github.com/epinio/epinio/pkg/api/core/v1/errors" + "k8s.io/client-go/rest" +) + +func runProxy(ctx context.Context, rw http.ResponseWriter, req *http.Request, destination *url.URL) apierror.APIErrors { + clientSetHTTP1, err := kubernetes.GetHTTP1Client(ctx) + if err != nil { + return apierror.InternalError(err) + } + + httpClient := clientSetHTTP1.CoreV1().RESTClient().(*rest.RESTClient).Client + + p := httputil.ReverseProxy{ + Director: func(req *http.Request) { + req.URL = destination + req.Host = destination.Host + // let kube authentication work + delete(req.Header, "Cookie") + delete(req.Header, "Authorization") + }, + Transport: httpClient.Transport, + FlushInterval: time.Millisecond * 100, + } + + p.ServeHTTP(rw, req) + + return nil +}