-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added kubernetes specs #34
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
245aed5
Added kubernetes specs
ShahNewazKhan e45bf63
Removed workstation ip whitelist for eks vpc
ShahNewazKhan 489516b
Added deploy flag functionality to kubernetes
ShahNewazKhan 8c27716
Go formated
ShahNewazKhan 46a3fcd
Moved to eks and vpc community modules | Added awsaccountid & awsregi…
ShahNewazKhan c6aa884
Fixed merge conficts from rebase
ShahNewazKhan c721d7c
Replaced execute stdout & stderr with multiwriter
ShahNewazKhan 016a4b5
Some fixes to k8s generation and tf execution
bmonkman File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"sync" | ||
|
||
"github.com/commitdev/commit0/internal/config" | ||
"github.com/commitdev/commit0/internal/templator" | ||
) | ||
|
||
func Generate(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) { | ||
templator.Kubernetes.TemplateFiles(config, false, wg) | ||
|
||
} | ||
|
||
func Execute(config *config.Commit0Config) { | ||
if config.Kubernetes.Deploy { | ||
log.Println("Planning infrastructure...") | ||
execute(exec.Command("terraform", "init")) | ||
execute(exec.Command("terraform", "plan")) | ||
} | ||
} | ||
|
||
func execute(cmd *exec.Cmd) { | ||
dir, err := os.Getwd() | ||
if err != nil { | ||
log.Fatalf("Getting working directory failed: %v\n", err) | ||
} | ||
|
||
cmd.Dir = fmt.Sprintf("%s/kubernetes/terraform/environments/staging", dir) | ||
|
||
stdoutPipe, _ := cmd.StdoutPipe() | ||
stderrPipe, _ := cmd.StderrPipe() | ||
|
||
var errStdout, errStderr error | ||
err = cmd.Start() | ||
if err != nil { | ||
log.Fatalf("Starting terraform command failed: %v\n", err) | ||
} | ||
|
||
go func() { | ||
_, errStdout = io.Copy(os.Stdout, stdoutPipe) | ||
}() | ||
go func() { | ||
_, errStderr = io.Copy(os.Stderr, stderrPipe) | ||
}() | ||
|
||
err = cmd.Wait() | ||
if err != nil { | ||
log.Fatalf("Executing terraform command failed: %v\n", err) | ||
} | ||
|
||
if errStdout != nil { | ||
log.Printf("Failed to capture stdout: %v\n", errStdout) | ||
} | ||
|
||
if errStderr != nil { | ||
log.Printf("Failed to capture stderr: %v\n", errStderr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
## Guidelines & Style Convention Summary | ||
|
||
- All Terraform configuration should be formatted with `terraform fmt` before being accepted into this repository. | ||
- This repository is Terraform version >= 0.12, as such, leverage features from this release whenever possible. | ||
See https://www.terraform.io/upgrade-guides/0-12.html for more information. | ||
- Leverage community-maintained Terraform modules whenever possible. | ||
- Attempt to minimize duplication whenever possible, but only within reason -- sometimes duplication is an acceptable solution. | ||
- Follow style conventions described in `docs/guide.pdf` whenever possible. | ||
- Whenever possible, inject resources down versus referencing resources across modules. This has been made easier with new features in v0.12. | ||
- Whenever possible, define the types of variables. | ||
|
||
### Module Conventions | ||
|
||
- All modules should contain the following: | ||
|
||
`README.md`: A description of the module. | ||
`main.tf`: Module entrypoint where instantiation of resources happens. | ||
`variables.tf`: Module variables. | ||
`outputs.tf`: Output values (optional). | ||
`files/`: Any / all files required by the module. | ||
|
||
- All module variables must have a description. | ||
- Again, leverage community-maintained Terraform modules whenever possible. | ||
- Avoid writing a module that is simply a wrapper of a Terraform resource unless absolutely necessary. | ||
|
||
### Environment Conventions | ||
|
||
- All environments should contain the following: | ||
|
||
`main.tf`: Toplevel terraform configuration file that instantiates the `environment` module. | ||
|
||
- Configuration should be pushed "top->down" from the `environment` module to it's submodules. | ||
|
||
### The Environment Module | ||
|
||
- The `environment` module can be considered the top-level module, all other modules are imported from this module. | ||
- Environment-specific variables should be exposed via the `variables.tf` file in this module, where they will be set from within the appropriate environment in the `environments/` directory. | ||
- The `environment` module contains the following: | ||
|
||
`main.tf`: Module entrypoint where instantiation of resources happens. | ||
`backend.tf`: Terraform remote state configuration. | ||
`provider.tf`: Provider configuration. | ||
`variables.tf`: Environment-specific variables are desclared here. | ||
`versions.tf`: Terraform version information. | ||
`files/`: (DEPRECATED) | ||
|
||
## Directory Structure | ||
|
||
``` | ||
README.md | ||
environments/ | ||
production/ | ||
main.tf | ||
staging/ | ||
main.tf | ||
development/ | ||
main.tf | ||
docs/ | ||
guide.pdf | ||
modules/ | ||
environment/ | ||
... | ||
<module-a>/ | ||
files/ | ||
scripts/ | ||
main.tf | ||
outputs.tf | ||
variables.tf | ||
<module-n>/ | ||
... | ||
``` | ||
|
||
## AWS Guidelines | ||
|
||
- TODO: Identity/Access Management (IAM) Guidelines | ||
|
||
## Kubernetes Guidelines | ||
|
||
- When to use the Terraform Kuberenetes Provider and when to use manifests? | ||
|
||
- Use the Terraform Kubernetes Provider (`provider "kubernetes"`) whenever you are provisioning a resource that could be considered relatively static (think Ingress, RoleBinding, CluterRoleBinding, etc). | ||
|
||
- Use conventional Kubernetes manifests / `kubectl` when provisioning resouirces that could be considered dynamic (think Deployments). | ||
|
||
## Application | ||
|
||
1. Set up a profile for your project with your credentials in a specific profile in `~/.aws/credentials` and then export the following env var: | ||
`export AWS_PROFILE=<project_name>` | ||
|
||
2. Run the following from the appropriate environment directory under `environments/`: | ||
|
||
``` | ||
environment/development$ terraform init | ||
environment/development$ terraform plan | ||
``` |
22 changes: 22 additions & 0 deletions
22
templates/kubernetes/terraform/environments/development/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Instantiate the development environment | ||
module "development" { | ||
source = "../../modules/environment" | ||
environment = "development" | ||
|
||
# Project configuration | ||
project = "{{ .Kubernetes.ClusterName }}" | ||
region = "{{ .Kubernetes.AWSRegion }}" | ||
allowed_account_ids = ["{{ .Kubernetes.AWSAccountId }}"] | ||
|
||
# ECR configuration | ||
ecr_repositories = ["{{ .Kubernetes.ClusterName }}"] | ||
|
||
# EKS configuration | ||
eks_worker_instance_type = "t2.small" | ||
eks_worker_asg_max_size = 2 | ||
|
||
# EKS-Optimized AMI for your region: https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html | ||
# https://us-east-1.console.aws.amazon.com/systems-manager/parameters/%252Faws%252Fservice%252Feks%252Foptimized-ami%252F1.14%252Famazon-linux-2%252Frecommended%252Fimage_id/description?region=us-east-1 | ||
eks_worker_ami = "ami-0392bafc801b7520f" | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
templates/kubernetes/terraform/environments/production/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Instantiate the production environment | ||
module "production" { | ||
source = "../../modules/environment" | ||
environment = "production" | ||
|
||
# Project configuration | ||
project = "{{ .Kubernetes.ClusterName }}" | ||
region = "{{ .Kubernetes.AWSRegion }}" | ||
allowed_account_ids = ["{{ .Kubernetes.AWSAccountId }}"] | ||
|
||
# ECR configuration | ||
ecr_repositories = ["{{ .Kubernetes.ClusterName }}"] | ||
|
||
# EKS configuration | ||
eks_worker_instance_type = "m4.large" | ||
eks_worker_asg_max_size = 3 | ||
|
||
# EKS-Optimized AMI for your region: https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html | ||
# https://us-east-1.console.aws.amazon.com/systems-manager/parameters/%252Faws%252Fservice%252Feks%252Foptimized-ami%252F1.14%252Famazon-linux-2%252Frecommended%252Fimage_id/description?region=us-east-1 | ||
eks_worker_ami = "ami-0392bafc801b7520f" | ||
} |
21 changes: 21 additions & 0 deletions
21
templates/kubernetes/terraform/environments/staging/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Instantiate the staging environment | ||
module "staging" { | ||
source = "../../modules/environment" | ||
environment = "staging" | ||
|
||
# Project configuration | ||
project = "{{ .Kubernetes.ClusterName }}" | ||
region = "{{ .Kubernetes.AWSRegion }}" | ||
allowed_account_ids = ["{{ .Kubernetes.AWSAccountId }}"] | ||
|
||
# ECR configuration | ||
ecr_repositories = ["{{ .Kubernetes.ClusterName }}"] | ||
|
||
# EKS configuration | ||
eks_worker_instance_type = "t2.small" | ||
eks_worker_asg_max_size = 2 | ||
# EKS-Optimized AMI for your region: https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html | ||
# https://us-east-1.console.aws.amazon.com/systems-manager/parameters/%252Faws%252Fservice%252Feks%252Foptimized-ami%252F1.14%252Famazon-linux-2%252Frecommended%252Fimage_id/description?region=us-east-1 | ||
eks_worker_ami = "ami-0392bafc801b7520f" | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
resource "aws_ecr_repository" "ecr_repository" { | ||
count = length(var.ecr_repositories) | ||
name = element(var.ecr_repositories, count.index) | ||
|
||
tags = { | ||
environment = var.environment | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
variable "environment" { | ||
description = "The environment (dev/staging/prod)" | ||
} | ||
|
||
variable "ecr_repositories" { | ||
description = "List of ECR repository names to create" | ||
type = list(string) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
terraform { | ||
required_version = ">= 0.12" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be called something else rather than "languages" if it's the right place for this. Or maybe we should start splitting this into sections.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, rename it modules? @Pritesh-Patel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't worry about this for now, I'll do some refactoring.