Skip to content

Commit 1b3258c

Browse files
authored
Merge pull request #76 from commitdev/add-init-and-apply-flags
Various fixes to applying kube tf and added flags
2 parents 280c651 + a274e8d commit 1b3258c

File tree

25 files changed

+111
-96
lines changed

25 files changed

+111
-96
lines changed

cmd/generate.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import (
1010
)
1111

1212
var configPath string
13+
var runInit bool
14+
var runApply bool
1315

1416
func init() {
15-
1617
generateCmd.PersistentFlags().StringVarP(&configPath, "config", "c", util.CommitYml, "config path")
18+
generateCmd.PersistentFlags().BoolVarP(&runInit, "init", "", false, "Initialize config after generating")
19+
generateCmd.PersistentFlags().BoolVarP(&runApply, "apply", "", false, "Apply config after generating")
1720

1821
rootCmd.AddCommand(generateCmd)
1922
}
@@ -29,7 +32,7 @@ var generateCmd = &cobra.Command{
2932
cfg := config.LoadConfig(configPath)
3033
cfg.Print()
3134

32-
generate.GenerateArtifactsHelper(t, cfg, "")
35+
generate.GenerateArtifactsHelper(t, cfg, "", runInit, runApply)
3336

3437
},
3538
}

internal/api/create_project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ func GenerateArtifacts(projectConfig util.ProjectConfiguration) {
4949
cfg := config.LoadConfig(generatedYml)
5050
cfg.Print()
5151

52-
generate.GenerateArtifactsHelper(t, cfg, projectConfig.ProjectName)
52+
generate.GenerateArtifactsHelper(t, cfg, projectConfig.ProjectName, false, false)
5353
}

internal/config/config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type aws struct {
7878
Region string
7979
EKS eks
8080
Cognito cognito
81-
S3Hosting s3Hosting `yaml:"s3_hosting"`
81+
S3Hosting s3Hosting `yaml:"s3Hosting"`
8282
Terraform terraform
8383
}
8484

@@ -87,17 +87,16 @@ type terraform struct {
8787
}
8888

8989
type cognito struct {
90-
Deploy bool
90+
Enabled bool
9191
}
9292

9393
type s3Hosting struct {
94-
Deploy bool
94+
Enabled bool // @TODO Remove this option? If they have frontend and AWS enabled we should set up S3, etc.
9595
}
9696

9797
type eks struct {
9898
ClusterName string `yaml:"clusterName"`
9999
WorkerAMI string `yaml:"workerAMI"`
100-
Deploy bool
101100
}
102101

103102
func LoadConfig(filePath string) *Commit0Config {

internal/generate/generate_helper.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/logrusorgru/aurora"
1717
)
1818

