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

fix: Use coalesce when desired default value is not null #2696

Merged
merged 4 commits into from
Jul 27, 2023

Conversation

Roberdvs
Copy link
Contributor

@Roberdvs Roberdvs commented Jul 21, 2023

First of all, thanks for the work on this module. We've been using it for a couple of years now ❤️

Description

Use coalesce when the desired default value of an expression is not null.

Motivation and Context

We maintain an opinionated wrapper module around this one where we used to install the EKS add-ons using their separate Terraform resources but eventually switched to doing it through this module since it supports it.

On our module, we would like to support providing add-on versions while still keeping the default behavior from this module of retrieving the matching version from a data_source if no version is provided.

This would be a snippet from our module.

variable "coredns_version" {
  type    = string
  default = null
}

variable "kube_proxy_version" {
  type    = string
  default = null
}
...
  cluster_addons = {
    coredns = {
      addon_version = var.coredns_version
    }
    kube-proxy = {
      addon_version = var.kube_proxy_version
    }
...

But since null is a valid expression when using the try function; try(each.value.addon_version, data.aws_eks_addon_version.this[each.key].version) is returning null instead of defaulting to use the version from the data_source as we would like.

Using coalesce instead should fix this.

> try(null, "v1.26.2-eksbuild.1")
null
> coalesce(null, "v1.26.2-eksbuild.1")
"v1.26.2-eksbuild.1"

As a workaround, we could re-implement the same logic for retrieving add-on versions from data sources in our wrapper module or maybe use dynamic blocks, but if you don't see anything wrong with this change it would simplify our module a bit.

Breaking Changes

This might be be considered a breaking change since it would change the behavior for end users that are explicitly passing null as input variable to the module, but it probably won't affect many users.

How Has This Been Tested?

After an upgrade from EKS 1.25 to EKS 1.26 with this input:

  cluster_addons = {
    kube-proxy = {
      addon_version = var.kube_proxy_version <-- null
    }
}

plan with try:

No changes. Your infrastructure matches the configuration

plan with coalesce:

Terraform will perform the following actions:

  # module.cluster.module.eks.aws_eks_addon.this["kube-proxy"] will be updated in-place
  ~ resource "aws_eks_addon" "this" {
      ~ addon_version     = "v1.25.6-eksbuild.2" -> "v1.26.2-eksbuild.1"
        id                = "test:kube-proxy"
        tags              = {}
        # (7 unchanged attributes hidden)

      + timeouts {}
    }

I hope I've made myself clear 😛. Let me know your thoughts and if you are OK with this change. And if you wish I can extend it to all try occurrences where null is not the desired default value.

main.tf Outdated Show resolved Hide resolved
Roberdvs and others added 2 commits July 21, 2023 15:42
@Roberdvs Roberdvs changed the title Change try to coalesce when desired default value is not null Use coalesce when desired default value is not null Jul 21, 2023
@Roberdvs Roberdvs marked this pull request as ready for review July 21, 2023 13:45
@Roberdvs Roberdvs changed the title Use coalesce when desired default value is not null fix: Use coalesce when desired default value is not null Jul 21, 2023
main.tf Outdated
@@ -386,7 +386,7 @@ resource "aws_eks_addon" "this" {
cluster_name = aws_eks_cluster.this[0].name
addon_name = try(each.value.name, each.key)

addon_version = try(each.value.addon_version, data.aws_eks_addon_version.this[each.key].version)
addon_version = coalesce(try(each.value.addon_version, null), data.aws_eks_addon_version.this[each.key].version))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my apologies, I gave you one too many parenthesis:

Suggested change
addon_version = coalesce(try(each.value.addon_version, null), data.aws_eks_addon_version.this[each.key].version))
addon_version = coalesce(try(each.value.addon_version, null), data.aws_eks_addon_version.this[each.key].version)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed! Thanks for the fix and sorry for the delay, I was disconnected these past days.

Copy link
Member

@bryantbiggs bryantbiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@bryantbiggs bryantbiggs merged commit c86f8d4 into terraform-aws-modules:master Jul 27, 2023
antonbabenko pushed a commit that referenced this pull request Jul 27, 2023
### [19.15.4](v19.15.3...v19.15.4) (2023-07-27)

### Bug Fixes

* Use `coalesce` when desired default value is not `null` ([#2696](#2696)) ([c86f8d4](c86f8d4))
@antonbabenko
Copy link
Member

This PR is included in version 19.15.4 🎉

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants