libnetwork: Sandbox: remove some intermediate vars

- remove some intermediate vars, or move them closer to where they're used.
- ResolveService: use strings.SplitN to limit number of elements. This
  code is only used to validate the input, results are not used.
- ResolveService: return early instead of breaking the loop. This makes
  it clearer from the code that were not returning anything (nil, nil).
- Controller.sandboxCleanup(): rename a var, and slight refactor of
  error-handling.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-08-16 20:17:35 +02:00
parent d3afa80b96
commit 4401ccac22
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 31 additions and 29 deletions

View file

@ -369,28 +369,24 @@ func (sb *Sandbox) ResolveIP(ip string) string {
// ResolveService returns all the backend details about the containers or hosts
// backing a service. Its purpose is to satisfy an SRV query.
func (sb *Sandbox) ResolveService(name string) ([]*net.SRV, []net.IP) {
srv := []*net.SRV{}
ip := []net.IP{}
log.G(context.TODO()).Debugf("Service name To resolve: %v", name)
// There are DNS implementations that allow SRV queries for names not in
// the format defined by RFC 2782. Hence specific validations checks are
// not done
parts := strings.Split(name, ".")
if len(parts) < 3 {
if parts := strings.SplitN(name, ".", 3); len(parts) < 3 {
return nil, nil
}
for _, ep := range sb.Endpoints() {
n := ep.getNetwork()
srv, ip = n.ResolveService(name)
srv, ip := n.ResolveService(name)
if len(srv) > 0 {
break
return srv, ip
}
}
return srv, ip
return nil, nil
}
func getDynamicNwEndpoints(epList []*Endpoint) []*Endpoint {

View file

@ -143,12 +143,10 @@ retry:
continue
}
eps := epState{
sbs.Eps = append(sbs.Eps, epState{
Nid: ep.getNetwork().ID(),
Eid: ep.ID(),
}
sbs.Eps = append(sbs.Eps, eps)
})
}
err := sb.controller.updateToStore(sbs)
@ -164,15 +162,13 @@ retry:
}
func (sb *Sandbox) storeDelete() error {
sbs := &sbState{
return sb.controller.deleteFromStore(&sbState{
c: sb.controller,
ID: sb.id,
Cid: sb.containerID,
dbIndex: sb.dbIndex,
dbExists: sb.dbExists,
}
return sb.controller.deleteFromStore(sbs)
})
}
func (c *Controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
@ -182,20 +178,18 @@ func (c *Controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
return
}
kvol, err := store.List(datastore.Key(sandboxPrefix), &sbState{c: c})
if err != nil && err != datastore.ErrKeyNotFound {
sandboxStates, err := store.List(datastore.Key(sandboxPrefix), &sbState{c: c})
if err != nil {
if err == datastore.ErrKeyNotFound {
// It's normal for no sandboxes to be found. Just bail out.
return
}
log.G(context.TODO()).Errorf("failed to get sandboxes for scope %s: %v", store.Scope(), err)
return
}
// It's normal for no sandboxes to be found. Just bail out.
if err == datastore.ErrKeyNotFound {
return
}
for _, kvo := range kvol {
sbs := kvo.(*sbState)
for _, s := range sandboxStates {
sbs := s.(*sbState)
sb := &Sandbox{
id: sbs.ID,
controller: sbs.c,
@ -235,13 +229,25 @@ func (c *Controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
var ep *Endpoint
if err != nil {
log.G(context.TODO()).Errorf("getNetworkFromStore for nid %s failed while trying to build sandbox for cleanup: %v", eps.Nid, err)
n = &Network{id: eps.Nid, ctrlr: c, drvOnce: &sync.Once{}, persist: true}
ep = &Endpoint{id: eps.Eid, network: n, sandboxID: sbs.ID}
ep = &Endpoint{
id: eps.Eid,
network: &Network{
id: eps.Nid,
ctrlr: c,
drvOnce: &sync.Once{},
persist: true,
},
sandboxID: sbs.ID,
}
} else {
ep, err = n.getEndpointFromStore(eps.Eid)
if err != nil {
log.G(context.TODO()).Errorf("getEndpointFromStore for eid %s failed while trying to build sandbox for cleanup: %v", eps.Eid, err)
ep = &Endpoint{id: eps.Eid, network: n, sandboxID: sbs.ID}
ep = &Endpoint{
id: eps.Eid,
network: n,
sandboxID: sbs.ID,
}
}
}
if _, ok := activeSandboxes[sb.ID()]; ok && err != nil {