-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Separator is no longer prepended when prefix is empty on podman generate systemd #13299
Conversation
pkg/systemd/generate/containers.go
Outdated
|
||
var serviceName string | ||
if options.ContainerPrefix == "" { | ||
serviceName = nameOrID |
There was a problem hiding this comment.
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 "".
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
pkg/systemd/generate/pods.go
Outdated
var serviceName string | ||
if options.PodPrefix == "" { | ||
serviceName = nameOrID | ||
} else { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
/approve |
Also please add a unit test in containers_test.go and pods_test.go. |
test/e2e/generate_systemd_test.go
Outdated
n.WaitWithDefaultTimeout() | ||
Expect(n).Should(Exit(0)) | ||
|
||
session := podmanTest.Podman([]string{"generate", "systemd", "--container-prefix", "", "--pod-prefix", "", "--separator", "_", "--name", "foo"}) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Thank you everyone for your suggestions. I will fix these issues and push the changes. |
4857269
to
e84e667
Compare
I have added tests to containers_test.go and pods_test.go. Hopefully they look good. |
@npate012 thanks for the PR. It looks like your change may have upset some other tests. |
The podman-remote tests are failing, you need to make sure that
The client binding is here: podman/pkg/bindings/generate/generate.go Line 12 in 0d2bd53
Either the client or server is not using the empty prefix. |
e84e667
to
a51f7f8
Compare
ContainerPrefix: "container", | ||
PodPrefix: "pod", | ||
Separator: "-", |
There was a problem hiding this comment.
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
).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
a51f7f8
to
4b144a3
Compare
…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>
4b144a3
to
714e5a1
Compare
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@Luap99 PTanotherL
Thanks for contributing, @npate012 |
There was a problem hiding this 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" |
There was a problem hiding this comment.
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.
[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 |
/lgtm |
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