Skip to content

Commit

Permalink
Update keda terraform to latest patterns (#2623)
Browse files Browse the repository at this point in the history
* update keda terraform to latest patterns

Signed-off-by: James Petersen <[email protected]>

* use tfgen with keda

Signed-off-by: James Petersen <[email protected]>

* cross product

Signed-off-by: James Petersen <[email protected]>

* add some comments on locals

Signed-off-by: James Petersen <[email protected]>

* properly set target repo

Signed-off-by: James Petersen <[email protected]>

* remove hard coded bounds

Signed-off-by: James Petersen <[email protected]>

---------

Signed-off-by: James Petersen <[email protected]>
  • Loading branch information
found-it authored May 6, 2024
1 parent 63c17ff commit 4c0e420
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 144 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TERRAFORM ?= $(shell command -v terraform)

# These images either do something with Alpine,
# or are somehow incompatible with tfgen (still using tagger etc.)
TFGEN_SKIP ?= busybox,calico,git,graalvm-native,harbor,k3s,keda,kubeflow,kubeflow-katib,maven,powershell,prometheus,static,terraform
TFGEN_SKIP ?= busybox,calico,git,graalvm-native,harbor,k3s,kubeflow,kubeflow-katib,maven,powershell,prometheus,static,terraform

# These are the tfgen generators applied to this repo (in order)
TFGEN_GENERATORS ?= Image01Outputs,Toplevel01Modules,Toplevel02Outputs
Expand Down
9 changes: 9 additions & 0 deletions generated.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions images/keda/config/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
variable "name" {
description = "Component name (e.g. keda, keda-adapter, keda-admission-webhooks)"
}

variable "package" {
description = "Package name (e.g. keda-2.14)"
}

variable "extra_packages" {
description = "Additional packages to install."
type = list(string)
default = []
}

locals {
entrypoints = {
"keda" = "/usr/bin/keda --zap-log-level=info --zap-encoder=console",
"keda-adapter" = "/usr/bin/keda-adapter --secure-port=6443 --logtostderr=true --v=0",
"keda-admission-webhooks" = "/usr/bin/keda-admission-webhooks --zap-log-level=info --zap-encoder=console",
}
}

module "accts" { source = "../../../tflib/accts" }

output "config" {
value = jsonencode({
contents = {
packages = concat([
var.package, // keda, keda-adapter, keda-admission-webhooks
"busybox",
"keda-compat",
], var.extra_packages)
}
accounts = module.accts.block
entrypoint = {
command = local.entrypoints[var.name]
}
})
}
18 changes: 0 additions & 18 deletions images/keda/configs/latest.adapter.apko.yaml

This file was deleted.

18 changes: 0 additions & 18 deletions images/keda/configs/latest.controller.apko.yaml

This file was deleted.

18 changes: 0 additions & 18 deletions images/keda/configs/latest.webhooks.apko.yaml

This file was deleted.

13 changes: 13 additions & 0 deletions images/keda/generated.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 103 additions & 31 deletions images/keda/main.tf
Original file line number Diff line number Diff line change
@@ -1,55 +1,127 @@
locals {
# List of all components
components = toset([
"controller",
"adapter",
"webhooks",
"keda",
"keda-adapter",
"keda-admission-webhooks",
])

packages = merge(
{ for k in local.components : k => k },
{
"controller" = "keda"
"adapter" = "keda-adapter"
"webhooks" = "keda-admission-webhooks"
},
)
# List of versions.
#
# When version metadata is not available this will look like
# "versions" = [""]
#
# When version metadata is available this will look like
# "versions" = ["2.13", "2.14"]
#
# Order matters here, we want the latest version to be the last element in
# the list so it's tagged with latest.
versions = [
for _, version_metadata in module.versions.versions : lookup(version_metadata, "version", "")
]

repositories = merge(
{ for k in local.components : k => k },
{
"controller" = var.target_repository
"adapter" = "${var.target_repository}-adapter"
"webhooks" = "${var.target_repository}-admission-webhooks"
},
)
# Cross product of versions and components
#
# When version metadata is not available this will look like
# "component_versions" = {
# "keda" = {
# "component" = "keda"
# "is_latest" = true
# "main" = "keda"
# }
# "keda-adapter" = {
# "component" = "keda-adapter"
# "is_latest" = true
# "main" = "keda-adapter"
# }
# ...
# }
#
# When version metadata is not available this will look like
# "component_versions" = {
# "keda2.14" = {
# "component" = "keda"
# "is_latest" = true
# "main" = "keda-2.14"
# }
# "keda-adapter2.14" = {
# "component" = "keda-adapter"
# "is_latest" = true
# "main" = "keda-2.14-adapter"
# }
# ...
# }
component_versions = merge([
for component in local.components : merge([
for key, version_metadata in module.versions.versions : {

# This sets the key to `$component` when version data does not exist and
# `$component$version` when version data exists. We just smash the two
# together to make it easier to access in subsequent modules.
format("%s%s", component, lookup(version_metadata, "version", "")) : {
is_latest = version_metadata.is_latest
component = component

# Strips off `keda` or `keda-` from the components and joins with the
# version_metadata key which will be `keda` when version data does not
# exist and `keda-$version` when version data exists
main = join("-", compact([key, trimprefix(replace(component, "keda", ""), "-")]))
}
}
]...)
]...)
}

variable "target_repository" {
description = "The docker repo into which the image and attestations should be published."
}

module "latest" {
for_each = local.repositories
# Versions module plugs into to the version metadata for the top-level package
module "versions" {
source = "../../tflib/versions"
package = basename(path.module)
}

# Need a config for every version of every component
module "config" {
for_each = local.component_versions
source = "./config"
name = each.value.component
package = each.value.main
}

# Need a publisher invocation for every version of every component
module "versioned" {
for_each = local.component_versions
source = "../../tflib/publisher"
name = basename(path.module)
target_repository = each.value
config = file("${path.module}/configs/latest.${each.key}.apko.yaml")
target_repository = replace(var.target_repository, "/keda", "/${each.value.component}")
config = module.config[each.key].config
build-dev = true

main_package = each.value.main
update-repo = each.value.is_latest
}

module "test-latest" {
source = "./tests"
digests = { for k, v in module.latest : k => v.image_ref }
# Tests need to be grouped by version. We want to test all components of
# the same version at the same time.
module "test" {
for_each = toset(local.versions)
source = "./tests"

digests = { for component in local.components : component => module.versioned["${component}${each.value}"].image_ref }
name = "${basename(path.module)}${each.value}"
}

# Tagger should be grouped by components. We want to tag multiple versions of
# the same component so we iterate over the versions list for each component
module "tagger" {
for_each = local.components
source = "../../tflib/tagger"

depends_on = [module.test-latest]
depends_on = [module.test]

tags = {
"latest" = module.latest[each.key].image_ref
"latest-dev" = module.latest[each.key].dev_ref
}
tags = merge(
[for version in local.versions : module.versioned["${each.value}${version}"].latest_tag_map]...
)
}
Loading

0 comments on commit 4c0e420

Please sign in to comment.