Skip to content

ENT-14194 2. Reset classes and variables between "then" bundle runs in cf-reactor - #6375

Draft
victormlg wants to merge 6 commits into
cfengine:masterfrom
victormlg:cf-reactor.ResetVarClasses
Draft

victormlg wants to merge 6 commits into
cfengine:masterfrom
victormlg:cf-reactor.ResetVarClasses

Conversation

@victormlg

@victormlg victormlg commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Classes that are defined inside a then bundle can stay defined between runs, even though the condition is not true anymore:

Exemple:

body when file_deleted(filename)
{
  file_deleted => "$(filename)";
}

bundle agent check_marker(file)
{
  classes:
    "marker_exists" expression => fileexists("/tmp/marker"), scope => "namespace";

  files:
    "$(file)" create => "true";

  reports:
    marker_exists::
      "marker exists";
    !marker_exists::
      "marker does not exist";
}

bundle reactor event_handler
{
  vars:
    "Z" string => "/tmp/a";

  events:
    "Watch /tmp/a"
      when => file_deleted("$(Z)"),
      then => check_marker("$(Z)");
}
  • 1st event: /tmp/marker exists, marker_exists is defined
  • delete /tmp/marker
  • 2nd event: /tmp/marker doesn't exists, marker_exists is still defined

Persistent classes should not be deleted

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Promises are skipped to prevent running them several times. However, on event, we want to run them every single time:
- we clear the promise lock cache
- we set the default if_elapsed time for bundles run from an events promise to be 0, so it doesn't skip the promises.

Fixed also connection cache and custom promise prologue and epilogue. Clear function cache before "then" bundle run

Signed-off-by: Victor Moene <victor.moene@northern.tech>
- No need for map anymore, since watcher keeps track of the promise
- No need to keep key as variable anymore, since we do not use a map
- No need to run bundle inside watcher.c
- No risk of running a bundle from a wrong key between policy reads

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Signed-off-by: Victor Moene <victor.moene@northern.tech>
The issue is that classes and variables declared once will stay declared until the next policy
reload. So a class can stay defined between runs, when the state it checks changed.
Instead, the state of all vars and classes is saved in a snapshot after a policy reload, and before every
"then" bundle run, we restore the snapshort, and the defined classes and vars of the ran bundles will stay
until the next event triggered run.

Signed-off-by: Victor Moene <victor.moene@northern.tech>
VarRefDestroy(ref);
}

static PromiseResult KeepAgentPromise(EvalContext *ctx, const Promise *pp, ARG_UNUSED void *param)
* removes the ones defined by the previous 'then' bundle runs, defines again
* the ones they cancelled and sets back the values they changed. Then
* reloads the persistent classes that have not expired. */
static void ContextSnapshotRestore(EvalContext *ctx)
@victormlg
victormlg marked this pull request as draft October 5, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants