Skip to content

Commit a310baa

Browse files
committed
Move kuberenetes stuff out into separate runs, fix backend, fix some issues with generated code
1 parent c0e62fd commit a310baa

File tree

16 files changed

+198
-50
lines changed

16 files changed

+198
-50
lines changed

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type terraform struct {
8787

8888
type eks struct {
8989
ClusterName string `yaml:"clusterName"`
90+
WorkerAMI string `yaml:"workerAMI"`
9091
Deploy bool
9192
}
9293

internal/generate/golang/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func Generate(t *templator.Templator, cfg *config.Commit0Config, service config.
2424

2525
util.TemplateFileIfDoesNotExist(basePath, "main.go", t.Go.GoMain, wg, data)
2626
util.TemplateFileIfDoesNotExist(basePath, "go.mod", t.Go.GoMod, wg, data)
27+
util.TemplateFileIfDoesNotExist(basePath, "server.go", t.Go.GoServer, wg, data)
2728
util.TemplateFileIfDoesNotExist(healthPath, "health.go", t.Go.GoHealthServer, wg, data)
2829

2930
file := fmt.Sprintf("%s.go", service.Name)

internal/generate/http/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ import (
1010

1111
func GenerateGoHTTPGW(templator *templator.Templator, data templator.GolangTemplateData, basePath string, wg *sync.WaitGroup) {
1212
path := filepath.Join(basePath, "http")
13-
util.TemplateFileAndOverwrite(path, "main.go", templator.Go.GoHTTPGW, wg, data)
13+
util.TemplateFileIfDoesNotExist(path, "main.go", templator.Go.GoHTTPGW, wg, data)
1414
}

internal/generate/kubernetes/generate.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/commitdev/commit0/internal/config"
1717
"github.com/commitdev/commit0/internal/templator"
1818
"github.com/commitdev/commit0/internal/util"
19+
"github.com/kyokomi/emoji"
20+
"github.com/logrusorgru/aurora"
1921
"github.com/manifoldco/promptui"
2022
"gopkg.in/yaml.v2"
2123
)
@@ -29,9 +31,26 @@ type Secrets struct {
2931
}
3032
}
3133

34+
// @TODO : These are specific to a k8s version. If we make the version a config option we will need to change this
35+
var amiLookup = map[string]string{
36+
"us-east-1": "ami-0392bafc801b7520f",
37+
"us-east-2": "ami-082bb518441d3954c",
38+
"us-west-2": "ami-05d586e6f773f6abf",
39+
"eu-west-1": "ami-059c6874350e63ca9",
40+
"eu-central-1": "ami-0e21bc066a9dbabfa",
41+
}
42+
3243
// Generate templates
3344
func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGroup, pathPrefix string) {
34-
data := templator.GenericTemplateData{*cfg}
45+
if cfg.Infrastructure.AWS.EKS.WorkerAMI == "" {
46+
ami, found := amiLookup[cfg.Infrastructure.AWS.Region]
47+
if !found {
48+
log.Fatalln(aurora.Red(emoji.Sprintf(":exclamation: Unable to look up an AMI for the chosen region")))
49+
}
50+
51+
cfg.Infrastructure.AWS.EKS.WorkerAMI = ami
52+
}
53+
data := templator.GenericTemplateData{Config: *cfg}
3554
t.Kubernetes.TemplateFiles(data, false, wg, pathPrefix)
3655
}
3756

@@ -50,17 +69,31 @@ func Execute(config *config.Commit0Config, pathPrefix string) {
5069
}
5170

