github.com/skippbox/kompose-origin@v0.0.0-20160524133224-16a9dca7bac2/project/utils.go (about)

     1  package project
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/engine-api/types/container"
     7  )
     8  
     9  // DefaultDependentServices return the dependent services (as an array of ServiceRelationship)
    10  // for the specified project and service. It looks for : links, volumesFrom, net and ipc configuration.
    11  func DefaultDependentServices(p *Project, s Service) []ServiceRelationship {
    12  	config := s.Config()
    13  	if config == nil {
    14  		return []ServiceRelationship{}
    15  	}
    16  
    17  	result := []ServiceRelationship{}
    18  	for _, link := range config.Links {
    19  		result = append(result, NewServiceRelationship(link, RelTypeLink))
    20  	}
    21  
    22  	for _, volumesFrom := range config.VolumesFrom {
    23  		result = append(result, NewServiceRelationship(volumesFrom, RelTypeVolumesFrom))
    24  	}
    25  
    26  	for _, dependsOn := range config.DependsOn {
    27  		result = append(result, NewServiceRelationship(dependsOn, RelTypeDependsOn))
    28  	}
    29  
    30  	result = appendNs(p, result, s.Config().NetworkMode, RelTypeNetNamespace)
    31  	result = appendNs(p, result, s.Config().Ipc, RelTypeIpcNamespace)
    32  
    33  	return result
    34  }
    35  
    36  func appendNs(p *Project, rels []ServiceRelationship, conf string, relType ServiceRelationshipType) []ServiceRelationship {
    37  	service := GetContainerFromIpcLikeConfig(p, conf)
    38  	if service != "" {
    39  		rels = append(rels, NewServiceRelationship(service, relType))
    40  	}
    41  	return rels
    42  }
    43  
    44  // NameAlias returns the name and alias based on the specified string.
    45  // If the name contains a colon (like name:alias) it will split it, otherwise
    46  // it will return the specified name as name and alias.
    47  func NameAlias(name string) (string, string) {
    48  	parts := strings.SplitN(name, ":", 2)
    49  	if len(parts) == 2 {
    50  		return parts[0], parts[1]
    51  	}
    52  	return parts[0], parts[0]
    53  }
    54  
    55  // GetContainerFromIpcLikeConfig returns name of the service that shares the IPC
    56  // namespace with the specified service.
    57  func GetContainerFromIpcLikeConfig(p *Project, conf string) string {
    58  	ipc := container.IpcMode(conf)
    59  	if !ipc.IsContainer() {
    60  		return ""
    61  	}
    62  
    63  	name := ipc.Container()
    64  	if name == "" {
    65  		return ""
    66  	}
    67  
    68  	if p.ServiceConfigs.Has(name) {
    69  		return name
    70  	}
    71  	return ""
    72  }