Skip to content

Commit

Permalink
dept: refactor common parts of proxy setup for exec and port-forward
Browse files Browse the repository at this point in the history
chore: update callers
  • Loading branch information
andreas-kupries committed Jan 26, 2023
1 parent 377e277 commit 4f3665f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 63 deletions.
36 changes: 4 additions & 32 deletions internal/api/v1/application/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
34 changes: 3 additions & 31 deletions internal/api/v1/application/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
49 changes: 49 additions & 0 deletions internal/api/v1/application/proxy.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4f3665f

Please sign in to comment.