Skip to content

Commit 8160045

Browse files
committed
Implement sync.Once concurrent access solution
Signed-off-by: Nelo-T. Wallus <[email protected]>
1 parent de12c71 commit 8160045

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

pkg/server/config.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net/http/httputil"
2626
"net/url"
2727
"os"
28+
"sync"
2829

2930
apiextensionsapiserver "k8s.io/apiextensions-apiserver/pkg/apiserver"
3031
"k8s.io/apimachinery/pkg/runtime"
@@ -207,7 +208,16 @@ func NewConfig(ctx context.Context, opts kcpserveroptions.CompletedOptions) (*Co
207208

208209
// break connections on the tcp layer. Setting the client timeout would
209210
// also apply to watches, which we don't want.
210-
c.GenericConfig.LoopbackClientConfig.Wrap(network.DefaultTransportWrapper)
211+
// To prevent data races when wrapping the default transport in
212+
// multiple goroutines the wrapping is done in a once.
213+
var wrapDefaultTransportWrapper = sync.Once{}
214+
c.GenericConfig.LoopbackClientConfig.Wrap(func(rt http.RoundTripper) http.RoundTripper {
215+
wrapDefaultTransportWrapper.Do(func() {
216+
rt = network.DefaultTransportWrapper(rt)
217+
})
218+
return rt
219+
})
220+
211221
// Set effective version to the default kube version of the vendored libs.
212222
c.GenericConfig.EffectiveVersion = utilversion.DefaultKubeEffectiveVersion()
213223

0 commit comments

Comments
 (0)