Class: KubeDeployTools::ImageRegistry::Driver::Aws

Inherits:
Base
  • Object
show all
Includes:
DeployConfigFileUtil
Defined in:
lib/kube_deploy_tools/image_registry/driver/aws.rb

Instance Method Summary collapse

Methods included from DeployConfigFileUtil

#check_and_err, #check_and_warn, #load_library

Methods inherited from Base

#authorize, #unauthorize_command

Constructor Details

#initialize(h) ⇒ Aws

Returns a new instance of Aws.



12
13
14
15
16
17
18
19
20
21
# File 'lib/kube_deploy_tools/image_registry/driver/aws.rb', line 12

def initialize(h)
  super(h)

  check_and_err(
    @registry.config&.has_key?('region'),
    "Expected .image_registries['#{@name}'] to have .config.region to be set "\
    "for the AWS image registry driver, but .config.region is not set "\
    "e.g. .config.region = 'us-west-2'"
  )
end

Instance Method Details

#authorize_commandObject



28
29
30
31
32
# File 'lib/kube_deploy_tools/image_registry/driver/aws.rb', line 28

def authorize_command
   = 
  raise "Unexpected login command: #{}" if .first(2) != ['docker', 'login']
  
end

#delete_image(image, dryrun) ⇒ Object



37
38
39
40
# File 'lib/kube_deploy_tools/image_registry/driver/aws.rb', line 37

def delete_image(image, dryrun)
  # In the AWS driver, the 'delete many' primitive is the primary one.
  delete_images([image], dryrun)
end

#delete_images(images, dryrun) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kube_deploy_tools/image_registry/driver/aws.rb', line 42

def delete_images(images, dryrun)
  # Aggregate images by repository and call aws ecr batch-delete-image
  # once per repository.
  ids_by_repository = {}
  images.each do |image|
    repository, tag = split_full_image_id(image)
    item = {'imageTag': tag}
    if ids_by_repository[repository].nil?
      ids_by_repository[repository] = [item]
    else
      ids_by_repository[repository].push(item)
    end
  end

  # JSON format documented here:
  # https://docs.aws.amazon.com/cli/latest/reference/ecr/batch-delete-image.html
  ids_by_repository.each do |repository, image_ids|
    # batch-delete-image has a threshold of 100 image_ids at a time
    image_chunks = image_ids.each_slice(100).to_a

    image_chunks.each do |images|
      cmd = [
        'aws', 'ecr', 'batch-delete-image',
        '--repository-name', repository,
        '--region', @registry.config.fetch('region'),
        '--image-ids', images.to_json,
      ]

      if dryrun
        Logger.info("Would run: #{cmd}")
      else
        Shellrunner.check_call(*cmd)
      end
    end
  end
end

#push_image(image) ⇒ Object



23
24
25
26
# File 'lib/kube_deploy_tools/image_registry/driver/aws.rb', line 23

def push_image(image)
  create_repository(image.repository) unless repository_exists?(image.repository)
  super(image)
end

#unauthorizeObject



34
35
# File 'lib/kube_deploy_tools/image_registry/driver/aws.rb', line 34

def unauthorize
end