8
8
9
9
mapset "github.com/deckarep/golang-set"
10
10
xds_discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
11
+ "github.com/google/uuid"
11
12
"github.com/pkg/errors"
12
13
13
14
"github.com/openservicemesh/osm/pkg/announcements"
@@ -33,24 +34,23 @@ func (s *Server) StreamAggregatedResources(server xds_discovery.AggregatedDiscov
33
34
}
34
35
35
36
// If maxDataPlaneConnections is enabled i.e. not 0, then check that the number of Envoy connections is less than maxDataPlaneConnections
36
- if s .cfg .GetMaxDataPlaneConnections () > 0 && s .proxyRegistry .GetConnectedProxyCount () >= s .cfg .GetMaxDataPlaneConnections () {
37
+ if s .cfg .GetMaxDataPlaneConnections () != 0 && s .proxyRegistry .GetConnectedProxyCount () >= s .cfg .GetMaxDataPlaneConnections () {
37
38
metricsstore .DefaultMetricsStore .ProxyMaxConnectionsRejected .Inc ()
38
39
return errTooManyConnections
39
40
}
40
41
41
42
log .Trace ().Msgf ("Envoy with certificate SerialNumber=%s connected" , certSerialNumber )
42
43
metricsstore .DefaultMetricsStore .ProxyConnectCount .Inc ()
43
44
45
+ kind , uuid , si , err := getCertificateCommonNameMeta (certCommonName )
46
+ if err != nil {
47
+ return fmt .Errorf ("error parsing certificate common name %s: %w" , certCommonName , err )
48
+ }
49
+
44
50
// This is the Envoy proxy that just connected to the control plane.
45
51
// NOTE: This is step 1 of the registration. At this point we do not yet have context on the Pod.
46
52
// Details on which Pod this Envoy is fronting will arrive via xDS in the NODE_ID string.
47
- // When this arrives we will call RegisterProxy() a second time - this time with Pod context!
48
- proxy , err := envoy .NewProxy (certCommonName , certSerialNumber , utils .GetIPFromContext (server .Context ()))
49
- if err != nil {
50
- log .Error ().Err (err ).Str (errcode .Kind , errcode .GetErrCodeWithMetric (errcode .ErrInitializingProxy )).
51
- Msgf ("Error initializing proxy with certificate SerialNumber=%s" , certSerialNumber )
52
- return err
53
- }
53
+ proxy := envoy .NewProxy (kind , uuid , si , utils .GetIPFromContext (server .Context ()))
54
54
55
55
if err := s .recordPodMetadata (proxy ); err == errServiceAccountMismatch {
56
56
// Service Account mismatch
@@ -332,6 +332,22 @@ func isCNforProxy(proxy *envoy.Proxy, cn certificate.CommonName) bool {
332
332
return identityForCN == proxy .Identity .ToK8sServiceAccount ()
333
333
}
334
334
335
+ func getCertificateCommonNameMeta (cn certificate.CommonName ) (envoy.ProxyKind , uuid.UUID , identity.ServiceIdentity , error ) {
336
+ // XDS cert CN is of the form <proxy-UUID>.<kind>.<proxy-identity>.<namespace>.<trust-domain>
337
+ chunks := strings .SplitN (cn .String (), constants .DomainDelimiter , 5 )
338
+ if len (chunks ) < 3 {
339
+ return "" , uuid.UUID {}, "" , errInvalidCertificateCN
340
+ }
341
+ proxyUUID , err := uuid .Parse (chunks [0 ])
342
+ if err != nil {
343
+ log .Error ().Err (err ).Str (errcode .Kind , errcode .GetErrCodeWithMetric (errcode .ErrParsingXDSCertCN )).
344
+ Msgf ("Error parsing %s into uuid.UUID" , chunks [0 ])
345
+ return "" , uuid.UUID {}, "" , err
346
+ }
347
+
348
+ return envoy .ProxyKind (chunks [1 ]), proxyUUID , identity .New (chunks [2 ], chunks [3 ]), nil
349
+ }
350
+
335
351
// recordPodMetadata records pod metadata and verifies the certificate issued for this pod
336
352
// is for the same service account as seen on the pod's service account
337
353
func (s * Server ) recordPodMetadata (p * envoy.Proxy ) error {
@@ -341,7 +357,7 @@ func (s *Server) recordPodMetadata(p *envoy.Proxy) error {
341
357
return nil
342
358
}
343
359
344
- pod , err := envoy . GetPodFromCertificate ( p . GetCertificateCommonName (), s .kubecontroller )
360
+ pod , err := s .kubecontroller . GetPodForProxy ( p )
345
361
if err != nil {
346
362
log .Warn ().Str ("proxy" , p .String ()).Msg ("Could not find pod for connecting proxy. No metadata was recorded." )
347
363
return nil
0 commit comments