Skip to content

feat(config-migrate): couple to infra-agent agent type, simplify#1640

Merged
DavSanchez merged 16 commits intomainfrom
feat/adapt-config-migrate-util-to-filesystem
Oct 2, 2025
Merged

feat(config-migrate): couple to infra-agent agent type, simplify#1640
DavSanchez merged 16 commits intomainfrom
feat/adapt-config-migrate-util-to-filesystem

Conversation

@DavSanchez
Copy link
Contributor

@DavSanchez DavSanchez commented Sep 10, 2025

What this PR does / why we need it

Follow-up for #1610 and #1619, this adapts the code for the config-migrate utility so it is coupled to our single use case (the newrelic-infra agent type) and performs its actions in the most straightforward way possible without rewriting the whole program.

I have added scenario testing with filesystem access mocked so you can check how this works. Please see converter.rs.

Remaining tasks after all the currently opened PRs are merged:

  • Document the new way to define filesystem items in INTEGRATING_AGENTS.md as it's promoted to the official one.
  • Remove file and map[string]file variable types and related structure (injected deps).
  • Increase the correctness of the rendering process by splitting the agent type definition type into two, an "unrendered" and a "rendered" one related via the rendering functions, only allowing inspection of final values for creating supervisors on the latter.

Checklist

[Place an '[x]' (no spaces) in all applicable fields. Please remove unrelated fields.]

  • Provided a meaningful title following conventional commit style.
  • Included a detailed description for the Pull Request.
  • Documentation under docs is aligned with the change.
  • Follows guidelines for Pull Requests in CONTRIBUTING.md.
  • Follows log level guidelines.

@DavSanchez DavSanchez marked this pull request as ready for review September 10, 2025 15:04
@DavSanchez DavSanchez requested a review from a team as a code owner September 10, 2025 15:04
@DavSanchez DavSanchez force-pushed the refactor/migrate-host-agent-types-to-filesystem branch from 7ccec20 to 058494c Compare September 10, 2025 23:56
@DavSanchez DavSanchez force-pushed the feat/adapt-config-migrate-util-to-filesystem branch from 73af478 to a82242b Compare September 11, 2025 00:08
@DavSanchez
Copy link
Contributor Author

E2E test for on-host migration scenario (part of nightly builds) is successful: https://github.com/newrelic/newrelic-agent-control/actions/runs/17630098266/job/50096362311

@DavSanchez DavSanchez force-pushed the feat/adapt-config-migrate-util-to-filesystem branch 2 times, most recently from a3ae80a to 85ec477 Compare September 11, 2025 00:42
Copy link
Contributor

@sigilioso sigilioso left a comment

Choose a reason for hiding this comment

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

The approach looks good! Find some comment below

let file_reader = &self.file_reader;

// Retrieve the configuration for an existing infrastructure-agent installation.
let config_agent_key = String::from("config_agent");
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use constants for the keys? I also wonder if we could have some sort of sanity check if the keys ever change in the Agent Type? Maybe a test involving the registry could be enough 🤔