19-
func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config, pathPrefix string) {
19+
func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config, pathPrefix string, runInit bool, runApply bool) {
2020
var wg sync.WaitGroup
2121
if !util.ValidateLanguage(cfg.Frontend.Framework) {
2222
log.Fatalln(aurora.Red(emoji.Sprintf(":exclamation: '%s' is not a supported framework.", cfg.Frontend.Framework)))
@@ -50,21 +50,15 @@ func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config,
5050
// Wait for all the templates to be generated
5151
wg.Wait()
5252

53-
log.Println("Executing commands")
54-
// @TODO : Move this stuff to another command? Or genericize it a bit.
55-
if cfg.Infrastructure.AWS.EKS.Deploy {
56-
terraform.Execute(cfg, pathPrefix)
57-
kubernetes.Execute(cfg, pathPrefix)
53+
log.Println(aurora.Cyan(emoji.Sprintf("Initializing Infrastructure")))
54+
if cfg.Infrastructure.AWS.EKS.ClusterName != "" && runInit {
55+
terraform.Init(cfg, pathPrefix)
5856
}
5957

60-
if cfg.Infrastructure.AWS.Cognito.Deploy {
61-
outputs := []string{
62-
"cognito_pool_id",
63-
"cognito_client_id",
64-
}
65-
outputValues := terraform.GetOutputs(cfg, pathPrefix, outputs)
66-
cfg.Frontend.Env.CognitoPoolID = outputValues["cognito_pool_id"]
67-
cfg.Frontend.Env.CognitoClientID = outputValues["cognito_client_id"]
58+
log.Println(aurora.Cyan(emoji.Sprintf("Creating Infrastructure")))
59+
if cfg.Infrastructure.AWS.EKS.ClusterName != "" && runApply {
60+
terraform.Execute(cfg, pathPrefix)
61+
kubernetes.Execute(cfg, pathPrefix)
6862
}
6963

7064
// @TODO : This strucuture probably needs to be adjusted. Probably too generic.

internal/generate/kubernetes/generate.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/commitdev/commit0/internal/config"
1010
"github.com/commitdev/commit0/internal/templator"
1111
"github.com/commitdev/commit0/internal/util"
12+
"github.com/kyokomi/emoji"
1213
"github.com/logrusorgru/aurora"
1314
)
1415

@@ -19,14 +20,12 @@ func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGr
1920
}
2021

2122
// Execute terrafrom init & plan
22-
func Execute(config *config.Commit0Config, pathPrefix string) {
23-
if config.Infrastructure.AWS.EKS.Deploy {
24-
envars := util.MakeAwsEnvars(util.GetSecrets())
23+
func Execute(cfg *config.Commit0Config, pathPrefix string) {
24+
envars := util.MakeAwsEnvars(util.GetSecrets())
2525

26-
pathPrefix = filepath.Join(pathPrefix, "kubernetes/terraform")
26+
pathPrefix = filepath.Join(pathPrefix, "kubernetes/terraform")
2727

28-
log.Println(aurora.Cyan(":alarm_clock: Applying kubernetes configuration..."))
29-
util.ExecuteCommand(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "environments/staging/kubernetes"), envars)
30-
util.ExecuteCommand(exec.Command("terraform", "plan"), filepath.Join(pathPrefix, "environments/staging/kubernetes"), envars)
31-
}
28+
log.Println(aurora.Cyan(emoji.Sprintf(":alarm_clock: Applying kubernetes configuration...")))
29+
util.ExecuteCommand(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "environments/staging"), envars)
30+
util.ExecuteCommand(exec.Command("terraform", "apply", "-auto-approve"), filepath.Join(pathPrefix, "environments/staging"), envars)
3231
}

internal/generate/terraform/generate.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,26 @@ func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGr
3737
t.Terraform.TemplateFiles(data, false, wg, pathPrefix)
3838
}
3939

40+
// GetOutputs captures the terraform output for the specific variables
4041
func GetOutputs(config *config.Commit0Config, pathPrefix string, outputs []string) map[string]string {
4142
outputsMap := make(map[string]string)
4243

4344
log.Println("Preparing aws environment...")
4445

4546
envars := util.MakeAwsEnvars(util.GetSecrets())
4647

47-
path := filepath.Join(pathPrefix, "terraform")
48+
pathPrefix = filepath.Join(pathPrefix, "environments/staging")
4849

4950
for _, output := range outputs {
50-
outputValue := util.ExecuteCommandOutput(exec.Command("terraform", "output", output), path, envars)
51+
outputValue := util.ExecuteCommandOutput(exec.Command("terraform", "output", output), pathPrefix, envars)
5152
outputsMap[output] = outputValue
5253
}
5354

5455
return outputsMap
5556
}
5657

