+
Skip to content

Conversation

liamg
Copy link
Member

@liamg liamg commented Aug 13, 2024

Adds basic Yor support to pull default tags values from a yaml config/$YOR_SIMPLE_TAGS env var.

e.g. the following Yor config file will result in tags A and B being applied to all resources which support tags.

tag_groups:
  - name: required_tags
    tags:
      - name: A
        value:
          default: 123456
      - name: B
        value:
          default: 654321

These are treated slightly differently from default tags in that they are still applied even if traditional default tags are not supported by the provider version/constraints.

@liamg liamg requested a review from aliscott August 13, 2024 12:46
@liamg liamg marked this pull request as ready for review August 13, 2024 12:47
@liamg liamg requested a review from tim775 August 13, 2024 13:34
}
}

sort.Strings(result)
Copy link
Member Author

Choose a reason for hiding this comment

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

This makes the output deterministic so we don't get random tests failing


tags := make(map[string]string)

// external tags (e.g. yor)
Copy link
Member

Choose a reason for hiding this comment

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

is this the right precedence for yor tags? they will be "overwritten" by default tags and any explicitly defined tags?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I think this makes sense for the first customer use-case - we effectively only fall back to Yor tags if the code doesn't already specify them.

Copy link
Member

Choose a reason for hiding this comment

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

but is that how terraform applies them? I would have guessed yor puts tags directly on resources so they would take precedence over default tags.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I think that's fair. After testing yor locally, it seems like the precedence is actually: yor tags, resource tags, default tags. Since the yor "default" tags actually overwrite any resource tags I have.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yor overrides even regular tags - when it runs it rewrites the .tf files with the specified tag values. This is something we could do in future, but Yor gets a lot more complicated with conditionals etc., so for now we wanted to simply support taking values from the config to fall back to, regardless of conditionals etc.

But yes, it would be nice to fully support this in future, where we override defaults and tags with the Yor ones, and ideally only when conditionals match too. Yor is written in Go so we could probably just call it when a config is specified and run it "for real" once we have a Go runner.

Copy link
Member Author

Choose a reason for hiding this comment

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

(sorry, wrote that before your comment appeared Alistair)

Copy link
Member Author

Choose a reason for hiding this comment

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

Writing a PR to take Yor over the other tags now 👍

Copy link
Member

Choose a reason for hiding this comment

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

I see your point -- if we're not actually supporting yor fully it seems safer to let other tags take precedence. I'm ok if you want to keep it as is, maybe just add the above as a comment explaining why they are the lowest precedence

t.Setenv("YOR_SIMPLE_TAGS", `{"Team": "Engineering"}`)
GoldenFileCommandTest(t, testutil.CalcGoldenFileTestdataDirName(), []string{"breakdown", "--config-file", "./testdata/infracost-config-yor.yml", "--format", "json"}, &GoldenFileOptions{
IsJSON: true,
RegexFilter: regexp.MustCompile(`"path": "[^"]*",`),
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious why the

JSONInclude: regexp.MustCompile("^(defaultTags|tags|name)$"),
JSONExclude: regexp.MustCompile("^(costComponents|pastBreakdown)$"),

didn't work for you? It would think it made the golden files a lost less noisy?

Copy link
Member Author

Choose a reason for hiding this comment

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

My main problem was misunderstanding how they worked tbh, I think they would work if I listed needed properties in the include - I had tried .* in include and path in exclude which failed to exclude path because an ancestor had already been included.

Copy link
Member

Choose a reason for hiding this comment

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

I guess it's "Show me only keys that match JSONInclude anywhere they appear in the tree, unless they appear under a key that matches JSONExclude"

@liamg liamg merged commit baaec9e into master Aug 13, 2024
@liamg liamg deleted the yor-support branch August 13, 2024 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载