github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/manager/state/store/networks.go (about) 1 package store 2 3 import ( 4 "strings" 5 6 "github.com/docker/swarmkit/api" 7 memdb "github.com/hashicorp/go-memdb" 8 ) 9 10 const tableNetwork = "network" 11 12 func init() { 13 register(ObjectStoreConfig{ 14 Table: &memdb.TableSchema{ 15 Name: tableNetwork, 16 Indexes: map[string]*memdb.IndexSchema{ 17 indexID: { 18 Name: indexID, 19 Unique: true, 20 Indexer: api.NetworkIndexerByID{}, 21 }, 22 indexName: { 23 Name: indexName, 24 Unique: true, 25 Indexer: api.NetworkIndexerByName{}, 26 }, 27 indexCustom: { 28 Name: indexCustom, 29 Indexer: api.NetworkCustomIndexer{}, 30 AllowMissing: true, 31 }, 32 }, 33 }, 34 Save: func(tx ReadTx, snapshot *api.StoreSnapshot) error { 35 var err error 36 snapshot.Networks, err = FindNetworks(tx, All) 37 return err 38 }, 39 Restore: func(tx Tx, snapshot *api.StoreSnapshot) error { 40 toStoreObj := make([]api.StoreObject, len(snapshot.Networks)) 41 for i, x := range snapshot.Networks { 42 toStoreObj[i] = x 43 } 44 return RestoreTable(tx, tableNetwork, toStoreObj) 45 }, 46 ApplyStoreAction: func(tx Tx, sa api.StoreAction) error { 47 switch v := sa.Target.(type) { 48 case *api.StoreAction_Network: 49 obj := v.Network 50 switch sa.Action { 51 case api.StoreActionKindCreate: 52 return CreateNetwork(tx, obj) 53 case api.StoreActionKindUpdate: 54 return UpdateNetwork(tx, obj) 55 case api.StoreActionKindRemove: 56 return DeleteNetwork(tx, obj.ID) 57 } 58 } 59 return errUnknownStoreAction 60 }, 61 }) 62 } 63 64 // CreateNetwork adds a new network to the store. 65 // Returns ErrExist if the ID is already taken. 66 func CreateNetwork(tx Tx, n *api.Network) error { 67 // Ensure the name is not already in use. 68 if tx.lookup(tableNetwork, indexName, strings.ToLower(n.Spec.Annotations.Name)) != nil { 69 return ErrNameConflict 70 } 71 72 return tx.create(tableNetwork, n) 73 } 74 75 // UpdateNetwork updates an existing network in the store. 76 // Returns ErrNotExist if the network doesn't exist. 77 func UpdateNetwork(tx Tx, n *api.Network) error { 78 // Ensure the name is either not in use or already used by this same Network. 79 if existing := tx.lookup(tableNetwork, indexName, strings.ToLower(n.Spec.Annotations.Name)); existing != nil { 80 if existing.GetID() != n.ID { 81 return ErrNameConflict 82 } 83 } 84 85 return tx.update(tableNetwork, n) 86 } 87 88 // DeleteNetwork removes a network from the store. 89 // Returns ErrNotExist if the network doesn't exist. 90 func DeleteNetwork(tx Tx, id string) error { 91 return tx.delete(tableNetwork, id) 92 } 93 94 // GetNetwork looks up a network by ID. 95 // Returns nil if the network doesn't exist. 96 func GetNetwork(tx ReadTx, id string) *api.Network { 97 n := tx.get(tableNetwork, id) 98 if n == nil { 99 return nil 100 } 101 return n.(*api.Network) 102 } 103 104 // FindNetworks selects a set of networks and returns them. 105 func FindNetworks(tx ReadTx, by By) ([]*api.Network, error) { 106 checkType := func(by By) error { 107 switch by.(type) { 108 case byName, byNamePrefix, byIDPrefix, byCustom, byCustomPrefix, byAll: 109 return nil 110 default: 111 return ErrInvalidFindBy 112 } 113 } 114 115 networkList := []*api.Network{} 116 appendResult := func(o api.StoreObject) { 117 networkList = append(networkList, o.(*api.Network)) 118 } 119 120 err := tx.find(tableNetwork, by, checkType, appendResult) 121 return networkList, err 122 }