libnetwork/driverapi: make EventNotify optional

Overlay is the only driver which makes use of the EventNotify facility,
yet all other driver implementations are forced to provide a stub
implementation. Move the EventNotify and DecodeTableEntry methods into a
new optional TableWatcher interface and remove the stubs from all the
other drivers.

Signed-off-by: Cory Snider <csnider@mirantis.com>
(cherry picked from commit 844023f794)
Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Cory Snider
2025-07-10 17:51:18 -04:00
parent c7e17ae65d
commit b22872af60
17 changed files with 34 additions and 111 deletions

View File

@@ -490,17 +490,19 @@ func (n *Network) Services() map[string]ServiceInfo {
// Walk through the driver's tables, have the driver decode the entries
// and return the tuple {ep ID, value}. value is a string that coveys
// relevant info about the endpoint.
for _, table := range n.driverTables {
if table.objType != driverapi.EndpointObject {
continue
}
for key, value := range agent.networkDB.GetTableByNetwork(table.name, nwID) {
epID, info := d.DecodeTableEntry(table.name, key, value.Value)
if ep, ok := eps[epID]; !ok {
log.G(context.TODO()).Errorf("Inconsistent driver and libnetwork state for endpoint %s", epID)
} else {
ep.info = info
eps[epID] = ep
if d, ok := d.(driverapi.TableWatcher); ok {
for _, table := range n.driverTables {
if table.objType != driverapi.EndpointObject {
continue
}
for key, value := range agent.networkDB.GetTableByNetwork(table.name, nwID) {
epID, info := d.DecodeTableEntry(table.name, key, value.Value)
if ep, ok := eps[epID]; !ok {
log.G(context.TODO()).Errorf("Inconsistent driver and libnetwork state for endpoint %s", epID)
} else {
ep.info = info
eps[epID] = ep
}
}
}
}
@@ -813,6 +815,11 @@ func (n *Network) handleDriverTableEvent(ev events.Event) {
log.G(context.TODO()).Errorf("Could not resolve driver %s while handling driver table event: %v", n.networkType, err)
return
}
ed, ok := d.(driverapi.TableWatcher)
if !ok {
log.G(context.TODO()).Errorf("Could not notify driver %s about table event: driver does not implement TableWatcher interface", n.networkType)
return
}
var (
etype driverapi.EventType
@@ -832,7 +839,7 @@ func (n *Network) handleDriverTableEvent(ev events.Event) {
etype = driverapi.Update
}
d.EventNotify(etype, n.ID(), event.Table, event.Key, value)
ed.EventNotify(etype, n.ID(), event.Table, event.Key, value)
}
func (c *Controller) handleNodeTableEvent(ev events.Event) {

View File

@@ -30,13 +30,6 @@ func (d *manager) CreateNetwork(id string, option map[string]interface{}, nInfo
return types.NotImplementedErrorf("not implemented")
}
func (d *manager) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *manager) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *manager) DeleteNetwork(nid string) error {
return types.NotImplementedErrorf("not implemented")
}

View File

@@ -59,6 +59,15 @@ type Driver interface {
// programming that was done so far
RevokeExternalConnectivity(nid, eid string) error
// Type returns the type of this driver, the network type this driver manages
Type() string
// IsBuiltIn returns true if it is a built-in driver
IsBuiltIn() bool
}
// TableWatcher is an optional interface for a network driver.
type TableWatcher interface {
// EventNotify notifies the driver when a CRUD operation has
// happened on a table of its interest as soon as this node
// receives such an event in the gossip layer. This method is
@@ -74,12 +83,6 @@ type Driver interface {
// For example: overlay driver returns the VTEP IP of the host that has the endpoint
// which is shown in 'network inspect --verbose'
DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string)
// Type returns the type of this driver, the network type this driver manages
Type() string
// IsBuiltIn returns true if it is a built-in driver
IsBuiltIn() bool
}
// NetworkInfo provides a go interface for drivers to provide network

View File

@@ -625,13 +625,6 @@ func (d *driver) NetworkFree(id string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
// Create a new network using bridge plugin
func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
if len(ipV4Data) == 0 || ipV4Data[0].Pool.String() == "0.0.0.0/0" {

View File

@@ -30,13 +30,6 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) DeleteNetwork(nid string) error {
return types.NotImplementedErrorf("not implemented")
}

View File

@@ -30,13 +30,6 @@ func (d *driver) NetworkFree(id string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
d.Lock()
defer d.Unlock()

View File

@@ -102,10 +102,3 @@ func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string
func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
return nil
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}

View File

@@ -30,13 +30,6 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) DeleteNetwork(nid string) error {
return types.NotImplementedErrorf("not implemented")
}

View File

@@ -96,10 +96,3 @@ func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string
func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
return nil
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}

View File

@@ -30,13 +30,6 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) DeleteNetwork(nid string) error {
return types.NotImplementedErrorf("not implemented")
}

View File

@@ -30,13 +30,6 @@ func (d *driver) NetworkFree(id string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
d.Lock()
defer d.Unlock()

View File

@@ -24,8 +24,10 @@ const (
secureOption = "encrypted"
)
// overlay driver must implement the discover-API.
var _ discoverapi.Discover = (*driver)(nil)
var (
_ discoverapi.Discover = (*driver)(nil)
_ driverapi.TableWatcher = (*driver)(nil)
)
type driver struct {
// Immutable; mu does not need to be held when accessing these fields.

View File

@@ -166,13 +166,6 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) DeleteNetwork(nid string) error {
return types.NotImplementedErrorf("not implemented")
}

View File

@@ -151,13 +151,6 @@ func (d *driver) NetworkFree(id string) error {
return d.call("FreeNetwork", fr, &api.FreeNetworkResponse{})
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) CreateNetwork(id string, options map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
create := &api.CreateNetworkRequest{
NetworkID: id,

View File

@@ -18,6 +18,8 @@ const (
NetworkType = "overlay"
)
var _ driverapi.TableWatcher = (*driver)(nil)
type driver struct {
networks networkTable
sync.Mutex

View File

@@ -248,13 +248,6 @@ func (ncfg *networkConfiguration) processIPAM(id string, ipamV4Data, ipamV6Data
return nil
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) createNetwork(config *networkConfiguration) *hnsNetwork {
network := &hnsNetwork{
id: config.ID,

View File

@@ -700,10 +700,3 @@ func (b *badDriver) NetworkAllocate(id string, option map[string]string, ipV4Dat
func (b *badDriver) NetworkFree(id string) error {
return types.NotImplementedErrorf("not implemented")
}
func (b *badDriver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (b *badDriver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}