Skip to content

Commit

Permalink
first draft that allows non-running containers to be retrieved using …
Browse files Browse the repository at this point in the history
…allowEmptyServices
  • Loading branch information
acouvreur committed Apr 21, 2024
1 parent 1ffbffb commit 91b0c22
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 4 additions & 1 deletion pkg/provider/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ func (p *DynConfBuilder) addServer(ctx context.Context, container dockerData, lo
}

if port == "" {
if p.AllowEmptyServices {
return nil
}
return errors.New("port is missing")
}

Expand Down Expand Up @@ -310,7 +313,7 @@ func (p *DynConfBuilder) getIPPort(ctx context.Context, container dockerData, se
port = getPort(container, serverPort)
}

if len(ip) == 0 {
if len(ip) == 0 && !p.AllowEmptyServices {
return "", "", fmt.Errorf("unable to find the IP address for the container %q: the server is ignored", container.Name)
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/provider/docker/pdocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,17 @@ func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.
}

func (p *Provider) listContainers(ctx context.Context, dockerClient client.ContainerAPIClient) ([]dockerData, error) {
containerList, err := dockerClient.ContainerList(ctx, dockertypes.ContainerListOptions{})
containerList, err := dockerClient.ContainerList(ctx, dockertypes.ContainerListOptions{
All: p.AllowEmptyServices,
})
if err != nil {
return nil, err
}

var inspectedContainers []dockerData
// get inspect containers
for _, container := range containerList {
dData := inspectContainers(ctx, dockerClient, container.ID)
dData := inspectContainers(ctx, dockerClient, container.ID, p.AllowEmptyServices)
if len(dData.Name) == 0 {
continue
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/provider/docker/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ type Shared struct {
defaultRuleTpl *template.Template
}

func inspectContainers(ctx context.Context, dockerClient client.ContainerAPIClient, containerID string) dockerData {
func inspectContainers(ctx context.Context, dockerClient client.ContainerAPIClient, containerID string, allowEmptyServices bool) dockerData {
containerInspected, err := dockerClient.ContainerInspect(ctx, containerID)
if err != nil {
log.Ctx(ctx).Warn().Err(err).Msgf("Failed to inspect container %s", containerID)
return dockerData{}
}

// This condition is here to avoid to have empty IP https://github.com/traefik/traefik/issues/2459
// We register only container which are running
if containerInspected.ContainerJSONBase != nil && containerInspected.ContainerJSONBase.State != nil && containerInspected.ContainerJSONBase.State.Running {
// We register only container which are running unless we explicitly want all containers
if allowEmptyServices || containerInspected.ContainerJSONBase != nil && containerInspected.ContainerJSONBase.State != nil && containerInspected.ContainerJSONBase.State.Running {
return parseContainer(containerInspected)
}

Expand Down Expand Up @@ -201,6 +201,10 @@ func getPort(container dockerData, serverPort string) string {
}

func getServiceName(container dockerData) string {
if condition {

}

serviceName := container.ServiceName

if values, err := getStringMultipleStrict(container.Labels, labelDockerComposeProject, labelDockerComposeService); err == nil {
Expand Down

0 comments on commit 91b0c22

Please sign in to comment.