Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit d25dee9

Browse files
authored
Merge pull request #1803 from ndeloof/compose_version
better interoperability with docker-compose on version label
2 parents 9ea051f + df9fa2d commit d25dee9

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ require (
3737
github.com/google/go-cmp v0.5.5
3838
github.com/hashicorp/go-multierror v1.1.0
3939
github.com/hashicorp/go-uuid v1.0.2
40+
github.com/hashicorp/go-version v1.3.0
4041
github.com/iancoleman/strcase v0.1.2
4142
github.com/joho/godotenv v1.3.0
4243
github.com/kr/pty v1.1.8 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,8 @@ github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2I
759759
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
760760
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
761761
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
762+
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
763+
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
762764
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
763765
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
764766
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

pkg/api/labels.go

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616

1717
package api
1818

19+
import (
20+
"fmt"
21+
22+
"github.com/hashicorp/go-version"
23+
24+
"github.com/docker/compose-cli/internal"
25+
)
26+
1927
const (
2028
// ProjectLabel allow to track resource related to a compose project
2129
ProjectLabel = "com.docker.compose.project"
@@ -42,3 +50,15 @@ const (
4250
// VersionLabel stores the compose tool version used to run application
4351
VersionLabel = "com.docker.compose.version"
4452
)
53+
54+
var ComposeVersion string
55+
56+
func init() {
57+
v, err := version.NewVersion(internal.Version)
58+
if err == nil {
59+
segments := v.Segments()
60+
if len(segments) > 2 {
61+
ComposeVersion = fmt.Sprintf("%d.%d.%d", segments[0], segments[1], segments[2])
62+
}
63+
}
64+
}

pkg/compose/create.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"github.com/pkg/errors"
3939
"github.com/sirupsen/logrus"
4040

41-
"github.com/docker/compose-cli/internal"
4241
"github.com/docker/compose-cli/pkg/api"
4342
"github.com/docker/compose-cli/pkg/progress"
4443
"github.com/docker/compose-cli/pkg/utils"
@@ -141,7 +140,7 @@ func prepareNetworks(project *types.Project) {
141140
for k, network := range project.Networks {
142141
network.Labels = network.Labels.Add(api.NetworkLabel, k)
143142
network.Labels = network.Labels.Add(api.ProjectLabel, project.Name)
144-
network.Labels = network.Labels.Add(api.VersionLabel, internal.Version)
143+
network.Labels = network.Labels.Add(api.VersionLabel, api.ComposeVersion)
145144
project.Networks[k] = network
146145
}
147146
}
@@ -184,7 +183,7 @@ func (s *composeService) ensureProjectVolumes(ctx context.Context, project *type
184183
for k, volume := range project.Volumes {
185184
volume.Labels = volume.Labels.Add(api.VolumeLabel, k)
186185
volume.Labels = volume.Labels.Add(api.ProjectLabel, project.Name)
187-
volume.Labels = volume.Labels.Add(api.VersionLabel, internal.Version)
186+
volume.Labels = volume.Labels.Add(api.VersionLabel, api.ComposeVersion)
188187
err := s.ensureVolume(ctx, volume)
189188
if err != nil {
190189
return err
@@ -216,7 +215,7 @@ func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project,
216215

217216
labels[api.ProjectLabel] = p.Name
218217
labels[api.ServiceLabel] = service.Name
219-
labels[api.VersionLabel] = internal.Version
218+
labels[api.VersionLabel] = api.ComposeVersion
220219
if _, ok := service.Labels[api.OneoffLabel]; !ok {
221220
labels[api.OneoffLabel] = "False"
222221
}

pkg/compose/create_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"path/filepath"
2222
"testing"
2323

24-
"github.com/docker/compose-cli/internal"
24+
"github.com/docker/compose-cli/pkg/api"
2525

2626
"github.com/compose-spec/compose-go/types"
2727
composetypes "github.com/compose-spec/compose-go/types"
@@ -78,6 +78,6 @@ func TestPrepareNetworkLabels(t *testing.T) {
7878
assert.DeepEqual(t, project.Networks["skynet"].Labels, types.Labels(map[string]string{
7979
"com.docker.compose.network": "skynet",
8080
"com.docker.compose.project": "myProject",
81-
"com.docker.compose.version": internal.Version,
81+
"com.docker.compose.version": api.ComposeVersion,
8282
}))
8383
}

0 commit comments

Comments
 (0)