Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform feature fixes #243

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/core/terraform_config/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] }
rafaelgomesxyz marked this conversation as resolved.
Show resolved Hide resolved

put("#{name} = #{content}", indent: 2)
end

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions lib/core/terraform_config/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Generator # rubocop:disable Metrics/ClassLength
WORKLOAD_SPEC_KEYS = %i[
type
containers
identity_link
zzaakiirr marked this conversation as resolved.
Show resolved Hide resolved
default_options
local_options
rollout_options
Expand Down Expand Up @@ -120,7 +121,7 @@ def agent_config_params
def workload_config_params
template
.slice(:name, :description, :tags)
.merge(gvc: gvc, identity: workload_identity)
zzaakiirr marked this conversation as resolved.
Show resolved Hide resolved
.merge(gvc: gvc)
.merge(workload_spec_params)
end

Expand Down Expand Up @@ -176,13 +177,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
Expand Down
8 changes: 4 additions & 4 deletions lib/core/terraform_config/workload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion lib/core/terraform_config/workload/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
zzaakiirr marked this conversation as resolved.
Show resolved Hide resolved

name = var.name
description = var.description
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions lib/core/terraform_config/workload/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,8 @@ variable "gvc" {
type = string
}

variable "identity" {
type = object({
self_link = string
})
variable "identity_link" {
type = string
default = null
}

Expand All @@ -155,7 +153,7 @@ variable "load_balancer" {
type = object({
direct = optional(
object({
enabled = number
enabled = bool
port = optional(
list(
object({
Expand Down
2 changes: 1 addition & 1 deletion spec/core/terraform_config/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,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(
zzaakiirr marked this conversation as resolved.
Show resolved Hide resolved
autoscaling: {
max_concurrency: 0,
Expand Down
2 changes: 1 addition & 1 deletion spec/core/terraform_config/gvc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
zzaakiirr marked this conversation as resolved.
Show resolved Hide resolved
env = {
var1 = "value"
var2 = 1
Expand Down
4 changes: 2 additions & 2 deletions spec/core/terraform_config/workload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
zzaakiirr marked this conversation as resolved.
Show resolved Hide resolved
type: type,
support_dynamic_tags: true,
containers: containers,
Expand All @@ -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: {
Expand Down