5271
envars := getAwsEnvars(readSecrets())
53-
log.Println("Planning infrastructure...")
54-
execute(exec.Command("terraform", "init"), pathPrefix, envars)
55-
execute(exec.Command("terraform", "plan"), pathPrefix, envars)
72+
73+
pathPrefix = filepath.Join(pathPrefix, "kubernetes/terraform")
74+
75+
// @TODO : A check here would be nice to see if this stuff exists first, mostly for testing
76+
log.Println(aurora.Cyan(emoji.Sprintf(":alarm_clock: Initializing remote backend...")))
77+
execute(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "bootstrap/remote-state"), envars)
78+
execute(exec.Command("terraform", "apply", "-auto-approve"), filepath.Join(pathPrefix, "bootstrap/remote-state"), envars)
79+
80+
log.Println(aurora.Cyan(":alarm_clock: Planning infrastructure..."))
81+
execute(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "environments/staging"), envars)
82+
execute(exec.Command("terraform", "plan"), filepath.Join(pathPrefix, "environments/staging"), envars)
83+
84+
log.Println(aurora.Cyan(":alarm_clock: Applying infrastructure configuration..."))
85+
execute(exec.Command("terraform", "apply"), filepath.Join(pathPrefix, "environments/staging"), envars)
86+
87+
log.Println(aurora.Cyan(":alarm_clock: Applying kubernetes configuration..."))
88+
execute(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "environments/staging/kubernetes"), envars)
89+
execute(exec.Command("terraform", "plan"), filepath.Join(pathPrefix, "environments/staging/kubernetes"), envars)
5690
}
5791
}
5892