57-
// Execute terrafrom init & plan
58-
func Execute(config *config.Commit0Config, pathPrefix string) {
58+
// Init sets up anything required by Execute
59+
func Init(config *config.Commit0Config, pathPrefix string) {
5960
// @TODO : Change this check. Most likely we should discover the accountid
6061
if config.Infrastructure.AWS.AccountId != "" {
6162
log.Println("Preparing aws environment...")
@@ -68,16 +69,31 @@ func Execute(config *config.Commit0Config, pathPrefix string) {
6869
log.Println(aurora.Cyan(emoji.Sprintf(":alarm_clock: Initializing remote backend...")))
6970
util.ExecuteCommand(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "bootstrap/remote-state"), envars)
7071
util.ExecuteCommand(exec.Command("terraform", "apply", "-auto-approve"), filepath.Join(pathPrefix, "bootstrap/remote-state"), envars)
72+
}
73+
}
7174

72-
log.Println(aurora.Cyan(":alarm_clock: Planning infrastructure..."))
73-
util.ExecuteCommand(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "environments/staging"), envars)
74-
util.ExecuteCommand(exec.Command("terraform", "plan"), filepath.Join(pathPrefix, "environments/staging"), envars)
75+
// Execute terrafrom init & plan. May modify the config passed in
76+
func Execute(cfg *config.Commit0Config, pathPrefix string) {
77+
// @TODO : Change this check. Most likely we should discover the accountid
78+
if cfg.Infrastructure.AWS.AccountId != "" {
79+
log.Println("Preparing aws environment...")
7580

76-
log.Println(aurora.Cyan(":alarm_clock: Applying infrastructure configuration..."))
77-
util.ExecuteCommand(exec.Command("terraform", "apply"), filepath.Join(pathPrefix, "environments/staging"), envars)
81+
envars := util.MakeAwsEnvars(util.GetSecrets())
82+
83+
pathPrefix = filepath.Join(pathPrefix, "terraform")
7884

79-
log.Println(aurora.Cyan(":alarm_clock: Applying kubernetes configuration..."))
80-
util.ExecuteCommand(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "environments/staging/kubernetes"), envars)
81-
util.ExecuteCommand(exec.Command("terraform", "plan"), filepath.Join(pathPrefix, "environments/staging/kubernetes"), envars)
85+
log.Println(aurora.Cyan(emoji.Sprintf(":alarm_clock: Applying infrastructure configuration...")))
86+
util.ExecuteCommand(exec.Command("terraform", "init"), filepath.Join(pathPrefix, "environments/staging"), envars)
87+
util.ExecuteCommand(exec.Command("terraform", "apply", "-auto-approve"), filepath.Join(pathPrefix, "environments/staging"), envars)
88+
89+
if cfg.Infrastructure.AWS.Cognito.Enabled {
90+
outputs := []string{
91+
"cognito_pool_id",
92+
"cognito_client_id",
93+
}
94+
outputValues := GetOutputs(cfg, pathPrefix, outputs)
95+
cfg.Frontend.Env.CognitoPoolID = outputValues["cognito_pool_id"]
96+
cfg.Frontend.Env.CognitoClientID = outputValues["cognito_client_id"]
97+
}
8298
}
8399
}

internal/util/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func ExecuteCommandOutput(cmd *exec.Cmd, pathPrefix string, envars []string) str
136136

137137
out, err := cmd.CombinedOutput()
138138
if err != nil {
139-
log.Fatalf("Executing terraform output failed: %v\n", err)
139+
log.Fatalf("Executing command failed: (%v) %s\n", err, out)
140140
}
141141
return string(out)
142142
}

templates/commit0/commit0.tmpl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ infrastructure:
1313
region: us-east-1
1414
eks:
1515
clusterName: staging
16-
deploy: true
1716
cognito:
18-
deploy: true
19-
s3_hosting:
20-
deploy: true
17+
enabled: true
18+
s3Hosting:
19+
enabled: true
2120

2221
frontend:
2322
framework: {{.FrontendFramework}}

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

100755100644
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ resource "kubernetes_config_map" "nginx_configuration" {
2222
use-forwarded-headers = "true",
2323
use-proxy-protocol = "false"
2424
}
25+
depends_on = [kubernetes_namespace.ingress_nginx]
2526
}
2627

2728
# resource "kubernetes_config_map" "nginx_configuration" {
@@ -33,6 +34,7 @@ resource "kubernetes_config_map" "nginx_configuration" {
3334
# "app.kubernetes.io/part-of" = "ingress-nginx"
3435
# }
3536
# }
37+
# depends_on = [kubernetes_namespace.ingress_nginx]
3638
# }
3739

3840
resource "kubernetes_config_map" "tcp_services" {
@@ -44,6 +46,7 @@ resource "kubernetes_config_map" "tcp_services" {
4446
"app.kubernetes.io/part-of" = "ingress-nginx"
4547
}
4648
}
49+
depends_on = [kubernetes_namespace.ingress_nginx]
4750
}
4851

4952
resource "kubernetes_config_map" "udp_services" {
@@ -55,6 +58,7 @@ resource "kubernetes_config_map" "udp_services" {
5558
"app.kubernetes.io/part-of" = "ingress-nginx"
5659
}
5760
}
61+
depends_on = [kubernetes_namespace.ingress_nginx]
5862
}
5963

6064
resource "kubernetes_service_account" "nginx_ingress_serviceaccount" {
@@ -66,6 +70,7 @@ resource "kubernetes_service_account" "nginx_ingress_serviceaccount" {
6670
"app.kubernetes.io/part-of" = "ingress-nginx"
6771
}
6872
}
73+
depends_on = [kubernetes_namespace.ingress_nginx]
6974
}
7075

7176
resource "kubernetes_cluster_role" "nginx_ingress_clusterrole" {
@@ -138,6 +143,7 @@ resource "kubernetes_role" "nginx_ingress_role" {
138143
api_groups = [""]
139144
resources = ["endpoints"]
140145
}
146+
depends_on = [kubernetes_namespace.ingress_nginx]
141147
}
142148

143149
resource "kubernetes_role_binding" "nginx_ingress_role_nisa_binding" {
@@ -159,6 +165,7 @@ resource "kubernetes_role_binding" "nginx_ingress_role_nisa_binding" {
159165
kind = "Role"
160166
name = "nginx-ingress-role"
161167
}
168+
depends_on = [kubernetes_namespace.ingress_nginx]
162169
}
163170

164171
resource "kubernetes_cluster_role_binding" "nginx_ingress_clusterrole_nisa_binding" {
@@ -324,6 +331,7 @@ resource "kubernetes_service" "ingress_nginx" {
324331
type = "LoadBalancer"
325332
external_traffic_policy = "Local"
326333
}
334+
depends_on = [kubernetes_namespace.ingress_nginx]
327335
}
328336

329337
# HTTPS Load balancer
@@ -359,4 +367,5 @@ resource "kubernetes_service" "ingress_nginx" {
359367
# }
360368
# type = "LoadBalancer"
361369
# }
362-
# }
370+
# depends_on = [kubernetes_namespace.ingress_nginx]
371+
# }

templates/kubernetes/terraform/modules/kubernetes/monitoring/cloudwatch_agent/main.tf renamed to templates/kubernetes/terraform/modules/kubernetes/monitoring/cloudwatch_agent.tf

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
data "aws_iam_policy" "CloudWatchAgentServerPolicy" {
2-
arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
3-
}
4-
51
resource "kubernetes_service_account" "cloudwatch_agent" {
62
metadata {
73
name = "cloudwatch-agent"
84
namespace = "amazon-cloudwatch"
95
}
6+
depends_on = [kubernetes_namespace.amazon_cloudwatch]
107
}
118

129
resource "kubernetes_cluster_role" "cloudwatch_agent_role" {
@@ -76,6 +73,7 @@ resource "kubernetes_config_map" "cwagentconfig" {
7673
}
7774
)
7875
}
76+
depends_on = [kubernetes_namespace.amazon_cloudwatch]
7977
}
8078

8179
resource "kubernetes_daemonset" "cloudwatch_agent" {
@@ -217,4 +215,4 @@ resource "kubernetes_daemonset" "cloudwatch_agent" {
217215
}
218216
}
219217
}
220-
}
218+
}

0 commit comments

Comments
 (0)