if let Some((key, value)) = map.next_entry::<String, Sequence>()? {
if map.next_key::<String>()?.is_some() {
Err(M::Error::custom(
"expected a single key-value pair in the map",
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we support a single config file with multiple keys?

integrations:
  - name: nri-docker
    # ...
  - name: nri-postgresql
 # ...
discovery:
  # ...
variables:
  # ...

I'm not completely sure if we were supporting it before (and I'm also not sure if the infra agent works well with such config), but the docs made me wonder.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You might be right. My goal was the simplest thing possible that works in our current setup, but it might make sense to open up for a bit more. Looking into it!

Copy link
Contributor

Choose a reason for hiding this comment

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

This highlighted the need of multiple files and now it is solved, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was tied to expecting a certain file structure for integrations and logging files and how to merge them into a single file, but now it's not needed yes!

Besides, I think it was better to have it accept generic YAML instead of defining structures for these file types, given it might change by the OHAI team and we might not notice (introducing new fields or something), so I removed the custom parse.

@DavSanchez DavSanchez force-pushed the refactor/migrate-host-agent-types-to-filesystem branch 2 times, most recently from 1686105 to 94e58ee Compare September 15, 2025 07:39
@DavSanchez DavSanchez force-pushed the feat/adapt-config-migrate-util-to-filesystem branch from 85ec477 to 79a2ea5 Compare September 15, 2025 08:31
@DavSanchez DavSanchez force-pushed the refactor/migrate-host-agent-types-to-filesystem branch from ec82968 to d217e84 Compare September 15, 2025 08:31
@DavSanchez DavSanchez force-pushed the feat/adapt-config-migrate-util-to-filesystem branch from 79a2ea5 to 36dce86 Compare September 15, 2025 08:32
@DavSanchez DavSanchez force-pushed the refactor/migrate-host-agent-types-to-filesystem branch from d217e84 to 5bbf894 Compare September 15, 2025 08:43
@DavSanchez DavSanchez force-pushed the feat/adapt-config-migrate-util-to-filesystem branch from 36dce86 to e6edf11 Compare September 15, 2025 08:44
@DavSanchez DavSanchez marked this pull request as draft September 16, 2025 12:54
@DavSanchez DavSanchez changed the base branch from refactor/migrate-host-agent-types-to-filesystem to feat/vartype-map-string-yaml September 19, 2025 18:26
@DavSanchez DavSanchez force-pushed the feat/adapt-config-migrate-util-to-filesystem branch 3 times, most recently from 1db5aba to 0985366 Compare September 19, 2025 18:35
@DavSanchez DavSanchez force-pushed the feat/vartype-map-string-yaml branch from ffc1b8f to f251eb1 Compare September 23, 2025 11:02
@DavSanchez DavSanchez force-pushed the feat/adapt-config-migrate-util-to-filesystem branch 3 times, most recently from bece22b to 3c55a30 Compare September 23, 2025 14:36
@DavSanchez DavSanchez force-pushed the feat/vartype-map-string-yaml branch 2 times, most recently from 3ad1536 to 8383858 Compare September 25, 2025 13:58
@DavSanchez DavSanchez force-pushed the feat/adapt-config-migrate-util-to-filesystem branch from 3c55a30 to cf0b832 Compare September 25, 2025 15:27
Copy link
Contributor

@sigilioso sigilioso left a comment

Choose a reason for hiding this comment

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

Thanks for taking care of this. Find some comments below.

Comment on lines 144 to 149
fn process_config_input(input: String) -> String {
// This regex matches {{VAR_NAME}} not already inside quotes
let re = Regex::new(r#"(?P<pre>[^'"]|^)\{\{([A-Za-z0-9_]+)\}\}(?P<post>[^'"]|$)"#).unwrap();
re.replace_all(&input, "${pre}'{{${2}}}'${post}")
.to_string()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We are building the once for each function call, right? Could we build it only once? Maybe something similar to what we do here:

fn template_functions_re() -> &'static Regex {
static RE_ONCE: OnceLock<Regex> = OnceLock::new();
RE_ONCE.get_or_init(|| Regex::new(TEMPLATE_PIPE_FUNCTIONS).unwrap())
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aah yes, makes total sense! I'll add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 142 to 143
/// This is a regex-based approach that may not cover all edge cases, but works for
/// the common scenarios we expect (small config strings).
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have any of those edge cases in mind? 🤔
I was thinking of values that are not supposed to be a string in the config, but I'm not sure there could be any.

I don't think we need to do it now but I think we should allocate some time to document the migration limitations.

Copy link
Contributor Author

@DavSanchez DavSanchez Oct 1, 2025

Choose a reason for hiding this comment

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

Not sure what would be cases that should work for the infra-agent but not in our migration. In all honesty I wrote the comment when I had another implementation (manual successive replacement of occurrences with String -> String functions) so it probably had flaws that the current implementation hasn't, so I'll reword the comments and start writing docs for the migration tool, if only in the top-level module.

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably these docs should have their own task (I don't think it is in the scope of this change)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reworded for now 2a8e204

Comment on lines 84 to 85
if !visited.insert(k) {
Err(ConversionError::DuplicateKeyFound(k.clone()))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if it is feasible but the error message could really help understanding where the problem is, right? It currently says duplicate key found in file and dir mappings: key", but I think it would be useful to say where the key is (unless I am not getting it right!).

By the way, do you think that adding a unit-test to check this error would be worth it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! 81754d9

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As for specifying where exactly the error occurs, this is something that might happen in the config (the stand-alone structure in defaults.rs) that before was controlled only by us (the configs define files_map and dirs_map and if these both define the same key it will modify the migration behavior) but you're right that now I accept an external config input. I'll try to be more verbose to help... or rewrite this config so this cannot even happen 😆

Copy link
Contributor Author

@DavSanchez DavSanchez Oct 1, 2025

Choose a reason for hiding this comment

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

Ok, I have removed the possibility of duplicate mapping keys altogether, so this shouldn't be an issue anymore, let me know your thoughts! d62334c

Copy link
Contributor

Choose a reason for hiding this comment

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

As for specifying where exactly the error occurs, this is something that might happen in the config (the stand-alone structure in defaults.rs)

I wasn't getting it right! I thought it were keys in the config files (which doesn't make sense because we can have duplicates there! (integrations might be defined many times!) 🤦

Copy link
Contributor

Choose a reason for hiding this comment

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

Anyway, I think the new changes simplify it quite a bit! 🚀

@DavSanchez DavSanchez force-pushed the feat/adapt-config-migrate-util-to-filesystem branch 3 times, most recently from 95ba5ce to d62334c Compare October 1, 2025 14:29
sigilioso
sigilioso previously approved these changes Oct 2, 2025
Copy link
Contributor

@sigilioso sigilioso left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

Base automatically changed from feat/vartype-map-string-yaml to main October 2, 2025 07:42
@DavSanchez DavSanchez dismissed sigilioso’s stale review October 2, 2025 07:42

The base branch was changed.

@DavSanchez DavSanchez enabled auto-merge (squash) October 2, 2025 07:42
sigilioso
sigilioso previously approved these changes Oct 2, 2025
@DavSanchez DavSanchez force-pushed the feat/adapt-config-migrate-util-to-filesystem branch from 7fa1adb to b81090f Compare October 2, 2025 08:11
@DavSanchez DavSanchez merged commit 52f2aab into main Oct 2, 2025
26 checks passed
@DavSanchez DavSanchez deleted the feat/adapt-config-migrate-util-to-filesystem branch October 2, 2025 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments