Skip to content

support import/export - #8

Merged
taka-okayama merged 10 commits into
mainfrom
feature/stage-import-export
Oct 30, 2026
Merged

taka-okayama merged 10 commits into
mainfrom
feature/stage-import-export

Conversation

@taka-okayama

@taka-okayama taka-okayama commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Add export and import to pass values between stages. A pre-stage exports a value in the store of a capability on one of its nodes or clusters. A capability or generator in a later stage imports it, and the recipe receives it as a param.

stages:
  bootstrap:
    export:
      proxy_ca_cert: {node: gateway, capability: squid.proxy, path: ca/cert}
inventory:
  nodes:
    cp:
      capabilities:
        - id: os.ca-trust
          import:
            cert: proxy_ca_cert   # same as {from: proxy_ca_cert, optional: false}

Changes

  • State: add export to pre-stages and import to capabilities and generators. They are kept through templates, -t merging and normalization. The capability of an export is resolved to its recipe ID.
  • Plan: check that each export has a source capability, and that each non-optional import refers to an export of a preceding stage and that its param is not also set by params.
  • Apply: at the start of each stage, resolve the export values from the current state, and inject the imported values into the params of the tasks. An optional import without an export is not injected.
  • State recording: after a pre-stage succeeds, record its export definitions (default: merge with the target, prune: keep only the names in the target, destroy: remove all). For each capability, record import in the normalized form and imported, the hash of each injected value.
  • Fixes:
    • plan validates the target State after merging the -t files.
    • Templates are applied in the documented precedence.

Not included (follow-up PR)

  • Plan-time detection of a changed export value or import mapping, and deferred updates.
  • Plan-time check of a missing export path (it is detected at the start of the importing stage).
  • Prune check of exports that are still imported, the display of value sources in plan, and the user guide.

Testing

  • make unit-test: all 34 packages pass. golangci-lint run ./internal/... ./cmd/...: 0 issues.
  • make e2e-test: pass. The new TestStage_ExportAndImport checks that a value written in a pre-stage reaches a main-stage recipe, and that state.json records the export, the import and the hash of the imported value.

state.Load did not validate the files, so an invalid target state, such as a reserved stage name, was accepted by plan.
…plan

- Add the export definition (node / cluster, capability, path) to stages.
- Validate the fields on load, and resolve the capability to the recipe ID
  on normalization (the version is ignored with a warning).
- Validate the references at plan: the union of the initial and the target
  state for the default plan, and the definitions kept by prune.
- Add import (param name -> {from, optional}) to capabilities. The short
  form `param: export` is the same as {from: export, optional: false}.
- Keep import through decoding, normalization, clone and merge; the later
  definition replaces the earlier one for each param.
- At plan, check that a non-optional import refers to an export of a
  preceding stage and that its param is not also set by params.
- Replace an export definition as a whole when merging target files.
The definition of a node, cluster or generator now overrides its
templates, and a later template overrides an earlier one. Before, a
template overrode node labels and cluster/generator fields, and an
earlier template won for node capabilities.
- Add import to generators and generator templates, and keep it through
  clone, merge and template application.
- Validate the imports of generators at plan, as for capabilities.
- Add import and imported to the State JSON Schema.
- Put the import of capabilities and generators into the vertex data of
  the plan, as written in the State.
- At the start of each stage, resolve the values of the exports of the
  preceding stages (in the construct order) from the current state, and
  stop if an export path is missing in the store.
- When building the tasks of a vertex, add the imported values to a copy
  of the params of the job; an optional import without the export is
  skipped. The job data is not changed.
- Resolve the export path in the same notation as the relative path of
  stateChanges (patch.TrimForPath / patch.ParsePath).
- After each pre-stage succeeds, record its exports in the current state:
  the default plan replaces and adds the definitions of the target and
  keeps the others, prune keeps the current definitions whose names are
  in the target, and destroy removes all. A failed stage leaves the
  record unchanged. _init and main have no exports.
- Add Exports.RestrictTo and Stage.ExportMerge / ExportRestrictTo, and
  use ExportMerge for the plan-time checks in place of effectiveExports.
- Exports.Merge no longer returns an error; it never failed.
- Add SetExports to the state manager to write the exports of the stage.
- Record in `imported` of a node or cluster capability the hash of each
  value injected by import, keyed by param name: on construct together
  with the capability, and on update after the tasks succeed, replacing
  the previous map. A param not injected (an optional import without
  the export) is not recorded. Generators do not record it.
- Add types.MakeHash (SHA-256 of the JSON form, in hex) and use it for
  Dict.Hash, so that the params and the imported values are hashed in
  the same way.
- Simplify taskGenerator.OnComplete with early returns.
- Record the import of a node or cluster capability in the State on
  construct and update, in the form with from / optional, so that a
  later plan can detect a change of the import.
- Accept import in Capability.Add, and replace the fields set as a
  whole (version, params, import, imported) through one helper.
Check that a value a pre-stage writes to the store reaches the params
of a recipe in the main stage, and that state.json records the export
definition, the normalized import and the hash of the injected value.
@taka-okayama taka-okayama self-assigned this Oct 30, 2026
@taka-okayama
taka-okayama merged commit 1a1aa57 into main Oct 30, 2026
5 checks passed
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.

1 participant