5993
func execute(cmd *exec.Cmd, pathPrefix string, envars []string) {
6094
dir := util.GetCwd()
6195

62-
kubDir := path.Join(pathPrefix, "kubernetes/terraform/environments/staging")
63-
cmd.Dir = path.Join(dir, kubDir)
96+
cmd.Dir = path.Join(dir, pathPrefix)
6497

6598
stdoutPipe, _ := cmd.StdoutPipe()
6699
stderrPipe, _ := cmd.StderrPipe()

internal/util/util.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ func CreateDirIfDoesNotExist(path string) error {
2121
return nil
2222
}
2323

24+
func CleanGoIdentifier(identifier string) string {
25+
return strings.ReplaceAll(identifier, "-", "")
26+
}
27+
2428
var FuncMap = template.FuncMap{
25-
"Title": strings.Title,
26-
"ToLower": strings.ToLower,
29+
"Title": strings.Title,
30+
"ToLower": strings.ToLower,
31+
"CleanGoIdentifier": CleanGoIdentifier,
2732
}
2833

2934
func GetCwd() string {

templates/golang/main.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func main() {
2222
//Server initialization & registration
2323
healthServer := health.NewHealthServer()
2424
healthpb.RegisterHealthServer(s, healthServer)
25-
{{ .Config.Name }}Server := {{ .Config.Name }}.New{{ .Config.Name | Title}}Server()
26-
{{ .Config.Name }}pb.Register{{ .Config.Name | Title}}Server(s, {{ .Config.Name }}Server)
25+
{{ .Config.Name | CleanGoIdentifier }}Server := {{ .Config.Name | CleanGoIdentifier }}.New{{ .Config.Name | Title | CleanGoIdentifier}}Server()
26+
{{ .Config.Name | CleanGoIdentifier }}pb.Register{{ .Config.Name | Title | CleanGoIdentifier}}Server(s, {{ .Config.Name | CleanGoIdentifier }}Server)
2727

2828

2929
log.Printf("Starting grpc server on %v...", grpcAddr)

templates/golang/server.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ type {{ .Service.Name | Title }}Server struct {
1010

1111
}
1212

13-
func New{{ .Service.Name | Title }}Server() *{{ .Service.Name | Title }}Server {
14-
return &{{ .Service.Name | Title }}Server{}
13+
func New{{ .Service.Name | Title | CleanGoIdentifier }}Server() *{{ .Service.Name | Title }}Server {
14+
return &{{ .Service.Name | Title | CleanGoIdentifier }}Server{}
1515
}
1616

17-
func (s *{{ .Service.Name | Title }}Server) Check(ctx context.Context, req *health_api.HealthCheckRequest) (*health_api.HealthCheckResponse, error) {
17+
func (s *{{ .Service.Name | Title | CleanGoIdentifier }}Server) Check(ctx context.Context, req *health_api.HealthCheckRequest) (*health_api.HealthCheckResponse, error) {
1818
resp := &health_api.HealthCheckResponse{
1919
Status: health_api.HealthCheckResponse_SERVING,
2020
}

templates/kubernetes/terraform/global/remote-state/main.tf renamed to templates/kubernetes/terraform/bootstrap/remote-state/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ resource "aws_s3_bucket" "terraform_remote_state" {
1111
}
1212
}
1313

14+
resource "aws_s3_bucket_public_access_block" "terraform_remote_state" {
15+
bucket = "${aws_s3_bucket.terraform_remote_state.id}"
16+
17+
18+
block_public_acls = true
19+
block_public_policy = true
20+
ignore_public_acls = true
21+
restrict_public_buckets = true
22+
}
23+
1424
resource "aws_dynamodb_table" "terraform_state_locks" {
1525
name = "{{ .Config.Name }}-terraform-state-locks"
1626
read_capacity = 2
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
terraform {
2+
backend "s3" {
3+
bucket = "project-{{ .Config.Name }}-terraform-state"
4+
key = "infrastructure/terraform/environments/development/main"
5+
encrypt = true
6+
region = "{{ .Config.Infrastructure.AWS.Region }}"
7+
dynamodb_table = "{{ .Config.Name }}-terraform-state-locks"
8+
}
9+
}
10+
11+
# Instantiate the development environment
12+
module "development" {
13+
source = "../../../modules/environment"
14+
environment = "development"
15+
16+
# Project configuration
17+
project = "{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"
18+
region = "{{ .Config.Infrastructure.AWS.Region }}"
19+
allowed_account_ids = ["{{ .Config.Infrastructure.AWS.AccountId }}"]
20+
21+
# ECR configuration
22+
ecr_repositories = ["{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"]
23+
24+
# EKS configuration
25+
eks_worker_instance_type = "t2.small"
26+
eks_worker_asg_max_size = 2
27+
28+
# EKS-Optimized AMI for your region: https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html
29+
# 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
30+
eks_worker_ami = "{{ .Config.Infrastructure.AWS.EKS.WorkerAMI }}"
31+
32+
}

templates/kubernetes/terraform/environments/development/main.tf

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,34 @@ terraform {
44
key = "infrastructure/terraform/environments/development/main"
55
encrypt = true
66
region = "{{ .Config.Infrastructure.AWS.Region }}"
7-
dynamodb_table = "terraform-state-locks"
7+
dynamodb_table = "{{ .Config.Name }}-terraform-state-locks"
88
}
99
}
1010

11-
# Instantiate the development environment
12-
module "development" {
13-
source = "../../modules/environment"
11+
# Provision kubernetes resources required to run services/applications
12+
module "kubernetes" {
13+
source = "../../modules/kubernetes"
14+
1415
environment = "development"
16+
region = "{{ .Config.Infrastructure.AWS.Region }}"
1517

16-
# Project configuration
17-
project = "{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"
18-
region = "{{ .Config.Infrastructure.AWS.Region }}"
19-
allowed_account_ids = ["{{ .Config.Infrastructure.AWS.AccountId }}"]
18+
# Authenticate with the EKS cluster via the cluster id
19+
cluster_name = "{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"
2020

21-
# ECR configuration
22-
ecr_repositories = ["{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"]
21+
# Assume-role policy used by monitoring fluentd daemonset
22+
assume_role_policy = data.aws_iam_policy_document.assumerole_root_policy.json
23+
}
2324

24-
# EKS configuration
25-
eks_worker_instance_type = "t2.small"
26-
eks_worker_asg_max_size = 2
25+
# Data sources for EKS IAM
26+
data "aws_caller_identity" "current" {}
2727

28-
# EKS-Optimized AMI for your region: https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html
29-
# 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
30-
eks_worker_ami = "ami-0392bafc801b7520f"
28+
data "aws_iam_policy_document" "assumerole_root_policy" {
29+
statement {
30+
actions = ["sts:AssumeRole"]
3131

32+
principals {
33+
type = "AWS"
34+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
35+
}
36+
}
3237
}

0 commit comments

Comments
 (0)