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

Quadlet - treat paths starting with systemd specifiers as absolute #17930

Merged

Conversation

ygalblum
Copy link
Contributor

If a path (Yaml, ConfigMap, EnvFile) starts with a systemd path specifier, treat the path as absolute
Add tests - unit, e2e and bats

Resolves: #17906

Does this PR introduce a user-facing change?

Yes

Quadlet - treat paths starting with systemd specifiers as absolute

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Mar 26, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ygalblum

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 26, 2023
@ygalblum ygalblum force-pushed the quadlet-systemd-specifiers branch from 384c968 to 401f083 Compare March 26, 2023 09:11
@rhatdan
Copy link
Member

rhatdan commented Mar 26, 2023

LGTM

@ygalblum ygalblum force-pushed the quadlet-systemd-specifiers branch from 401f083 to 1624f81 Compare March 26, 2023 12:38
@ygalblum ygalblum force-pushed the quadlet-systemd-specifiers branch from 1624f81 to 30f7432 Compare March 27, 2023 09:16
Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

test/system/252-quadlet.bats Outdated Show resolved Hide resolved
@@ -969,7 +969,7 @@ func addNetworks(quadletUnitFile *parser.UnitFile, groupName string, serviceUnit
}

func getAbsolutePath(quadletUnitFile *parser.UnitFile, filePath string) (string, error) {
if !filepath.IsAbs(filePath) {
if filePath[0] != '%' && !filepath.IsAbs(filePath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely non-blocking: A comment on the % special casing may help future readers.

But don't bother repushing for it.

Copy link
Contributor Author

@ygalblum ygalblum Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following other comments, I changes the implementation to use a function and added an explanation on top of it. PTAL

Comment on lines 562 to 563
local tmp_path=$(mktemp -d --tmpdir quadlet.volume.XXXXXX)
local tmp_dir=$(basename "$tmp_path")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having a hard time understanding the point of doing this instead of using $PODMAN_TMPDIR

# Argh. Although BATS provides $BATS_TMPDIR, it's just /tmp!
# That's bloody worthless. Let's make our own, in which subtests
# can write whatever they like and trust that it'll be deleted
# on cleanup.
# TODO: do this outside of setup, so it carries across tests?
PODMAN_TMPDIR=$(mktemp -d --tmpdir=${BATS_TMPDIR:-/tmp} podman_bats.XXXXXX)

I'm also finding it impossible to understand the systemd.unit(5) documentation for %T:

This is either /tmp or the path "$TMPDIR", "$TEMP" or "$TMP" are set to.

...because there is no indication of the process space in which those envariables are checked.

Anyhow. Please use $PODMAN_TMPDIR or provide an explanation as to why that's not possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the line you've added, the base directory for PODMAN_TMPDIR can be overridden by setting BATS_TMPDIR. So, if the latter is set, the directory will not be created under /tmp which will cause systemd to point to a non-existing path. For this reason I wanted to ensure the location of the directory. But, if it is never really set, I can use PODMAN_TMPDIR
Having said that, the idea behind the test is to check the support for the systemd specifiers. I chose to use %T, but I don't mind using a different one (https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Specifiers), if you think it makes more sense.
Please let me know what you think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From bats(7):

$BATS_TMPDIR is the base temporary directory used by bats to create its temporary files / directories. (default: $TMPDIR. If $TMPDIR is not set, /tmp is used.)

...but in your current implementation, you use mktemp --tmpdir, which:

...if DIR is not specified, use $TMPDIR if set, else /tmp.

...so as best I can tell, that's transitively equivalent to $PODMAN_TMPDIR... unless I'm missing something...

(and again, it still isn't clear to me what %T expands to. Presumably /tmp in root-systemd, but I have no clue what happens in user-systemd, and I think it's kind of important to understand and document that)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so I'll switch to using PODMAN_DIR and update the rest of the path accordingly.
As for %T, since for other specifiers the documentation provides information about root and user, I understand that it is the same for both. In addition, the tests pass, so it is probably true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 564 to 565
local file_name=hello.txt
local file_content="hello"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use random_string here too? That eliminates a (small, unlikely) number of false negatives.

    local file_name="f$(random_string 10).txt"
    local file_content="data_$(random_string 15)"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, great idea. I'll update it as part of whatever we decide on the previous comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -969,7 +969,7 @@ func addNetworks(quadletUnitFile *parser.UnitFile, groupName string, serviceUnit
}

func getAbsolutePath(quadletUnitFile *parser.UnitFile, filePath string) (string, error) {
if !filepath.IsAbs(filePath) {
if filePath[0] != '%' && !filepath.IsAbs(filePath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it is possible to set the path to an empty string but if it is this will cause a panic, you should check the len(filePath) > 0 before to avoid this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in the new startsWithSystemdSpecifier function

@@ -969,7 +969,7 @@ func addNetworks(quadletUnitFile *parser.UnitFile, groupName string, serviceUnit
}

func getAbsolutePath(quadletUnitFile *parser.UnitFile, filePath string) (string, error) {
if !filepath.IsAbs(filePath) {
if filePath[0] != '%' && !filepath.IsAbs(filePath) {
Copy link
Contributor

@eriksjolund eriksjolund Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be a special case for %% ?

$ man systemd.unit | grep "Single percent"
       │"%%"      │ Single percent sign                           │ Use "%%" in place of "%" to specify a single      │

Edit 1:
Ah, it quickly gets a bit complicated.

%U is the UID and would never start with a slash.

%h is the home directory and would always start with a slash.

Maybe there are also variants where it's not possible to conclude whether the expanded string starts with a slash or not.

Should the implementation go try to analyse all the different variants? Maybe it's not worth it. I don't know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in the startsWithSystemdSpecifier function

@ygalblum ygalblum force-pushed the quadlet-systemd-specifiers branch from 30f7432 to 1b6190c Compare March 28, 2023 07:16
If a path (Yaml, ConfigMap, EnvFile) starts with a systemd path
specifier, treat the path as absolute
Add tests - unit, e2e and bats

Signed-off-by: Ygal Blum <[email protected]>
@ygalblum ygalblum force-pushed the quadlet-systemd-specifiers branch from 1b6190c to da96ff6 Compare March 28, 2023 07:30
Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Restarted the flaked job.

@vrothberg
Copy link
Member

@eriksjolund @Luap99 please take another look

Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vrothberg
Copy link
Member

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Mar 28, 2023
@openshift-merge-robot openshift-merge-robot merged commit 365131e into containers:main Mar 28, 2023
@ygalblum ygalblum deleted the quadlet-systemd-specifiers branch March 28, 2023 15:03
@github-actions github-actions bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label Sep 4, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. release-note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Quadlet: EnvironmentFile paths starting with systemd specifiers are not recognized as absolute
7 participants