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

POC: appendable string arrays in containers.conf #1675

Merged
merged 1 commit into from
Oct 5, 2023

Conversation

vrothberg
Copy link
Member

@vrothberg vrothberg commented Sep 28, 2023

As discussed in containers/podman#20000, we need an opt-in mechanism to append string arrays during the loading sequence of containers.conf files.

At the moment, existing fields/data will be overriden with each loaded config that sets the specified field/option. The TOML (toml.io) config format does not allow for attributing fields and structs are implicitly represented as "tables". I wanted to extend a string array with a simple boolean field, for instance:

env=["FOO=bar"]
env.append=true

TOML doesn't suppor tthe upper idea as it's not a properly formatted table. So I looked for alternatives and found that TOML supports so-called "mixed-type arrays". As the same suggests, such arrays allow for including more than one type and that seemed like a reasonable candidate as it allows for extending the existing syntax without introducing new fields or even yet-another way of loading conf files.

The new format can be seen in the tests. Please note that this is just a tested POC. Integrating the POC in containers.conf may turn into a bigger journey as Podman is directly (ab)using many of the fields. Since they have to be changed to the new type (see POC), Podman will not compile without changes.

Makefile Outdated
@@ -100,6 +100,7 @@ test: test-unit
test-unit: netavark-testplugin
go test --tags $(BUILDTAGS) -v ./libimage/...
go test --tags $(BUILDTAGS) -v ./libnetwork/...
go test --tags $(BUILDTAGS) -v ./internal/...
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it is time to change this to go test --tags $(BUILDTAGS) -v ./..., people will forget to add the packages when they add top level dirs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed. I opened #1681 to not forget about it.

Comment on lines 26 to 30
kind := reflect.ValueOf(x).Kind()
switch kind {
case reflect.String: // Strings are directly appended to the slice.
loadedStrings = append(loadedStrings, fmt.Sprintf("%v", x))
case reflect.Map: // The attribute struct is represented as a map.
Copy link
Member

Choose a reason for hiding this comment

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

I think this is better written as

switch val := x.(type) {
	case string:
		slice = append(slice, val)
	case map[string]interface{}:
		...
	default:
		// no match
	}

This avoids the extra type casts you need

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not know such a switch table is possible. Thanks for sharing!

@vrothberg
Copy link
Member Author

@Luap99 @rhatdan do you prefer merging the code or keep the PR open as the POC?

@rhatdan
Copy link
Member

rhatdan commented Oct 3, 2023

I am all for merging this.

@rhatdan
Copy link
Member

rhatdan commented Oct 3, 2023

LGTM

As discussed in containers/podman#20000, we
need an opt-in mechanism to _append_ string arrays during loading
sequence of containers.conf files.

At the moment, existing fields/data will be overriden with each loaded
config that sets the specified field/option.  The TOML (toml.io) config
format does not allow for attributing fields and structs are implicitly
represented as "tables".  I wanted to extend a string array with a
simple boolean field, for instance:
```TOML
env=["FOO=bar"]
env.append=true
```

TOML doesn't suppor tthe upper idea as it's not a properly formatted
table.  So I looked for alternatives and found that TOML supports
so-called "mixed-type arrays".  As the same suggests, such arrays allow
for including more than one type and that seemed like a reasonable
candidate as it allows for _extending_ the existing syntax without
introducing new fields or even yet-another way of loading conf files.

The new format can be seen in the tests.  Please note that this is just
a _tested_ POC.  Integrating the POC in containers.conf may turn into a
bigger journey as Podman is directly (ab)using many of the fields.
Since they have to be changed to the new type (see POC), Podman will not
compile without changes.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
@vrothberg
Copy link
Member Author

Rebased

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

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Oct 5, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Luap99, vrothberg

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 merged commit 12e7534 into containers:main Oct 5, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants