Repository navigation
Conversation
|
@SimonRichardson This PR is ready for review. However, it is based on top of #141's branch (I performed a couple of merges). I suggest reviewing and merging #141 first before this one. |
bf86684 to
637f83a
Compare
tonyandrewmeyer
left a comment
There was a problem hiding this comment.
Thanks for this, and for the detailed description. Mostly small suggestions. The one I'd like an answer on before merging is the website handler, since catching the exception there means the hook no longer fails and gets retried.
| try: | ||
| port = self.api_port() | ||
| except AgentConfException: | ||
| logger.error("machine does not appear to be a controller") | ||
| self.unit.status = BlockedStatus("machine does not appear to be a controller") | ||
| return |
There was a problem hiding this comment.
This is a behaviour change: before, api_port() raising meant the hook errored and Juju would retry it (so the relation would get the port once runtime.conf had one), but now the hook succeeds and the relation never gets port, since relation-joined won't run again. The status set here is also replaced by _on_collect_status, which already reports this failure.
Is swallowing the error what we want here? If so, I think it needs a test, and the status line can go.
There was a problem hiding this comment.
Changing its original behaviour is not what I wanted.
I made a mistake here. I missed that self.api_port() raises AgentConfException from other reasons besides a None port.
Therefore:
if port is None:
logger.error("machine does not appear to be a controller")
self.unit.status = BlockedStatus("machine does not appear to be a controller")
return
is not identical to
try:
port = self.api_port()
except AgentConfException:
Although that original if port is None doesn't do anything because api_port() always returns an int (not a zero) 🤔
I have reverted this change.
| [tool.pyright] | ||
| pythonVersion = "3.10" | ||
| reportUnnecessaryTypeIgnoreComment = "error" | ||
| include = ["src", "tests"] |
There was a problem hiding this comment.
Running pyright on its own (or in an editor) can't resolve the charms.* imports (7 errors for me), since it's only run_tests that puts lib on the path. What about adding this?
| include = ["src", "tests"] | |
| include = ["src", "tests"] | |
| extraPaths = ["lib"] |
There was a problem hiding this comment.
Oh thanks for the catch. I added it.
|
|
||
| parsed_url = urllib.parse.urlsplit("//" + api_addresses[0]) | ||
| if not parsed_url.port: | ||
| if parsed_url.port is None: |
There was a problem hiding this comment.
I think the old check was actually doing the right thing here, since port 0 isn't a usable API port either (with is None we'd end up with a *:0 scrape target). Is there a case where we'd want to accept it? Feel free to push back.
There was a problem hiding this comment.
No push back from me. This change introduced a behaviour change, where a zero port will be passed through without an exception. I have reverted it.
| }, | ||
| "tls_config": { | ||
| "ca_file": self.ca_cert(), | ||
| "ca_file": ca_cert if ca_cert is not None else "", |
There was a problem hiding this comment.
Previously this would have been sent as null when there's no ca-cert. Is "" handled the same way on the Prometheus side? If we're not sure, I'd rather keep ca_cert() returning str (or raising), since a controller should always have one.
There was a problem hiding this comment.
From my small exploration, I don't have an evidence that they are handled differently on the Prometheus side yet.
But I wouldn't want to introduce an empty string here. I will revert it.
I agree that ca_cert reaching here should always be a string. Just to double check:
self.ca_cert()should raiseAgenConfExceptionwhen CA certificate cannot be found.- Here we will catch the exception and put the charm into
BlockedStatus
try:
ca_cert = self.ca_cert()
except AgentConfException as e:
self.unit.status = BlockedStatus(
f"can't read CA certificates from runtime.conf: {e}"
)
logger.error("cannot read CA certificates from agent runtime.conf: %s", e)
return None
return [
{
...
"tls_config": {
"ca_file": ca_cert,
"server_name": "juju-apiserver",
},
}
]
And I should probably add this try catch to _on_collect_status too.
| [project.optional-dependencies] | ||
| dev = [ | ||
| "coverage", | ||
| "pyright", |
There was a problem hiding this comment.
CI installs with uv pip install ".[dev]", which doesn't use uv.lock, so we'll pick up each new pyright as it's released, and a new check could turn CI red without any change here. Do we want a pin (or uv sync) so that new checks arrive deliberately? (ruff is the same already, so feel free to leave this for a separate PR.)
There was a problem hiding this comment.
This problem will be addressed in a subsequent PR (#146) - although there are a couple of issues from that PR I need to fix too. Thanks for raising this!
I prefer the CI respects uv.lock, and doesn't run on new version everytime.
| ] No newline at end of file | ||
| ] | ||
|
|
||
| [tool.pyright] |
There was a problem hiding this comment.
Is the plan to annotate the rest in a follow-up? pyright's standard mode doesn't report missing annotations at all (it infers return types, and treats unannotated parameters as Unknown), so most of the handlers that take an event aren't really being checked yet. ruff check --select ANN gives 107 hits across src/ for me. Turning on the ruff ANN rules and/or typeCheckingMode = "strict" once those are fixed would stop new unannotated code creeping in. Propbably not for this PR, but it would be good to have an issue for it.
There was a problem hiding this comment.
Yes. I enabled strict mode before and found that would make the PR a bit bigger. I wanted this PR to be minimal first, so that it was easier for me to stack other PRs on top, and doesn't affect incoming PRs from other maintainers too much (at the end of the cycle).
I will create an issue to track it as a follow up.
…ors when running pyright; update CONTRITBUTING.md
This PR adds type checking with
pyright.The section below discusses the errors raised by
pyright, and my proposed fixes.ops.testing.SIMULATE_CAN_CONNECTnot foundIn
tests/__init__.pySolution: Remove
ops.testing.SIMULATE_CAN_CONNECT = Truefollowing canonical/operator#873.urllib.requestnot recognizedIn both
src/configchangesocket.pyandsrc/controlsocket.py.This is a trivial fix.
body[...] = valuetype mismatchIn
controlsocket.pybodyis inferred asdict[str, str | None]when it is first initialized. However,bool/floatvalues are later inserted to it.Solution: annotate the type of
body._current_open_telemetry_configreturns type mismatchIn
src/charm.pyValues of
self.config(of type ConfigData) has a Union typebool | int | float | str. Therefore,pyrightinfers the returned object as:which doesn't match
tuple[bool, float, str, bool]in the function signature.Solution: use
cast(T, ...)hdrs=Nonetype mismatch inurllib.error.HTTPErrorIn
tests/test_sockets.py:urllib.error.HTTPErrorexpectshdrs: email.message.Message, notNone. See here.Solution: initialize an empty
email.message.Messageobject.test_sockets.py— MockOpener not assignable to OpenerDirectorA
MockOpenerobject is passed intoControlSocketClient, for example:The
openerparameter ofControlSocketClienthas typeurllib.request.OpenerDirector | None = None. Therefore,pyrightflags a non-compatible type.This is an expected type error, but not a serious one. Since this is for testing purposes, and we control the behaviour of
MockOpener, I proposed ignoring this error with# type: ignore.JujuControllerCharm.api_portshould return intIf this function doesn't raise, it returns
urllib.parse.SplitResult.port. This.portattribute is anint.JujuControllerCharm.ca_certreturn type annotationPreviously:
In theory,
self._controller_runtime_config("ca-cert")can return None, or a value that is not of typestr. I updated this function to strictly handle mis-match type.As a drive-by, I added
JujuControllerCharm._controller_runtime_configreturn type sinceruntime_conf.get(key)returns None ifkeydoesn't exist.Follow up
typeCheckingMode = "strict".