Skip to content

Commit

Permalink
fix(docker): Correctly set fastForward flag at poll interval start (#246
Browse files Browse the repository at this point in the history
)
  • Loading branch information
robzienert committed Mar 28, 2018
1 parent c50ba87 commit 167893d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class DockerMonitor extends CommonPollingMonitor<ImageDelta, DockerPollingDelta>
}
dockerRegistryAccounts.updateAccounts()
dockerRegistryAccounts.accounts.forEach({ account ->
pollSingle(new PollContext((String) account.name, account, sendEvents))
pollSingle(new PollContext((String) account.name, account, !sendEvents))
})
}

Expand Down Expand Up @@ -136,9 +136,11 @@ class DockerMonitor extends CommonPollingMonitor<ImageDelta, DockerPollingDelta>
delta.items.findAll { it != null }.parallelStream().forEach { ImageDelta item ->
if (item != null) {
cache.setLastDigest(item.image.account, item.image.repository, item.image.tag, item.image.digest)
log.info("New tagged image: {}: {}. Digest is now [$item.image.digest].", kv("account", item.image.account), kv("image", item.imageId))
log.info("New tagged image: {}, {}. Digest is now [$item.image.digest].", kv("account", item.image.account), kv("image", item.imageId))
if (sendEvents) {
postEvent(delta.cachedImages, item.image, item.imageId)
} else {
registry.counter(missedNotificationId.withTags("monitor", getClass().simpleName, "reason", "fastForward")).increment()
}
}
}
Expand All @@ -152,7 +154,7 @@ class DockerMonitor extends CommonPollingMonitor<ImageDelta, DockerPollingDelta>
void postEvent(List<String> cachedImagesForAccount, TaggedImage image, String imageId) {
if (!echoService.isPresent()) {
log.warn("Cannot send tagged image notification: Echo is not enabled")
registry.counter(missedNotificationId.withTag("monitor", getClass().simpleName)).increment()
registry.counter(missedNotificationId.withTags("monitor", getClass().simpleName, "reason", "echoDisabled")).increment()
return
}
if (!cachedImagesForAccount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ class DockerMonitorSpec extends Specification {
def dockerRegistryCache = Mock(DockerRegistryCache)
def dockerRegistryAccounts = Mock(DockerRegistryAccounts)
def echoService = Mock(EchoService)
def taggedImage = new TaggedImage(
tag: "tag",
account: "account",
registry: "registry",
repository: "repository",
digest: "digest"
)

@Unroll
void 'should only publish events if account has been indexed previously'() {
given:
def taggedImage = new TaggedImage(
tag: "tag",
account: "account",
registry: "registry",
repository: "repository",
digest: "digest"
)

when:
new DockerMonitor(properties, registry, discoveryClient, dockerRegistryCache, dockerRegistryAccounts, Optional.of(echoService), Optional.empty())
.postEvent(cachedImages, taggedImage, "imageId")
Expand Down Expand Up @@ -74,6 +76,15 @@ class DockerMonitorSpec extends Specification {
}

void 'should include decorated artifact in the payload'() {
given:
def taggedImage = new TaggedImage(
tag: "tag",
account: "account",
registry: "registry",
repository: "repository",
digest: "digest"
)

when:
new DockerMonitor(properties, registry, discoveryClient, dockerRegistryCache, dockerRegistryAccounts, Optional.of(echoService), Optional.empty())
.postEvent(["job1"], taggedImage, "imageId")
Expand All @@ -89,4 +100,36 @@ class DockerMonitorSpec extends Specification {
})

}

@Unroll
void "should update cache if image is not already cached"() {
given:
def subject = createSubject()
List<String> cachedImages = [
'prefix:dockerRegistry:v2:account:registry:tag',
'prefix:dockerRegistry:v2:account:anotherregistry:tag',
]

when:
def result = subject.shouldUpdateCache(cachedImages, keyFromTaggedImage(taggedImage), taggedImage, trackDigest)

then:
dockerRegistryCache.getLastDigest(_, _, _) >> { "digest" }
assert result == updateCache

where:
taggedImage | trackDigest || updateCache
new TaggedImage(tag: "tag", account: "account", registry: "registry", repository: "repository", digest: "digest") | false || false
new TaggedImage(tag: "new", account: "account", registry: "registry", repository: "repository", digest: "digest") | false || true
new TaggedImage(tag: "tag", account: "account", registry: "registry", repository: "repository", digest: "digest2") | true || true

}

private DockerMonitor createSubject(Optional<EchoService> echoService) {
return new DockerMonitor(properties, registry, discoveryClient, dockerRegistryCache, dockerRegistryAccounts, echoService, Optional.empty())
}

private String keyFromTaggedImage(TaggedImage taggedImage) {
return new DockerRegistryV2Key("prefix", DockerRegistryCache.ID, taggedImage.account, taggedImage.registry, taggedImage.tag).toString()
}
}

0 comments on commit 167893d

Please sign in to comment.