github.com/rawahars/moby@v24.0.4+incompatible/libnetwork/drivers/overlay/overlay.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package overlay
     5  
     6  //go:generate protoc -I.:../../Godeps/_workspace/src/github.com/gogo/protobuf  --gogo_out=import_path=github.com/docker/docker/libnetwork/drivers/overlay,Mgogoproto/gogo.proto=github.com/gogo/protobuf/gogoproto:. overlay.proto
     7  
     8  import (
     9  	"fmt"
    10  	"sync"
    11  
    12  	"github.com/docker/docker/libnetwork/datastore"
    13  	"github.com/docker/docker/libnetwork/discoverapi"
    14  	"github.com/docker/docker/libnetwork/driverapi"
    15  	"github.com/sirupsen/logrus"
    16  )
    17  
    18  const (
    19  	networkType  = "overlay"
    20  	vethPrefix   = "veth"
    21  	vethLen      = len(vethPrefix) + 7
    22  	vxlanEncap   = 50
    23  	secureOption = "encrypted"
    24  )
    25  
    26  type driver struct {
    27  	bindAddress      string
    28  	advertiseAddress string
    29  	config           map[string]interface{}
    30  	peerDb           peerNetworkMap
    31  	secMap           *encrMap
    32  	networks         networkTable
    33  	initOS           sync.Once
    34  	localJoinOnce    sync.Once
    35  	keys             []*key
    36  	peerOpMu         sync.Mutex
    37  	sync.Mutex
    38  }
    39  
    40  // Register registers a new instance of the overlay driver.
    41  func Register(r driverapi.Registerer, config map[string]interface{}) error {
    42  	c := driverapi.Capability{
    43  		DataScope:         datastore.GlobalScope,
    44  		ConnectivityScope: datastore.GlobalScope,
    45  	}
    46  	d := &driver{
    47  		networks: networkTable{},
    48  		peerDb: peerNetworkMap{
    49  			mp: map[string]*peerMap{},
    50  		},
    51  		secMap: &encrMap{nodes: map[string][]*spi{}},
    52  		config: config,
    53  	}
    54  
    55  	return r.RegisterDriver(networkType, d, c)
    56  }
    57  
    58  func (d *driver) configure() error {
    59  	// Apply OS specific kernel configs if needed
    60  	d.initOS.Do(applyOStweaks)
    61  
    62  	return nil
    63  }
    64  
    65  func (d *driver) Type() string {
    66  	return networkType
    67  }
    68  
    69  func (d *driver) IsBuiltIn() bool {
    70  	return true
    71  }
    72  
    73  func (d *driver) nodeJoin(advertiseAddress, bindAddress string, self bool) {
    74  	if self {
    75  		d.Lock()
    76  		d.advertiseAddress = advertiseAddress
    77  		d.bindAddress = bindAddress
    78  		d.Unlock()
    79  
    80  		// If containers are already running on this network update the
    81  		// advertise address in the peerDB
    82  		d.localJoinOnce.Do(func() {
    83  			d.peerDBUpdateSelf()
    84  		})
    85  	}
    86  }
    87  
    88  // DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
    89  func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
    90  	switch dType {
    91  	case discoverapi.NodeDiscovery:
    92  		nodeData, ok := data.(discoverapi.NodeDiscoveryData)
    93  		if !ok || nodeData.Address == "" {
    94  			return fmt.Errorf("invalid discovery data")
    95  		}
    96  		d.nodeJoin(nodeData.Address, nodeData.BindAddress, nodeData.Self)
    97  	case discoverapi.EncryptionKeysConfig:
    98  		encrData, ok := data.(discoverapi.DriverEncryptionConfig)
    99  		if !ok {
   100  			return fmt.Errorf("invalid encryption key notification data")
   101  		}
   102  		keys := make([]*key, 0, len(encrData.Keys))
   103  		for i := 0; i < len(encrData.Keys); i++ {
   104  			k := &key{
   105  				value: encrData.Keys[i],
   106  				tag:   uint32(encrData.Tags[i]),
   107  			}
   108  			keys = append(keys, k)
   109  		}
   110  		if err := d.setKeys(keys); err != nil {
   111  			logrus.Warn(err)
   112  		}
   113  	case discoverapi.EncryptionKeysUpdate:
   114  		var newKey, delKey, priKey *key
   115  		encrData, ok := data.(discoverapi.DriverEncryptionUpdate)
   116  		if !ok {
   117  			return fmt.Errorf("invalid encryption key notification data")
   118  		}
   119  		if encrData.Key != nil {
   120  			newKey = &key{
   121  				value: encrData.Key,
   122  				tag:   uint32(encrData.Tag),
   123  			}
   124  		}
   125  		if encrData.Primary != nil {
   126  			priKey = &key{
   127  				value: encrData.Primary,
   128  				tag:   uint32(encrData.PrimaryTag),
   129  			}
   130  		}
   131  		if encrData.Prune != nil {
   132  			delKey = &key{
   133  				value: encrData.Prune,
   134  				tag:   uint32(encrData.PruneTag),
   135  			}
   136  		}
   137  		if err := d.updateKeys(newKey, priKey, delKey); err != nil {
   138  			return err
   139  		}
   140  	default:
   141  	}
   142  	return nil
   143  }
   144  
   145  // DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
   146  func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
   147  	return nil
   148  }