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: Detect insights-client status in non-legacy mode #125

Merged

Conversation

m-horky
Copy link
Contributor

@m-horky m-horky commented Jul 12, 2024

  • Card ID: CCT-525

With legacy_upload=True mode, insights-client --status returns code
0 if the system is registered and 1 otherwise. In non-legacy mode, 0 is
always returned.

Even though the /etc/insights-client/.registered file is not a public
API, it is reliable and we should be able to use it to detect the
registration status.

@m-horky m-horky force-pushed the cct-525/insights-client-status branch from 69fe879 to 98969eb Compare July 12, 2024 12:15
@jirihnidek
Copy link
Contributor

In non-legacy mode, 0 is always returned. For this reason, we should rely on the message emitted to standard output.

Shouldn't we rather fix insights-client? I really don't like the idea of cancer code ... bad code causing bad code in other projects.

@m-horky
Copy link
Contributor Author

m-horky commented Jul 15, 2024

I'm not sure if insights-client is broken if it returns 0. You are asking for a yes/no response, so it correctly returns "registered" or "non-registered" -- I'd expect other exit code if we couldn't talk to API or something similar.

IMO, the legacy upload is bad -- we shouldn't be returning non-zero code there.

@jirihnidek
Copy link
Contributor

The logic of returning zero, when system is connected, is correct. Returning non-zero code, when system is not connected for various reasons, is also correct. The subscription-manager status returns such codes. I believe that we should be consistent and follow this behavior in other client tools including rhc and insights-client.

@jirihnidek
Copy link
Contributor

There is really low risk of doing such change in insights-client. If somebody checks returned value of insights-client --status e.g. in some script or any other automation, then it is also expected that some non zero value is returned.

I'm not willing to do wrong things in rhc, because there is some bug in insights-client. For this reason I'm closing this PR.

@jirihnidek jirihnidek closed this Jul 16, 2024
@m-horky m-horky reopened this Aug 5, 2024
@m-horky
Copy link
Contributor Author

m-horky commented Aug 5, 2024

I'd like you to reconsider this PR. It seems too risky to change the return codes for insight-client. Since the behavior is delivered in a Core, it will be pushed everywhere, that's not how RHEL behaves.

It wouldn't be responsible. We should include this patch, even though it's not as pretty as we'd have hoped. Unfortunately, the reality is what is is.

@subpop
Copy link
Collaborator

subpop commented Aug 27, 2024

I agree that poor code in insights-client should not create poor code in other projects. I also agree that exit codes from programs should be consistent. I also disagree with this patch, but more on the grounds that relying on output strings is dangerous. If a change in core later changes this output, suddenly rhc clients everywhere will start misbehaving. I am generally against tightly-coupling programs like this; it creates tenuous dependencies that become extremely complex to maintain. Making any change to insights-client comes with a very high risk of breaking other projects because of the exact reason I described in the previous sentence; it's terrible CLI has become a de-facto API that other tooling rely upon. Could we modify rhc to look for the existence of the ".registered" and ".unregistered" marker files instead of the standard output or exit codes of insights-client? It's still a fragile dependency, but it feels less likely to cause other programs to break.

@m-horky
Copy link
Contributor Author

m-horky commented Aug 27, 2024

That's not a bad idea. .registered and .unregistered is already de-facto API which is used semi-internally. While I'm not happy that the files are stored in /etc/, it's the best thing we have. I'll update the PR to try that out instead.

@subpop
Copy link
Collaborator

subpop commented Aug 27, 2024

It's not elegant, but the absence of those files can be more elegantly handled than string output changing without warning.

@m-horky m-horky force-pushed the cct-525/insights-client-status branch 2 times, most recently from ce5168d to 105e756 Compare August 28, 2024 08:36
Copy link
Collaborator

@subpop subpop left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks!

@subpop
Copy link
Collaborator

subpop commented Aug 28, 2024

I want to wait for @jirihnidek opinion in this case before merging, since he raised concerns about the original implementation.

Copy link
Contributor

@jirihnidek jirihnidek left a comment

Choose a reason for hiding this comment

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

I have some questions and concerns.

It seems that there are conflicts, because I fixed all lint issues as part of another PR and this PR was merged to main branch. I have some questions.

insights.go Outdated Show resolved Hide resolved
@m-horky m-horky force-pushed the cct-525/insights-client-status branch 2 times, most recently from 9697f7c to a3dcc54 Compare September 11, 2024 08:13
Copy link
Contributor

@jirihnidek jirihnidek left a comment

Choose a reason for hiding this comment

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

It is more reliable approach, but I think that we should not ignore some critical error cases.

insights.go Outdated
// asking Inventory, its two modes (legacy v. non-legacy API) behave
// differently (they return different texts with different exit codes).
// The `.registered` file is always present on the registered system.
_ = exec.Command("/usr/bin/insights-client", "--status").Run()
Copy link
Contributor

Choose a reason for hiding this comment

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

I would not ignore returned error here. E.g. If the file /usr/bin/insights-client did not exist, then I would return such error.

* Card ID: CCT-525

With `legacy_upload=True` mode, `insights-client --status` returns code
0 if the system is registered and 1 otherwise. In non-legacy mode, 0 is
always returned.

Even though the `/etc/insights-client/.registered` file is not a public
API, it is reliable and we should be able to use it to detect the
registration status.
@m-horky m-horky force-pushed the cct-525/insights-client-status branch from a3dcc54 to 88c3457 Compare September 11, 2024 09:03
Copy link
Contributor

@jirihnidek jirihnidek left a comment

Choose a reason for hiding this comment

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

I have one more request and it seems that integration tests are failing due to change in this PR. This should be also fixed before PR is merged.

// When stderr is not empty, then we should return this as error
// to be able to print this error in rhc output
stdErr := errBuffer.String()
if len(stdErr) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would not delete old functionality altogether, because it will change behavior as you can see on this example:

root@localhost:/path/to/rhc# cat /usr/bin/insights-client 
#!/bin/bash

exit 1
root@localhost:/path/to/rhc# ./rhc status
Connection status for thinkpad-p1:

● Connected to Red Hat Subscription Management
● Cannot detect Red Hat Insights status: exit status 1
● The yggdrasil service is active

Manage your connected systems: https://red.ht/connector

If insights-client returns non-zero value, can we return false, nil to be consistent with old behavior?

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 wouldn't say that would be correct, because you get non-zero code even on network errors. It might have been the previous behavior, but it could be misleading. I would rather report the cached value risking it's out of date then report it's not connected to insights because the network glitched up.

Copy link
Contributor

@jirihnidek jirihnidek left a comment

Choose a reason for hiding this comment

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

I hope that we will be able to solve this in better way soon.

@jirihnidek jirihnidek merged commit 5023890 into RedHatInsights:main Sep 19, 2024
15 of 24 checks passed
@m-horky m-horky deleted the cct-525/insights-client-status branch October 4, 2024 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants