mirror of
https://github.com/docker/compose.git
synced 2025-07-07 05:44:25 +02:00
define const for labels
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
2278370ffa
commit
eb60bbb74f
@ -165,7 +165,7 @@ func toProgressEvent(jm jsonmessage.JSONMessage, w progress.Writer) {
|
|||||||
func (s *local) Down(ctx context.Context, projectName string) error {
|
func (s *local) Down(ctx context.Context, projectName string) error {
|
||||||
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
||||||
Filters: filters.NewArgs(
|
Filters: filters.NewArgs(
|
||||||
filters.Arg("label", "com.docker.compose.project="+projectName),
|
projectFilter(projectName),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
if err != nil {
|
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 {
|
func (s *local) Logs(ctx context.Context, projectName string, w io.Writer) error {
|
||||||
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
||||||
Filters: filters.NewArgs(
|
Filters: filters.NewArgs(
|
||||||
filters.Arg("label", "com.docker.compose.project="+projectName),
|
projectFilter(projectName),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -221,7 +221,7 @@ func (s *local) Logs(ctx context.Context, projectName string, w io.Writer) error
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
consumer := formatter.NewLogConsumer(w)
|
consumer := formatter.NewLogConsumer(w)
|
||||||
for _, c := range list {
|
for _, c := range list {
|
||||||
service := c.Labels["com.docker.compose.service"]
|
service := c.Labels[serviceLabel]
|
||||||
containerId := c.ID
|
containerId := c.ID
|
||||||
go func() {
|
go func() {
|
||||||
s.containerService.Logs(ctx,containerId, containers.LogsRequest{
|
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) {
|
func (s *local) Ps(ctx context.Context, projectName string) ([]compose.ServiceStatus, error) {
|
||||||
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
list, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
||||||
Filters: filters.NewArgs(
|
Filters: filters.NewArgs(
|
||||||
filters.Arg("label", "com.docker.compose.project="+projectName),
|
projectFilter(projectName),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -250,7 +250,7 @@ func (s *local) Ps(ctx context.Context, projectName string) ([]compose.ServiceSt
|
|||||||
// TODO group by service
|
// TODO group by service
|
||||||
status = append(status, compose.ServiceStatus{
|
status = append(status, compose.ServiceStatus{
|
||||||
ID: c.ID,
|
ID: c.ID,
|
||||||
Name: c.Labels["com.docker.compose.service"],
|
Name: c.Labels[serviceLabel],
|
||||||
Replicas: 0,
|
Replicas: 0,
|
||||||
Desired: 0,
|
Desired: 0,
|
||||||
Ports: nil,
|
Ports: nil,
|
||||||
@ -260,6 +260,7 @@ func (s *local) Ps(ctx context.Context, projectName string) ([]compose.ServiceSt
|
|||||||
return status, nil
|
return status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *local) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
|
func (s *local) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
|
||||||
_, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{All: true})
|
_, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{All: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -287,10 +288,10 @@ func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number i
|
|||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
labels := map[string]string{
|
labels := map[string]string{
|
||||||
"com.docker.compose.project": p.Name,
|
projectLabel: p.Name,
|
||||||
"com.docker.compose.service": s.Name,
|
serviceLabel: s.Name,
|
||||||
"com.docker.compose.config-hash": hash,
|
configHashLabel: hash,
|
||||||
"com.docker.compose.container-number": strconv.Itoa(number),
|
containerNumberLabel: strconv.Itoa(number),
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -34,8 +34,8 @@ import (
|
|||||||
func (s *local) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
|
func (s *local) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
|
||||||
actual, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
actual, err := s.containerService.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
||||||
Filters: filters.NewArgs(
|
Filters: filters.NewArgs(
|
||||||
filters.Arg("label", "com.docker.compose.project="+project.Name),
|
filters.Arg("label", fmt.Sprintf("%s=%s", projectLabel, project.Name)),
|
||||||
filters.Arg("label", "com.docker.compose.service="+service.Name),
|
filters.Arg("label", fmt.Sprintf("%s=%s", serviceLabel, service.Name)),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -80,7 +80,7 @@ func (s *local) ensureService(ctx context.Context, project *types.Project, servi
|
|||||||
}
|
}
|
||||||
for _, container := range actual {
|
for _, container := range actual {
|
||||||
container := container
|
container := container
|
||||||
diverged := container.Labels["com.docker.compose.config-hash"] != expected
|
diverged := container.Labels[configHashLabel] != expected
|
||||||
if diverged {
|
if diverged {
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
return s.recreateContainer(ctx, project, service, container)
|
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) {
|
func nextContainerNumber(containers []moby.Container) (int, error) {
|
||||||
max := 0
|
max := 0
|
||||||
for _, c := range containers {
|
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 {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@ -164,7 +164,7 @@ func (s *local) recreateContainer(ctx context.Context, project *types.Project, s
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
number, err := strconv.Atoi(container.Labels["com.docker.compose.container-number"])
|
number, err := strconv.Atoi(container.Labels[containerNumberLabel])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
35
local/labels.go
Normal file
35
local/labels.go
Normal 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))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user