Skip to content

feat(initsystem): read and stream OpenRC service logs - #525

Merged
kke merged 5 commits into
mainfrom
feat/openrc-service-logs
Nov 24, 2026
Merged

kke merged 5 commits into
mainfrom
feat/openrc-service-logs

Conversation

@kke

@kke kke commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Service.Logs and Service.StreamLogs returned "not supported" on OpenRC hosts, so k0sproject/k0sctl#1290 (k0sctl logs) can't work on Alpine. This adds ServiceLogs and StreamServiceLogs to initsystem.OpenRC.

OpenRC has no standard log location. Each service script sets one with output_log/error_log or with --stdout/--stderr in the supervisor arguments. The provider sources the conf.d file and the service script in a subshell, as openrc-run does, collects the log files that exist, and runs tail on them. Guessing /var/log/<svc>.log would not work for k0s: its script names the files after the binary, so both k0scontroller and k0sworker log to /var/log/k0s.log and /var/log/k0s.err. A service that logs only to syslog returns an error that wraps errors.ErrUnsupported.

When stdout and stderr go to separate files, Logs returns up to N lines from each file, stdout first. I tested it by hand in an alpine:3.22 container through the localhost client. There is no integration test because the repo has no service-level integration tests yet.

OpenRC has no log location of its own. Each service script sets it
with output_log/error_log or with --stdout/--stderr in the supervisor
arguments. Find the log files by sourcing the conf.d file and the
service script in a subshell, as openrc-run does, and tail them.

The k0s script names its log files after the binary, so both
k0scontroller and k0sworker log to /var/log/k0s.log and
/var/log/k0s.err. A service that logs only to syslog returns an error
that wraps errors.ErrUnsupported.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@github-actions github-actions Bot added the additive-change Applied by the API diff workflow label Nov 23, 2026
@github-actions

Copy link
Copy Markdown

Exported API changed by this pull request

Compared against fea9a2d.

This only adds exported API.

Compared for linux, windows, darwin. Entries read ./package.Symbol; the module root package is unprefixed.

Compatible changes (2):

- ./initsystem.OpenRC.ServiceLogs: added
- ./initsystem.OpenRC.StreamServiceLogs: added

@kke
kke requested a balanced review from Copilot November 23, 2026 15:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

OpenRC log discovery mishandles configuration inheritance and supervisor arguments, causing missing or incorrect logs, while stdout ordering and cancellation handling violate the API contracts.

5 open issues
What changed in this PR

Adds OpenRC support to the library’s service-log APIs, enabling log reading and streaming on hosts such as Alpine for consumers like k0sctl logs.

Changes:

  • Discovers existing log files from service configuration and supervisor arguments.
  • Uses tail to read recent lines or follow logs, returning errors.ErrUnsupported when no log files are found.
  • Adds unit tests for command construction, error propagation, streaming, and cancellation.
File Description
initsystem/​openrc.go Implements OpenRC log-file discovery, reading, and streaming.
initsystem/​openrc_test.go Adds mocked log API tests and interface conformance checks.

Review effort: Balanced

Comment thread initsystem/openrc.go Resolved
Comment thread initsystem/openrc.go Resolved
Comment thread initsystem/openrc.go Resolved
Comment thread initsystem/openrc.go Resolved
Comment thread initsystem/openrc.go Resolved
Load rc.conf, the base and instance conf.d files and their runlevel
overrides before the service script. Pass the supervisor arguments
through eval so quoting inside them works. Read only the arguments of
the active supervisor, and let the last --stdout or --stderr win.
Print stdout before stderr regardless of flag order.

Treat a cancelled context during the lookup as a clean stop in
StreamServiceLogs.

Run the lookup script under sh in a test against a temporary OpenRC
tree.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke
kke requested a balanced review from Copilot November 23, 2026 18:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

OpenRC log discovery can miss valid logs or select incorrect paths due to missing helpers, empty-setting parsing, and runlevel fallback bugs, while a test can execute host service configuration.

3 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Initialize OpenRC helpers before discovering conditional log settings

initsystem/​openrc.go:126

For a service or conf.d file that sets output_log inside if yesno "$log_enabled"; then ...; fi, discovery skips the assignment and returns errors.ErrUnsupported even when file logging is enabled. This runs under a bare shell without OpenRC's helper functions, and _rig_load hides the command-not-found diagnostic. openrc-run loads its helper libraries before configuration. Initialize the corresponding OpenRC helper environment before sourcing these files, and add a helper-dependent discovery test.

5 resolved since last review

Review effort: Balanced

Comment thread initsystem/openrc.go Resolved
Comment thread initsystem/openrc.go Resolved
Comment thread initsystem/openrc_internal_test.go Resolved
Pick a runlevel conf.d file when it exists, not when sourcing it
succeeds, so a runlevel file that ends in a false test no longer
loads the plain file too. Skip empty output_log and error_log
settings so an empty one does not swallow the next flag. Load the
OpenRC helper functions before the configuration, with a yesno
fallback, so settings that depend on them resolve.

Stub rc-service and rc-status in the lookup test so it never reads
the OpenRC setup of the host that runs it.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke
kke requested a balanced review from Copilot November 23, 2026 21:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

OpenRC log discovery still mishandles configuration precedence, s6 file targets, chroot paths, and attached supervisor options, causing incorrect log selection or unsupported errors for valid configurations.

No open issues 🎉

4 previously missed issues

In code that hasn't changed since last review

Medium severity Honor RC_PATH precedence when loading log settings

initsystem/​openrc.go:124

With RC_PATH=/etc/rc:/etc and a script resolved under /etc/init.d, this lookup misses log settings overridden by /etc/rc/conf.d, so it can tail the wrong file. OpenRC's loader loads configuration roots from RC_PATH in reverse order. Preserve that precedence for both global and service-specific settings rather than reading only the conf.d directory beside the script.

Medium severity Resolve s6 output_log and error_log targets

initsystem/​openrc.go:155

For supervisor=s6 with an existing output_log or error_log, this exits without returning either file, making both log APIs report errors.ErrUnsupported. OpenRC's s6 backend explicitly redirects stdout/stderr to those files when they are not superseded by logger commands; s6 does not always keep separate supervisor-owned logs. Please resolve those effective file targets for s6 and update the test that currently expects them to be ignored.

Medium severity Parse attached -1 and -2 log path arguments

initsystem/​openrc.go:163

OpenRC's supervisor parser uses getopt_long, which accepts attached short-option values such as supervise_daemon_args="-1/var/log/svc.log -2/var/log/svc.err". These cases only match standalone -1 and -2, so that valid configuration produces no discovered files and both log APIs return errors.ErrUnsupported. Handle the attached forms as well.

Medium severity Resolve log paths relative to the effective chroot

initsystem/​openrc.go:171

With chroot=/srv/jail and output_log=/var/log/svc.log, supervise-daemon writes /srv/jail/var/log/svc.log: its log paths are relative to the chroot. This lookup checks /var/log/svc.log on the host instead, so both log APIs report unsupported when the real file exists, or read an unrelated host file. Resolve the effective chroot, including supervisor-argument overrides, before checking and returning log paths.

3 resolved since last review

Review effort: Balanced

Return output_log and error_log for s6 unless a logger command
replaces them. s6 redirects before it enters the chroot, so the paths
stay as they are.

For supervise-daemon and start-stop-daemon, read the chroot from the
chroot variable and from --chroot or -r in the arguments, and look for
the log files inside it. Accept short options with an attached value,
such as -1/var/log/svc.log.

Note that the lookup follows the OpenRC 0.63 loader, which does not
read RC_PATH.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke
kke requested a balanced review from Copilot November 23, 2026 22:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Relative log paths are resolved incorrectly against the service directory and chroot, causing both log APIs to report unsupported logging or read the wrong file.

1 open issue

Review effort: Balanced

Comment thread initsystem/openrc.go Resolved
supervise-daemon and start-stop-daemon open the log files after they
change root and directory. Resolve a relative log path from the
service directory, set by the directory variable or by --chdir or -d,
inside the chroot. s6 redirects from its service directory before it
changes root or directory, so resolve its relative paths from there.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke
kke requested a balanced review from Copilot November 23, 2026 23:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because OpenRC log discovery can use incorrect service context or paths, while combined tail output can merge unrelated stdout and stderr entries.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@kke

kke commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

OpenRC log discovery can use incorrect service context or paths, while combined tail output can merge unrelated stdout and stderr entries

This does what openrc-run does, sourcing the same files in the same order. This was tested on alpine and it works for everything I tried. The combined tail output is what it is. OpenRC writes stdout and stderr into separate files and there's no reasonable way to track the exact order they appeared.

@kke
kke merged commit df01f18 into main Nov 24, 2026
18 checks passed
@kke
kke deleted the feat/openrc-service-logs branch November 24, 2026 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

additive-change Applied by the API diff workflow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants