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

Separator is no longer prepended when prefix is empty on podman generate systemd #13299

Conversation

nirmal-j-patel
Copy link
Contributor

When podman generate systemd is invoked, it previously did not check if
container-prefix or pod-prefix are empty. When these are empty, the file name
starts with the separator, which is hyphen by default. This results in files
like '-containername.service'.

The code now checks if these prefixes are empty. If they are, the filename no
longer adds a separator. Instead, it uses name or ID of the container or pod.

Closes #13272

Signed-off-by: Nirmal Patel npate012@gmail.com

@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 20, 2022

var serviceName string
if options.ContainerPrefix == "" {
serviceName = nameOrID
Copy link
Member

Choose a reason for hiding this comment

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

would you consider serviceName := NameOrID and then use a single IF condition for options.ContainerPrefix != "". Also, personal thing, but i prefer len(options.ContainerPrefix) == 0 over "".

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 it would be better to put this into a small function and use this function for container and pod name,
e.g.

function getServiceName(prefix, sperator, name) string {
   if prefix == "" {
      return name
   }
   return fmt.Sprintf("%s%s%s", prefix, separator, name)
}

serviceName := getServiceName(...)

This way would do not need to duplicate the logic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have created a function for this. I also used + to concatenate string instead of Sprintf because fmt was not imported in common.go and I wasn't sure if I should import it only for this function. I also changed the code to use a single if.

var serviceName string
if options.PodPrefix == "" {
serviceName = nameOrID
} else {
Copy link
Member

Choose a reason for hiding this comment

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

same comment about reducing this down to one conditional

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the code to use a function which has a single if condition

@@ -423,6 +423,20 @@ var _ = Describe("Podman generate systemd", func() {

})

It("podman generate systemd --container-prefix ''", func() {
Copy link
Member

Choose a reason for hiding this comment

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

@vrothberg PTAL at the tests added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran the test using GOPATH=~/go go test -v test/e2e/libpod_suite_test.go test/e2e/common_test.go test/e2e/config.go test/e2e/config_amd64.go test/e2e/generate_systemd_test.go

and I did not see any failure. So I pushed those changed. Hopefully there will not be any failures in CI. The following is the summary of the output of command.

Ran 32 of 32 Specs in 429.065 seconds
SUCCESS! -- 32 Passed | 0 Failed | 0 Pending | 0 Skipped
--- PASS: TestLibpod (429.07s)
PASS
ok  	command-line-arguments	429.114s

@mheon
Copy link
Member

mheon commented Feb 20, 2022

/approve

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 20, 2022
@Luap99
Copy link
Member

Luap99 commented Feb 20, 2022

Also please add a unit test in containers_test.go and pods_test.go.

n.WaitWithDefaultTimeout()
Expect(n).Should(Exit(0))

session := podmanTest.Podman([]string{"generate", "systemd", "--container-prefix", "", "--pod-prefix", "", "--separator", "_", "--name", "foo"})
Copy link
Member

Choose a reason for hiding this comment

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

Could you merge the two pod tests? This way, the pod and its containers are created once and we save some test cycles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I merged the two pod tests to a single test. Thank you for the suggestion.

@nirmal-j-patel
Copy link
Contributor Author

Thank you everyone for your suggestions. I will fix these issues and push the changes.

@nirmal-j-patel nirmal-j-patel force-pushed the fix_systemd_generate_name_on_empty_prefix branch from 4857269 to e84e667 Compare February 23, 2022 17:57
@nirmal-j-patel
Copy link
Contributor Author

Also please add a unit test in containers_test.go and pods_test.go.

I have added tests to containers_test.go and pods_test.go. Hopefully they look good.

@TomSweeneyRedHat
Copy link
Member

@npate012 thanks for the PR. It looks like your change may have upset some other tests.

@Luap99
Copy link
Member

Luap99 commented Feb 24, 2022

The podman-remote tests are failing, you need to make sure that podman-remote generate systemd ... will also work.
The API endpoint handler is here:

func GenerateSystemd(w http.ResponseWriter, r *http.Request) {

The client binding is here:
func Systemd(ctx context.Context, nameOrID string, options *SystemdOptions) (*entities.GenerateSystemdReport, error) {

Either the client or server is not using the empty prefix.

@nirmal-j-patel nirmal-j-patel force-pushed the fix_systemd_generate_name_on_empty_prefix branch from e84e667 to a51f7f8 Compare March 5, 2022 22:23
Comment on lines -37 to -39
ContainerPrefix: "container",
PodPrefix: "pod",
Separator: "-",
Copy link
Member

Choose a reason for hiding this comment

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

I understand why you changed this but this will break api users, who rely on this default.

I think the correct fix would be to make the fields a pointer (*string).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I appreciate your review and help. Could you please expand on how making the fields a pointer would work? I am not able to understand this strategy. Thank you.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry this comment got lost in the inbox.
change this to

ContainerPrefix  *string   `schema:"containerPrefix"`
PodPrefix        *string   `schema:"podPrefix"`
Separator        *string   `schema:"separator"`

Leave the default unset.

Now in the code below you can check if var == nil, in this case you set the correct default of container, pod, -.
If not nil you can just use the value.

Note this is just my theory I have not tested if this will actually work. But this change will allow us to differentiate between an empty string ("") or not set at all (nil).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made this change and it works.

@nirmal-j-patel nirmal-j-patel force-pushed the fix_systemd_generate_name_on_empty_prefix branch from a51f7f8 to 4b144a3 Compare March 17, 2022 00:04
…ate systemd

When podman generate systemd is invoked, it previously did not check if
container-prefix or pod-prefix are empty. When these are empty, the file name
starts with the separator, which is hyphen by default. This results in files
like '-containername.service'.

The code now checks if these prefixes are empty. If they are, the filename no
longer adds a separator. Instead, it uses name or ID of the container or pod.

Closes containers#13272

Signed-off-by: Nirmal Patel <npate012@gmail.com>
@nirmal-j-patel nirmal-j-patel force-pushed the fix_systemd_generate_name_on_empty_prefix branch from 4b144a3 to 714e5a1 Compare March 17, 2022 00:27
@nirmal-j-patel nirmal-j-patel marked this pull request as ready for review March 17, 2022 02:22
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 17, 2022
@nirmal-j-patel
Copy link
Contributor Author

Thanks everyone for your help. I feel this pull request is ready for review. All tests pass successfully. I have changed the status from draft to ready for review.

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
@Luap99 PTanotherL

@vrothberg
Copy link
Member

Thanks for contributing, @npate012

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, one minor nit but it is not worth to repush for this

}

if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
return
}

var ContainerPrefix = "container"
Copy link
Member

Choose a reason for hiding this comment

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

the idiomatic go way would be to write containerPrefix := "container", also local variables should start lower case.

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Mar 17, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Luap99, mheon, npate012

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

@Luap99
Copy link
Member

Luap99 commented Mar 17, 2022

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Mar 17, 2022
@openshift-merge-robot openshift-merge-robot merged commit 6ce9677 into containers:main Mar 17, 2022
@nirmal-j-patel nirmal-j-patel deleted the fix_systemd_generate_name_on_empty_prefix branch July 3, 2022 20:23
@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 21, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 21, 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

podman generate systemd should only prepend separator when prefix is non-empty
7 participants