define const for labels

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-11-17 15:56:39 +01:00
parent 2278370ffa
commit eb60bbb74f
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
3 changed files with 50 additions and 14 deletions

View File

@ -165,7 +165,7 @@ func toProgressEvent(jm jsonmessage.JSONMessage, w progress.Writer) {
func (s *local) Down(ctx context.Context, projectName string) error {
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs(
filters.Arg("label", "com.docker.compose.project="+projectName),
projectFilter(projectName),
),
})
if err != nil {
@ -212,7 +212,7 @@ func (s *local) Down(ctx context.Context, projectName string) error {
func (s *local) Logs(ctx context.Context, projectName string, w io.Writer) error {
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs(
filters.Arg("label", "com.docker.compose.project="+projectName),
projectFilter(projectName),
),
})
if err != nil {
@ -221,7 +221,7 @@ func (s *local) Logs(ctx context.Context, projectName string, w io.Writer) error
var wg sync.WaitGroup
consumer := formatter.NewLogConsumer(w)
for _, c := range list {
service := c.Labels["com.docker.compose.service"]
service := c.Labels[serviceLabel]
containerId := c.ID
go func() {
s.containerService.Logs(ctx,containerId, containers.LogsRequest{
@ -239,7 +239,7 @@ func (s *local) Logs(ctx context.Context, projectName string, w io.Writer) error
func (s *local) Ps(ctx context.Context, projectName string) ([]compose.ServiceStatus, error) {
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs(
filters.Arg("label", "com.docker.compose.project="+projectName),
projectFilter(projectName),
),
})
if err != nil {
@ -250,7 +250,7 @@ func (s *local) Ps(ctx context.Context, projectName string) ([]compose.ServiceSt
// TODO group by service
status = append(status, compose.ServiceStatus{
ID: c.ID,
Name: c.Labels["com.docker.compose.service"],
Name: c.Labels[serviceLabel],
Replicas: 0,
Desired: 0,
Ports: nil,
@ -260,6 +260,7 @@ func (s *local) Ps(ctx context.Context, projectName string) ([]compose.ServiceSt
return status, nil
}
func (s *local) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
_, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{All: true})
if err != nil {
@ -287,10 +288,10 @@ func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number i
return nil, nil, nil, err
}
labels := map[string]string{
"com.docker.compose.project": p.Name,
"com.docker.compose.service": s.Name,
"com.docker.compose.config-hash": hash,
"com.docker.compose.container-number": strconv.Itoa(number),
projectLabel: p.Name,
serviceLabel: s.Name,
configHashLabel: hash,
containerNumberLabel: strconv.Itoa(number),
}
var (

View File

@ -34,8 +34,8 @@ import (
func (s *local) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
actual, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs(
filters.Arg("label", "com.docker.compose.project="+project.Name),
filters.Arg("label", "com.docker.compose.service="+service.Name),
filters.Arg("label", fmt.Sprintf("%s=%s", projectLabel, project.Name)),
filters.Arg("label", fmt.Sprintf("%s=%s", serviceLabel, service.Name)),
),
})
if err != nil {
@ -80,7 +80,7 @@ func (s *local) ensureService(ctx context.Context, project *types.Project, servi
}
for _, container := range actual {
container := container
diverged := container.Labels["com.docker.compose.config-hash"] != expected
diverged := container.Labels[configHashLabel] != expected
if diverged {
eg.Go(func() error {
return s.recreateContainer(ctx, project, service, container)
@ -103,7 +103,7 @@ func (s *local) ensureService(ctx context.Context, project *types.Project, servi
func nextContainerNumber(containers []moby.Container) (int, error) {
max := 0
for _, c := range containers {
n, err := strconv.Atoi(c.Labels["com.docker.compose.container-number"])
n, err := strconv.Atoi(c.Labels[containerNumberLabel])
if err != nil {
return 0, err
}
@ -164,7 +164,7 @@ func (s *local) recreateContainer(ctx context.Context, project *types.Project, s
if err != nil {
return err
}
number, err := strconv.Atoi(container.Labels["com.docker.compose.container-number"])
number, err := strconv.Atoi(container.Labels[containerNumberLabel])
if err != nil {
return err
}

35
local/labels.go Normal file
View File

@ -0,0 +1,35 @@
// +build local
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package local
import (
"fmt"
"github.com/docker/docker/api/types/filters"
)
const (
projectLabel = "com.docker.compose.project"
serviceLabel = "com.docker.compose.service"
configHashLabel = "com.docker.compose.config-hash"
containerNumberLabel = "com.docker.compose.container-number"
)
func projectFilter(projectName string) filters.KeyValuePair {
return filters.Arg("label", fmt.Sprintf("%s=%s", projectLabel, projectName))
}