github.com/xiaobinqt/libcompose@v1.1.0/docker/service/utils.go (about)

     1  package service
     2  
     3  import (
     4  	"github.com/docker/docker/api/types/container"
     5  	"github.com/xiaobinqt/libcompose/project"
     6  )
     7  
     8  // DefaultDependentServices return the dependent services (as an array of ServiceRelationship)
     9  // for the specified project and service. It looks for : links, volumesFrom, net and ipc configuration.
    10  // It uses default project implementation and append some docker specific ones.
    11  func DefaultDependentServices(p *project.Project, s project.Service) []project.ServiceRelationship {
    12  	result := project.DefaultDependentServices(p, s)
    13  
    14  	result = appendNs(p, result, s.Config().NetworkMode, project.RelTypeNetNamespace)
    15  	result = appendNs(p, result, s.Config().Ipc, project.RelTypeIpcNamespace)
    16  
    17  	return result
    18  }
    19  
    20  func appendNs(p *project.Project, rels []project.ServiceRelationship, conf string, relType project.ServiceRelationshipType) []project.ServiceRelationship {
    21  	service := GetContainerFromIpcLikeConfig(p, conf)
    22  	if service != "" {
    23  		rels = append(rels, project.NewServiceRelationship(service, relType))
    24  	}
    25  	return rels
    26  }
    27  
    28  // GetContainerFromIpcLikeConfig returns name of the service that shares the IPC
    29  // namespace with the specified service.
    30  func GetContainerFromIpcLikeConfig(p *project.Project, conf string) string {
    31  	ipc := container.IpcMode(conf)
    32  	if !ipc.IsContainer() {
    33  		return ""
    34  	}
    35  
    36  	name := ipc.Container()
    37  	if name == "" {
    38  		return ""
    39  	}
    40  
    41  	if p.ServiceConfigs.Has(name) {
    42  		return name
    43  	}
    44  	return ""
    45  }