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

Add ability to refer to other project service accounts in Project Factory #2900

Merged
merged 3 commits into from
Feb 19, 2025

Conversation

wiktorn
Copy link
Collaborator

@wiktorn wiktorn commented Feb 16, 2025

  • Allow interpolation of service accounts created for other projects, by specifying:
  iam:
    "roles/....":
    - ${project}/${service_account}
  • Allow lookup of project service accounts for shared_vpc_service_config.network_users (also other projects, just to keep the code the same, though it doesn't make much sense)
  • Allow lookup of project and other projects service accounts for bucket IAM
  • Add support for extra_dirs for tfvars tests
  • Add tfvars tests for project_factory

Checklist

I applicable, I acknowledge that I have:

  • Read the contributing guide
  • Ran terraform fmt on all modified files
  • Regenerated the relevant README.md files using tools/tfdoc.py
  • Made sure all relevant tests pass

Breaking Changes

`modules/project`: move input variable `service_agents_config.services_enabled` to `project_reuse.project_attributes.services_enabled`

Copy link
Collaborator

@ludoo ludoo left a comment

Choose a reason for hiding this comment

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

I like this approach, it's lean and tries incremental matches. I would put own service accounts first, then other projects, then context.

I don't really grasp your TODO though, can you explain it?

@wiktorn
Copy link
Collaborator Author

wiktorn commented Feb 16, 2025

The idea is to feed all "local" context into local.context variable:

diff --git a/modules/project-factory/main.tf b/modules/project-factory/main.tf
index 736bfb093..1e5416684 100644
--- a/modules/project-factory/main.tf
+++ b/modules/project-factory/main.tf
@@ -20,21 +20,25 @@ locals {
   context = {
     folder_ids = merge(
       var.factories_config.context.folder_ids,
       local.hierarchy
     )
     iam_principals = merge(
       var.factories_config.context.iam_principals,
       {
         for k, v in module.automation-service-accounts :
         k => v.iam_email
-      }
+      },
+      {
+        for k, v in module.service-accounts :
+        k => v.iam_email
+      },
     )
   }
 }
@@ -112,29 +116,23 @@ module "projects-iam" {
   iam = {
     for k, v in lookup(each.value, "iam", {}) : k => [
       for vv in v : try(
-        # other projects service accounts
-        module.service-accounts[vv].iam_email,
-        # other automation service account
-        local.context.iam_principals[vv],
-        # project service accounts
-        module.service-accounts["${each.key}/${vv}"].iam_email,
-        # automation service account
+        # project service account
         local.context.iam_principals["${each.key}/${vv}"],
-        # other context
+        # other projects and external context
         local.context.iam_principals[vv],
         # passthrough
         vv
       )
     ]
   }

And replicate above change in other 2 iam_*. This will simplify the code and I fail to find a case, where it will give different result.

Question is what to do about buckets:

module "buckets" {
...
  iam = {
    for k, v in each.value.iam : k => [
      for vv in v : try(
        # other projects service accounts
        module.service-accounts[vv].iam_email,
        module.service-accounts["${each.value.project}/${vv}"].iam_email,
        var.factories_config.context.iam_principals[vv],
        vv
      )
    ]
  }

Should this use exactly the same construct, as we use for projects-iam, I do not follow why IaC service accounts are excluded here.

@ludoo
Copy link
Collaborator

ludoo commented Feb 16, 2025

The idea is to feed all "local" context into local.context variable:

Ok I get it and it makes sense. :)

Should this use exactly the same construct, as we use for projects-iam, I do not follow why IaC service accounts are excluded here.

There's a very delicate balance with dependency cycles. If it's excluded it's usually because I ran into a cycle. I broke up the project module to avoid some, but at some point we need to draw a line. :)

Try, and see what you get. We need a "golden set" of patterns in YAML to test against, I don't think we have one for the new approach of using in-project buckets and service accounts. I would not merge without testing though, I can help you compile a set of YAMLs tomorrow if you want.

@wiktorn wiktorn force-pushed the wiktorn-pf-multi-iam branch from bb43d22 to ca1134e Compare February 18, 2025 10:38
@github-actions github-actions bot added the on:tools New or changed tool label Feb 18, 2025
@wiktorn wiktorn force-pushed the wiktorn-pf-multi-iam branch 2 times, most recently from 9288f33 to ee3eb98 Compare February 19, 2025 08:29
@wiktorn wiktorn marked this pull request as ready for review February 19, 2025 08:29
@wiktorn wiktorn force-pushed the wiktorn-pf-multi-iam branch from 96ded3c to 523845c Compare February 19, 2025 14:04
@wiktorn wiktorn force-pushed the wiktorn-pf-multi-iam branch from 523845c to ddaf830 Compare February 19, 2025 15:13
@wiktorn wiktorn merged commit 58051e4 into fast-dev Feb 19, 2025
19 checks passed
@wiktorn wiktorn deleted the wiktorn-pf-multi-iam branch February 19, 2025 15:47
@ludoo
Copy link
Collaborator

ludoo commented Feb 19, 2025

🎉

@wiktorn wiktorn added the breaks:modules Breaking changes in a module interface or resources. label Feb 19, 2025
@juliocc
Copy link
Collaborator

juliocc commented Feb 19, 2025

Nice even number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaks:modules Breaking changes in a module interface or resources. on:modules on:tools New or changed tool
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants