Merge pull request #46553 from thaJeztah/no_panic
libnetwork: Controller: getKeys, getPrimaryKeyTag: prevent panic and small refactor
This commit is contained in:
commit
adea457841
1 changed files with 17 additions and 11 deletions
|
@ -266,38 +266,44 @@ func (c *Controller) agentSetup(clusterProvider cluster.Provider) error {
|
||||||
|
|
||||||
// For a given subsystem getKeys sorts the keys by lamport time and returns
|
// For a given subsystem getKeys sorts the keys by lamport time and returns
|
||||||
// slice of keys and lamport time which can used as a unique tag for the keys
|
// slice of keys and lamport time which can used as a unique tag for the keys
|
||||||
func (c *Controller) getKeys(subsys string) ([][]byte, []uint64) {
|
func (c *Controller) getKeys(subsystem string) (keys [][]byte, tags []uint64) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
sort.Sort(ByTime(c.keys))
|
sort.Sort(ByTime(c.keys))
|
||||||
|
|
||||||
keys := [][]byte{}
|
keys = make([][]byte, 0, len(c.keys))
|
||||||
tags := []uint64{}
|
tags = make([]uint64, 0, len(c.keys))
|
||||||
for _, key := range c.keys {
|
for _, key := range c.keys {
|
||||||
if key.Subsystem == subsys {
|
if key.Subsystem == subsystem {
|
||||||
keys = append(keys, key.Key)
|
keys = append(keys, key.Key)
|
||||||
tags = append(tags, key.LamportTime)
|
tags = append(tags, key.LamportTime)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
keys[0], keys[1] = keys[1], keys[0]
|
if len(keys) > 1 {
|
||||||
tags[0], tags[1] = tags[1], tags[0]
|
// TODO(thaJeztah): why are we swapping order here? This code was added in https://github.com/moby/libnetwork/commit/e83d68b7d1fd9c479120914024242238f791b4dc
|
||||||
|
keys[0], keys[1] = keys[1], keys[0]
|
||||||
|
tags[0], tags[1] = tags[1], tags[0]
|
||||||
|
}
|
||||||
return keys, tags
|
return keys, tags
|
||||||
}
|
}
|
||||||
|
|
||||||
// getPrimaryKeyTag returns the primary key for a given subsystem from the
|
// getPrimaryKeyTag returns the primary key for a given subsystem from the
|
||||||
// list of sorted key and the associated tag
|
// list of sorted key and the associated tag
|
||||||
func (c *Controller) getPrimaryKeyTag(subsys string) ([]byte, uint64, error) {
|
func (c *Controller) getPrimaryKeyTag(subsystem string) (key []byte, lamportTime uint64, _ error) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
sort.Sort(ByTime(c.keys))
|
sort.Sort(ByTime(c.keys))
|
||||||
keys := []*types.EncryptionKey{}
|
keys := make([]*types.EncryptionKey, 0, len(c.keys))
|
||||||
for _, key := range c.keys {
|
for _, k := range c.keys {
|
||||||
if key.Subsystem == subsys {
|
if k.Subsystem == subsystem {
|
||||||
keys = append(keys, key)
|
keys = append(keys, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(keys) < 2 {
|
||||||
|
return nil, 0, fmt.Errorf("no primary key found for %s subsystem: %d keys found on controller, expected at least 2", subsystem, len(keys))
|
||||||
|
}
|
||||||
return keys[1].Key, keys[1].LamportTime, nil
|
return keys[1].Key, keys[1].LamportTime, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue