Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from commitdev/kubernetes
Added kubernetes specs
- Loading branch information
Showing
56 changed files
with
2,087 additions
and
12 deletions.
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.