Skip to content

Commit 0ee2113

Browse files
committed
agent: Refactoring, adding changelog
1 parent 63c013e commit 0ee2113

File tree

3 files changed

+39
-47
lines changed

3 files changed

+39
-47
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
## UNRELEASED
44
* Feature - Support for provisioning Tasks with ENIs
5+
* Bug - Fixed a bug where unsupported Docker API client versions could be registered
6+
[#1010](https://github.com/aws/amazon-ecs-agent/issues/1010)
57

68
## 1.14.5
79
* Enhancement - Retry failed container image pull operations [#975](https://github.com/aws/amazon-ecs-agent/pull/975)
810
* Enhancement - Set read and write timeouts for websocket connectons [#993](https://github.com/aws/amazon-ecs-agent/pull/993)
911
* Enhancement - Add support for the SumoLogic Docker log driver plugin
10-
[#992](https://github.com/aws/amazon-ecs-agent/pull/992)
12+
[#992](https://github.com/aws/amazon-ecs-agent/pull/992)
1113
* Bug - Fixed a memory leak issue when submitting the task state change [#967](https://github.com/aws/amazon-ecs-agent/pull/967)
1214
* Bug - Fixed a race condition where a container can be created twice when agent restarts. [#939](https://github.com/aws/amazon-ecs-agent/pull/939)
1315
* Bug - Fixed an issue where `microsoft/windowsservercore:latest` was not

agent/engine/dockerclient/dockerclientfactory.go

+31-23
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ import (
2323

2424
const (
2525
// minAPIVersionKey is the docker.Env key for min API version
26+
// This is supported in Docker API versions >=1.25
27+
// https://docs.docker.com/engine/api/version-history/#v125-api-changes
2628
minAPIVersionKey = "MinAPIVersion"
2729
// apiVersionKey is the docker.Env key for API version
2830
apiVersionKey = "ApiVersion"
31+
// zeroPatch is a string to append patch number zero if the major minor version lacks it
32+
zeroPatch = ".0"
2933
)
3034
// Factory provides a collection of docker remote clients that include a
3135
// recommended client version as well as a set of alternative supported
@@ -116,11 +120,11 @@ func (f *factory) getClient(version DockerVersion) (dockeriface.Client, error) {
116120
// findDockerVersions loops over all known API versions and finds which ones
117121
// are supported by the docker daemon on the host
118122
func findDockerVersions(endpoint string) map[DockerVersion]dockeriface.Client {
119-
// if the client version returns a min version and api version, then use it to return
120-
// Docker versions
123+
// if the client version returns a MinAPIVersion and APIVersion, then use it to return
124+
// all the Docker clients between MinAPIVersion and APIVersion
121125
clients, err := findDockerVersionsfromMinMaxVersions(endpoint)
122126
if err != nil {
123-
log.Debugf("Unable to get Docker clients from min API version: %v", err)
127+
log.Infof("Unable to get Docker clients from min API version: %v", err)
124128
} else {
125129
return clients
126130
}
@@ -156,36 +160,40 @@ func findDockerVersionsfromMinMaxVersions(endpoint string) (map[DockerVersion]do
156160

157161
clients := make(map[DockerVersion]dockeriface.Client)
158162
// check if the docker.Env obj has MinAPIVersion key
159-
if clientVersion.Exists(minAPIVersionKey) {
160-
minAPIVersion := clientVersion.Get(minAPIVersionKey)
161-
apiVersion := clientVersion.Get(apiVersionKey)
162-
for _, version := range getKnownAPIVersions() {
163-
dockerClient, err := getDockerClientForVersion(endpoint, string(version), minAPIVersion, apiVersion)
164-
if err != nil {
165-
log.Debugf("Unable to get Docker client for version %s: %v", version, err)
166-
continue
167-
}
168-
clients[version] = dockerClient
163+
if !clientVersion.Exists(minAPIVersionKey) {
164+
return nil, errors.New("min API version not found in client version")
165+
}
166+
minAPIVersion := clientVersion.Get(minAPIVersionKey)
167+
apiVersion := clientVersion.Get(apiVersionKey)
168+
for _, version := range getKnownAPIVersions() {
169+
dockerClient, err := getDockerClientForVersion(endpoint, string(version), minAPIVersion, apiVersion)
170+
if err != nil {
171+
log.Infof("Unable to get Docker client for version %s: %v", version, err)
172+
continue
169173
}
170-
return clients, nil
174+
clients[version] = dockerClient
171175
}
172-
return nil, errors.New("min API version not found in client version")
176+
return clients, nil
173177
}
174178

175179
func getDockerClientForVersion(
176180
endpoint string,
177181
version string,
178182
minAPIVersion string,
179183
apiVersion string) (dockeriface.Client, error) {
180-
lessThanMinCheck := "<"+minAPIVersion
181-
moreThanMaxCheck := ">"+apiVersion
182-
minVersionCheck, minErr := utils.Version(version).Matches(lessThanMinCheck)
183-
maxVersionCheck, maxErr := utils.Version(version).Matches(moreThanMaxCheck)
184-
if minErr != nil {
185-
return nil, errors.Wrapf(minErr, "error while comparing version %s with minAPIVersion %s", version, minAPIVersion)
184+
// Adding patch number zero to Docker versions to reuse the existing semver
185+
// comparator
186+
// TODO: remove this logic later when non-semver comparator is implemented
187+
version += zeroPatch
188+
lessThanMinCheck := "<" + minAPIVersion + zeroPatch
189+
moreThanMaxCheck := ">" + apiVersion + zeroPatch
190+
minVersionCheck, err := utils.Version(version).Matches(lessThanMinCheck)
191+
if err != nil {
192+
return nil, errors.Wrapf(err, "error while comparing version %s with minAPIVersion %s", version, minAPIVersion)
186193
}
187-
if maxErr != nil {
188-
return nil, errors.Wrapf(minErr, "error while comparing version %s with apiVersion %s", version, apiVersion)
194+
maxVersionCheck, err := utils.Version(version).Matches(moreThanMaxCheck)
195+
if err != nil {
196+
return nil, errors.Wrapf(err, "error while comparing version %s with apiVersion %s", version, apiVersion)
189197
}
190198
// do not add the version when it is less than min api version or greater
191199
// than api version

agent/utils/compare_versions.go

+5-23
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import (
2020
"strings"
2121
)
2222

23-
// zeroPatch is a string to append patch number zero if the major minor version lacks it
24-
const zeroPatch = ".0"
25-
2623
type Version string
2724

2825
type semver struct {
@@ -43,8 +40,7 @@ type semver struct {
4340
// * <x.y.z -- Matches a version less than the selector version
4441
// * x.y.z,a.b.c -- Matches if the version matches either of the two selector versions
4542
func (lhs Version) Matches(selector string) (bool, error) {
46-
lhsVer := appendIfNoPatch(string(lhs))
47-
lhsVersion, err := parseSemver(lhsVer)
43+
lhsVersion, err := parseSemver(string(lhs))
4844
if err != nil {
4945
return false, err
5046
}
@@ -63,29 +59,25 @@ func (lhs Version) Matches(selector string) (bool, error) {
6359
}
6460

6561
if strings.HasPrefix(selector, ">=") {
66-
rhs := appendIfNoPatch(selector[2:])
67-
rhsVersion, err := parseSemver(rhs)
62+
rhsVersion, err := parseSemver(selector[2:])
6863
if err != nil {
6964
return false, err
7065
}
7166
return compareSemver(lhsVersion, rhsVersion) >= 0, nil
7267
} else if strings.HasPrefix(selector, ">") {
73-
rhs := appendIfNoPatch(selector[1:])
74-
rhsVersion, err := parseSemver(rhs)
68+
rhsVersion, err := parseSemver(selector[1:])
7569
if err != nil {
7670
return false, err
7771
}
7872
return compareSemver(lhsVersion, rhsVersion) > 0, nil
7973
} else if strings.HasPrefix(selector, "<=") {
80-
rhs := appendIfNoPatch(selector[2:])
81-
rhsVersion, err := parseSemver(rhs)
74+
rhsVersion, err := parseSemver(selector[2:])
8275
if err != nil {
8376
return false, err
8477
}
8578
return compareSemver(lhsVersion, rhsVersion) <= 0, nil
8679
} else if strings.HasPrefix(selector, "<") {
87-
rhs := appendIfNoPatch(selector[1:])
88-
rhsVersion, err := parseSemver(rhs)
80+
rhsVersion, err := parseSemver(selector[1:])
8981
if err != nil {
9082
return false, err
9183
}
@@ -136,16 +128,6 @@ func parseSemver(version string) (semver, error) {
136128
return result, nil
137129
}
138130

139-
// TODO: remove this logic once non-semver comparator is implemented
140-
func appendIfNoPatch(version string) string {
141-
versionParts := strings.Split(version, ".")
142-
// Only major and minor versions are present
143-
if len(versionParts) == 2 {
144-
version += zeroPatch
145-
}
146-
return version
147-
}
148-
149131
// compareSemver compares two semvers, 'lhs' and 'rhs', and returns -1 if lhs is less
150132
// than rhs, 0 if they are equal, and 1 lhs is greater than rhs
151133
func compareSemver(lhs, rhs semver) int {

0 commit comments

Comments
 (0)