Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implement compose_target_name #1138

Closed
wants to merge 8 commits into from

Conversation

MatteoVoges
Copy link
Contributor

@MatteoVoges MatteoVoges commented Feb 7, 2024

Proposed changes

  • fix functionality for compose-node-name
    • rename compose-node-name to compose-target-name, because it should be kapitan-lingo
    • mark compose-node-name deprecated
    • add test for compose-target-name for reclass
  • update examples to be compatible with compose-target-name (no breaking changes, just improvements)
  • add new scoping for cached.args

@MatteoVoges MatteoVoges added the bugfix fixing a bug label Feb 7, 2024
@MatteoVoges MatteoVoges self-assigned this Feb 7, 2024
@MatteoVoges
Copy link
Contributor Author

MatteoVoges commented Feb 7, 2024

Will add tests for compose_node_name so that it won't happen again, that we break it!

@MatteoVoges
Copy link
Contributor Author

Currently this test fails:

    def test_compile_not_matching_targets(self):
        with (
            self.assertLogs(logger="kapitan.targets", level="ERROR") as cm,
            contextlib.redirect_stdout(io.StringIO()),
        ):
            # as of now, we cannot capture stdout with contextlib.redirect_stdout
            # since we only do logger.error(e) in targets.py before exiting
            with self.assertRaises(SystemExit) as ca:
                unmatched_filename = "inventory/targets/minikube-es-fake.yml"
                correct_filename = "inventory/targets/minikube-es.yml"
                os.rename(src=correct_filename, dst=unmatched_filename)
                sys.argv = ["kapitan", "compile"]

                try:
                    main()
                finally:
                    # correct the filename again, even if assertion fails
                    if os.path.exists(unmatched_filename):
                        os.rename(src=unmatched_filename, dst=correct_filename)
        error_message_substr = "is missing the corresponding yml file"
        self.assertTrue(" ".join(cm.output).find(error_message_substr) != -1)

And this makes sense, because the test tests actually unintended behavior.

  1. If the file name and target name does not match, I don't want a SystemExit and
  2. I don't want to search the logs for is missing the file.

@MatteoVoges
Copy link
Contributor Author

MatteoVoges commented Feb 9, 2024

And this makes sense, because the test tests actually unintended behavior.
1. If the file name and target name does not match, I don't want a SystemExit and
2. I don't want to search the logs for is missing the file.

The error happens in the validate_matching_target_name function. Because I modified the examples to use the name ${_reclass_:name:full}, which is the filename, the test does not succeed anymore.

Now the question is, if we handle a mismatch of filename and parameters.kapitan.vars.target?
And I surely will modify the test.

@MatteoVoges MatteoVoges marked this pull request as ready for review February 9, 2024 19:26
@MatteoVoges MatteoVoges mentioned this pull request Feb 9, 2024
2 tasks
@ademariag
Copy link
Contributor

Now the question is, if we handle a mismatch of filename and parameters.kapitan.vars.target? And I surely will modify the test.

Context, we added this behaviour before starting using ${_reclass_:name:short}, and it was to catch the case in which someone would copy the target file without changing the target parameter properly. Nowadays we should discourage to set the target manually, making it automatically get the value from the filename.

Reasoning...

I usually have this setup:

  target: ${_reclass_:name:full}
  target_name: ${_reclass_:name:short}
  target_path: ${_reclass_:name:path}
  
  ...
  
  kapitan:
    vars:
      target: ${target}

IMHO compose_target_name=True should be interpreted as "automatically infer the target name from the directory/path", so a mismatch between parameters.kapitan.vars.target and the "composed" target name should fail

for inventory/targets/test1/test2.yml

with compose_target_name=True:

  • ${_reclass_:name:short}=test2
  • ${_reclass_:name:full}=test1.test2

path/filename should match test1/test2 (just filename check is not enough because it could be in the directory test3/)

