Commit Graph

4 Commits

Author SHA1 Message Date
Phil Sphicas 5cd799cc5d Allow source substring extraction
When performing substitutions, there are occasions when the source value
does not exactly match the format required by the destination document
(e.g. the values.yaml structure of an Armada chart).

This change provides the ability extract a substring of the source
value, and substitute that into the destination document.

Two optional fields are added to `src` under `metadata.substitutions`:

  * `pattern`: a regular expression, with optional capture groups
  * `match_group`: the number of the desired capture group

The canonical use case is a chart that requires an image with the repo
name and tag in separate fields, while the substitution source has the
full image path as a single value.

For example, assuming that the source document "software-versions" has:

    data:
      images:
        hello: docker.io/library/hello-world:latest

Then the following set of substitutions would put the repo and tag in
the applicable values in the destination document:

    metadata:
      substitutions:
        - src:
            schema: pegleg/SoftwareVersions/v1
            name: software-versions
            path: .images.hello
            pattern: '^(.*):(.*)'
            match_group: 1
          dest:
            path: .values.images.hello.repo
        - src:
            schema: pegleg/SoftwareVersions/v1
            name: software-versions
            path: .images.hello
            pattern: '^(.*):(.*)'
            match_group: 2
          dest:
            path: .values.images.hello.tag
    data:
      values:
        images:
          hello:
            repo:  # docker.io/library/hello-world
            tag:   # latest

Change-Id: I2fcb0d2b8e2fe3d85479ac2bad0b7b90f434eb77
2022-01-18 13:04:25 -08:00
Felipe Monteiro d5462d0c70 schema: Fix metadata schema patterns
This patch set fixes the schema pattern enforced by metadata_document
and metadata_control. Currently, both allow a schema with either
pattern:

- ^metadata/Control/v\d+$
- ^metadata/Document/v\d+$

However, the metadata_control schema should only allow the former
and the metadata_document schema should only allow the latter.

Change-Id: Ic1b88a7158755818002de4c88cdf2d7b716f656d
2019-01-18 21:52:04 +00:00
Felipe Monteiro c9d71a6509 substitution: Recursive pattern replacement
Patterns may now be replaced recursively. This can be
achieved by using specifying a ``pattern`` value and
``recurse`` (with a required ``depth`` argument).

Example:

    substitutions:
      - dest:
          path: .
          pattern: REGEX
          recurse:
            depth: -1
        src:
          schema: deckhand/Passphrase/v1
          name: example-password
          path: .

NOTE:

  Recursive selection of patterns will only consider
  matching patterns. Non-matching patterns will be ignored.
  Thus, even if recursion can "pass over" non-matching patterns,
  they will be silently ignored.

This is useful for reducing the number of substitution
stanzas that are required for the purposes of performing
all the string pattern replacements that are required.
Best practice is to limit the scope of the recursion
as much as possible: e.g. avoid passing in "$" as the
``jsonpath``, but rather a JSON path that lives closer
to the nested strings in question.

Included in this patch set:

* recursive implementation for pattern replacement
* unit tests for most scenarios and edge cases
* docstring updates
* documentation updates
* schema updates for validation

Change-Id: I85048349097ed696667fae80f1180808d264bbcf
2018-09-12 09:24:46 -06:00
Scott Hussey e40f3e443f Simplify schema validation
- Treat internal Deckhand schemas equivalent to other
  service schemas
- Remove validating sections other than `data` outside of
  base schema
- Create schemas for metadata sections metadata/Control/v1 and
  metadata/Document/v1
- Use a single validator and let that validator check for document
  structure (validate against the base schema and metadata)
  and for post-validation also validate against service schemas

Change-Id: I5f9b9a3cfa1692a69b5982a6424edd65bdfed0ef
2018-07-03 02:07:33 +00:00