Skip to content

Importing files containing YAML Anchors #357

Closed

Description

@hypnotisttom

Describe the Bug

When I attempt to import a file containing a YAML Anchor into a file containing the YAML Alias of said anchor, I get:

invalid stack config file 'orgs/<REDACTED>/anchor-test-resolve.yaml' yaml: unknown anchor 'enabled' referenced

Expected Behavior

I would expect the import to occur first, the anchor to be found, and the alias to resolve it.

It works when I copy the contents of the anchor file into the file containing the alias, so I wonder if this has to do with the validation occurring before the import.

Steps to Reproduce

anchor-test.yaml

anchors:
  enabled: &enabled true

anchor-test-resolve.yaml

import:
  - orgs/<REDACTED>/_defaults
  - orgs/<REDACTED>/anchor-test

components:
  terraform:
    anchor-test-resolve:
      metadata:
        component: <REDACTED>
        inherits:
          - base-<REDACTED>-defaults
          - <REDACTED>-default-template-rule
      vars:
        rule_tree:
          rules:
            children:
              Redirector:
                behaviors:
                  - <REDACTED>:
                      options:
                        enabled: *enabled

Screenshots

No response

Environment

Linux
Atmos 1.32.4

Additional Context

The reason why we are wanting to do this is because we want to manage the values of the anchors by a different team via CODEOWNERS, whereas the aliases are set into a specific ordered list in a file maintained by our internal team.

Activity

  1. added
    bug
    on Apr 4, 2023
  2. osterman commented on Apr 4, 2023

    @osterman
    Member

    The scope of YAML anchors is tied to the file boundary. This is consistent with every YAML parser out there. I don't believe this is something we can solve easily while relying on the mainstream YAML parsers.

  3. osterman commented on Apr 4, 2023

    @osterman
    Member

    The reason why we are wanting to do this is because we want to manage the values of the anchors by a different team via CODEOWNERS, whereas the aliases are set into a specific ordered list in a file maintained by our internal team.

    Have you you looked into the following:

  4. added
    enhancement
    and removed
    bug
    on Apr 4, 2023
  5. hypnotisttom commented on Apr 4, 2023

    @hypnotisttom
    Author

    Thanks Erik.

    We're definitely using OPA.

    Regarding the Go templates...

    We need to enforce what specific fields can be set by the non-privileged team, which can include values for a particular object key that is in a list of objects.

    If the non-privileged team is providing the context with an import, we would need a way to ensure the specific import is specified in the file they are allowed to manage.

    I thought it would be easier to simply import their file and then use the things from their file where we need them. However, I can appreciate that a) for yaml anchors, you need to parse the yaml, thus this isn't possible (I was thinking you'd resolve the imports first, but it makes sense you are parsing the yaml before importing), and b) Go templates might work if we can enforce that their file imports our file.

    So, if I put the context in an import block for the non-privileged team to use, can I write an OPA policy that indicates which file must be imported for that specific context?

    If so, I still think it would be easier to maintain if I didn't have to write policy and could govern it by the order of imports, but at least it might solve the use case.

    Thanks,

    Tom

  6. osterman commented on Feb 3, 2024

    @osterman
    Member

    hypnotisttom we've introduced support for Sprig functions (some time ago) in go templates. So you should be able to do something like this.

    anchors:
      enabled: &enabled true
    

    Then where you need to use it, include it as content that gets injected before processing of the file as YAML, similar as to how Helm works, et al.

    {{ .Files.Get "files/configMap.yaml" }}
    import:
      - orgs/<REDACTED>/_defaults
      - orgs/<REDACTED>/anchor-test
    
    components:
      terraform:
        anchor-test-resolve:
          metadata:
            component: <REDACTED>
            inherits:
              - base-<REDACTED>-defaults
              - <REDACTED>-default-template-rule
          vars:
            rule_tree:
              rules:
                children:
                  Redirector:
                    behaviors:
                      - <REDACTED>:
                          options:
                            enabled: *enabled
    

    I haven't tested this, and if for some reason the .Files.Get is not exposed, please open a separate issue to address that. Also, we suggest against using too much Go templating. It's really intended to be used as an escape hatch.

    Beyond this, we don't have plans to implement some hack specifically addressing cross-file YAML anchors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions