Module: MovableInk::AWS::EKS

Included in:
MovableInk::AWS
Defined in:
lib/movable_ink/aws/eks.rb

Instance Method Summary collapse

Instance Method Details

#eks(region: my_region) ⇒ Object



7
8
9
10
# File 'lib/movable_ink/aws/eks.rb', line 7

def eks(region: my_region)
  @eks_client ||= {}
  @eks_client[region] ||= Aws::EKS::Client.new(region: region)
end

#generate_kubeconfig(region: my_region, cluster_name:) ⇒ Object



12
13
14
15
16
17
18
19
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
# File 'lib/movable_ink/aws/eks.rb', line 12

def generate_kubeconfig(region: my_region, cluster_name:)
  client = eks(region: region)

  resp = run_with_backoff do
    begin
      client.describe_cluster({ name: cluster_name })
    rescue Aws::EKS::Errors::ResourceNotFoundException
      return nil
    end
  end

  cluster_arn = resp.cluster.arn
  cluster_server_address = resp.cluster.endpoint
  cluster_certificate_authority_data = resp.cluster.certificate_authority.data

  {
    'apiVersion' => 'v1',
    'clusters' => [{
      'cluster' => {
        'certificate-authority-data' => cluster_certificate_authority_data,
        'server' => cluster_server_address,
      },
      'name' => cluster_arn
    }],
    'contexts' => [{
      'context' => {
        'cluster' => cluster_arn,
        'user' => cluster_arn,
      },
      'name' => cluster_arn,
    }],
    'current-context' => cluster_arn,
    'kind' => 'Config',
    'preferences' => {},
    'users' => [{
      'name' => cluster_arn,
      'user' => {
        'exec' => {
          'apiVersion' => 'client.authentication.k8s.io/v1alpha1',
          'args' => ['--region', region, 'eks', 'get-token', '--cluster-name', cluster_name],
          'command' => 'aws',
        }
      }
    }]
  }.to_yaml
end