From b570490db222728db35707e5c38ac6714a9f4dda Mon Sep 17 00:00:00 2001 From: takutaka Date: Thu, 7 May 2020 18:39:17 +0900 Subject: [PATCH 1/2] use sprig --- pkg/terraform/terraform.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/terraform/terraform.go b/pkg/terraform/terraform.go index 8f84f76..46fff49 100644 --- a/pkg/terraform/terraform.go +++ b/pkg/terraform/terraform.go @@ -11,6 +11,7 @@ import ( "text/template" "time" + "github.com/Masterminds/sprig" "github.com/takutakahashi/loadbalancer-controller/api/v1beta1" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" @@ -262,7 +263,7 @@ func (t TerraformClient) genWithTpl(path string) (string, error) { } else { length = 31 } - err = tmpl.Execute(&result, struct { + err = tmpl.Funcs(sprig.TxtFuncMap()).Execute(&result, struct { Name string B *v1beta1.AWSBackend ServiceIn bool From 6fdfcbab0935cb921d4dbee6b013658bc9c715ea Mon Sep 17 00:00:00 2001 From: takutaka Date: Thu, 7 May 2020 19:58:58 +0900 Subject: [PATCH 2/2] mod to tf: define static resource and remove count --- pkg/terraform/terraform.go | 5 +++-- src/terraform/AWSBackend/lb.tf.tpl | 10 ++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/terraform/terraform.go b/pkg/terraform/terraform.go index 46fff49..1f2363f 100644 --- a/pkg/terraform/terraform.go +++ b/pkg/terraform/terraform.go @@ -7,6 +7,7 @@ import ( "io" "net" "os" + "path/filepath" "regexp" "text/template" "time" @@ -252,7 +253,7 @@ func (t TerraformClient) genTfvars() (string, error) { func (t TerraformClient) genWithTpl(path string) (string, error) { awsBackend := t.awsBackend if awsBackend != nil { - tmpl, err := template.ParseFiles(path) + tmpl, err := template.New(filepath.Base(path)).Funcs(sprig.TxtFuncMap()).ParseFiles(path) if err != nil { return "", err } @@ -263,7 +264,7 @@ func (t TerraformClient) genWithTpl(path string) (string, error) { } else { length = 31 } - err = tmpl.Funcs(sprig.TxtFuncMap()).Execute(&result, struct { + err = tmpl.Execute(&result, struct { Name string B *v1beta1.AWSBackend ServiceIn bool diff --git a/src/terraform/AWSBackend/lb.tf.tpl b/src/terraform/AWSBackend/lb.tf.tpl index 5e18d20..91570ed 100644 --- a/src/terraform/AWSBackend/lb.tf.tpl +++ b/src/terraform/AWSBackend/lb.tf.tpl @@ -30,12 +30,14 @@ resource "aws_lb_target_group" "{{ $name }}_tg" { vpc_id = var.vpc_id } -resource "aws_lb_target_group_attachment" "{{ $name }}_tga" { - count = length(var.{{ $name }}_targets) +{{ range $i, $t := $tg.Targets }} + +resource "aws_lb_target_group_attachment" "{{ $name }}_tga_{{ $t.Destination.IP | replace "." "-" }}_{{ $t.Port }}" { target_group_arn = aws_lb_target_group.{{ $name }}_tg.arn - target_id = var.{{ $name }}_targets[count.index].destination + target_id = "{{ $t.Destination.IP }}" availability_zone = "ap-northeast-1a" - port = var.{{ $name }}_targets[count.index].port + port = {{ $t.Port }} } {{- end }} +{{- end }}