Module: Pangea::Kubernetes::Backends::AwsEks

Includes:
Base
Defined in:
lib/pangea/kubernetes/backends/aws_eks.rb

Overview

AWS EKS backend — creates managed EKS clusters with VPC, IAM, and node groups.

Class Method Summary collapse

Methods included from Base

included

Class Method Details

.backend_nameObject



27
# File 'lib/pangea/kubernetes/backends/aws_eks.rb', line 27

def backend_name = :aws

.create_cluster(ctx, name, config, result, tags) ⇒ Object

Create the EKS cluster



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/pangea/kubernetes/backends/aws_eks.rb', line 127

def create_cluster(ctx, name, config, result, tags)
  # Determine subnet IDs
  subnet_ids = if config.network&.subnet_ids&.any?
                 config.network.subnet_ids
               elsif result.network
                 if result.network.respond_to?(:subnet_ids)
                   result.network.subnet_ids
                 else
                   result.network.select { |k, _| k.to_s.start_with?('subnet_') }.values.map(&:id)
                 end
               else
                 []
               end

  # Determine role ARN
  role_arn = config.role_arn || result.iam&.dig(:cluster_role)&.arn

  cluster_attrs = {
    name: "#{name}-cluster",
    role_arn: role_arn,
    version: config.kubernetes_version,
    vpc_config: {
      subnet_ids: subnet_ids,
      endpoint_private_access: config.network&.private_endpoint || true,
      endpoint_public_access: config.network&.public_endpoint || false,
      security_group_ids: config.network&.security_group_ids || []
    },
    tags: tags.merge(Name: "#{name}-cluster")
  }

  cluster_attrs[:enabled_cluster_log_types] = config.logging if config.logging.any?

  if config.encryption_at_rest
    cluster_attrs[:encryption_config] = {
      resources: ['secrets']
    }
  end

  ctx.aws_eks_cluster(:"#{name}_cluster", cluster_attrs)
end

.create_iam(ctx, name, config, tags) ⇒ Object

Create IAM role for the EKS cluster and node groups



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pangea/kubernetes/backends/aws_eks.rb', line 70

def create_iam(ctx, name, config, tags)
  iam = Architecture::AwsEksIamResult.new

  # Cluster role — use provided role_arn or create one
  unless config.role_arn
    assume_role_policy = {
      Version: '2012-10-17',
      Statement: [{
        Effect: 'Allow',
        Principal: { Service: 'eks.amazonaws.com' },
        Action: 'sts:AssumeRole'
      }]
    }

    iam.cluster_role = ctx.aws_iam_role(
      :"#{name}_cluster_role",
      name: "#{name}-eks-cluster-role",
      assume_role_policy: assume_role_policy,
      tags: tags.merge(Name: "#{name}-cluster-role")
    )

    iam.cluster_policy_attachment = ctx.aws_iam_role_policy_attachment(
      :"#{name}_cluster_policy",
      role: iam.cluster_role.ref(:name),
      policy_arn: 'arn:aws:iam::aws:policy/AmazonEKSClusterPolicy'
    )
  end

  # Node role
  node_assume_role_policy = {
    Version: '2012-10-17',
    Statement: [{
      Effect: 'Allow',
      Principal: { Service: 'ec2.amazonaws.com' },
      Action: 'sts:AssumeRole'
    }]
  }

  iam.node_role = ctx.aws_iam_role(
    :"#{name}_node_role",
    name: "#{name}-eks-node-role",
    assume_role_policy: node_assume_role_policy,
    tags: tags.merge(Name: "#{name}-node-role")
  )

  %w[AmazonEKSWorkerNodePolicy AmazonEKS_CNI_Policy AmazonEC2ContainerRegistryReadOnly].each do |policy|
    ctx.aws_iam_role_policy_attachment(
      :"#{name}_node_#{policy.downcase.gsub(/[^a-z0-9]/, '_')}",
      role: iam.node_role.ref(:name),
      policy_arn: "arn:aws:iam::aws:policy/#{policy}"
    )
  end

  iam
end

.create_network(ctx, name, config, tags) ⇒ Object

Create VPC + subnets for the EKS cluster



41
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
# File 'lib/pangea/kubernetes/backends/aws_eks.rb', line 41

def create_network(ctx, name, config, tags)
  network = Architecture::NetworkResult.new

  vpc_cidr = config.network&.vpc_cidr || '10.0.0.0/16'
  network.vpc = ctx.aws_vpc(
    :"#{name}_vpc",
    cidr_block: vpc_cidr,
    enable_dns_hostnames: true,
    enable_dns_support: true,
    tags: tags.merge(Name: "#{name}-vpc")
  )

  # Create 2 subnets in different AZs (EKS requirement)
  %w[a b].each_with_index do |az_suffix, idx|
    subnet = ctx.aws_subnet(
      :"#{name}_subnet_#{az_suffix}",
      vpc_id: network.vpc.id,
      cidr_block: "10.0.#{idx}.0/24",
      availability_zone: "#{config.region}#{az_suffix}",
      map_public_ip_on_launch: true,
      tags: tags.merge(Name: "#{name}-subnet-#{az_suffix}")
    )
    network.add_subnet(:"subnet_#{az_suffix}", subnet)
  end

  network
end

.create_node_pool(ctx, name, cluster_ref, pool_config, tags) ⇒ Object

Create an EKS managed node group



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/pangea/kubernetes/backends/aws_eks.rb', line 169

def create_node_pool(ctx, name, cluster_ref, pool_config, tags)
  pool_name = :"#{name}_#{pool_config.name}"

  node_group_attrs = {
    cluster_name: cluster_ref.ref(:name),
    node_group_name: "#{name}-#{pool_config.name}",
    node_role_arn: "${aws_iam_role.#{name}_node_role.arn}",
    instance_types: pool_config.instance_types,
    scaling_config: {
      desired_size: pool_config.effective_desired_size,
      min_size: pool_config.min_size,
      max_size: pool_config.max_size
    },
    disk_size: pool_config.disk_size_gb,
    tags: tags.merge(
      Name: "#{name}-#{pool_config.name}",
      NodePool: pool_config.name.to_s
    )
  }

  node_group_attrs[:labels] = pool_config.labels if pool_config.labels.any?

  if pool_config.taints.any?
    node_group_attrs[:taint] = pool_config.taints.map do |t|
      { key: t[:key], value: t[:value], effect: t[:effect] }
    end
  end

  ctx.aws_eks_node_group(pool_name, node_group_attrs)
end

.load_provider!Object



31
32
33
34
35
36
37
38
# File 'lib/pangea/kubernetes/backends/aws_eks.rb', line 31

def load_provider!
  require required_gem
rescue LoadError => e
  raise LoadError,
        "Backend :aws requires gem 'pangea-aws'. " \
        "Add it to your Gemfile: gem 'pangea-aws'\n" \
        "Original error: #{e.message}"
end

.managed_kubernetes?Boolean

Returns:

  • (Boolean)


28
# File 'lib/pangea/kubernetes/backends/aws_eks.rb', line 28

def managed_kubernetes? = true

.required_gemObject



29
# File 'lib/pangea/kubernetes/backends/aws_eks.rb', line 29

def required_gem = 'pangea-aws'