Browse Source

Merge pull request #46530 from thaJeztah/libnetwork_walkless_step1

libnetwork: assorted cleanups in Sandbox
Sebastiaan van Stijn 1 year ago
parent
commit
e3975fba84
2 changed files with 45 additions and 48 deletions
  1. 18 27
      libnetwork/sandbox.go
  2. 27 21
      libnetwork/sandbox_store.go

+ 18 - 27
libnetwork/sandbox.go

@@ -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 {
@@ -492,46 +488,41 @@ func (sb *Sandbox) ResolveName(name string, ipType int) ([]net.IP, bool) {
 	return nil, false
 }
 
-func (sb *Sandbox) resolveName(req string, networkName string, epList []*Endpoint, alias bool, ipType int) ([]net.IP, bool) {
-	var ipv6Miss bool
-
+func (sb *Sandbox) resolveName(nameOrAlias string, networkName string, epList []*Endpoint, lookupAlias bool, ipType int) (_ []net.IP, ipv6Miss bool) {
 	for _, ep := range epList {
-		name := req
-		n := ep.getNetwork()
-
-		if networkName != "" && networkName != n.Name() {
+		if lookupAlias && len(ep.aliases) == 0 {
 			continue
 		}
 
-		if alias {
-			if ep.aliases == nil {
-				continue
-			}
+		nw := ep.getNetwork()
+		if networkName != "" && networkName != nw.Name() {
+			continue
+		}
 
-			var ok bool
+		name := nameOrAlias
+		if lookupAlias {
 			ep.mu.Lock()
-			name, ok = ep.aliases[req]
+			alias, ok := ep.aliases[nameOrAlias]
 			ep.mu.Unlock()
 			if !ok {
 				continue
 			}
+			name = alias
 		} else {
 			// If it is a regular lookup and if the requested name is an alias
 			// don't perform a svc lookup for this endpoint.
 			ep.mu.Lock()
-			if _, ok := ep.aliases[req]; ok {
-				ep.mu.Unlock()
+			_, ok := ep.aliases[nameOrAlias]
+			ep.mu.Unlock()
+			if ok {
 				continue
 			}
-			ep.mu.Unlock()
 		}
 
-		ip, miss := n.ResolveName(name, ipType)
-
+		ip, miss := nw.ResolveName(name, ipType)
 		if ip != nil {
 			return ip, false
 		}
-
 		if miss {
 			ipv6Miss = miss
 		}

+ 27 - 21
libnetwork/sandbox_store.go

@@ -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 {