Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjohn committed Apr 29, 2024
1 parent 5f1716e commit 8dc4a7f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion pilot/pkg/model/endpointshards.go
Expand Up @@ -101,7 +101,8 @@ func (es *EndpointShards) CopyEndpoints(portMap map[string]int) map[int][]*Istio
res := map[int][]*IstioEndpoint{}
for _, v := range es.Shards {
for _, ep := range v {
k := ptr.NonEmptyOrDefault(ep.ServicePortNameKey, ep.ServicePortName)
// use the port name as the key, unless LegacyClusterPortKey is set and takes precedence
k := ptr.NonEmptyOrDefault(ep.LegacyClusterPortKey, ep.ServicePortName)
portNum, f := portMap[k]
if !f {
continue
Expand Down
4 changes: 3 additions & 1 deletion pilot/pkg/model/push_context.go
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"math"
"sort"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -1446,7 +1447,8 @@ func (ps *PushContext) initServiceRegistry(env *Environment, configsUpdate sets.
for _, s := range allServices {
portMap := map[string]int{}
for _, port := range s.Ports {
portMap[fmt.Sprintf("%s~%d", port.Name, port.Port)] = port.Port
// In EDS we match on port *name*. But for historical reasons, we match on port number for CDS.
portMap[strconv.Itoa(port.Port)] = port.Port
}

svcKey := s.Key()
Expand Down
7 changes: 5 additions & 2 deletions pilot/pkg/model/service.go
Expand Up @@ -485,8 +485,11 @@ type IstioEndpoint struct {
Address string

// ServicePortName tracks the name of the port, this is used to select the IstioEndpoint by service port.
ServicePortName string
ServicePortNameKey string
ServicePortName string
// LegacyClusterPortKey provides an alternative key from ServicePortName to support legacy quirks in the API.
// Basically, EDS merges by port name, but CDS historically ignored port name and matched on number.
// Note that for Kubernetes Service, this is identical - its only ServiceEntry where these checks can differ
LegacyClusterPortKey string

// ServiceAccount holds the associated service account.
ServiceAccount string
Expand Down
20 changes: 10 additions & 10 deletions pilot/pkg/serviceregistry/serviceentry/conversion.go
Expand Up @@ -15,8 +15,8 @@
package serviceentry

import (
"fmt"
"net/netip"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -286,8 +286,8 @@ func (s *Controller) convertEndpoint(service *model.Service, servicePort *networ
EndpointPort: instancePort,
ServicePortName: servicePort.Name,

ServicePortNameKey: fmt.Sprintf("%s~%d", servicePort.Name, servicePort.Number),
Network: network.ID(wle.Network),
LegacyClusterPortKey: strconv.Itoa(int(servicePort.Number)),
Network: network.ID(wle.Network),
Locality: model.Locality{
Label: locality,
ClusterID: clusterID,
Expand Down Expand Up @@ -344,12 +344,12 @@ func (s *Controller) convertServiceEntryToInstances(cfg config.Config, services
}
out = append(out, &model.ServiceInstance{
Endpoint: &model.IstioEndpoint{
Address: string(service.Hostname),
EndpointPort: endpointPort,
ServicePortName: serviceEntryPort.Name,
ServicePortNameKey: fmt.Sprintf("%s~%d", serviceEntryPort.Name, serviceEntryPort.Number),
Labels: nil,
TLSMode: model.DisabledTLSModeLabel,
Address: string(service.Hostname),
EndpointPort: endpointPort,
ServicePortName: serviceEntryPort.Name,
LegacyClusterPortKey: strconv.Itoa(int(serviceEntryPort.Number)),
Labels: nil,
TLSMode: model.DisabledTLSModeLabel,
},
Service: service,
ServicePort: convertPort(serviceEntryPort),
Expand Down Expand Up @@ -399,7 +399,7 @@ func convertWorkloadInstanceToServiceInstance(workloadInstance *model.WorkloadIn
}
ep := workloadInstance.Endpoint.ShallowCopy()
ep.ServicePortName = serviceEntryPort.Name
ep.ServicePortNameKey = fmt.Sprintf("%s~%d", serviceEntryPort.Name, serviceEntryPort.Number)
ep.LegacyClusterPortKey = strconv.Itoa(int(serviceEntryPort.Number))

ep.EndpointPort = targetPort
out = append(out, &model.ServiceInstance{
Expand Down

0 comments on commit 8dc4a7f

Please sign in to comment.