Skip to content

Commit

Permalink
addressed comments from @tomleb + other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrkefc committed Jan 31, 2025
1 parent f84202b commit 0afa897
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 186 deletions.
5 changes: 2 additions & 3 deletions Dockerfile.proxy
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
FROM golang:1.23 AS builder
WORKDIR /app

COPY go.mod .
COPY go.sum .
COPY . .
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 go build -o proxy ./cmd/proxy

FROM scratch
Expand Down
2 changes: 1 addition & 1 deletion cmd/proxy/proxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ metadata:
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "create"]
verbs: ["get", "create", "update"]

---
apiVersion: rbac.authorization.k8s.io/v1
Expand Down
1 change: 1 addition & 0 deletions examples/proxyclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func main() {
}

proxyClient, err := proxyclient.New(
ctx,
connectSecret,
namespace,
certSecretName,
Expand Down
30 changes: 16 additions & 14 deletions forward/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ type PortForward struct {
podClient v1.PodController
namespace string
labelSelector string
Ports []string
ports []string

readyCh chan struct{}
stopCh chan struct{}
cancel context.CancelFunc
}

Expand Down Expand Up @@ -59,15 +58,13 @@ func New(restConfig *rest.Config, podClient v1.PodController, namespace string,
podClient: podClient,
namespace: namespace,
labelSelector: labelSelector,
Ports: ports,
ports: ports,
readyCh: make(chan struct{}, 1),
stopCh: make(chan struct{}, 1),
}, nil
}

func (r *PortForward) Stop() {
r.cancel()
r.stopCh <- struct{}{}
}

func (r *PortForward) Start() error {
Expand All @@ -85,7 +82,7 @@ func (r *PortForward) Start() error {
logrus.Infoln("Goroutine stopped.")
return
default:
err := r.runForwarder(ctx, r.readyCh, r.stopCh, r.Ports)
err := r.runForwarder(ctx, r.readyCh, r.ports)
if err != nil {
if errors.Is(err, portforward.ErrLostConnectionToPod) {
logrus.Errorf("Lost connection to pod (no automatic retry in this refactor): %v", err)
Expand All @@ -100,17 +97,22 @@ func (r *PortForward) Start() error {
}
}()

// wait for the port forward to be ready if not failed
<-r.readyCh
// wait for the port forward to be ready if not failed or cancelled
for {
select {
case <-ctx.Done():
return nil

if readyErr != nil {
return readyErr
case <-r.readyCh:
if readyErr != nil {
return readyErr
}
return nil
}
}

return nil
}

func (r *PortForward) runForwarder(ctx context.Context, readyCh, stopCh chan struct{}, ports []string) error {
func (r *PortForward) runForwarder(ctx context.Context, readyCh chan struct{}, ports []string) error {
podName, err := lookForPodName(ctx, r.namespace, r.labelSelector, r.podClient)
if err != nil {
return err
Expand All @@ -134,7 +136,7 @@ func (r *PortForward) runForwarder(ctx context.Context, readyCh, stopCh chan str
}, http.MethodPost, &serverURL)

stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
forwarder, err := portforward.New(dialer, ports, stopCh, readyCh, stdout, stderr)
forwarder, err := portforward.New(dialer, ports, ctx.Done(), readyCh, stdout, stderr)
if err != nil {
return err
}
Expand Down
61 changes: 30 additions & 31 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,67 @@ go 1.23
require (
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/client_golang v1.19.1
github.com/rancher/dynamiclistener v0.6.0
github.com/rancher/lasso v0.0.0-20240705194423-b2a060d103c1
github.com/rancher/wrangler/v3 v3.0.0
github.com/rancher/wrangler/v3 v3.0.1-rc.2
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
k8s.io/api v0.31.1
k8s.io/apimachinery v0.31.1
k8s.io/client-go v0.31.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/spdystream v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rancher/lasso v0.0.0-20240924233157-8f384efc8813 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.30.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit 0afa897

Please sign in to comment.