Skip to content

Commit 86df4a7

Browse files
committed
revert: restore original code with minimal diff
1 parent 8f9c83c commit 86df4a7

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

Makefile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ test-infrastructure: $(SETUP_ENVTEST) ## Run unit and integration tests with rac
943943
# Note: Fuzz tests are not executed with race detector because they would just time out.
944944
# To achieve that, all files with fuzz tests have the "!race" build tag, to still run fuzz tests
945945
# we have an additional `go test` run that focuses on "TestFuzzyConversion".
946-
cd test/infrastructure; KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race $(TEST_ARGS) ./...
946+
cd test/infrastructure; KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race ./... $(TEST_ARGS)
947947
$(MAKE) test-infrastructure-conversions TEST_ARGS="$(TEST_ARGS)"
948948

949949
.PHONY: test-infrastructure-conversions
@@ -956,11 +956,10 @@ test-infrastructure-verbose: ## Run unit and integration tests with race detecto
956956

957957
.PHONY: test-infrastructure-junit
958958
test-infrastructure-junit: $(SETUP_ENVTEST) $(GOTESTSUM) ## Run unit and integration tests with race detector and generate a junit report for docker infrastructure provider
959-
@mkdir -p "$(ARTIFACTS)"
960-
cd test/infrastructure; set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race $(TEST_ARGS) -json ./...; echo $$? > $(ARTIFACTS)/junit.infra_docker.exitcode) | tee $(ARTIFACTS)/junit.infra_docker.stdout
959+
cd test/infrastructure; set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -race -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.infra_docker.exitcode) | tee $(ARTIFACTS)/junit.infra_docker.stdout
961960
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.infra_docker.xml --raw-command cat $(ARTIFACTS)/junit.infra_docker.stdout
962961
exit $$(cat $(ARTIFACTS)/junit.infra_docker.exitcode)
963-
cd test/infrastructure; set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -run "^TestFuzzyConversion$$" $(TEST_ARGS) -json ./...; echo $$? > $(ARTIFACTS)/junit-fuzz.infra_docker.exitcode) | tee $(ARTIFACTS)/junit-fuzz.infra_docker.stdout
962+
cd test/infrastructure; set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -run "^TestFuzzyConversion$$" -json ./... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit-fuzz.infra_docker.exitcode) | tee $(ARTIFACTS)/junit-fuzz.infra_docker.stdout
964963
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit-fuzz.infra_docker.xml --raw-command cat $(ARTIFACTS)/junit-fuzz.infra_docker.stdout
965964
exit $$(cat $(ARTIFACTS)/junit-fuzz.infra_docker.exitcode)
966965

