@@ -337,6 +337,135 @@ func (s) TestEDSParseRespProtoAdditionalAddrs(t *testing.T) {
337337 }
338338}
339339
340+ func (s ) TestUnmarshalEndpointHashKey (t * testing.T ) {
341+ baseCLA := & v3endpointpb.ClusterLoadAssignment {
342+ Endpoints : []* v3endpointpb.LocalityLbEndpoints {
343+ {
344+ Locality : & v3corepb.Locality {Region : "r" },
345+ LbEndpoints : []* v3endpointpb.LbEndpoint {
346+ {
347+ HostIdentifier : & v3endpointpb.LbEndpoint_Endpoint {
348+ Endpoint : & v3endpointpb.Endpoint {
349+ Address : & v3corepb.Address {
350+ Address : & v3corepb.Address_SocketAddress {
351+ SocketAddress : & v3corepb.SocketAddress {
352+ Address : "test-address" ,
353+ PortSpecifier : & v3corepb.SocketAddress_PortValue {
354+ PortValue : 8080 ,
355+ },
356+ },
357+ },
358+ },
359+ },
360+ },
361+ },
362+ },
363+ LoadBalancingWeight : & wrapperspb.UInt32Value {Value : 1 },
364+ },
365+ },
366+ }
367+
368+ tests := []struct {
369+ name string
370+ metadata * v3corepb.Metadata
371+ wantHashKey string
372+ compatEnvVar bool
373+ }{
374+ {
375+ name : "no metadata" ,
376+ metadata : nil ,
377+ wantHashKey : "" ,
378+ },
379+ {
380+ name : "empty metadata" ,
381+ metadata : & v3corepb.Metadata {},
382+ wantHashKey : "" ,
383+ },
384+ {
385+ name : "filter metadata without envoy.lb" ,
386+ metadata : & v3corepb.Metadata {
387+ FilterMetadata : map [string ]* structpb.Struct {
388+ "test-filter" : {},
389+ },
390+ },
391+ wantHashKey : "" ,
392+ },
393+ {
394+ name : "nil envoy.lb" ,
395+ metadata : & v3corepb.Metadata {
396+ FilterMetadata : map [string ]* structpb.Struct {
397+ "envoy.lb" : nil ,
398+ },
399+ },
400+ wantHashKey : "" ,
401+ },
402+ {
403+ name : "envoy.lb without hash key" ,
404+ metadata : & v3corepb.Metadata {
405+ FilterMetadata : map [string ]* structpb.Struct {
406+ "envoy.lb" : {
407+ Fields : map [string ]* structpb.Value {
408+ "hash_key" : {
409+ Kind : & structpb.Value_NumberValue {NumberValue : 123.0 },
410+ },
411+ },
412+ },
413+ },
414+ },
415+ wantHashKey : "" ,
416+ },
417+ {
418+ name : "envoy.lb with hash key, compat mode off" ,
419+ metadata : & v3corepb.Metadata {
420+ FilterMetadata : map [string ]* structpb.Struct {
421+ "envoy.lb" : {
422+ Fields : map [string ]* structpb.Value {
423+ "hash_key" : {
424+ Kind : & structpb.Value_StringValue {StringValue : "test-hash-key" },
425+ },
426+ },
427+ },
428+ },
429+ },
430+ wantHashKey : "test-hash-key" ,
431+ },
432+ {
433+ name : "envoy.lb with hash key, compat mode on" ,
434+ metadata : & v3corepb.Metadata {
435+ FilterMetadata : map [string ]* structpb.Struct {
436+ "envoy.lb" : {
437+ Fields : map [string ]* structpb.Value {
438+ "hash_key" : {
439+ Kind : & structpb.Value_StringValue {StringValue : "test-hash-key" },
440+ },
441+ },
442+ },
443+ },
444+ },
445+ wantHashKey : "" ,
446+ compatEnvVar : true ,
447+ },
448+ }
449+
450+ for _ , test := range tests {
451+ t .Run (test .name , func (t * testing.T ) {
452+ testutils .SetEnvConfig (t , & envconfig .XDSEndpointHashKeyBackwardCompat , test .compatEnvVar )
453+
454+ cla := proto .Clone (baseCLA ).(* v3endpointpb.ClusterLoadAssignment )
455+ cla .Endpoints [0 ].LbEndpoints [0 ].Metadata = test .metadata
456+ marshalledCLA := testutils .MarshalAny (t , cla )
457+ _ , update , err := unmarshalEndpointsResource (marshalledCLA )
458+ if err != nil {
459+ t .Fatalf ("unmarshalEndpointsResource() got error = %v, want success" , err )
460+ }
461+ got := update .Localities [0 ].Endpoints [0 ].HashKey
462+ if got != test .wantHashKey {
463+ t .Errorf ("unmarshalEndpointResource() endpoint hash key: got %s, want %s" , got , test .wantHashKey )
464+ }
465+ })
466+ }
467+ }
468+
340469func (s ) TestUnmarshalEndpoints (t * testing.T ) {
341470 var v3EndpointsAny = testutils .MarshalAny (t , func () * v3endpointpb.ClusterLoadAssignment {
342471 clab0 := newClaBuilder ("test" , nil )
0 commit comments