Skip to content

Commit f2e07ef

Browse files
authored
Merge pull request #67 from commitdev/cleanup-terraform-and-fix-backends
Cleaned up terraform and fixed backends
2 parents 367da9b + 9ee7335 commit f2e07ef

File tree

18 files changed

+104
-53
lines changed

18 files changed

+104
-53
lines changed

cmd/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ var generateCmd = &cobra.Command{
7777
react.Generate(t, cfg, &wg)
7878
}
7979

80-
util.TemplateFileIfDoesNotExist("", "README.md", t.Readme, &wg, cfg)
80+
util.TemplateFileIfDoesNotExist("", "README.md", t.Readme, &wg, templator.GenericTemplateData{*cfg})
8181

8282
// Wait for all the templates to be generated
8383
wg.Wait()

internal/generate/kubernetes/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
"github.com/commitdev/commit0/internal/templator"
1313
)
1414

15-
func Generate(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
16-
templator.Kubernetes.TemplateFiles(config, false, wg)
17-
15+
func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGroup) {
16+
data := templator.GenericTemplateData{*cfg}
17+
t.Kubernetes.TemplateFiles(data, false, wg)
1818
}
1919

2020
func Execute(config *config.Commit0Config) {

internal/generate/react/generate.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"github.com/commitdev/commit0/internal/templator"
99
)
1010