test/infrastructure/inmemory/pkg/server/proxy/dial.go

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,53 +33,49 @@ import (
3333

3434
const defaultTimeout = 10 * time.Second
3535

36-
// Dialer opens connections via Kubernetes API server port-forward.
37-
//
38-
// Safe for concurrent use: each dial builds its own SPDY transport and http.Client
39-
// to avoid races.
36+
// Dialer creates connections using Kubernetes API Server port-forwarding.
4037
type Dialer struct {
4138
proxy Proxy
4239
clientset *kubernetes.Clientset
4340
timeout time.Duration
4441
}
4542

46-
// NewDialer returns a Dialer for proxy p.
47-
// Options may set timeouts or other fields. The timeout also updates p.KubeConfig.
43+
// NewDialer creates a new dialer for a given API server scope.
4844
func NewDialer(p Proxy, options ...func(*Dialer) error) (*Dialer, error) {
4945
if p.Port == 0 {
5046
return nil, errors.New("port required")
5147
}
5248

53-
d := &Dialer{proxy: p}
49+
dialer := &Dialer{
50+
proxy: p,
51+
}
5452

5553
for _, option := range options {
56-
if err := option(d); err != nil {
54+
err := option(dialer)
55+
if err != nil {
5756
return nil, err
5857
}
5958
}
6059

61-
if d.timeout == 0 {
62-
d.timeout = defaultTimeout
60+
if dialer.timeout == 0 {
61+
dialer.timeout = defaultTimeout
6362
}
64-
p.KubeConfig.Timeout = d.timeout
65-
66-
cs, err := kubernetes.NewForConfig(p.KubeConfig)
63+
p.KubeConfig.Timeout = dialer.timeout
64+
clientset, err := kubernetes.NewForConfig(p.KubeConfig)
6765
if err != nil {
6866
return nil, err
6967
}
70-
d.clientset = cs
71-
return d, nil
68+
dialer.clientset = clientset
69+
return dialer, nil
7270
}
7371

74-
// DialContextWithAddr matches gRPC's dialer signature and calls DialContext.
72+
// DialContextWithAddr is a GO grpc compliant dialer construct.
7573
func (d *Dialer) DialContextWithAddr(ctx context.Context, addr string) (net.Conn, error) {
7674
return d.DialContext(ctx, scheme, addr)
7775
}
7876

79-
// DialContext opens a port-forwarded connection to addr.
80-
//
81-
// Builds a fresh SPDY transport each time to avoid shared-state races.
82-
// ctx is kept for gRPC compatibility but ignored here.
77+
// DialContext creates proxied port-forwarded connections.
78+
// ctx is currently unused, but fulfils the type signature used by GRPC.
8379
func (d *Dialer) DialContext(_ context.Context, _ string, addr string) (net.Conn, error) {
8480
proxyTransport, upgrader, err := spdy.RoundTripperFor(d.proxy.KubeConfig)
8581
if err != nil {
@@ -96,17 +92,24 @@ func (d *Dialer) DialContext(_ context.Context, _ string, addr string) (net.Conn
9692

9793
dialer := spdy.NewDialer(upgrader, httpClient, "POST", req.URL())
9894

95+
// Create a new connection from the dialer.
96+
//
97+
// Warning: Any early return should close this connection, otherwise we're going to leak them.
9998
connection, _, err := dialer.Dial(portforward.PortForwardProtocolV1Name)
10099
if err != nil {
101100
return nil, errors.Wrap(err, "error upgrading connection")
102101
}
103102

104-
// Port must match proxy.Port; request ID is always "0".
103+
// Create the headers.
105104
headers := http.Header{}
105+
106+
// Set the header port number to match the proxy one.
106107
headers.Set(corev1.PortHeader, fmt.Sprintf("%d", d.proxy.Port))
108+
109+
// We only create a single stream over the connection
107110
headers.Set(corev1.PortForwardRequestIDHeader, "0")
108111

109-
// Error stream: required by protocol, closed immediately.
112+
// Create the error stream.
110113
headers.Set(corev1.StreamType, corev1.StreamTypeError)
111114
errorStream, err := connection.CreateStream(headers)
112115
if err != nil {
@@ -115,14 +118,18 @@ func (d *Dialer) DialContext(_ context.Context, _ string, addr string) (net.Conn
115118
connection.Close(),
116119
})
117120
}
121+
// Close the error stream right away, we're not writing to it.
118122
if err := errorStream.Close(); err != nil {
119123
return nil, kerrors.NewAggregate([]error{
120124
err,
121125
connection.Close(),
122126
})
123127
}
124128

125-
// Data stream: switch type and create the I/O stream.
129+
// Create the data stream.
130+
//
131+
// NOTE: Given that we're reusing the headers,
132+
// we need to overwrite the stream type before creating it.
126133
headers.Set(corev1.StreamType, corev1.StreamTypeData)
127134
dataStream, err := connection.CreateStream(headers)
128135
if err != nil {
@@ -132,17 +139,17 @@ func (d *Dialer) DialContext(_ context.Context, _ string, addr string) (net.Conn
132139
})
133140
}
134141

142+
// Create the net.Conn and return.
135143
return NewConn(connection, dataStream), nil
136144
}
137145

138-
// DialTimeout sets the per-dial timeout (also applied to KubeConfig).
146+
// DialTimeout sets the timeout.
139147
func DialTimeout(duration time.Duration) func(*Dialer) error {
140148
return func(d *Dialer) error {
141149
return d.setTimeout(duration)
142150
}
143151
}
144152

145-
// setTimeout updates timeout; used by DialTimeout.
146153
func (d *Dialer) setTimeout(duration time.Duration) error {
147154
d.timeout = duration
148155
return nil

0 commit comments

Comments
 (0)