Skip to content

Commit b760afa

Browse files
htoyoda18ndeloof
authored andcommitted
refactor: extract API version constants to dedicated file
Signed-off-by: hiroto.toyoda <[email protected]>
1 parent a2a5c86 commit b760afa

7 files changed

Lines changed: 87 additions & 14 deletions

File tree

pkg/compose/api_versions.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
// Docker Engine API version constants.
20+
// These versions correspond to specific Docker Engine releases and their features.
21+
const (
22+
// APIVersion144 represents Docker Engine API version 1.44 (Engine v25.0).
23+
//
24+
// New features in this version:
25+
// - Endpoint-specific MAC address configuration
26+
// - Multiple networks can be connected during container creation
27+
// - healthcheck.start_interval parameter support
28+
//
29+
// Before this version:
30+
// - MAC address was container-wide only
31+
// - Extra networks required post-creation NetworkConnect calls
32+
// - healthcheck.start_interval was not available
33+
APIVersion144 = "1.44"
34+
35+
// APIVersion148 represents Docker Engine API version 1.48 (Engine v28.0).
36+
//
37+
// New features in this version:
38+
// - Volume mounts with type=image support
39+
//
40+
// Before this version:
41+
// - Only bind, volume, and tmpfs mount types were supported
42+
APIVersion148 = "1.48"
43+
44+
// APIVersion149 represents Docker Engine API version 1.49 (Engine v28.1).
45+
//
46+
// New features in this version:
47+
// - Network interface_name configuration
48+
// - Platform parameter in ImageList API
49+
//
50+
// Before this version:
51+
// - interface_name was not configurable
52+
// - ImageList didn't support platform filtering
53+
APIVersion149 = "1.49"
54+
)
55+
56+
// Docker Engine version strings for user-facing error messages.
57+
// These should be used in error messages to provide clear version requirements.
58+
const (
59+
// DockerEngineV25 is the major version string for Docker Engine 25.x
60+
DockerEngineV25 = "v25"
61+
62+
// DockerEngineV28 is the major version string for Docker Engine 28.x
63+
DockerEngineV28 = "v28"
64+
65+
// DockerEngineV28_1 is the specific version string for Docker Engine 28.1
66+
DockerEngineV28_1 = "v28.1"
67+
)
68+
69+
// Build tool version constants
70+
const (
71+
// BuildxMinVersion is the minimum required version of buildx for compose build
72+
BuildxMinVersion = "0.17.0"
73+
)

pkg/compose/build_bake.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ func (s *composeService) getBuildxPlugin() (*manager.Plugin, error) {
424424
return nil, fmt.Errorf("failed to get version of buildx")
425425
}
426426