11-
func Generate(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
12-
templator.React.TemplateFiles(config, false, wg)
13-
if config.Frontend.CI.System != "" {
14-
ci.Generate(templator.CI, config, config.Frontend.CI, "react/", wg)
11+
func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGroup) {
12+
data := templator.GenericTemplateData{*cfg}
13+
14+
t.React.TemplateFiles(data, false, wg)
15+
if cfg.Frontend.CI.System != "" {
16+
ci.Generate(t.CI, cfg, cfg.Frontend.CI, "react/", wg)
1517
}
1618
}

internal/templator/template_data.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ package templator
22

33
import "github.com/commitdev/commit0/internal/config"
44

5+
// GenericTemplateData holds data for use in any template, it just contains the config struct
6+
type GenericTemplateData struct {
7+
Config config.Commit0Config
8+
}
9+
510
// GolangTemplateData holds data for use in golang related templates
611
type GolangTemplateData struct {
712
Config config.Commit0Config

internal/templator/templator.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"sync"
77
"text/template"
88

9-
"github.com/commitdev/commit0/internal/config"
109
"github.com/commitdev/commit0/internal/util"
1110
"github.com/gobuffalo/packr/v2"
1211
"github.com/gobuffalo/packr/v2/file"
@@ -126,16 +125,16 @@ type DirectoryTemplator struct {
126125
Templates []*template.Template
127126
}
128127

129-
func (d *DirectoryTemplator) TemplateFiles(config *config.Commit0Config, overwrite bool, wg *sync.WaitGroup) {
128+
func (d *DirectoryTemplator) TemplateFiles(data interface{}, overwrite bool, wg *sync.WaitGroup) {
130129
for _, template := range d.Templates {
131130
d, f := filepath.Split(template.Name())
132131
if strings.HasSuffix(f, ".tmpl") {
133132
f = strings.Replace(f, ".tmpl", "", -1)
134133
}
135134
if overwrite {
136-
util.TemplateFileAndOverwrite(d, f, template, wg, config)
135+
util.TemplateFileAndOverwrite(d, f, template, wg, data)
137136
} else {
138-
util.TemplateFileIfDoesNotExist(d, f, template, wg, config)
137+
util.TemplateFileIfDoesNotExist(d, f, template, wg, data)
139138
}
140139
}
141140
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
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 = "terraform-state-locks"
8+
}
9+
}
10+
111
# Instantiate the development environment
212
module "development" {
313
source = "../../modules/environment"
414
environment = "development"
515

616
# Project configuration
7-
project = "{{ .Infrastructure.AWS.EKS.ClusterName }}"
8-
region = "{{ .Infrastructure.AWS.Region }}"
9-
allowed_account_ids = ["{{ .Infrastructure.AWS.AccountId }}"]
17+
project = "{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"
18+
region = "{{ .Config.Infrastructure.AWS.Region }}"
19+
allowed_account_ids = ["{{ .Config.Infrastructure.AWS.AccountId }}"]
1020

1121
# ECR configuration
12-
ecr_repositories = ["{{ .Infrastructure.AWS.EKS.ClusterName }}"]
22+
ecr_repositories = ["{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"]
1323

1424
# EKS configuration
1525
eks_worker_instance_type = "t2.small"

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
terraform {
2+
backend "s3" {
3+
bucket = "project-{{ .Config.Name }}-terraform-state"
4+
key = "infrastructure/terraform/environments/production/main"
5+
encrypt = true
6+
region = "{{ .Config.Infrastructure.AWS.Region }}"
7+
dynamodb_table = "terraform-state-locks"
8+
}
9+
}
10+
111
# Instantiate the production environment
212
module "production" {
313
source = "../../modules/environment"
414
environment = "production"
515

616
# Project configuration
7-
project = "{{ .Infrastructure.AWS.EKS.ClusterName }}"
8-
region = "{{ .Infrastructure.AWS.Region }}"
9-
allowed_account_ids = ["{{ .Infrastructure.AWS.AccountId }}"]
17+
project = "{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"
18+
region = "{{ .Config.Infrastructure.AWS.Region }}"
19+
allowed_account_ids = ["{{ .Config.Infrastructure.AWS.AccountId }}"]
1020

1121
# ECR configuration
12-
ecr_repositories = ["{{ .Infrastructure.AWS.EKS.ClusterName }}"]
22+
ecr_repositories = ["{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"]
1323

1424
# EKS configuration
1525
eks_worker_instance_type = "m4.large"

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
terraform {
2+
backend "s3" {
3+
bucket = "project-{{ .Config.Name }}-terraform-state"
4+
key = "infrastructure/terraform/environments/staging/main"
5+
encrypt = true
6+
region = "{{ .Config.Infrastructure.AWS.Region }}"
7+
dynamodb_table = "terraform-state-locks"
8+
}
9+
}
10+
111
# Instantiate the staging environment
212
module "staging" {
313
source = "../../modules/environment"
414
environment = "staging"
515

616
# Project configuration
7-
project = "{{ .Infrastructure.AWS.EKS.ClusterName }}"
8-
region = "{{ .Infrastructure.AWS.Region }}"
9-
allowed_account_ids = ["{{ .Infrastructure.AWS.AccountId }}"]
17+
project = "{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"
18+
region = "{{ .Config.Infrastructure.AWS.Region }}"
19+
allowed_account_ids = ["{{ .Config.Infrastructure.AWS.AccountId }}"]
1020

1121
# ECR configuration
12-
ecr_repositories = ["{{ .Infrastructure.AWS.EKS.ClusterName }}"]
22+
ecr_repositories = ["{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"]
1323

1424
# EKS configuration
1525
eks_worker_instance_type = "t2.small"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
provider "aws" {
2+
region = "{{ .Config.Infrastructure.AWS.Region }}"
3+
}
4+
5+
resource "aws_s3_bucket" "terraform_remote_state" {
6+
bucket = "project-{{ .Config.Name }}-terraform-state"
7+
acl = "private"
8+
9+
versioning {
10+
enabled = true
11+
}
12+
}
13+
14+
resource "aws_dynamodb_table" "terraform_state_locks" {
15+
name = "{{ .Config.Name }}-terraform-state-locks"
16+
read_capacity = 2
17+
write_capacity = 2
18+
hash_key = "LockID"
19+
20+
attribute {
21+
name = "LockID"
22+
type = "S"
23+
}
24+
}

templates/kubernetes/terraform/modules/eks/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Create KubernetesAdmin role for aws-iam-authenticator
22
resource "aws_iam_role" "kubernetes_admin_role" {
3-
name = "kubernetes-admin"
3+
name = "{{ .Config.Name }}-kubernetes-admin"
44
assume_role_policy = var.assume_role_policy
55
description = "Kubernetes administrator role (for AWS IAM Authenticator)"
66
}

0 commit comments

Comments
 (0)