Skip to content

Commit 4233b8b

Browse files
authored
Terraform feature fixes (#243)
* Use raw identiy_link for workload TF config * Always remove quotes from expression values * Fix terraform variable type * Fix gitignore for generated files * Use inherit_env variable in TF workload module
1 parent 344c666 commit 4233b8b

File tree

8 files changed

+18
-23
lines changed

8 files changed

+18
-23
lines changed

lib/core/terraform_config/dsl.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def argument(name, value, optional: false, raw: false)
3030
"#{tf_value(value)}\n"
3131
end
3232

33+
# remove quotes from expression values
34+
content = content.gsub(/("#{EXPRESSION_PATTERN}.*")/) { ::Regexp.last_match(1)[1...-1] }
35+
3336
put("#{name} = #{content}", indent: 2)
3437
end
3538

@@ -58,7 +61,6 @@ def tf_string_value(value)
5861
def tf_hash_value(value)
5962
JSON.pretty_generate(value.crush)
6063
.gsub(/"(\w+)":/) { "#{::Regexp.last_match(1)}:" } # remove quotes from keys
61-
.gsub(/("#{EXPRESSION_PATTERN}.*")/) { ::Regexp.last_match(1)[1...-1] } # remove quotes from expression values
6264
end
6365

6466
def expression?(value)

lib/core/terraform_config/generator.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Generator # rubocop:disable Metrics/ClassLength
66
WORKLOAD_SPEC_KEYS = %i[
77
type
88
containers
9+
identity_link
910
default_options
1011
local_options
1112
rollout_options
@@ -120,7 +121,7 @@ def agent_config_params
120121
def workload_config_params
121122
template
122123
.slice(:name, :description, :tags)
123-
.merge(gvc: gvc, identity: workload_identity)
124+
.merge(gvc: gvc)
124125
.merge(workload_spec_params)
125126
end
126127

@@ -176,13 +177,6 @@ def policy_bindings
176177
end
177178
end
178179

179-
def workload_identity
180-
identity_link = template.dig(:spec, :identity_link)
181-
return if identity_link.nil?
182-
183-
"cpln_identity.#{identity_link.split('/').last}"
184-
end
185-
186180
def kind
187181
@kind ||= template[:kind]
188182
end

lib/core/terraform_config/workload.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Workload < Base
2121
].freeze
2222

2323
attr_reader :type, :name, :gvc, :containers,
24-
:description, :tags, :support_dynamic_tags, :firewall_spec, :identity,
24+
:description, :tags, :support_dynamic_tags, :firewall_spec, :identity_link,
2525
:options, :local_options, :rollout_options, :security_options, :load_balancer, :job
2626

2727
def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
@@ -33,7 +33,7 @@ def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
3333
tags: nil,
3434
support_dynamic_tags: false,
3535
firewall_spec: nil,
36-
identity: nil,
36+
identity_link: nil,
3737
options: nil,
3838
local_options: nil,
3939
rollout_options: nil,
@@ -52,7 +52,7 @@ def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
5252

5353
@containers = containers
5454
@firewall_spec = firewall_spec
55-
@identity = identity
55+
@identity_link = identity_link
5656

5757
@options = options
5858
@local_options = local_options
@@ -71,7 +71,7 @@ def to_tf
7171
argument :type, type
7272
argument :name, name
7373
argument :gvc, gvc
74-
argument :identity, identity, optional: true
74+
argument :identity_link, identity_link, optional: true
7575
argument :support_dynamic_tags, support_dynamic_tags, optional: true
7676

7777
RAW_ARGS.each { |arg_name| argument arg_name, send(:"#{arg_name}_arg"), raw: true, optional: true }

lib/core/terraform_config/workload/main.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ resource "cpln_workload" "workload" {
22
type = var.type
33

44
gvc = var.gvc
5-
identity_link = var.identity != null ? var.identity.self_link : null
5+
identity_link = var.identity_link
66

77
name = var.name
88
description = var.description
@@ -19,6 +19,7 @@ resource "cpln_workload" "workload" {
1919
args = container.value.args
2020
command = container.value.command
2121
env = container.value.envs
22+
inherit_env = container.value.inherit_env
2223
image = container.value.image
2324

2425
cpu = container.value.cpu

lib/core/terraform_config/workload/variables.tf

+3-5
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,8 @@ variable "gvc" {
133133
type = string
134134
}
135135

136-
variable "identity" {
137-
type = object({
138-
self_link = string
139-
})
136+
variable "identity_link" {
137+
type = string
140138
default = null
141139
}
142140

@@ -155,7 +153,7 @@ variable "load_balancer" {
155153
type = object({
156154
direct = optional(
157155
object({
158-
enabled = number
156+
enabled = bool
159157
port = optional(
160158
list(
161159
object({

spec/core/terraform_config/generator_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@
497497
}
498498
)
499499

500-
expect(main_tf_config.identity).to eq("cpln_identity.identity-name")
500+
expect(main_tf_config.identity_link).to eq("//gvc/gvc-name/identity/identity-name")
501501
expect(main_tf_config.options).to eq(
502502
autoscaling: {
503503
max_concurrency: 0,

spec/core/terraform_config/gvc_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
}
3535
domain = "app.example.com"
3636
locations = ["aws-us-east-1", "aws-us-east-2"]
37-
pull_secrets = ["cpln_secret.docker.name"]
37+
pull_secrets = [cpln_secret.docker.name]
3838
env = {
3939
var1 = "value"
4040
var2 = 1

spec/core/terraform_config/workload_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
description: "main workload description",
1010
tags: { tag1: "tag1_value", tag2: "tag2_value" },
1111
gvc: "cpln_gvc.app-name.name",
12-
identity: "cpln_identity.identity-name",
12+
identity_link: "//gvc/gvc-name/identity/app-identity",
1313
type: type,
1414
support_dynamic_tags: true,
1515
containers: containers,
@@ -35,7 +35,7 @@ module "main" {
3535
type = "standard"
3636
name = "main"
3737
gvc = cpln_gvc.app-name.name
38-
identity = cpln_identity.identity-name
38+
identity_link = "//gvc/gvc-name/identity/app-identity"
3939
support_dynamic_tags = true
4040
containers = {
4141
rails: {

0 commit comments

Comments
 (0)