Skip to content

Commit 7c4587c

Browse files
authored
feat: eksctl_nodegroup (#34)
In contrast to nodegroups embedded in `eksctl_cluster` `spec`, `eksctl_nodegroup` resources allows you to add/remove additional nodegroups to/from eksctl_cluster without calling many EKS APIs, which helps large scale deployment.
1 parent 1b0a95e commit 7c4587c

File tree

7 files changed

+460
-46
lines changed

7 files changed

+460
-46
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Benefits:
1212
Features:
1313

1414
- Manage eksctl clusters using Terraform
15-
- [Support for AssumeRole and Cross-Account usage](#assume-role-and-cross-account)
15+
- [Add/remove nodegroups using Terraform](#add-and-remove-nodegroups)
16+
- [Support for AssumeRole and Cross-Account usage](#assumerole-and-cross-account)
1617
- [Install and upgrade eksctl version using Terraform](#declarative-binary-version-management)
1718
- [Cluster canary deployment using ALB](#cluster-canary-deployment-using-alb)
1819
- [Cluster canary deployment using Route 53 + NLB](#cluster-canary-deployment-using-route-53-and-nlb)
@@ -763,6 +764,48 @@ resource "eksctl_cluster" "mystack" {
763764
// snip
764765
```
765766

767+
### Add and remove Node Groups
768+
769+
In addition to declaring nodegroups in `eksctl_cluster`'s `spec,` you can add
770+
one or more nodegroups by using `eksctl_nodegroup`:
771+
772+
```hcl-terraform
773+
resource "eksctl_cluster" "red" {
774+
name = "red1"
775+
region = "us-east-2"
776+
api_version = "eksctl.io/v1alpha5"
777+
version = "1.16"
778+
vpc_id = module.vpc.vpc_id
779+
spec = <<-EOS
780+
nodeGroups:
781+
- name: ng1
782+
instanceType: m5.large
783+
desiredCapacity: 1
784+
targetGroupARNs:
785+
- ${aws_lb_target_group.green.arn}
786+
EOS
787+
}
788+
789+
resource "eksctl_nodegroup" "ng2" {
790+
assume_role {
791+
role_arn = var.role_arn
792+
}
793+
name = "ng1"
794+
region = eksctl_cluster.red.region
795+
cluster = eksctl_cluster.red.name
796+
nodes_min = 1
797+
nodes = 1
798+
# And all the `eksctl-create-nodegroup` flags are available as their `snake_case` form.
799+
# See `eksctl create nodegroup -h` and
800+
# https://github.com/mumoshu/terraform-provider-eksctl/pull/34/files#diff-d490f9a73df8d38ad25b7d26bf1152d178c08df0980f55b3c86fc6991b2b9839R165-R202
801+
# for the full list.
802+
# For example, `--install-nvidia-plugin` can be spciefied as `install_nvidia_driver = true`.
803+
}
804+
```
805+
806+
It's almost a matter of preference whether to use, but generally `eksctl_nodegroup` is faster to `apply` as it involves
807+
fewer AWS API calls.
808+
766809
### AssumeRole and Cross Account
767810

768811
Providing the `assume_role` block, you can let the provider to call `sts:AssumeRole` for assuming an AWS role

examples/existingvpc/main.tf

Lines changed: 92 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
provider "eksctl" {}
22
provider "helmfile" {}
3+
provider "kubectl" {}
34

45
terraform {
56
required_providers {
@@ -10,7 +11,12 @@ terraform {
1011

1112
helmfile = {
1213
source = "mumoshu/helmfile"
13-
version = "0.12.0"
14+
version = "0.0.1"
15+
}
16+
17+
kubectl = {
18+
source = "mumoshu/kubectl"
19+
version = "0.0.1"
1420
}
1521
}
1622
}
@@ -21,6 +27,9 @@ variable "region" {
2127
description = "AWS region"
2228
}
2329

30+
variable "role_arn" {
31+
}
32+
2433
variable "myip" {
2534

2635
}
@@ -193,12 +202,16 @@ resource "aws_lb_target_group" "tg2" {
193202
}
194203

195204
resource "eksctl_cluster" "red" {
196-
eksctl_bin = "eksctl-dev"
197-
name = "red"
198-
region = "us-east-2"
205+
assume_role {
206+
role_arn = var.role_arn
207+
}
208+
eksctl_bin = "eksctl"
209+
name = "red2"
210+
region = var.region
199211
api_version = "eksctl.io/v1alpha5"
200212
version = "1.16"
201213
vpc_id = module.vpc.vpc_id
214+
kubeconfig_path = "mykubeconfig"
202215
spec = <<EOS
203216
204217
nodeGroups:
@@ -268,35 +281,50 @@ EOS
268281
module.vpc]
269282
}
270283

271-
resource "eksctl_courier_alb" "my_alb_courier" {
272-
listener_arn = aws_alb_listener.podinfo.arn
273-
274-
priority = "11"
275-
276-
step_weight = 10
277-
step_interval = "5s"
278-
279-
hosts = [
280-
"exmaple.com"]
281-
282-
destination {
283-
target_group_arn = aws_lb_target_group.tg1.arn
284-
285-
weight = 100
286-
}
287-
288-
destination {
289-
target_group_arn = aws_lb_target_group.tg2.arn
290-
weight = 0
284+
resource "eksctl_nodegroup" "ng1" {
285+
assume_role {
286+
role_arn = var.role_arn
291287
}
292-
293-
depends_on = [
294-
eksctl_cluster.red,
295-
helmfile_release_set.mystack1
296-
]
288+
name = "ng1"
289+
region = eksctl_cluster.red.region
290+
cluster = eksctl_cluster.red.name
291+
nodes_min = 1
292+
nodes = 1
297293
}
294+
//
295+
//resource "eksctl_courier_alb" "my_alb_courier" {
296+
// listener_arn = aws_alb_listener.podinfo.arn
297+
//
298+
// priority = "11"
299+
//
300+
// step_weight = 10
301+
// step_interval = "5s"
302+
//
303+
// hosts = [
304+
// "exmaple.com"]
305+
//
306+
// destination {
307+
// target_group_arn = aws_lb_target_group.tg1.arn
308+
//
309+
// weight = 100
310+
// }
311+
//
312+
// destination {
313+
// target_group_arn = aws_lb_target_group.tg2.arn
314+
// weight = 0
315+
// }
316+
//
317+
// depends_on = [
318+
// eksctl_cluster.red,
319+
//// helmfile_release_set.mystack1
320+
// ]
321+
//}
298322

299323
resource "helmfile_release_set" "mystack1" {
324+
aws_region = var.region
325+
aws_assume_role {
326+
role_arn = var.role_arn
327+
}
300328
content = file("./helmfile.yaml")
301329
environment = "default"
302330
kubeconfig = eksctl_cluster.red.kubeconfig_path
@@ -305,6 +333,29 @@ resource "helmfile_release_set" "mystack1" {
305333
]
306334
}
307335

336+
resource "kubectl_ensure" "meta" {
337+
aws_region = var.region
338+
aws_assume_role {
339+
role_arn = var.role_arn
340+
}
341+
342+
kubeconfig = eksctl_cluster.red.kubeconfig_path
343+
344+
namespace = "kube-system"
345+
resource = "configmap"
346+
name = "aws-auth"
347+
348+
labels = {
349+
"key1" = "one"
350+
"key2" = "two"
351+
}
352+
353+
annotations = {
354+
"key3" = "three"
355+
"key4" = "four"
356+
}
357+
}
358+
308359
output "kubeconfig_path" {
309360
value = eksctl_cluster.red.kubeconfig_path
310361
}
@@ -344,20 +395,20 @@ output "vpc_cidr_block" {
344395
output "vpc_subnet_groups" {
345396
value = {
346397
"public" = [
347-
for i in range(length(module.vpc.azs)):
348-
{
349-
cidr = module.vpc.public_subnets_cidr_blocks[i],
350-
az = module.vpc.azs[i],
351-
id = module.vpc.public_subnets[i],
352-
}
398+
for i in range(length(module.vpc.azs)):
399+
{
400+
cidr = module.vpc.public_subnets_cidr_blocks[i],
401+
az = module.vpc.azs[i],
402+
id = module.vpc.public_subnets[i],
403+
}
353404
],
354405
"private" = [
355-
for i in range(length(module.vpc.azs)):
356-
{
357-
cidr = module.vpc.private_subnets_cidr_blocks[i],
358-
az = module.vpc.azs[i],
359-
id = module.vpc.private_subnets[i],
360-
}
406+
for i in range(length(module.vpc.azs)):
407+
{
408+
cidr = module.vpc.private_subnets_cidr_blocks[i],
409+
az = module.vpc.azs[i],
410+
id = module.vpc.private_subnets[i],
411+
}
361412
]
362413
}
363414
}

examples/lib/lib.mk

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
WORKSPACE ?= $(shell pwd)
22
HELMFILE_ROOT ?= ../../../terraform-provider-helmfile
3+
KUBECTL_ROOT ?= ../../../terraform-provider-kubectl
34
TERRAFORM ?= terraform
45

56
.PHONY: build
67
build: VER=0.0.1
78
build:
89
mkdir -p .terraform/plugins/darwin_amd64
910
cd ../..; make clean build
10-
cp ../../dist/darwin_amd64/terraform-provider-eksctl $(WORKSPACE)/.terraform/plugins/darwin_amd64/
11-
chmod +x $(WORKSPACE)/.terraform/plugins/darwin_amd64/terraform-provider-eksctl
1211
cd $(HELMFILE_ROOT); make build
12+
cd $(KUBECTL_ROOT); make build
1313
# For terraform up to v0.12
14+
#
15+
# eksctl
16+
cp ../../dist/darwin_amd64/terraform-provider-eksctl $(WORKSPACE)/.terraform/plugins/darwin_amd64/
17+
chmod +x $(WORKSPACE)/.terraform/plugins/darwin_amd64/terraform-provider-eksctl
18+
# helmfile
1419
cp $(HELMFILE_ROOT)/dist/darwin_amd64/terraform-provider-helmfile $(WORKSPACE)/.terraform/plugins/darwin_amd64/
1520
chmod +x $(WORKSPACE)/.terraform/plugins/darwin_amd64/terraform-provider-helmfile
21+
# kubectl
22+
cp $(KUBECTL_ROOT)/dist/darwin_amd64/terraform-provider-kubectl $(WORKSPACE)/.terraform/plugins/darwin_amd64/
23+
chmod +x $(WORKSPACE)/.terraform/plugins/darwin_amd64/terraform-provider-kubectl
1624
# For tereraform v0.13+
25+
#
26+
# eksctl
1727
mkdir -p $(WORKSPACE)/.terraform/plugins/registry.terraform.io/mumoshu/eksctl/$(VER)/darwin_amd64/
1828
cp ../../dist/darwin_amd64/terraform-provider-eksctl $(WORKSPACE)/.terraform/plugins/registry.terraform.io/mumoshu/eksctl/$(VER)/darwin_amd64/terraform-provider-eksctl_v$(VER)
1929
chmod +x $(WORKSPACE)/.terraform/plugins/registry.terraform.io/mumoshu/eksctl/$(VER)/darwin_amd64/terraform-provider-eksctl_v$(VER)
30+
# helmfile
2031
mkdir -p $(WORKSPACE)/.terraform/plugins/registry.terraform.io/mumoshu/helmfile/$(VER)/darwin_amd64/
2132
cp $(HELMFILE_ROOT)/dist/darwin_amd64/terraform-provider-helmfile $(WORKSPACE)/.terraform/plugins/registry.terraform.io/mumoshu/helmfile/$(VER)/darwin_amd64/terraform-provider-helmfile_v$(VER)
2233
chmod +x $(WORKSPACE)/.terraform/plugins/registry.terraform.io/mumoshu/helmfile/$(VER)/darwin_amd64/terraform-provider-helmfile_v$(VER)
34+
# kubectl
35+
mkdir -p $(WORKSPACE)/.terraform/plugins/registry.terraform.io/mumoshu/kubectl/$(VER)/darwin_amd64/
36+
cp $(KUBECTL_ROOT)/dist/darwin_amd64/terraform-provider-kubectl $(WORKSPACE)/.terraform/plugins/registry.terraform.io/mumoshu/kubectl/$(VER)/darwin_amd64/terraform-provider-kubectl_v$(VER)
37+
chmod +x $(WORKSPACE)/.terraform/plugins/registry.terraform.io/mumoshu/kubectl/$(VER)/darwin_amd64/terraform-provider-kubectl_v$(VER)

pkg/provider/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/mumoshu/terraform-provider-eksctl/pkg/resource/cluster"
77
"github.com/mumoshu/terraform-provider-eksctl/pkg/resource/courier"
88
"github.com/mumoshu/terraform-provider-eksctl/pkg/resource/iamserviceaccount"
9+
"github.com/mumoshu/terraform-provider-eksctl/pkg/resource/nodegroup"
910
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
1011
)
1112

@@ -19,6 +20,7 @@ func Provider() terraform.ResourceProvider {
1920
},
2021
ResourcesMap: map[string]*schema.Resource{
2122
"eksctl_cluster": cluster.ResourceCluster(),
23+
"eksctl_nodegroup": nodegroup.Resource(),
2224
"eksctl_iamserviceaccount": iamserviceaccount.Resource(),
2325
"eksctl_courier_alb": courier.ResourceALB(),
2426
"eksctl_courier_route53_record": courier.ResourceRoute53Record(),

pkg/resource/nodegroup/context.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package nodegroup
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk"
6+
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
7+
)
8+
9+
func mustContext(a *schema.ResourceData) *sdk.Context {
10+
config := tfsdk.ConfigFromResourceData(a)
11+
sess, creds := sdk.AWSCredsFromConfig(config)
12+
13+
return &sdk.Context{Sess: sess, Creds: creds}
14+
}

0 commit comments

Comments
 (0)