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

Code before:
		return
	}

	err := proto.Unmarshal(value, &epRec)
	if err != nil {
		log.G(context.TODO()).Errorf("Failed to unmarshal service table value: %v", err)
		return
	}

	containerName := epRec.Name
	svcName := epRec.ServiceName
	svcID := epRec.ServiceID
	vip := net.ParseIP(epRec.VirtualIP)
	ip := net.ParseIP(epRec.EndpointIP)
	ingressPorts := epRec.IngressPorts
	serviceAliases := epRec.Aliases
	taskAliases := epRec.TaskAliases

	if containerName == "" || ip == nil {
		log.G(context.TODO()).Errorf("Invalid endpoint name/ip received while handling service table event %s", value)
		return
	}

	switch ev.(type) {
	case networkdb.CreateEvent, networkdb.UpdateEvent:
		log.G(context.TODO()).Debugf("handleEpTableEvent ADD/UPD %s R:%v", eid, epRec)
		if svcID != "" {
			// This is a remote task part of a service
			if epRec.ServiceDisabled {
				if err := c.rmServiceBinding(svcName, svcID, nid, eid, containerName, vip, ingressPorts, serviceAliases, taskAliases, ip, "handleEpTableEvent", true, false); err != nil {
					log.G(context.TODO()).Errorf("failed disabling service binding for %s epRec:%v err:%v", eid, epRec, err)
					return
				}
			} else {
				if err := c.addServiceBinding(svcName, svcID, nid, eid, containerName, vip, ingressPorts, serviceAliases, taskAliases, ip, "handleEpTableEvent"); err != nil {
					log.G(context.TODO()).Errorf("failed adding service binding for %s epRec:%v err:%v", eid, epRec, err)
					return
				}
			}
		} else {
			// This is a remote container simply attached to an attachable network
			if err := c.addContainerNameResolution(nid, eid, containerName, taskAliases, ip, "handleEpTableEvent"); err != nil {
				log.G(context.TODO()).Errorf("failed adding container name resolution for %s epRec:%v err:%v", eid, epRec, err)
			}
		}

	case networkdb.DeleteEvent:
		log.G(context.TODO()).Debugf("handleEpTableEvent DEL %s R:%v", eid, epRec)
		if svcID != "" {
			// This is a remote task part of a service
			if err := c.rmServiceBinding(svcName, svcID, nid, eid, containerName, vip, ingressPorts, serviceAliases, taskAliases, ip, "handleEpTableEvent", true, true); err != nil {

Review comment (bug):
When things are weird, it might still help to know whether an event was a create or an update?

Could also log in one place, before the `switch`?

```go
log.G(context.TODO()).WithFields(log.Fields{
	"eid": eid,
	"T": fmt.Sprintf("%T", ev), // maybe there's a better way?
	"R": epRec,
}).Debug("handleEpTableEvent)
```

Code after:

	logger := log.G(context.TODO()).WithFields(log.Fields{
		"nid": nid,
		"eid": eid,
		"T":   fmt.Sprintf("%T", ev),
		"R":   epRec,
	})

	if containerName == "" || ip == nil {
		logger.Errorf("Invalid endpoint name/ip received while handling service table event %s", value)
		return
	}

	logger.Debug("handleEpTableEvent")

	switch ev.(type) {
	case networkdb.CreateEvent, networkdb.UpdateEvent:
		if svcID != "" {
			// This is a remote task part of a service
			if epRec.ServiceDisabled {
				if err := c.rmServiceBinding(svcName, svcID, nid, eid, containerName, vip, ingressPorts, serviceAliases, taskAliases, ip, "handleEpTableEvent", true, false); err != nil {
					logger.WithError(err).Error("failed disabling service binding")
					return
				}
			} else {
				if err := c.addServiceBinding(svcName, svcID, nid, eid, containerName, vip, ingressPorts, serviceAliases, taskAliases, ip, "handleEpTableEvent"); err != nil {
					logger.WithError(err).Error("failed adding service binding")
					return
				}
			}
		} else {
			// This is a remote container simply attached to an attachable network
			if err := c.addContainerNameResolution(nid, eid, containerName, taskAliases, ip, "handleEpTableEvent"); err != nil {
				logger.WithError(err).Errorf("failed adding container name resolution")
			}
		}

	case networkdb.DeleteEvent:
		if svcID != "" {
			// This is a remote task part of a service
			if err := c.rmServiceBinding(svcName, svcID, nid, eid, containerName, vip, ingressPorts, serviceAliases, taskAliases, ip, "handleEpTableEvent", true, true); err != nil {
				logger.WithError(err).Error("failed removing service binding")
				return
			}
		} else {
			// This is a remote container simply attached to an attachable network
			if err := c.delContainerNameResolution(nid, eid, containerName, taskAliases, ip, "handleEpTableEvent"); err != nil {
				logger.WithError(err).Errorf("failed removing container name resolution")
			}
		}
	}