Skip to content

Commit

Permalink
handling docker-pullable image ids
Browse files Browse the repository at this point in the history
Also changed the RE that parses image names and references to also
include parsing the "protocol" (currently only 'docker' and
'docker-pullable') and made sure both tag and digest are parsed
correctly. (? at the end of RE means it is optional: will apear 0 or 1
times.)
  • Loading branch information
Erez Freiberger committed Nov 22, 2016
1 parent 638f4d7 commit eebf813
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
7 changes: 5 additions & 2 deletions app/models/container_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class ContainerImage < ApplicationRecord


DOCKER_IMAGE_PREFIX = "docker://"
DOCKER_PULLABLE_PREFIX = "docker-pullable://".freeze
DOCKER_PREFIXES = [DOCKER_IMAGE_PREFIX, DOCKER_PULLABLE_PREFIX].freeze

belongs_to :container_image_registry
belongs_to :ext_management_system, :foreign_key => "ems_id"
Expand Down Expand Up @@ -45,9 +47,10 @@ def operating_system=(value)
end

def docker_id
if image_ref.start_with?(DOCKER_IMAGE_PREFIX)
return image_ref[DOCKER_IMAGE_PREFIX.length..-1]
DOCKER_PREFIXES.each do |prefix|
return image_ref[prefix.length..-1] if image_ref.start_with?(prefix)
end
nil
end

# The guid is required by the smart analysis infrastructure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,31 +728,35 @@ def parse_base_item(item)
def parse_image_name(image, image_ref)
# parsing using same logic as in docker
# https://github.com/docker/docker/blob/348f6529b71502b561aa493e250fd5be248da0d5/reference/reference.go#L174
parts = %r{
image_definition_re = %r{
\A
(?<protocol>#{ContainerImage::DOCKER_PULLABLE_PREFIX})?
(?:(?:
(?<host>([^\.:/]+\.)+[^\.:/]+)|
(?:(?<host2>[^:/]+)(?::(?<port>\d+)))|
(?<localhost>localhost)
)/)?
(?<name>(?:[^:/@]+/)*[^/:@]+)
(?:(?::(?<tag>.+))|(?:\@(?<digest>.+)))?
(?::(?<tag>[^:/@]+))?
(?:\@(?<digest>.+))?
\z
}x.match(image)
}x
image_parts = image_definition_re.match(image)
image_ref_parts = image_definition_re.match(image_ref)

hostname = parts[:host] || parts[:host2] || parts[:localhost]
hostname = image_parts[:host] || image_parts[:host2] || image_parts[:localhost]
[
{
:name => parts[:name],
:tag => parts[:tag],
:digest => parts[:digest],
:name => image_parts[:name],
:tag => image_parts[:tag],
:digest => image_parts[:digest] || (image_ref_parts[:digest] if image_ref_parts),
:image_ref => image_ref,
:registered_on => Time.now.utc
},
hostname && {
:name => hostname,
:host => hostname,
:port => parts[:port],
:port => image_parts[:port],
},
]
end
Expand Down
3 changes: 3 additions & 0 deletions spec/models/container_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
image = FactoryGirl.create(:container_image, :image_ref => "docker://id")
expect(image.docker_id).to eq("id")

image = FactoryGirl.create(:container_image, :image_ref => "docker-pullable://repo/name@id")
expect(image.docker_id).to eq("repo/name@id")

image = FactoryGirl.create(:container_image, :image_ref => "rocket://id")
expect(image.docker_id).to eq(nil)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,26 @@
:image_ref => example_ref},
:registry => {:name => "localhost", :host => "localhost", :port => nil}},

# tag and digest together
{:image_name => "reg.example.com:1234/name1:tagos@sha256:123abcdef",
:image => {:name => "name1", :tag => "tagos", :digest => "sha256:123abcdef",
:image_ref => example_ref},
:registry => {:name => "reg.example.com", :host => "reg.example.com", :port => "1234"}},

# digest from new docker-pullable
{:image_name => "reg.example.com:1234/name1:tagos",
:image => {:name => "name1", :tag => "tagos", :digest => "sha256:321bcd",
:image_ref => "docker-pullable://reg.example.com:1234/name1@sha256:321bcd"},
:registry => {:name => "reg.example.com", :host => "reg.example.com", :port => "1234"}},

{:image_name => "example@sha256:1234567abcdefg",
:image => {:name => "example", :tag => nil, :digest => "sha256:1234567abcdefg",
:image_ref => example_ref},
:registry => nil}]

example_images.each do |ex|
it "tests '#{ex[:image_name]}'" do
result_image, result_registry = parser.send(:parse_image_name, ex[:image_name], example_ref)
result_image, result_registry = parser.send(:parse_image_name, ex[:image_name], ex[:image][:image_ref])

expect(result_image.except(:registered_on)).to eq(ex[:image])
expect(result_registry).to eq(ex[:registry])
Expand Down

0 comments on commit eebf813

Please sign in to comment.