in istio/pkg/controllers/istioconnector/controller.go [259:310]
func updateIstioServiceEndpoints(se *v1alpha3.ServiceEntry, action discovery.EventType, targetInst *event.InstanceEntry) error {
targetInstanceId := targetInst.InstanceId
newInsts := []*event.InstanceEntry{}
var seAsMSE *event.MicroserviceEntry
// Convert ServiceEntry back to service center service to apply changes to its service center instances
if res := event.NewServiceEntry(se).Convert(); res == nil {
return fmt.Errorf("failed to parse existing Istio ServiceEntry")
} else {
seAsMSE = res
}
switch discovery.EventType(action) {
case discovery.EVT_DELETE:
// Filter out the deleted instance
for _, existingInst := range seAsMSE.Instances {
if existingInst.InstanceId != targetInstanceId {
newInsts = append(newInsts, existingInst)
}
}
if len(seAsMSE.Instances) == len(newInsts) {
log.Warnf("could not push delete for target Service Center instance id %s, instance was not found\n", targetInstanceId)
}
seAsMSE.Instances = newInsts
case discovery.EVT_CREATE:
// CREATE still requires check to determine whether the endpoint already exists; UPDATE is used in this case.
fallthrough
case discovery.EVT_UPDATE:
updated := false
for i, existingInst := range seAsMSE.Instances {
if existingInst.InstanceId == targetInstanceId {
// Found existing instance, update with new instance
seAsMSE.Instances[i] = targetInst
updated = true
break
}
}
if !updated {
// Instance does not already exist, add as new instance
seAsMSE.Instances = append(seAsMSE.Instances, targetInst)
}
}
// Convert the microservice entry back to istio service entry; the serviceports for the changed endpoints will be regenerated appropriately by conversion logic
var regenedSe *v1alpha3.ServiceEntry
if res := seAsMSE.Convert(); res == nil {
return fmt.Errorf("failed to parse changes for Istio ServiceEntry")
} else {
regenedSe = res.ServiceEntry
}
// Only take regened ports and new workloadentries, preserves rest of original serviceentry
se.Spec.Endpoints = regenedSe.Spec.Endpoints
se.Spec.Ports = regenedSe.Spec.Ports
return nil
}