Class: KubeDeployTools::ImageRegistry::Driver::Gcp

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

Instance Method Summary collapse

Methods inherited from Base

#authorize_command, #delete_images, #push_image, #unauthorize_command

Constructor Details

#initialize(registry:) ⇒ Gcp

Returns a new instance of Gcp.



6
7
8
9
10
11
# File 'lib/kube_deploy_tools/image_registry/driver/gcp.rb', line 6

def initialize(registry:)
  super

  @gcloud_config_dir = Dir.mktmpdir
  @activated = false
end

Instance Method Details

#authorizeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kube_deploy_tools/image_registry/driver/gcp.rb', line 13

def authorize
  # Always prefer and activate a service account under a protected namespace if present.
  if @activated
    return
  elsif ENV.member?('GOOGLE_APPLICATION_CREDENTIALS')
    raise "Failed to activate service account" unless ()[2].success?
    @activated = true
  else
    user = current_user
    if ! user.empty?
      Logger.info "Skipping Google activation, using current user #{user}"
      @activated = true
    else
      raise 'No usable Google authorization for pushing images; specify GOOGLE_APPLICATION_CREDENTIALS?'
    end
  end
end

#delete_image(image_id, dryrun) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kube_deploy_tools/image_registry/driver/gcp.rb', line 37

def delete_image(image_id, dryrun)
  # Need the id path to be [HOSTNAME]/[PROJECT-ID]/[IMAGE]
  if dryrun
    Logger.info("DRYRUN: delete gcp image #{image_id}")
  else
    # --quiet removes the user-input component
    _, err, status = Shellrunner.run_call('gcloud', 'container', 'images', 'delete', '--quiet', image_id, '--force-delete-tags')
    if !status.success?
      # gcloud gives a deceptive error msg when the image does not exist
      if err.include?('is not a valid name')
        Logger.warn("Image #{image_id} does not exist, skipping")
      else
        raise "gcloud image deletion failed!"
      end
    end
  end
end

#unauthorizeObject

Delete temporary config dir for gcloud authentication



32
33
34
35
# File 'lib/kube_deploy_tools/image_registry/driver/gcp.rb', line 32

def unauthorize
  Logger.info "Cleaning up authorization for #{@registry.prefix}"
  FileUtils.rm_rf(@gcloud_config_dir) unless @gcloud_config_dir.nil?
end