Skip to content

Commit 25e9573

Browse files
Initial open source commit
1 parent f03c6e0 commit 25e9573

17 files changed

+987
-0
lines changed

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# .tfvars files
9+
*.tfvars
10+
11+
*.sw*
12+
13+
# Generated files
14+
builds/
15+
16+
17+
#intellij idea files
18+
.idea/
19+
20+
# directory with files to ignore
21+
ignored
22+
.plan
23+
changes.exist

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- repo: git://github.com/antonbabenko/pre-commit-terraform
2+
rev: v1.16.0
3+
hooks:
4+
- id: terraform_fmt
5+
- id: terraform_docs

CODE_OF_CONDUCT.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, sex characteristics, gender identity and expression,
9+
level of experience, education, socio-economic status, nationality, personal
10+
appearance, race, religion, or sexual identity and orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at rtyler@scribd.com. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72+
73+
[homepage]: https://www.contributor-covenant.org
74+
75+
For answers to common questions about this code of conduct, see
76+
https://www.contributor-covenant.org/faq

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Scribd, Inc
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,75 @@
11
# terraform-aws-recycle-eks
2+
3+
This module creates a terraform module to recycle EKS worker nodes. The high level functionalities are explained below,
4+
- Use a lamdba to take an instance id as an input, to put it in standby state. Using autoscaling api to automatically add a new instance to the group while putting the old instance to standby state. The old instance will get into "Standby" state only when the new instance is in fully "Inservice" state
5+
- Taint this "Standby" node in EKS using K8S API in Lambda to prevent new pods from getting scheduled into this node
6+
- Periodically use K8S API check for status of “stateful” pods on that node based on the label selector provided. Another Lambda will do that
7+
- Once all stateful pods have completed on the node, use K8S API in another Lambda to drain the standby node
8+
- Once the number of running pod reached 0, shut down that standby instance using AWS SDK.
9+
- We are not termnating the node, only shutting it down, hust in case. In future releases, we will be start terminating the nodes
10+
11+
## TODO:
12+
- Check for new node in service before proceeding to put the existing node in standby state. Right now we are putting a sleep of 300 sec.
13+
- Stop using anonymous role and find a way to map the role with a proper user
14+
- get_bearer_token() function used in all lambda. Refactor the code to use as a Python module.
15+
- Better logging and exception handling
16+
17+
There are two main components:
18+
19+
1. Lambdas
20+
2. Step Function in AWS, to chain the Lambdas and pass on the parameters form one Lamda to another Lamda
21+
22+
23+
## Usage
24+
25+
**Set up all supported AWS / Datadog integrations**
26+
27+
```
28+
module "recycl-eks-worker-npde" {
29+
source = "git::git@github.com:scribd/terraform-aws-recycle-eks.git"
30+
name = "string"
31+
tags = {
32+
Environment = "dev"
33+
Terraform = "true"
34+
}
35+
vpc_subnet_ids = ["subnet-12345678", "subnet-87654321"]
36+
vpc_security_group_ids = ["sg-12345678"]
37+
aws_region = "us-east-2"
38+
39+
}
40+
```
41+
42+
## Running of step function
43+
44+
```
45+
Step function takes an json input
46+
47+
{
48+
"instance_id": "i-1234567890",
49+
"cluster_name": "eks-cluster-name-where-the-instance-belongs-to",
50+
"label_selector":"airflow_version=1.2.3,airflow-worker" #you can put a comma separated value for labels, either key=value or only key
51+
}
52+
This label selector will be the identifier on which the step function will wait and rest all pods will be ignored.
53+
54+
```
55+
56+
## Development
57+
58+
Releases are cut using [semantic-release](https://github.com/semantic-release/semantic-release).
59+
60+
Please write commit messages following [Angular commit guidelines](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines)
61+
62+
63+
### Release flow
64+
65+
Semantic-release is configured with the [default branch workflow](https://semantic-release.gitbook.io/semantic-release/usage/configuration#branches)
66+
67+
For this project, releases will be cut from master as features and bugs are developed.
68+
69+
70+
### Maintainers
71+
- [Kuntal](https://github.com/kuntalkumarbasu)
72+
73+
### Reference
74+
- There is an excellent module on [Gracefully drain Kubernetes pods from EKS worker nodes during autoscaling scale-in events](https://github.com/aws-samples/amazon-k8s-node-drainer). We followed some of the principles in the Lambdas.
75+
- [Orchestrating Amazon Kubernetes Service (EKS) from AWS Lambda](https://medium.com/@alejandro.millan.frias/managing-kubernetes-from-aws-lambda-7922c3546249) is another writeup that we referrenced while connecting to EKS from Lambda

iam.tf

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# defining aws roles and policies for a lambda function
2+
3+
data "aws_iam_policy_document" "lambda-assume-role" {
4+
statement {
5+
actions = ["sts:AssumeRole"]
6+
7+
principals {
8+
type = "Service"
9+
identifiers = ["lambda.amazonaws.com"]
10+
}
11+
}
12+
}
13+
resource "aws_iam_policy" "lambda_eks_policy" {
14+
name = "lambda_eks_policy"
15+
path = "/"
16+
description = "Policy to provide permissions to lambda functions"
17+
18+
policy = <<EOF
19+
{
20+
"Version": "2012-10-17",
21+
"Statement": [
22+
{
23+
"Action": [
24+
"sts:GetCallerIdentity",
25+
"eks:DescribeCluster",
26+
"autoscaling:*",
27+
"ec2:TerminateInstances",
28+
"ec2:StopInstances"
29+
],
30+
"Effect": "Allow",
31+
"Resource": "*"
32+
}
33+
]
34+
}
35+
EOF
36+
}
37+
38+
resource "aws_iam_role_policy_attachment" "lambda_eks_policy" {
39+
role = aws_iam_role.lambda-exec.name
40+
policy_arn = aws_iam_policy.lambda_eks_policy.arn
41+
}
42+
43+
resource "aws_iam_role" "lambda-exec" {
44+
name = "${var.name}-lambda-exec-role"
45+
tags = var.tags
46+
max_session_duration = 43200
47+
assume_role_policy = data.aws_iam_policy_document.lambda-assume-role.json
48+
}
49+
50+
resource "aws_iam_role_policy_attachment" "lambda-exec" {
51+
role = aws_iam_role.lambda-exec.name
52+
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
53+
}
54+
55+
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSClusterPolicy" {
56+
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
57+
role = aws_iam_role.lambda-exec.name
58+
}
59+
60+
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSServicePolicy" {
61+
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy"
62+
role = aws_iam_role.lambda-exec.name
63+
}
64+
65+
66+
# defining aws roles and policies for a step functions state machine
67+
68+
data "aws_iam_policy_document" "sfn-assume-role" {
69+
statement {
70+
actions = ["sts:AssumeRole"]
71+
72+
principals {
73+
type = "Service"
74+
identifiers = ["states.${var.aws_region}.amazonaws.com"]
75+
}
76+
}
77+
}
78+
79+
resource "aws_iam_role" "sfn-exec" {
80+
name = "${var.name}-sfn-exec"
81+
tags = var.tags
82+
max_session_duration = 43200
83+
assume_role_policy = data.aws_iam_policy_document.sfn-assume-role.json
84+
}
85+
86+
data "aws_iam_policy_document" "lambda-invoke" {
87+
statement {
88+
actions = [
89+
"lambda:InvokeFunction"
90+
]
91+
resources = [
92+
"*",
93+
]
94+
}
95+
}
96+
97+
resource "aws_iam_policy" "lambda-invoke" {
98+
name = "${var.name}-lambda-invoke"
99+
policy = data.aws_iam_policy_document.lambda-invoke.json
100+
}
101+
102+
resource "aws_iam_role_policy_attachment" "lambda-invoke" {
103+
role = aws_iam_role.sfn-exec.name
104+
policy_arn = aws_iam_policy.lambda-invoke.arn
105+
}
106+
107+
108+
resource "kubernetes_cluster_role" "lambda-access" {
109+
metadata {
110+
name = "lambda-access"
111+
}
112+
113+
rule {
114+
api_groups = [""]
115+
resources = ["nodes", "pods"]
116+
verbs = ["get", "list", "watch", "patch"]
117+
}
118+
}
119+
120+
resource "kubernetes_cluster_role_binding" "lambda-user-role-binding" {
121+
metadata {
122+
name = "lambda-user-role-binding"
123+
}
124+
role_ref {
125+
api_group = "rbac.authorization.k8s.io"
126+
kind = "ClusterRole"
127+
name = "lambda-access"
128+
}
129+
subject {
130+
kind = "User"
131+
name = "system:anonymous"
132+
api_group = "rbac.authorization.k8s.io"
133+
namespace = "kube-system"
134+
}
135+
}

0 commit comments

Comments
 (0)