Repository: moby/moby
File: daemon/libnetwork/agent.go (Go)

Code before:
			// This is a remote task part of a service
			if !prev.ServiceDisabled {
				err := c.rmServiceBinding(prev.ServiceName, prev.ServiceID, nid, eid,
					prev.Name, prev.VirtualIP.AsSlice(), prev.IngressPorts,
					prev.Aliases, prev.TaskAliases, prev.EndpointIP.AsSlice(),
					"handleEpTableEvent", true, true)
				if err != nil {
					logger.WithError(err).Error("failed removing service binding")
				}
			}
		} else {
			// This is a remote container simply attached to an attachable network
			err := c.delContainerNameResolution(nid, eid, prev.Name, prev.TaskAliases,
				prev.EndpointIP.AsSlice(), "handleEpTableEvent")
			if err != nil {
				logger.WithError(err).Errorf("failed removing container name resolution")
			}
		}
	}

	if epRec != nil {
		if epRec.ServiceID != "" {
			// This is a remote task part of a service
			if epRec.ServiceDisabled {
				// Don't double-remove a service binding
				if prev == nil || prev.ServiceID != epRec.ServiceID || !prev.ServiceDisabled {
					err := c.rmServiceBinding(epRec.ServiceName, epRec.ServiceID,
						nid, eid, epRec.Name, epRec.VirtualIP.AsSlice(),
						epRec.IngressPorts, epRec.Aliases, epRec.TaskAliases,
						epRec.EndpointIP.AsSlice(), "handleEpTableEvent", true, false)
					if err != nil {
						logger.WithError(err).Error("failed disabling service binding")
						return
					}
				}
			} else {
				err := c.addServiceBinding(epRec.ServiceName, epRec.ServiceID, nid, eid,
					epRec.Name, epRec.VirtualIP.AsSlice(), epRec.IngressPorts,
					epRec.Aliases, epRec.TaskAliases, epRec.EndpointIP.AsSlice(),
					"handleEpTableEvent")
				if err != nil {
					logger.WithError(err).Error("failed adding service binding")
					return
				}
			}
		} else {
			// This is a remote container simply attached to an attachable network
			err := c.addContainerNameResolution(nid, eid, epRec.Name, epRec.TaskAliases,
				epRec.EndpointIP.AsSlice(), "handleEpTableEvent")
			if err != nil {
				logger.WithError(err).Errorf("failed adding container name resolution")

Review comment (refactor):
No good answer to this one; just that these conditions start to become ... complex, and potentially brittle. I guess we could abstract it or have some intermediate var to describe the condition, but don't think that makes it much better 🤷 

Code after:
	logger.Debug("handleEpTableEvent")

	equivalent := prev.EquivalentTo(epRec)
	if prev != nil {
		if equivalent && prev.ServiceDisabled == epRec.ServiceDisabled {
			// Avoid flapping if we would otherwise remove a service
			// binding then immediately replace it with an equivalent one.
			return
		}

		if prev.ServiceID != "" {
			// This is a remote task part of a service
			if epRec == nil || !equivalent {
				// Either the endpoint is deleted from NetworkDB or has
				// been replaced with a different one. Remove the old
				// binding. The new binding, if any, will be added
				// below.
				err := c.rmServiceBinding(prev.ServiceName, prev.ServiceID, nid, eid,
					prev.Name, prev.VirtualIP.AsSlice(), prev.IngressPorts,
					prev.Aliases, prev.TaskAliases, prev.EndpointIP.AsSlice(),
					"handleEpTableEvent", true, true)
				if err != nil {
					logger.WithError(err).Error("failed removing service binding")
				}
			}
		} else if !prev.ServiceDisabled && (!equivalent || epRec == nil || epRec.ServiceDisabled) {
			// This is a remote container simply attached to an attachable network
			err := c.delContainerNameResolution(nid, eid, prev.Name, prev.TaskAliases,
				prev.EndpointIP.AsSlice(), "handleEpTableEvent")
			if err != nil {
				logger.WithError(err).Errorf("failed removing container name resolution")
			}
		}
	}

	if epRec != nil {
		if epRec.ServiceID != "" {
			// This is a remote task part of a service
			if equivalent && !prev.ServiceDisabled && epRec.ServiceDisabled {
				err := c.rmServiceBinding(epRec.ServiceName, epRec.ServiceID,
					nid, eid, epRec.Name, epRec.VirtualIP.AsSlice(),
					epRec.IngressPorts, epRec.Aliases, epRec.TaskAliases,
					epRec.EndpointIP.AsSlice(), "handleEpTableEvent", true, false)
				if err != nil {
					logger.WithError(err).Error("failed disabling service binding")
					return
				}
			} else if !epRec.ServiceDisabled {
				err := c.addServiceBinding(epRec.ServiceName, epRec.ServiceID, nid, eid,
					epRec.Name, epRec.VirtualIP.AsSlice(), epRec.IngressPorts,
					epRec.Aliases, epRec.TaskAliases, epRec.EndpointIP.AsSlice(),