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 (*artifact.Config).Unpack not preserving not overridden values #3930

Merged

Conversation

AndersonQ
Copy link
Member

What does this PR do?

(*artifact.Config).Unpack was creating a new instance of Config and manually coping all values from the original to the new one. New values added to config, RetrySleepInitDuration in this case, were neither maintaining its default value nor being updated with the new value. Now it uses reflection to ensure all values are correctly copied.

Why is it important?

When applying a new agent.download config some of the default values were set to the zero value and even if they'd come on the new fleet config, they'd be ignored.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • [ ] I have added an entry in ./changelog/fragments using the changelog tool
  • [ ] I have added an integration test or an E2E test

How to test this PR locally

Try any of the following:

Related issues

Questions to ask yourself

  • How are we going to support this in production?
  • How are we going to measure its adoption?
  • How are we going to debug this?
  • What are the metrics I should take care of?
  • ...

(*artifact.Config).Unpack was creating a new instance of Config and manually coping all values from the original to the new one. New values added to config, RetrySleepInitDuration in this case, were neither maintaining its default value nor being updated with the new value.
Now it uses reflection to ensure all values are correctly copied.
@AndersonQ AndersonQ added Team:Elastic-Agent Label for the Agent team backport-v8.12.0 Automated backport with mergify labels Dec 19, 2023
@AndersonQ AndersonQ self-assigned this Dec 19, 2023
@AndersonQ AndersonQ requested a review from a team as a code owner December 19, 2023 19:10
@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent (Team:Elastic-Agent)

@cmacknz
Copy link
Member

cmacknz commented Dec 19, 2023

*artifact.Config).Unpack was creating a new instance of Config and manually coping all values from the original to the new one.

Do we know why it was doing this? Are there any hints in the commit history? This seems intentional so I am wondering if this was because of a bug or something from the past.

@cmacknz
Copy link
Member

cmacknz commented Dec 19, 2023

Ah the answer is the comment, thanks!

Copy link
Contributor

@blakerouse blakerouse left a comment

Choose a reason for hiding this comment

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

This is just nasty. Really sucks that this needs to be done this way. I think the way you handled it is the best way to go currently.

Really glad you added a test to ensure those structures stay inline.

@cmacknz
Copy link
Member

cmacknz commented Dec 19, 2023

Since we need to special case the transport, which includes the proxy configuration, I wonder if we should implement #3820 as part of this PR to ensure we didn't break it.

It's already in the current sprint and assigned to Anderson anyway.

Copy link
Contributor

mergify bot commented Dec 20, 2023

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b 3914-fix-artifact-config-unpack-main upstream/3914-fix-artifact-config-unpack-main
git merge upstream/main
git push upstream 3914-fix-artifact-config-unpack-main

@AndersonQ
Copy link
Member Author

buildkite test this

@pierrehilbert
Copy link
Contributor

invalid character 'E' looking for beginning of value

Could it be something coming from your change?

Comment on lines 263 to 265
if tmpField.IsValid() && tmpField.CanSet() {
tmpField.Set(cValue.Field(i))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You cannot be certain that tmpType.Filed(i) and cValue.Field(i)return the equivalent fields, it is safer to usehttps://pkg.go.dev/reflect#Value.FieldByName`

Suggested change
if tmpField.IsValid() && tmpField.CanSet() {
tmpField.Set(cValue.Field(i))
}
if tmpField.IsValid() && tmpField.CanSet() {
tmpField.Set(cValue.FieldByName(name))
}

If you change the field ordering from configWithoutHTTPTransportSettings and Config, then run the test TestConfig_Unpack it will fail.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for catching this, this suggests there is a test case missing to catch this.

DropPath: tmp.DropPath,
HTTPTransportSettings: transport,
if cField.IsValid() && cField.CanSet() {
cField.Set(tmpValue.Field(i))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing here

Suggested change
cField.Set(tmpValue.Field(i))
cField.Set(tmpValue.FieldByName(name))

@jlind23
Copy link
Contributor

jlind23 commented Dec 20, 2023

@AndersonQ will you also tackle #3820 as suggested by Craig abovE?

@AndersonQ
Copy link
Member Author

@AndersonQ will you also tackle #3820 as suggested by Craig abovE?

Not in this PR

@AndersonQ
Copy link
Member Author

buildkite test this

Copy link

@AndersonQ AndersonQ merged commit 1ee9cc7 into elastic:main Jan 2, 2024
mergify bot pushed a commit that referenced this pull request Jan 2, 2024
…3930)

(*artifact.Config).Unpack was creating a new instance of Config and manually coping all values from the original to the new one. New values added to config, RetrySleepInitDuration in this case, were neither maintaining its default value nor being updated with the new value.
Now it uses reflection to ensure all values are correctly copied.
---------

Co-authored-by: Pierre HILBERT <pierre.hilbert@elastic.co>
(cherry picked from commit 1ee9cc7)

# Conflicts:
#	NOTICE.txt
#	go.mod
AndersonQ added a commit that referenced this pull request Jan 2, 2024
…ot overridden values (#3979)

(*artifact.Config).Unpack was creating a new instance of Config and manually coping all values from the original to the new one. New values added to config, RetrySleepInitDuration in this case, were neither maintaining its default value nor being updated with the new value.
Now it uses reflection to ensure all values are correctly copied.
---------

Co-authored-by: Pierre HILBERT <pierre.hilbert@elastic.co>
(cherry picked from commit 1ee9cc7)

---------

Co-authored-by: Anderson Queiroz <anderson.queiroz@elastic.co>
cmacknz pushed a commit to cmacknz/elastic-agent that referenced this pull request Jan 8, 2024
…lastic#3930)

(*artifact.Config).Unpack was creating a new instance of Config and manually coping all values from the original to the new one. New values added to config, RetrySleepInitDuration in this case, were neither maintaining its default value nor being updated with the new value.
Now it uses reflection to ensure all values are correctly copied.
---------

Co-authored-by: Pierre HILBERT <pierre.hilbert@elastic.co>
cmacknz pushed a commit that referenced this pull request Jan 17, 2024
…ot overridden values (#3979)

(*artifact.Config).Unpack was creating a new instance of Config and manually coping all values from the original to the new one. New values added to config, RetrySleepInitDuration in this case, were neither maintaining its default value nor being updated with the new value.
Now it uses reflection to ensure all values are correctly copied.
---------

Co-authored-by: Pierre HILBERT <pierre.hilbert@elastic.co>
(cherry picked from commit 1ee9cc7)

---------

Co-authored-by: Anderson Queiroz <anderson.queiroz@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-v8.12.0 Automated backport with mergify skip-changelog Team:Elastic-Agent Label for the Agent team
Projects
None yet
7 participants