Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var generateCmd = &cobra.Command{
react.Generate(t, cfg, &wg)
}

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

// Wait for all the templates to be generated
wg.Wait()
Expand Down
6 changes: 3 additions & 3 deletions internal/generate/kubernetes/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/commitdev/commit0/internal/templator"
)

func Generate(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
templator.Kubernetes.TemplateFiles(config, false, wg)

func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGroup) {
data := templator.GenericTemplateData{*cfg}
t.Kubernetes.TemplateFiles(data, false, wg)
}

func Execute(config *config.Commit0Config) {
Expand Down
10 changes: 6 additions & 4 deletions internal/generate/react/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"github.com/commitdev/commit0/internal/templator"
)

func Generate(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
templator.React.TemplateFiles(config, false, wg)
if config.Frontend.CI.System != "" {
ci.Generate(templator.CI, config, config.Frontend.CI, "react/", wg)
func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGroup) {
data := templator.GenericTemplateData{*cfg}

t.React.TemplateFiles(data, false, wg)
if cfg.Frontend.CI.System != "" {
ci.Generate(t.CI, cfg, cfg.Frontend.CI, "react/", wg)
}
}
5 changes: 5 additions & 0 deletions internal/templator/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package templator

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

// GenericTemplateData holds data for use in any template, it just contains the config struct
type GenericTemplateData struct {
Config config.Commit0Config
}

// GolangTemplateData holds data for use in golang related templates
type GolangTemplateData struct {
Config config.Commit0Config
Expand Down
7 changes: 3 additions & 4 deletions internal/templator/templator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sync"
"text/template"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/util"
"github.com/gobuffalo/packr/v2"
"github.com/gobuffalo/packr/v2/file"
Expand Down Expand Up @@ -126,16 +125,16 @@ type DirectoryTemplator struct {
Templates []*template.Template
}

func (d *DirectoryTemplator) TemplateFiles(config *config.Commit0Config, overwrite bool, wg *sync.WaitGroup) {
func (d *DirectoryTemplator) TemplateFiles(data interface{}, overwrite bool, wg *sync.WaitGroup) {
for _, template := range d.Templates {
d, f := filepath.Split(template.Name())
if strings.HasSuffix(f, ".tmpl") {
f = strings.Replace(f, ".tmpl", "", -1)
}
if overwrite {
util.TemplateFileAndOverwrite(d, f, template, wg, config)
util.TemplateFileAndOverwrite(d, f, template, wg, data)
} else {
util.TemplateFileIfDoesNotExist(d, f, template, wg, config)
util.TemplateFileIfDoesNotExist(d, f, template, wg, data)
}
}
}
Expand Down
18 changes: 14 additions & 4 deletions templates/kubernetes/terraform/environments/development/main.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
terraform {
backend "s3" {
bucket = "project-{{ .Config.Name }}-terraform-state"
key = "infrastructure/terraform/environments/development/main"
encrypt = true
region = "{{ .Config.Infrastructure.AWS.Region }}"
dynamodb_table = "terraform-state-locks"
}
}

# Instantiate the development environment
module "development" {
source = "../../modules/environment"
environment = "development"

# Project configuration
project = "{{ .Infrastructure.AWS.EKS.ClusterName }}"
region = "{{ .Infrastructure.AWS.Region }}"
allowed_account_ids = ["{{ .Infrastructure.AWS.AccountId }}"]
project = "{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"
region = "{{ .Config.Infrastructure.AWS.Region }}"
allowed_account_ids = ["{{ .Config.Infrastructure.AWS.AccountId }}"]

# ECR configuration
ecr_repositories = ["{{ .Infrastructure.AWS.EKS.ClusterName }}"]
ecr_repositories = ["{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"]

# EKS configuration
eks_worker_instance_type = "t2.small"
Expand Down
18 changes: 14 additions & 4 deletions templates/kubernetes/terraform/environments/production/main.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
terraform {
backend "s3" {
bucket = "project-{{ .Config.Name }}-terraform-state"
key = "infrastructure/terraform/environments/production/main"
encrypt = true
region = "{{ .Config.Infrastructure.AWS.Region }}"
dynamodb_table = "terraform-state-locks"
}
}

# Instantiate the production environment
module "production" {
source = "../../modules/environment"
environment = "production"

# Project configuration
project = "{{ .Infrastructure.AWS.EKS.ClusterName }}"
region = "{{ .Infrastructure.AWS.Region }}"
allowed_account_ids = ["{{ .Infrastructure.AWS.AccountId }}"]
project = "{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"
region = "{{ .Config.Infrastructure.AWS.Region }}"
allowed_account_ids = ["{{ .Config.Infrastructure.AWS.AccountId }}"]

# ECR configuration
ecr_repositories = ["{{ .Infrastructure.AWS.EKS.ClusterName }}"]
ecr_repositories = ["{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"]

# EKS configuration
eks_worker_instance_type = "m4.large"
Expand Down
18 changes: 14 additions & 4 deletions templates/kubernetes/terraform/environments/staging/main.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
terraform {
backend "s3" {
bucket = "project-{{ .Config.Name }}-terraform-state"
key = "infrastructure/terraform/environments/staging/main"
encrypt = true
region = "{{ .Config.Infrastructure.AWS.Region }}"
dynamodb_table = "terraform-state-locks"
}
}

# Instantiate the staging environment
module "staging" {
source = "../../modules/environment"
environment = "staging"

# Project configuration
project = "{{ .Infrastructure.AWS.EKS.ClusterName }}"
region = "{{ .Infrastructure.AWS.Region }}"
allowed_account_ids = ["{{ .Infrastructure.AWS.AccountId }}"]
project = "{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"
region = "{{ .Config.Infrastructure.AWS.Region }}"
allowed_account_ids = ["{{ .Config.Infrastructure.AWS.AccountId }}"]

# ECR configuration
ecr_repositories = ["{{ .Infrastructure.AWS.EKS.ClusterName }}"]
ecr_repositories = ["{{ .Config.Infrastructure.AWS.EKS.ClusterName }}"]

# EKS configuration
eks_worker_instance_type = "t2.small"
Expand Down
24 changes: 24 additions & 0 deletions templates/kubernetes/terraform/global/remote-state/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
provider "aws" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To create the actual backend bucket and dynamo db. This will need to be run in a separate tf apply before the tf init of the rest of the infra. This will be an interesting piece to automate..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move it to a seperate .tf file and use terraform apply target to apply specifically only that portion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That wouldn't be a great workflow going forward though, because you wouldn't be able to run plan/apply in the main dir.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using the depends on clause?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They have to be run separately, because one creates the global state and should never be run again, and the other one uses the global state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, does it make sense to enclose the backend init in a shell script then or boto3?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That way we can both spin up and tear down the backend init based on tf apply and tf destroy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna stick with this for now and we'll see how complex the process gets to see if it warrants adding helpers..

region = "{{ .Config.Infrastructure.AWS.Region }}"
}

resource "aws_s3_bucket" "terraform_remote_state" {
bucket = "project-{{ .Config.Name }}-terraform-state"
acl = "private"

versioning {
enabled = true
}
}

resource "aws_dynamodb_table" "terraform_state_locks" {
name = "{{ .Config.Name }}-terraform-state-locks"
read_capacity = 2
write_capacity = 2
hash_key = "LockID"

attribute {
name = "LockID"
type = "S"
}
}
2 changes: 1 addition & 1 deletion templates/kubernetes/terraform/modules/eks/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Create KubernetesAdmin role for aws-iam-authenticator
resource "aws_iam_role" "kubernetes_admin_role" {
name = "kubernetes-admin"
name = "{{ .Config.Name }}-kubernetes-admin"
assume_role_policy = var.assume_role_policy
description = "Kubernetes administrator role (for AWS IAM Authenticator)"
}
Expand Down
9 changes: 0 additions & 9 deletions templates/kubernetes/terraform/modules/environment/backend.tf

This file was deleted.

4 changes: 2 additions & 2 deletions templates/kubernetes/terraform/modules/kube2iam/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ resource "aws_iam_role_policy" "node_kube2iam_policy" {
## ALB Ingress Controller
# Create a role and establish a trust relationship with the worker nodes
resource "aws_iam_role" "k8s_worker_alb_ingress_controller_role" {
name = "k8s-alb-ingress-controller"
name = "{{ .Config.Name }}-k8s-alb-ingress-controller"
assume_role_policy = data.aws_iam_policy_document.k8s_worker_assumerole_policy.json
force_detach_policies = true
}
Expand Down Expand Up @@ -127,4 +127,4 @@ resource "aws_iam_role_policy" "k8s_worker_alb_ingress_controller_role_policy" {
name = "worker-alb-ingress-controller-policy"
role = aws_iam_role.k8s_worker_alb_ingress_controller_role.id
policy = data.aws_iam_policy_document.k8s_alb_ingress_controller_access_policy.json
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_iam_role" "k8s_monitoring" {
name = "k8s-${var.environment}-monitoring"
name = "{{ .Config.Name }}-k8s-${var.environment}-monitoring"
assume_role_policy = var.assume_role_policy
force_detach_policies = true
}
Expand Down Expand Up @@ -35,4 +35,4 @@ module "cloudwatch_agent" {
environment = var.environment
region = var.region
cluster_name = var.cluster_name
}
}
10 changes: 5 additions & 5 deletions templates/kubernetes/terraform/modules/vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module "vpc" {
source = "terraform-aws-modules/vpc/aws"

name = "${var.project}-${var.environment}-vpc"
cidr = "10.10.0.0/16"
cidr = "10.20.0.0/16"

azs = ["${var.region}a", "${var.region}b", "${var.region}c"] # Most regions have 3+ azs
private_subnets = ["10.10.40.0/24", "10.10.42.0/24", "10.10.44.0/24"]
public_subnets = ["10.10.41.0/24", "10.10.43.0/24", "10.10.45.0/24"]
database_subnets = ["10.10.50.0/24", "10.10.52.0/24", "10.10.54.0/24"]
private_subnets = ["10.20.40.0/24", "10.20.42.0/24", "10.20.44.0/24"]
public_subnets = ["10.20.41.0/24", "10.20.43.0/24", "10.20.45.0/24"]
database_subnets = ["10.20.50.0/24", "10.20.52.0/24", "10.20.54.0/24"]

# Allow kubernetes ALB ingress controller to auto-detect
private_subnet_tags = {
Expand All @@ -31,4 +31,4 @@ module "vpc" {
environment = var.environment
}

}
}
2 changes: 1 addition & 1 deletion templates/react/package.json.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "{{ .Frontend.App.Name }}",
"name": "{{ .Config.Frontend.App.Name }}",
"version": "0.1.0",
"private": true,
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion templates/react/public/index.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>{{ .Frontend.App.Name }}</title>
<title>{{ .Config.Frontend.App.Name }}</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
14 changes: 7 additions & 7 deletions templates/react/src/config/index.js.tmpl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
export default {
app: {
name: '{{ .Frontend.App.Name }}',
name: '{{ .Config.Frontend.App.Name }}',
},
account: {
enabled: {{ .Frontend.Account.Enabled }},
required: {{ .Frontend.Account.Required }},
enabled: {{ .Config.Frontend.Account.Enabled }},
required: {{ .Config.Frontend.Account.Required }},
},
header: {
enabled: {{ .Frontend.Header.Enabled }},
enabled: {{ .Config.Frontend.Header.Enabled }},
},
sidenav: {
enabled: {{ .Frontend.Sidenav.Enabled }},
enabled: {{ .Config.Frontend.Sidenav.Enabled }},
items: [
{{ range .Frontend.Sidenav.Items }}
{{ range .Config.Frontend.Sidenav.Items }}
{
path: '{{ .Path }}',
label: '{{ .Label }}',
Expand All @@ -22,7 +22,7 @@ export default {
]
},
views: [
{{ range .Frontend.Views }}
{{ range .Config.Frontend.Views }}
{
path: '{{ .Path }}',
component: '{{ .Component }}',
Expand Down
2 changes: 1 addition & 1 deletion templates/util/README.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# {{.Name}}
# {{.Config.Name}}

@TODO : Fill in readme about how to use all the components the user configured