Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(provisioner/terraform/tfparse): skip evaluation of unrelated parameters#16023

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
johnstcn merged 4 commits intomainfromcj/16021/skip-no-workspace-tags
Jan 3, 2025

Conversation

johnstcn
Copy link
Member

@johnstcnjohnstcn commentedJan 3, 2025
edited
Loading

Fixes#16021

tfparse had been enforcing the "no functions, no locals" rule forallcoder_parameter default values, where we only want to enforce this for ones referenced bycoder_workspace_tags.

  • Improves tfparse test coverage to include more parameter types and values
  • Adds tests with unrelated parameters that should be ignored by tfparse
  • Modifies tfparse to only attempt evaluation of variables/parameters referenced by coder_workspace_tags

@johnstcnjohnstcn self-assigned thisJan 3, 2025
@johnstcnjohnstcn changed the titlefix(provisioner/terraform/tfparse): skip evaluation of defaults if no workspace_tags foundfix(provisioner/terraform/tfparse): skip evaluation of unrelated parametersJan 3, 2025
"stringparam":"a",
"numparam":"1",
"boolparam":"true",
"listparam":`["a", "b"]`,
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

review: this ends up being allowed due to thedefault ofcoder_parameter being a string.

}`,
},
expectTags:nil,
expectError:`can't convert variable default value to string: unsupported type []interface {}`,
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

review: this is also not allowed by the provider:

│   on main.tf line 38, in data "coder_workspace_tags" "tags":│   38:   tags = {│   39:     "listvar" = ["a"]│   40:   }││ Inappropriate value for attribute "tags": element "listvar": string required.

}`,
},
expectTags:nil,
expectError:`can't convert variable default value to string: unsupported type map[string]interface {}`,
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

review: also not allowed by the provider:

│   on main.tf line 38, in data "coder_workspace_tags" "tags":│   38:   tags = {│   39:     "mapvar" = {a = "b"}│   40:   }││ Inappropriate value for attribute "tags": element "mapvar": string required.

}

varfoundbool
needle:=strings.Join([]string{"data",dataResource.Type,dataResource.Name},".")
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

As far as I can tell, there is no way to break a variable reference over multiple lines e.g.

data.\coder_parameter.\foo.\value

Copy link
Member

Choose a reason for hiding this comment

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

correct 👍

Copy link
Member

@mtojekmtojek left a comment

Choose a reason for hiding this comment

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

I approve this change although I'm concerned about the overall parsing logic. With another patch it will become harder to debug or touch internals in the future.

}

varfoundbool
needle:=strings.Join([]string{"data",dataResource.Type,dataResource.Name},".")
Copy link
Member

Choose a reason for hiding this comment

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

correct 👍

Comment on lines 265 to 269
for_,tv:=rangetags {
ifstrings.Contains(tv,v.Name) {
found=true
}
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: you could extract this block and the one below to a common helper function

@johnstcnjohnstcnforce-pushed thecj/16021/skip-no-workspace-tags branch fromce68fe2 toe56cac7CompareJanuary 3, 2025 13:19
@johnstcn
Copy link
MemberAuthor

Smoke-tested on personal deployment with example from linked issue.

Screen.Recording.2025-01-03.at.13.27.45.mov

Note that issue described in#16022 is still present if that parameter is referenced incoder_workspace_tags.

mtojek and matifali reacted with thumbs up emoji

}
varfoundbool
for_,tv:=rangetags {
ifstrings.Contains(tv,v.Name) {
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Suggested change
ifstrings.Contains(tv,v.Name) {
ifstrings.Contains(tv,"var."+v.Name) {

}
varfoundbool
for_,tv:=rangetags {
ifstrings.Contains(tv,v.Name) {
Copy link
Member

Choose a reason for hiding this comment

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

won't astrings.Contains also contain variable names that are subsets?

Likevar.Foo will match bothFoo andvar.FooBar

Copy link
Member

@EmyrkEmyrkJan 3, 2025
edited
Loading

Choose a reason for hiding this comment

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

Here is a test case:

{name:"overlapping var name",files:map[string]string{`main.tf`:`      // This file is the same as the above, except for this comment.      variable "a" {         type = string         default = "1"      }      variable "ab" {       description = "This is a variable of type string"       type        = string       default     = jsonencode(["a", "b"])      }      data "coder_workspace_tags" "tags" {         tags = {            "foo": "bar",            "a": var.a,         }      }`,   },reqTags:map[string]string{"foo":"noclobber"},wantTags:map[string]string{"owner":"","scope":"organization","foo":"bar","a":"1"},},

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Gotcha, we may then need to do some more "intelligent" parsing.
I hesitate to reach for a regexp.... but\b may be exactly what we need here

Copy link
Member

Choose a reason for hiding this comment

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

Is the reason you do a contains because it could be an expression likefoo(var.name)?

Copy link
Member

Choose a reason for hiding this comment

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

Is there any way to parse the expression and get the actual tokens? Or we only havestring in the HCL?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Yep. I'm looking into HCL traversals now as they might allow us to do this cleanly.

Emyrk reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

OK, I've modified the implementation to instead:

  • Gather the set of variables referenced by the tags up-front,
  • Only evaluate the default value(s) forcoder_parameters referenced by those tags.
  • As default values of coder_parameters can reference other variables, to avoid needing to perform a graph traversal of all the dependencies here I've elected to simply continue passing in all the variable default values. We don't immediately enforce default values for all variables.
  • To avoid unrelated variables with default values of complex types being rejected, I added some 'last-ditch' support for JSON-encoding them.

@johnstcnjohnstcn requested a review fromEmyrkJanuary 3, 2025 18:43
@johnstcnjohnstcn merged commit1ab10cf intomainJan 3, 2025
30 checks passed
@johnstcnjohnstcn deleted the cj/16021/skip-no-workspace-tags branchJanuary 3, 2025 19:32
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsJan 3, 2025
@matifali
Copy link
Member

/cherry-pick release-2.18

@matifali
Copy link
Member

/cherry-pick release/2.18

Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@mtojekmtojekmtojek approved these changes

@SasSwartSasSwartSasSwart approved these changes

@EmyrkEmyrkAwaiting requested review from Emyrk

Assignees

@johnstcnjohnstcn

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

coder_parameter regression: cannot use lists and functions anymore

5 participants

@johnstcn@matifali@Emyrk@mtojek@SasSwart

[8]ページ先頭

©2009-2025 Movatter.jp