From 44bc9274d6ac7e0635e945a287f91516818260b2 Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Tue, 12 Nov 2024 13:29:01 +0300 Subject: [PATCH 1/5] Use raw identiy_link for workload TF config --- lib/core/terraform_config/generator.rb | 10 ++-------- lib/core/terraform_config/workload.rb | 8 ++++---- lib/core/terraform_config/workload/main.tf | 2 +- lib/core/terraform_config/workload/variables.tf | 6 ++---- spec/core/terraform_config/generator_spec.rb | 2 +- spec/core/terraform_config/workload_spec.rb | 4 ++-- 6 files changed, 12 insertions(+), 20 deletions(-) diff --git a/lib/core/terraform_config/generator.rb b/lib/core/terraform_config/generator.rb index f0cd9b3a..763a53a3 100644 --- a/lib/core/terraform_config/generator.rb +++ b/lib/core/terraform_config/generator.rb @@ -6,6 +6,7 @@ class Generator # rubocop:disable Metrics/ClassLength WORKLOAD_SPEC_KEYS = %i[ type containers + identity_link default_options local_options rollout_options @@ -107,7 +108,7 @@ def volumeset_config_params def workload_config_params template .slice(:name, :description, :tags) - .merge(gvc: gvc, identity: workload_identity) + .merge(gvc: gvc) .merge(workload_spec_params) end @@ -163,13 +164,6 @@ def policy_bindings end end - def workload_identity - identity_link = template.dig(:spec, :identity_link) - return if identity_link.nil? - - "cpln_identity.#{identity_link.split('/').last}" - end - def kind @kind ||= template[:kind] end diff --git a/lib/core/terraform_config/workload.rb b/lib/core/terraform_config/workload.rb index d019e729..8b997c19 100644 --- a/lib/core/terraform_config/workload.rb +++ b/lib/core/terraform_config/workload.rb @@ -21,7 +21,7 @@ class Workload < Base ].freeze attr_reader :type, :name, :gvc, :containers, - :description, :tags, :support_dynamic_tags, :firewall_spec, :identity, + :description, :tags, :support_dynamic_tags, :firewall_spec, :identity_link, :options, :local_options, :rollout_options, :security_options, :load_balancer, :job def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength @@ -33,7 +33,7 @@ def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength tags: nil, support_dynamic_tags: false, firewall_spec: nil, - identity: nil, + identity_link: nil, options: nil, local_options: nil, rollout_options: nil, @@ -52,7 +52,7 @@ def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength @containers = containers @firewall_spec = firewall_spec - @identity = identity + @identity_link = identity_link @options = options @local_options = local_options @@ -71,7 +71,7 @@ def to_tf argument :type, type argument :name, name argument :gvc, gvc - argument :identity, identity, optional: true + argument :identity_link, identity_link, optional: true argument :support_dynamic_tags, support_dynamic_tags, optional: true RAW_ARGS.each { |arg_name| argument arg_name, send(:"#{arg_name}_arg"), raw: true, optional: true } diff --git a/lib/core/terraform_config/workload/main.tf b/lib/core/terraform_config/workload/main.tf index 38780f77..36ef508b 100644 --- a/lib/core/terraform_config/workload/main.tf +++ b/lib/core/terraform_config/workload/main.tf @@ -2,7 +2,7 @@ resource "cpln_workload" "workload" { type = var.type gvc = var.gvc - identity_link = var.identity != null ? var.identity.self_link : null + identity_link = var.identity_link name = var.name description = var.description diff --git a/lib/core/terraform_config/workload/variables.tf b/lib/core/terraform_config/workload/variables.tf index c5ed200b..0556c1ed 100644 --- a/lib/core/terraform_config/workload/variables.tf +++ b/lib/core/terraform_config/workload/variables.tf @@ -133,10 +133,8 @@ variable "gvc" { type = string } -variable "identity" { - type = object({ - self_link = string - }) +variable "identity_link" { + type = string default = null } diff --git a/spec/core/terraform_config/generator_spec.rb b/spec/core/terraform_config/generator_spec.rb index 976380fe..31e36107 100644 --- a/spec/core/terraform_config/generator_spec.rb +++ b/spec/core/terraform_config/generator_spec.rb @@ -469,7 +469,7 @@ } ) - expect(main_tf_config.identity).to eq("cpln_identity.identity-name") + expect(main_tf_config.identity_link).to eq("//gvc/gvc-name/identity/identity-name") expect(main_tf_config.options).to eq( autoscaling: { max_concurrency: 0, diff --git a/spec/core/terraform_config/workload_spec.rb b/spec/core/terraform_config/workload_spec.rb index 6b433d43..a4ed93b5 100644 --- a/spec/core/terraform_config/workload_spec.rb +++ b/spec/core/terraform_config/workload_spec.rb @@ -9,7 +9,7 @@ description: "main workload description", tags: { tag1: "tag1_value", tag2: "tag2_value" }, gvc: "cpln_gvc.app-name.name", - identity: "cpln_identity.identity-name", + identity_link: "//gvc/gvc-name/identity/app-identity", type: type, support_dynamic_tags: true, containers: containers, @@ -35,7 +35,7 @@ module "main" { type = "standard" name = "main" gvc = cpln_gvc.app-name.name - identity = cpln_identity.identity-name + identity_link = "//gvc/gvc-name/identity/app-identity" support_dynamic_tags = true containers = { rails: { From d8d45ab1cbdc06e298da1c7d7c0a242bf06d404d Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Tue, 12 Nov 2024 16:24:21 +0300 Subject: [PATCH 2/5] Always remove quotes from expression values --- lib/core/terraform_config/dsl.rb | 4 +++- spec/core/terraform_config/gvc_spec.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/core/terraform_config/dsl.rb b/lib/core/terraform_config/dsl.rb index cb3b1b29..64ffdd62 100644 --- a/lib/core/terraform_config/dsl.rb +++ b/lib/core/terraform_config/dsl.rb @@ -30,6 +30,9 @@ def argument(name, value, optional: false, raw: false) "#{tf_value(value)}\n" end + # remove quotes from expression values + content = content.gsub(/("#{EXPRESSION_PATTERN}.*")/) { ::Regexp.last_match(1)[1...-1] } + put("#{name} = #{content}", indent: 2) end @@ -58,7 +61,6 @@ def tf_string_value(value) def tf_hash_value(value) JSON.pretty_generate(value.crush) .gsub(/"(\w+)":/) { "#{::Regexp.last_match(1)}:" } # remove quotes from keys - .gsub(/("#{EXPRESSION_PATTERN}.*")/) { ::Regexp.last_match(1)[1...-1] } # remove quotes from expression values end def expression?(value) diff --git a/spec/core/terraform_config/gvc_spec.rb b/spec/core/terraform_config/gvc_spec.rb index fd12b77e..e9b8f935 100644 --- a/spec/core/terraform_config/gvc_spec.rb +++ b/spec/core/terraform_config/gvc_spec.rb @@ -34,7 +34,7 @@ } domain = "app.example.com" locations = ["aws-us-east-1", "aws-us-east-2"] - pull_secrets = ["cpln_secret.docker.name"] + pull_secrets = [cpln_secret.docker.name] env = { var1 = "value" var2 = 1 From c719ac5b5893222e88d05d272b9efa2311fe79ba Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Tue, 12 Nov 2024 19:55:07 +0300 Subject: [PATCH 3/5] Fix terraform variable type --- lib/core/terraform_config/workload/variables.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/terraform_config/workload/variables.tf b/lib/core/terraform_config/workload/variables.tf index 0556c1ed..4057d4a0 100644 --- a/lib/core/terraform_config/workload/variables.tf +++ b/lib/core/terraform_config/workload/variables.tf @@ -153,7 +153,7 @@ variable "load_balancer" { type = object({ direct = optional( object({ - enabled = number + enabled = bool port = optional( list( object({ From cb3abb1deab44af45307724121bf101e7ed8f1f8 Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Fri, 15 Nov 2024 10:46:18 +0300 Subject: [PATCH 4/5] Fix gitignore for generated files --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a977a4d4..b0cb4689 100644 --- a/.gitignore +++ b/.gitignore @@ -18,5 +18,5 @@ /spec/dummy/.controlplane/controlplane*-tmp-*.yml # Generated configs -terraform/ -.controlplane/ +/terraform/ +/.controlplane/ From 1be9c1546b4ec620352453e354d67320a7cb566d Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Tue, 26 Nov 2024 12:51:07 +0300 Subject: [PATCH 5/5] Use inherit_env variable in TF workload module --- lib/core/terraform_config/workload/main.tf | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/core/terraform_config/workload/main.tf b/lib/core/terraform_config/workload/main.tf index 36ef508b..62565361 100644 --- a/lib/core/terraform_config/workload/main.tf +++ b/lib/core/terraform_config/workload/main.tf @@ -19,6 +19,7 @@ resource "cpln_workload" "workload" { args = container.value.args command = container.value.command env = container.value.envs + inherit_env = container.value.inherit_env image = container.value.image cpu = container.value.cpu