Skip to content

Commit

Permalink
DRY SE ports construction
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Dorosh <kevin.dorosh@solo.io>
  • Loading branch information
Kevin Dorosh committed Jul 5, 2023
1 parent 0eb683e commit 5a0ee02
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
27 changes: 2 additions & 25 deletions pilot/pkg/serviceregistry/kube/controller/ambientindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,31 +848,8 @@ func (c *Controller) constructWorkload(pod *v1.Pod, waypoint *workloadapi.Gatewa
for _, podIP := range pod.Status.PodIPs {
addresses = append(addresses, parseIP(podIP.IP))
}
for _, svc := range c.ambientIndex.servicesMap {

if svc.Attributes.ServiceEntry == nil {
// if we are here then this is dev error
log.Warn("dev error: service entry spec is nil; it should have been populated by the service entry handler")
continue
}

if svc.Attributes.ServiceEntry.WorkloadSelector == nil {
// nothing to do. we construct the ztunnel config if `endpoints` are provided in the service entry handler
continue
}

if svc.Attributes.ServiceEntry.Endpoints != nil {
// it is an error to provide both `endpoints` and `workloadSelector` in a service entry
continue
}

sel := svc.Attributes.ServiceEntry.WorkloadSelector.Labels
if !labels.Instance(sel).SubsetOf(pod.Labels) {
continue
}

ports := getPortsForServiceEntry(svc, nil)
workloadServices[namespacedHostname(svc.Attributes.ServiceEntryNamespace, svc.Hostname.String())] = ports
for nsName, ports := range c.getWorkloadServices(nil, pod.Labels) {
workloadServices[nsName] = ports
}

wl := &workloadapi.Workload{
Expand Down
59 changes: 34 additions & 25 deletions pilot/pkg/serviceregistry/kube/controller/ambientindex_external.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,31 +248,8 @@ func (c *Controller) constructWorkloadFromWorkloadEntry(workloadEntry *v1alpha3.

// for constructing a workload from a standalone workload entry, which can be selected by many service entries
if parentServiceEntry == nil {
for _, svc := range c.ambientIndex.servicesMap {

if svc.Attributes.ServiceEntry == nil {
// if we are here then this is dev error
log.Warn("dev error: service entry spec is nil; it should have been populated by the service entry handler")
continue
}

if svc.Attributes.ServiceEntry.WorkloadSelector == nil {
// nothing to do. we construct the ztunnel config if `endpoints` are provided in the service entry handler
continue
}

if svc.Attributes.ServiceEntry.Endpoints != nil {
// it is an error to provide both `endpoints` and `workloadSelector` in a service entry
continue
}

sel := svc.Attributes.ServiceEntry.WorkloadSelector.Labels
if !labels.Instance(sel).SubsetOf(workloadEntry.Labels) {
continue
}

ports := getPortsForServiceEntry(svc, workloadEntry)
workloadServices[namespacedHostname(svc.Attributes.ServiceEntryNamespace, svc.Hostname.String())] = ports
for nsName, ports := range c.getWorkloadServices(workloadEntry, workloadEntry.Labels) {
workloadServices[nsName] = ports
}
}

Expand Down Expand Up @@ -470,6 +447,38 @@ func (c *Controller) cleanupOldWorkloadEntriesInlinedOnServiceEntry(svc *model.S
}
}

func (c *Controller) getWorkloadServices(workloadEntry *v1alpha3.WorkloadEntry, workloadLabels map[string]string) map[string]*workloadapi.PortList {
workloadServices := map[string]*workloadapi.PortList{}
for _, svc := range c.ambientIndex.servicesMap {

if svc.Attributes.ServiceEntry == nil {
// if we are here then this is dev error
log.Warn("dev error: service entry spec is nil; it should have been populated by the service entry handler")
continue
}

if svc.Attributes.ServiceEntry.WorkloadSelector == nil {
// nothing to do. we construct the ztunnel config if `endpoints` are provided in the service entry handler
continue
}

if svc.Attributes.ServiceEntry.Endpoints != nil {
// it is an error to provide both `endpoints` and `workloadSelector` in a service entry
continue
}

sel := svc.Attributes.ServiceEntry.WorkloadSelector.Labels
if !labels.Instance(sel).SubsetOf(workloadLabels) {
continue
}

ports := getPortsForServiceEntry(svc, workloadEntry)
workloadServices[namespacedHostname(svc.Attributes.ServiceEntryNamespace, svc.Hostname.String())] = ports
}

return workloadServices
}

func getVIPsFromServiceEntry(svc *model.Service) []string {
var vips []string
vips = append(vips, svc.Attributes.ServiceEntry.Addresses...)
Expand Down

0 comments on commit 5a0ee02

Please sign in to comment.