Skip to content

Commit

Permalink
add network support
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
  • Loading branch information
glours committed Oct 15, 2024
1 parent fab7feb commit 9e97da5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
if err != nil {
continue
}
s.extractComposeConfiguration(service, inspect, volumes, secrets)
s.extractComposeConfiguration(&service, inspect, volumes, secrets, networks)
}
set[serviceLabel] = service
}
Expand Down Expand Up @@ -253,7 +253,7 @@ SERVICES:
return project, nil
}

func (s *composeService) extractComposeConfiguration(service types.ServiceConfig, inspect moby.ContainerJSON, volumes types.Volumes, secrets types.Secrets) {
func (s *composeService) extractComposeConfiguration(service *types.ServiceConfig, inspect moby.ContainerJSON, volumes types.Volumes, secrets types.Secrets, networks types.Networks) {
service.Environment = types.NewMappingWithEquals(inspect.Config.Env)
if inspect.Config.Healthcheck != nil {
healthConfig := inspect.Config.Healthcheck
Expand All @@ -266,6 +266,11 @@ func (s *composeService) extractComposeConfiguration(service types.ServiceConfig
maps.Copy(volumes, detectedVolumes)
maps.Copy(secrets, detectedSecrets)
}
if len(inspect.NetworkSettings.Networks) > 0 {
detectedNetworks, networkConfigs := s.toComposeNetwork(inspect.NetworkSettings.Networks)
service.Networks = networkConfigs
maps.Copy(networks, detectedNetworks)
}
}

func increment(scale *int) *int {
Expand Down
25 changes: 25 additions & 0 deletions pkg/compose/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/versions"
)

Expand Down Expand Up @@ -170,3 +171,27 @@ func (s *composeService) toComposeVolumes(volumes []types.MountPoint) (map[strin
}
return volumeConfigs, serviceVolumeConfigs, secretConfigs, serviceSecretConfigs
}

func (s *composeService) toComposeNetwork(networks map[string]*network.EndpointSettings) (map[string]compose.NetworkConfig, map[string]*compose.ServiceNetworkConfig) {
networkConfigs := make(map[string]compose.NetworkConfig)
serviceNetworkConfigs := make(map[string]*compose.ServiceNetworkConfig)

for name, net := range networks {
if strings.HasSuffix(name, "_default") {
continue
}
inspect, err := s.apiClient().NetworkInspect(context.Background(), name, network.InspectOptions{})
if err != nil {
networkConfigs[name] = compose.NetworkConfig{}
} else {
networkConfigs[name] = compose.NetworkConfig{
Internal: inspect.Internal,
}

}
serviceNetworkConfigs[name] = &compose.ServiceNetworkConfig{
Aliases: net.Aliases,
}
}
return networkConfigs, serviceNetworkConfigs
}

0 comments on commit 9e97da5

Please sign in to comment.