I think the test should fail if this is not true, and warn that you should not override the target manually

However with compose_target_name=False:
${_reclass_:name:short}=test2
${_reclass_:name:full}=test2

we should allow for someone to change the target in cases like:

test1/test2.yml
test3/test2.yml

as otherwise they would both be test2 and we want to allow for this manual setup.

I believe long term compose_target_name=False should be the default, perhaps in the upcoming release?

simu added a commit to projectsyn/kapitan that referenced this pull request Mar 4, 2024
This commit is based on the test case introduced in PR kapicorp#1138.

We move the compose-node-name test implementation to `tests/` so it's
stored in the same place as the other tests and make the test class
inheritable so that we can reuse the test for reclass-rs.

Additionally we extend the test to check that all targets found by
`search_targets()` are rendered by `render_targets()`. This is needed
since we don't use reclass(-rs)'s target discovery logic in
`search_targets()` but instead use a simplified version that's
implemented directly in Kapitan's inventory backend base class.

However, `render_targets()` then renders whatever targets reclass(-rs)'s
target discovery finds, so if there's a mismatch we'd want to be
informed.

Co-authored-by: Matteo Voges <[email protected]>
Co-authored-by: Simon Gerber <[email protected]>
simu added a commit to projectsyn/kapitan that referenced this pull request Mar 4, 2024
This commit is based on the test case introduced in PR kapicorp#1138.

We move the compose-node-name test implementation to `tests/` so it's
stored in the same place as the other tests and make the test class
inheritable so that we can reuse the test for reclass-rs.

Additionally we extend the test to check that all targets found by
`search_targets()` are rendered by `render_targets()`. This is needed
since we don't use reclass(-rs)'s target discovery logic in
`search_targets()` but instead use a simplified version that's
implemented directly in Kapitan's inventory backend base class.

However, `render_targets()` then renders whatever targets reclass(-rs)'s
target discovery finds, so if there's a mismatch we'd want to be
informed.

Co-authored-by: Matteo Voges <[email protected]>
Co-authored-by: Simon Gerber <[email protected]>
simu added a commit to projectsyn/kapitan that referenced this pull request Mar 4, 2024
This commit is based on the test case introduced in PR kapicorp#1138.

We move the compose-node-name test implementation to `tests/` so it's
stored in the same place as the other tests and make the test class
inheritable so that we can reuse the test for reclass-rs.

Additionally we extend the test to check that all targets found by
`search_targets()` are rendered by `render_targets()`. This is needed
since we don't use reclass(-rs)'s target discovery logic in
`search_targets()` but instead use a simplified version that's
implemented directly in Kapitan's inventory backend base class.

However, `render_targets()` then renders whatever targets reclass(-rs)'s
target discovery finds, so if there's a mismatch we'd want to be
informed.

Co-authored-by: Matteo Voges <[email protected]>
Co-authored-by: Simon Gerber <[email protected]>
simu added a commit to projectsyn/kapitan that referenced this pull request Mar 7, 2024
This commit is based on the test case introduced in PR kapicorp#1138.

We move the compose-node-name test implementation to `tests/` so it's
stored in the same place as the other tests and make the test class
inheritable so that we can reuse the test for reclass-rs.

Additionally we extend the test to check that all targets found by
`search_targets()` are rendered by `render_targets()`. This is needed
since we don't use reclass(-rs)'s target discovery logic in
`search_targets()` but instead use a simplified version that's
implemented directly in Kapitan's inventory backend base class.

However, `render_targets()` then renders whatever targets reclass(-rs)'s
target discovery finds, so if there's a mismatch we'd want to be
informed.

Co-authored-by: Matteo Voges <[email protected]>
Co-authored-by: Simon Gerber <[email protected]>
@ademariag
Copy link
Contributor

partially implemented by #1164 which was an urgent fix. Please rebase from that.

@ademariag
Copy link
Contributor

#1173

@ademariag ademariag closed this Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants