Skip to content

Commit 6b7e4f6

Browse files
authored
Merge pull request #34 from commitdev/kubernetes
Added kubernetes specs
2 parents 516299b + 016a4b5 commit 6b7e4f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2087
-12
lines changed

cmd/generate.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/commitdev/commit0/internal/generate/docker"
99
"github.com/commitdev/commit0/internal/generate/golang"
1010
"github.com/commitdev/commit0/internal/generate/http"
11+
"github.com/commitdev/commit0/internal/generate/kubernetes"
1112
"github.com/commitdev/commit0/internal/generate/proto"
1213
"github.com/commitdev/commit0/internal/generate/react"
1314
"github.com/commitdev/commit0/internal/templator"
@@ -20,11 +21,12 @@ var configPath string
2021
var language string
2122

2223
const (
23-
Go = "go"
24-
React = "react"
24+
Go = "go"
25+
React = "react"
26+
Kubernetes = "kubernetes"
2527
)
2628

27-
var supportedLanguages = [...]string{Go, React}
29+
var supportedLanguages = [...]string{Go, React, Kubernetes}
2830

2931
func init() {
3032

@@ -59,6 +61,8 @@ var generateCmd = &cobra.Command{
5961
docker.GenerateGoDockerCompose(t, cfg, &wg)
6062
case React:
6163
react.Generate(t, cfg, &wg)
64+
case Kubernetes:
65+
kubernetes.Generate(t, cfg, &wg)
6266
}
6367

6468
util.TemplateFileIfDoesNotExist("", "README.md", t.Readme, &wg, cfg)
@@ -68,7 +72,14 @@ var generateCmd = &cobra.Command{
6872
docker.GenerateGoHTTPGWDockerFile(t, cfg, &wg)
6973
}
7074

75+
// Wait for all the templates to be generated
7176
wg.Wait()
77+
78+
switch language {
79+
case Kubernetes:
80+
kubernetes.Execute(cfg)
81+
}
82+
7283
},
7384
}
7485

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ require (
88
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
99
github.com/k0kubun/pp v3.0.1+incompatible
1010
github.com/mattn/go-colorable v0.1.2 // indirect
11-
github.com/rogpeppe/go-internal v1.3.2 // indirect
11+
github.com/rogpeppe/go-internal v1.5.0 // indirect
1212
github.com/spf13/cobra v0.0.5
1313
github.com/stretchr/testify v1.4.0 // indirect
14-
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392 // indirect
14+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
1515
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
16+
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 // indirect
1617
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
1718
gopkg.in/yaml.v2 v2.2.4
1819
)

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
4848
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4949
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
5050
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
51-
github.com/rogpeppe/go-internal v1.3.2 h1:XU784Pr0wdahMY2bYcyK6N1KuaRAdLtqD4qd8D18Bfs=
52-
github.com/rogpeppe/go-internal v1.3.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
51+
github.com/rogpeppe/go-internal v1.5.0 h1:Usqs0/lDK/NqTkvrmKSwA/3XkZAs7ZAW/eLeQ2MVBTw=
52+
github.com/rogpeppe/go-internal v1.5.0/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
5353
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
5454
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
5555
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
@@ -72,8 +72,8 @@ github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:
7272
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
7373
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
7474
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
75-
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392 h1:ACG4HJsFiNMf47Y4PeRoebLNy/2lXT9EtprMuTFWt1M=
76-
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
75+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
76+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
7777
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
7878
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
7979
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -86,8 +86,8 @@ golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5h
8686
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8787
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8888
golang.org/x/sys v0.0.0-20190515120540-06a5c4944438/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
89-
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 h1:rOhMmluY6kLMhdnrivzec6lLgaVbMHMn2ISQXJeJ5EM=
90-
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
89+
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=
90+
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9191
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
9292
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
9393
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0 h1:Dh6fw+p6FyRl5x/FvNswO1ji0lIGzm3KP8Y9VkS9PTE=

internal/config/config.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ type Commit0Config struct {
5555
Maintainers []Maintainers `yaml:"maintainers"`
5656
Network Network `yaml:"network"`
5757
Services []Service `yaml:"services"`
58-
React React `yaml:react`
58+
React React `yaml:"react"`
59+
Kubernetes Kubernetes `yaml:"kubernetes"`
60+
}
61+
62+
type Kubernetes struct {
63+
ClusterName string `yaml:"clusterName"`
64+
Deploy bool `yaml:"deploy"`
65+
AWSAccountId string `yaml:"awsAccountId"`
66+
AWSRegion string `yaml:"awsRegion"`
5967
}
6068

6169
func LoadConfig(filePath string) *Commit0Config {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package kubernetes
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"log"
7+
"os"
8+
"os/exec"
9+
"sync"
10+
11+
"github.com/commitdev/commit0/internal/config"
12+
"github.com/commitdev/commit0/internal/templator"
13+
)
14+
15+
func Generate(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
16+
templator.Kubernetes.TemplateFiles(config, false, wg)
17+
18+
}
19+
20+
func Execute(config *config.Commit0Config) {
21+
if config.Kubernetes.Deploy {
22+
log.Println("Planning infrastructure...")
23+
execute(exec.Command("terraform", "init"))
24+
execute(exec.Command("terraform", "plan"))
25+
}
26+
}
27+
28+
func execute(cmd *exec.Cmd) {
29+
dir, err := os.Getwd()
30+
if err != nil {
31+
log.Fatalf("Getting working directory failed: %v\n", err)
32+
}
33+
34+
cmd.Dir = fmt.Sprintf("%s/kubernetes/terraform/environments/staging", dir)
35+
36+
stdoutPipe, _ := cmd.StdoutPipe()
37+
stderrPipe, _ := cmd.StderrPipe()
38+
39+
var errStdout, errStderr error
40+
err = cmd.Start()
41+
if err != nil {
42+
log.Fatalf("Starting terraform command failed: %v\n", err)
43+
}
44+
45+
go func() {
46+
_, errStdout = io.Copy(os.Stdout, stdoutPipe)
47+
}()
48+
go func() {
49+
_, errStderr = io.Copy(os.Stderr, stderrPipe)
50+
}()
51+
52+
err = cmd.Wait()
53+
if err != nil {
54+
log.Fatalf("Executing terraform command failed: %v\n", err)
55+
}
56+
57+
if errStdout != nil {
58+
log.Printf("Failed to capture stdout: %v\n", errStdout)
59+
}
60+
61+
if errStderr != nil {
62+
log.Printf("Failed to capture stderr: %v\n", errStderr)
63+
}
64+
}

internal/templator/templator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Templator struct {
4141
Go *GoTemplator
4242
Docker *DockerTemplator
4343
React *DirectoryTemplator
44+
Kubernetes *DirectoryTemplator
4445
}
4546

4647
func NewTemplator(box *packr.Box) *Templator {
@@ -54,6 +55,7 @@ func NewTemplator(box *packr.Box) *Templator {
5455
Readme: NewSingleFileTemplator(box, "util/README.tmpl"),
5556
Docker: NewDockerFileTemplator(box),
5657
React: NewDirectoryTemplator(box, "react"),
58+
Kubernetes: NewDirectoryTemplator(box, "kubernetes"),
5759
}
5860
}
5961

templates/commit0/commit0.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ maintainers:
77
# - name: bob
88
# email: bob@test.com
99

10+
kubernetes:
11+
clusterName: staging
12+
deploy: true
13+
awsAccountId: 1234
14+
awsRegion: us-east-1
15+
1016
network:
1117
grpc:
1218
host: 0.0.0.0
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
## Guidelines & Style Convention Summary
2+
3+
- All Terraform configuration should be formatted with `terraform fmt` before being accepted into this repository.
4+
- This repository is Terraform version >= 0.12, as such, leverage features from this release whenever possible.
5+
See https://www.terraform.io/upgrade-guides/0-12.html for more information.
6+
- Leverage community-maintained Terraform modules whenever possible.
7+
- Attempt to minimize duplication whenever possible, but only within reason -- sometimes duplication is an acceptable solution.
8+
- Follow style conventions described in `docs/guide.pdf` whenever possible.
9+
- Whenever possible, inject resources down versus referencing resources across modules. This has been made easier with new features in v0.12.
10+
- Whenever possible, define the types of variables.
11+
12+
### Module Conventions
13+
14+
- All modules should contain the following:
15+
16+
`README.md`: A description of the module.
17+
`main.tf`: Module entrypoint where instantiation of resources happens.
18+
`variables.tf`: Module variables.
19+
`outputs.tf`: Output values (optional).
20+
`files/`: Any / all files required by the module.
21+
22+
- All module variables must have a description.
23+
- Again, leverage community-maintained Terraform modules whenever possible.
24+
- Avoid writing a module that is simply a wrapper of a Terraform resource unless absolutely necessary.
25+
26+
### Environment Conventions
27+
28+
- All environments should contain the following:
29+
30+
`main.tf`: Toplevel terraform configuration file that instantiates the `environment` module.
31+
32+
- Configuration should be pushed "top->down" from the `environment` module to it's submodules.
33+
34+
### The Environment Module
35+
36+
- The `environment` module can be considered the top-level module, all other modules are imported from this module.
37+
- 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.
38+
- The `environment` module contains the following:
39+
40+
`main.tf`: Module entrypoint where instantiation of resources happens.
41+
`backend.tf`: Terraform remote state configuration.
42+
`provider.tf`: Provider configuration.
43+
`variables.tf`: Environment-specific variables are desclared here.
44+
`versions.tf`: Terraform version information.
45+
`files/`: (DEPRECATED)
46+
47+
## Directory Structure
48+
49+
```
50+
README.md
51+
environments/
52+
production/
53+
main.tf
54+
staging/
55+
main.tf
56+
development/
57+
main.tf
58+
docs/
59+
guide.pdf
60+
modules/
61+
environment/
62+
...
63+
<module-a>/
64+
files/
65+
scripts/
66+
main.tf
67+
outputs.tf
68+
variables.tf
69+
<module-n>/
70+
...
71+
```
72+
73+
## AWS Guidelines
74+
75+
- TODO: Identity/Access Management (IAM) Guidelines
76+
77+
## Kubernetes Guidelines
78+
79+
- When to use the Terraform Kuberenetes Provider and when to use manifests?
80+
81+
- Use the Terraform Kubernetes Provider (`provider "kubernetes"`) whenever you are provisioning a resource that could be considered relatively static (think Ingress, RoleBinding, CluterRoleBinding, etc).
82+
83+
- Use conventional Kubernetes manifests / `kubectl` when provisioning resouirces that could be considered dynamic (think Deployments).
84+
85+
## Application
86+
87+
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:
88+
`export AWS_PROFILE=<project_name>`
89+
90+
2. Run the following from the appropriate environment directory under `environments/`:
91+
92+
```
93+
environment/development$ terraform init
94+
environment/development$ terraform plan
95+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Instantiate the development environment
2+
module "development" {
3+
source = "../../modules/environment"
4+
environment = "development"
5+
6+
# Project configuration
7+
project = "{{ .Kubernetes.ClusterName }}"
8+
region = "{{ .Kubernetes.AWSRegion }}"
9+
allowed_account_ids = ["{{ .Kubernetes.AWSAccountId }}"]
10+
11+
# ECR configuration
12+
ecr_repositories = ["{{ .Kubernetes.ClusterName }}"]
13+
14+
# EKS configuration
15+
eks_worker_instance_type = "t2.small"
16+
eks_worker_asg_max_size = 2
17+
18+
# EKS-Optimized AMI for your region: https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html
19+
# 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
20+
eks_worker_ami = "ami-0392bafc801b7520f"
21+
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Instantiate the production environment
2+
module "production" {
3+
source = "../../modules/environment"
4+
environment = "production"
5+
6+
# Project configuration
7+
project = "{{ .Kubernetes.ClusterName }}"
8+
region = "{{ .Kubernetes.AWSRegion }}"
9+
allowed_account_ids = ["{{ .Kubernetes.AWSAccountId }}"]
10+
11+
# ECR configuration
12+
ecr_repositories = ["{{ .Kubernetes.ClusterName }}"]
13+
14+
# EKS configuration
15+
eks_worker_instance_type = "m4.large"
16+
eks_worker_asg_max_size = 3
17+
18+
# EKS-Optimized AMI for your region: https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html
19+
# 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
20+
eks_worker_ami = "ami-0392bafc801b7520f"
21+
}

0 commit comments

Comments
 (0)