427-
if versions.LessThan(buildx.Version[1:], "0.17.0") {
428-
return nil, fmt.Errorf("compose build requires buildx 0.17 or later")
427+
if versions.LessThan(buildx.Version[1:], BuildxMinVersion) {
428+
return nil, fmt.Errorf("compose build requires buildx %s or later", BuildxMinVersion)
429429
}
430430

431431
return buildx, nil

pkg/compose/convergence.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
743743
}
744744
// Starting API version 1.44, the ContainerCreate API call takes multiple networks
745745
// so we include all the configurations there and can skip the one-by-one calls here
746-
if versions.LessThan(apiVersion, "1.44") {
746+
if versions.LessThan(apiVersion, APIVersion144) {
747747
// the highest-priority network is the primary and is included in the ContainerCreate API
748748
// call via container.NetworkMode & network.NetworkingConfig
749749
// any remaining networks are connected one-by-one here after creation (but before start)

pkg/compose/convergence_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func TestCreateMobyContainer(t *testing.T) {
353353
// force `RuntimeVersion` to fetch fresh version
354354
runtimeVersion = runtimeVersionCache{}
355355
apiClient.EXPECT().ServerVersion(gomock.Any()).Return(moby.Version{
356-
APIVersion: "1.44",
356+
APIVersion: APIVersion144,
357357
}, nil).AnyTimes()
358358

359359
service := types.ServiceConfig{

pkg/compose/convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func (s *composeService) ToMobyHealthCheck(ctx context.Context, check *compose.H
7373
if err != nil {
7474
return nil, err
7575
}
76-
if versions.LessThan(version, "1.44") {
77-
return nil, errors.New("can't set healthcheck.start_interval as feature require Docker Engine v25 or later")
76+
if versions.LessThan(version, APIVersion144) {
77+
return nil, fmt.Errorf("can't set healthcheck.start_interval as feature require Docker Engine %s or later", DockerEngineV25)
7878
} else {
7979
startInterval = time.Duration(*check.StartInterval)
8080
}

pkg/compose/create.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func (s *composeService) prepareContainerMACAddress(ctx context.Context, service
360360
if macAddress != "" && mainNw != nil && mainNw.MacAddress != "" && mainNw.MacAddress != macAddress {
361361
return "", fmt.Errorf("the service-level mac_address should have the same value as network %s", nwName)
362362
}
363-
if versions.GreaterThanOrEqualTo(version, "1.44") {
363+
if versions.GreaterThanOrEqualTo(version, APIVersion144) {
364364
if mainNw != nil && mainNw.MacAddress == "" {
365365
mainNw.MacAddress = macAddress
366366
}
@@ -374,7 +374,7 @@ func (s *composeService) prepareContainerMACAddress(ctx context.Context, service
374374
}
375375

376376
if len(withMacAddress) > 1 {
377-
return "", fmt.Errorf("a MAC address is specified for multiple networks (%s), but this feature requires Docker Engine v25 or later", strings.Join(withMacAddress, ", "))
377+
return "", fmt.Errorf("a MAC address is specified for multiple networks (%s), but this feature requires Docker Engine %s or later", strings.Join(withMacAddress, ", "), DockerEngineV25)
378378
}
379379

380380
if mainNw != nil && mainNw.MacAddress != "" {
@@ -527,7 +527,7 @@ func defaultNetworkSettings(project *types.Project,
527527
// so we can pass all the extra networks we want the container to be connected to
528528
// in the network configuration instead of connecting the container to each extra
529529
// network individually after creation.
530-
if versions.GreaterThanOrEqualTo(version, "1.44") {
530+
if versions.GreaterThanOrEqualTo(version, APIVersion144) {
531531
if len(service.Networks) > 1 {
532532
serviceNetworks := service.NetworksByPriority()
533533
for _, networkKey := range serviceNetworks[1:] {
@@ -541,10 +541,10 @@ func defaultNetworkSettings(project *types.Project,
541541
}
542542
}
543543

544-
if versions.LessThan(version, "1.49") {
544+
if versions.LessThan(version, APIVersion149) {
545545
for _, config := range service.Networks {
546546
if config != nil && config.InterfaceName != "" {
547-
return "", nil, fmt.Errorf("interface_name requires Docker Engine v28.1 or later")
547+
return "", nil, fmt.Errorf("interface_name requires Docker Engine %s or later", DockerEngineV28_1)
548548
}
549549
}
550550
}
@@ -861,8 +861,8 @@ func (s *composeService) buildContainerVolumes(
861861
if err != nil {
862862
return nil, nil, err
863863
}
864-
if versions.LessThan(version, "1.48") {
865-
return nil, nil, fmt.Errorf("volume with type=image require Docker Engine v28 or later")
864+
if versions.LessThan(version, APIVersion148) {
865+
return nil, nil, fmt.Errorf("volume with type=image require Docker Engine %s or later", DockerEngineV28)
866866
}
867867
}
868868
mounts = append(mounts, m)

pkg/compose/images.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
6161
if err != nil {
6262
return nil, err
6363
}
64-
withPlatform := versions.GreaterThanOrEqualTo(version, "1.49")
64+
withPlatform := versions.GreaterThanOrEqualTo(version, APIVersion149)
6565

6666
summary := map[string]api.ImageSummary{}
6767
var mux sync.Mutex

0 commit comments

Comments
 (0)