Skip to content

feat: Add response and log functionalities#603

Merged
roosterfish merged 2 commits into
canonical:v3from
HomayoonAlimohammadi:KU-5052/unauth-response
Feb 5, 2026
Merged

feat: Add response and log functionalities#603
roosterfish merged 2 commits into
canonical:v3from
HomayoonAlimohammadi:KU-5052/unauth-response

Conversation

@HomayoonAlimohammadi

@HomayoonAlimohammadi HomayoonAlimohammadi commented Jan 29, 2026

Copy link
Copy Markdown
Contributor

Context

While migrating Canonical Kubernetes from Microcluster v2 to v3, I noticed that we're moving away from the lxd/reponse and it's now baked into Microcluster itself. These two functions were used in Canonical Kubernetes but are not available in Microcluster. Also, the logging package is internal to Microcluster which makes testing on the k8s snap side impossible. The new pkg/log is supposed to surface the logging functionality.

Overview

This pull request introduces new functionality for error handling in HTTP responses and adds a new logging utility package. The most important changes are the addition of helper functions for standardized error responses and the creation of a log package to simplify logger usage with context.

Error handling improvements:

  • Added Unauthorized and ErrorResponse helper functions to response.go for generating 401 and custom error responses, respectively.

Logging utility:

  • Introduced a new microcluster/types/log package that wraps logger context handling, providing a WithLogger function and exposing the logger context key for easier logger management from external repos.

@HomayoonAlimohammadi HomayoonAlimohammadi changed the title feat(response): Add Unauthorized and ErrorResponse functions feat: Add response and log functionalities Jan 29, 2026
Comment thread pkg/log/log.go Outdated

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.

Pull request overview

This PR adds reusable helpers for HTTP error responses and introduces a public logging utility package so external consumers can integrate with Microcluster’s logging/context mechanism.

Changes:

  • Added Unauthorized and ErrorResponse helpers in microcluster/rest/response/response.go for standardized 401 and arbitrary-code error responses.
  • Introduced pkg/log/log.go to expose the logger context key (CtxLogger) and a WithLogger helper that stores a slog.Logger in a context.Context.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/log/log.go New public logging helper package that wraps the internal logger context key and provides WithLogger for attaching a logger to a context.
microcluster/rest/response/response.go Extends the HTTP response helpers with 401 and generic error-response constructors and wires in errors.New for message-based error responses.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread microcluster/rest/response/response.go Outdated
@HomayoonAlimohammadi HomayoonAlimohammadi force-pushed the KU-5052/unauth-response branch 2 times, most recently from 7c09f3b to e30187f Compare January 29, 2026 13:36
Comment thread internal/log/logger.go Outdated

@roosterfish roosterfish left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for adding the two additional response funcs. However I still don't see the need for the proposed logging changes.

@HomayoonAlimohammadi

Copy link
Copy Markdown
Contributor Author

Thank you for the review @roosterfish. I've reached out on MM with more context around this change.

@roosterfish

Copy link
Copy Markdown
Contributor

Thank you for the review @roosterfish. I've reached out on MM with more context around this change.

Ty for providing the additional context.
When you test your API handlers as a standalone, the passed context requires the logger to be set (which happens automatically when the handler is fired inside Microcluster).

So as part of our unit tests we also run ctx := context.WithValue(context.TODO(), log.CtxLogger, slog.Default()) to ensure we have a context with the required logger set to e.g. run the database funcs which might need to access the logger.

Deriving another default logger on the internal/log.LoggerFromContext func isn't trivial as we don't know at this point which log handler was passed when starting Microcluster.
If you want to test the API handler external from Microcluster, pass a context with the "context" key populated as shown above.
If we start returning a default logger, we might miss places where we unintentionally pass the wrong context (not the daemon's shutdown ctx), so it takes the default but instead we want the logs to be routed to the actual logger which isn't available as the context is wrong.

@HomayoonAlimohammadi

Copy link
Copy Markdown
Contributor Author

Thank you for all the information @roosterfish! I've updated the PR with what we've discussed

Comment thread microcluster/types/log.go
Comment thread microcluster/types/log/log.go Outdated
Comment thread microcluster/types/log.go Outdated
@HomayoonAlimohammadi HomayoonAlimohammadi force-pushed the KU-5052/unauth-response branch 2 times, most recently from e0ae35a to e49501f Compare February 3, 2026 15:42
@roosterfish

Copy link
Copy Markdown
Contributor

Can you please rebase, there are now conflicts after merging in other PRs.

@HomayoonAlimohammadi HomayoonAlimohammadi force-pushed the KU-5052/unauth-response branch 2 times, most recently from c250354 to e81370e Compare February 4, 2026 08:27
Comment thread microcluster/types/log.go
Comment thread microcluster/types/log.go
@HomayoonAlimohammadi

Copy link
Copy Markdown
Contributor Author

@roosterfish Thank you for the review. I've updated the PR with your instructions. Please let me know if it needs further polishing.

@roosterfish roosterfish left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The current logger changes feel too invasive to me.

In one of the recent comments I mentioned the approach we take for logging.

I understand you want to have an easy way to derive a context with the logger set to mimic what is done internally by Microcluster so you can test e.g. API handlers as standalone (as we do in our unit tests).
The easiest (which already works today) is to derive a context on your own using context.WithValue(context.TODO(), log.CtxLogger, slog.Default()).
But together we identified that log.CtxLogger is in the internal/ package. To overcome this limitation my suggestion was to move this constant into microcluster/types/log instead so you can access it too. It can even be a just a file like log.go (no need for an extra log/ package).

But this should not require this amount of changes. Only the places where we currently use log.CtxLogger need to be updated to point to the micrcluster/types package which would then contain the constant.

Comment thread internal/log/logger.go
Comment thread microcluster/types/log/log.go Outdated
Comment thread microcluster/app.go Outdated
Comment thread internal/daemon/daemon.go Outdated
@roosterfish

Copy link
Copy Markdown
Contributor

If you intend to have a little helper func, you can keep the ContextWithLogger func and we can then use it in the three places where we run context.WithValue.

@HomayoonAlimohammadi HomayoonAlimohammadi force-pushed the KU-5052/unauth-response branch 2 times, most recently from 3f6125c to c1dfed7 Compare February 5, 2026 14:19
Comment thread microcluster/types/log/log.go Outdated

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.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread microcluster/types/log.go
Comment thread microcluster/rest/response/response.go
…ons for improved error handling

Signed-off-by: Homayoon Alimohammadi <homayoon.alimohammadi@canonical.com>
…zed logger type

Signed-off-by: Homayoon Alimohammadi <homayoon.alimohammadi@canonical.com>

@roosterfish roosterfish left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@roosterfish roosterfish merged commit c3e5374 into canonical:v3 Feb 5, 2026
5 checks passed
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