-
Notifications
You must be signed in to change notification settings - Fork 880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
one cluster should have only one transport to reduce the number of TCP connections #5615
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,21 @@ package proxy | |
|
||
import ( | ||
"context" | ||
"crypto/md5" //nolint:gosec | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
"sort" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"golang.org/x/sync/singleflight" | ||
authenticationv1 "k8s.io/api/authentication/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/httpstream" | ||
|
@@ -44,6 +49,13 @@ import ( | |
// SecretGetterFunc is a function to get secret. | ||
type SecretGetterFunc func(context.Context, string, string) (*corev1.Secret, error) | ||
|
||
type clusterEndpointInfo struct { | ||
Transport *http.Transport | ||
} | ||
|
||
var clusterEndpointInfoStore sync.Map | ||
var singleExecution singleflight.Group | ||
|
||
// ConnectCluster returns a handler for proxy cluster. | ||
func ConnectCluster(ctx context.Context, cluster *clusterapis.Cluster, proxyPath string, secretGetter SecretGetterFunc, responder registryrest.Responder) (http.Handler, error) { | ||
tlsConfig, err := GetTLSConfigForCluster(ctx, cluster, secretGetter) | ||
|
@@ -159,12 +171,33 @@ func Location(cluster *clusterapis.Cluster, tlsConfig *tls.Config) (*url.URL, ht | |
return nil, nil, err | ||
} | ||
|
||
proxyTransport, err := createProxyTransport(cluster, tlsConfig) | ||
clusterHash := generateMd5HashForCluster(cluster) | ||
if v, ok := clusterEndpointInfoStore.Load(clusterHash); ok { | ||
clusterEndpointsInfo := v.(clusterEndpointInfo) | ||
return location, clusterEndpointsInfo.Transport, nil | ||
} | ||
|
||
endpointInfo, err, _ := singleExecution.Do(clusterHash, func() (interface{}, error) { | ||
if value, ok := clusterEndpointInfoStore.Load(clusterHash); ok { | ||
return value, nil | ||
} | ||
proxyTransport, err := createProxyTransport(cluster, tlsConfig) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if tlsConfig changes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will not work fine, cannot request successfully. But I think no need to consider the scenario abourt secrets changes. |
||
if err != nil { | ||
return nil, err | ||
} | ||
clusterEndpoint := clusterEndpointInfo{ | ||
Transport: proxyTransport, | ||
} | ||
clusterEndpointInfoStore.Store(clusterHash, clusterEndpointInfo{ | ||
Transport: proxyTransport, | ||
}) | ||
return clusterEndpoint, nil | ||
}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return location, proxyTransport, nil | ||
transport := endpointInfo.(clusterEndpointInfo).Transport | ||
return location, transport, nil | ||
} | ||
|
||
func constructLocation(cluster *clusterapis.Cluster) (*url.URL, error) { | ||
|
@@ -238,3 +271,18 @@ func SkipGroup(group string) bool { | |
return false | ||
} | ||
} | ||
|
||
func generateMd5HashForCluster(cluster *clusterapis.Cluster) string { | ||
usedFields := make([]string, 0) | ||
proxyHeaderKeys := make([]string, 0, len(cluster.Spec.ProxyHeader)) | ||
for key := range cluster.Spec.ProxyHeader { | ||
proxyHeaderKeys = append(proxyHeaderKeys, key) | ||
} | ||
sort.Strings(proxyHeaderKeys) | ||
for _, key := range proxyHeaderKeys { | ||
usedFields = append(usedFields, key, cluster.Spec.ProxyHeader[key]) | ||
} | ||
usedFields = append(usedFields, string(cluster.UID), cluster.Spec.ProxyURL, cluster.Spec.APIEndpoint) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is UID used by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UID changed means the cluster rejoined, should create a new transport. |
||
hash := md5.Sum([]byte(strings.Join(usedFields, ""))) //nolint:gosec | ||
return hex.EncodeToString(hash[:]) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems that the go.mod file has not changed substantially. you need to restore it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
difference in indirect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's necessary.