Class: Kdep::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/kdep/registry.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry_url) ⇒ Registry

Returns a new instance of Registry.



12
13
14
15
16
17
18
# File 'lib/kdep/registry.rb', line 12

def initialize(registry_url)
  url = registry_url.chomp("/")
  parts = url.split("/", 2)
  @host = parts[0]
  @path_prefix = parts[1]
  @registry_url = url
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/kdep/registry.rb', line 10

def host
  @host
end

#path_prefixObject (readonly)

Returns the value of attribute path_prefix.



10
11
12
# File 'lib/kdep/registry.rb', line 10

def path_prefix
  @path_prefix
end

Instance Method Details

#list_tags(image) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kdep/registry.rb', line 20

def list_tags(image)
  repository = @path_prefix ? "#{@path_prefix}/#{image}" : image
  uri = URI("https://#{@host}/v2/#{repository}/tags/list")

  req = Net::HTTP::Get.new(uri)
  apply_auth(req, repository)

  response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  # Handle 401 with ACR token exchange
  if response.code == "401" && @host.end_with?(".azurecr.io")
    token = acr_exchange_token(repository)
    if token
      req = Net::HTTP::Get.new(uri)
      req["Authorization"] = "Bearer #{token}"
      response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
        http.request(req)
      end
    end
  end

  # Follow redirects
  if response.code == "301" || response.code == "302"
    redirect_uri = URI(response["location"])
    req = Net::HTTP::Get.new(redirect_uri)
    apply_auth(req, repository)
    response = Net::HTTP.start(redirect_uri.host, redirect_uri.port, use_ssl: true) do |http|
      http.request(req)
    end
  end

  return [] if response.code == "404"

  unless response.code.start_with?("2")
    raise Error, "Registry returned #{response.code}: #{response.body}"
  end

  data = JSON.parse(response.body)
  data["tags"] || []
end