From 16ba0e867b571d17ac8e87af7ab5720228d53d52 Mon Sep 17 00:00:00 2001 From: Luca Bello <36242061+lucabello@users.noreply.github.com> Date: Fri, 20 Oct 2023 09:37:45 +0200 Subject: [PATCH] upkeep: use refactored ci and pyright (#540) * upkeep: use refactored ci and pyright * tox fmt --- .github/workflows/issues.yaml | 50 ++----------------- .github/workflows/promote.yaml | 2 +- .github/workflows/pull-request.yaml | 6 +-- .github/workflows/release-edge.yaml | 11 ---- .github/workflows/release-libs.yaml | 11 ---- .github/workflows/release.yaml | 11 ++++ .github/workflows/update-libs.yaml | 3 +- .../prometheus_k8s/v0/prometheus_scrape.py | 22 ++++---- .../v1/prometheus_remote_write.py | 10 ++-- pyproject.toml | 30 ----------- src/charm.py | 16 +++--- tox.ini | 13 ++--- 12 files changed, 49 insertions(+), 136 deletions(-) delete mode 100644 .github/workflows/release-edge.yaml delete mode 100644 .github/workflows/release-libs.yaml create mode 100644 .github/workflows/release.yaml diff --git a/.github/workflows/issues.yaml b/.github/workflows/issues.yaml index f58df741..196fa568 100644 --- a/.github/workflows/issues.yaml +++ b/.github/workflows/issues.yaml @@ -5,49 +5,7 @@ on: [issues] jobs: update: name: Update Issue - runs-on: ubuntu-latest - steps: - - name: Dump Github Context - run: | - echo "update=false" >> $GITHUB_ENV - if [ ${{ github.event_name }} != "issues" ]; then - echo "This action only operates on issues" - exit 0 - fi - echo "update=true" >> $GITHUB_ENV - - name: Determine action - run: | - if [ ${{ github.event.action }} == "opened" ]; then - echo "action=open" >> $GITHUB_ENV - fi - if [ ${{ github.event.action }} == "reopened" ]; then - echo "action=reopen" >> $GITHUB_ENV - fi - if [ ${{ github.event.action }} == "closed" ]; then - echo "action=close" >> $GITHUB_ENV - fi - - name: Determine type - run: | - if ${{ contains(github.event.*.labels.*.name, 'Type: Bug') }}; then - echo "type=bug" >> $GITHUB_ENV - else - echo "type=story" >> $GITHUB_ENV - fi - - name: Update - if: ${{ env.update == 'true' }} - env: - ID: ${{ github.event.issue.html_url }} - TITLE: ${{github.event.issue.title }} - COMPONENT: prometheus - DESCRIPTION: Opened by ${{ github.event.issue.user.login }}. - run: | - data=$(jq -n \ - --arg id "$ID" \ - --arg action "${{ env.action }}" \ - --arg title "$TITLE" \ - --arg description "$DESCRIPTION" \ - --arg component "$COMPONENT" \ - --arg type "${{ env.type }}" \ - '{data: {id: $id, action: $action, title: $title, description: $description, component: $component, type: $type}}') - - curl -X POST -H 'Content-type: application/json' --data "${data}" "${{ secrets.JIRA_URL }}" + uses: canonical/observability/.github/workflows/issues.yaml@main + secrets: inherit + with: + component: prometheus diff --git a/.github/workflows/promote.yaml b/.github/workflows/promote.yaml index f3b2ee56..afb45b73 100644 --- a/.github/workflows/promote.yaml +++ b/.github/workflows/promote.yaml @@ -14,7 +14,7 @@ on: jobs: promote: name: Promote - uses: canonical/observability/.github/workflows/promote-charm.yaml@main + uses: canonical/observability/.github/workflows/charm-promote.yaml@main with: promotion: ${{ github.event.inputs.promotion }} secrets: inherit diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index d6d93769..1080b5fd 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -1,9 +1,5 @@ name: Pull Requests -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - on: pull_request: branches: @@ -12,5 +8,5 @@ on: jobs: pull-request: name: PR - uses: canonical/observability/.github/workflows/pull-request.yaml@main + uses: canonical/observability/.github/workflows/charm-pull-request.yaml@main secrets: inherit diff --git a/.github/workflows/release-edge.yaml b/.github/workflows/release-edge.yaml deleted file mode 100644 index abdf3bbf..00000000 --- a/.github/workflows/release-edge.yaml +++ /dev/null @@ -1,11 +0,0 @@ -name: Release to Edge - -on: - push: - branches: - - main - -jobs: - release: - uses: canonical/observability/.github/workflows/release-charm.yaml@main - secrets: inherit diff --git a/.github/workflows/release-libs.yaml b/.github/workflows/release-libs.yaml deleted file mode 100644 index c76a882e..00000000 --- a/.github/workflows/release-libs.yaml +++ /dev/null @@ -1,11 +0,0 @@ -name: Release Libraries - -on: - push: - branches: - - main - -jobs: - release-libs: - uses: canonical/observability/.github/workflows/release-libs.yaml@main - secrets: inherit diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 00000000..7b491387 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,11 @@ +name: Release Charm to Edge and Publish Libraries + +on: + push: + branches: + - main + +jobs: + release: + uses: canonical/observability/.github/workflows/charm-release.yaml@main + secrets: inherit diff --git a/.github/workflows/update-libs.yaml b/.github/workflows/update-libs.yaml index 62149226..46b56bcd 100644 --- a/.github/workflows/update-libs.yaml +++ b/.github/workflows/update-libs.yaml @@ -9,5 +9,6 @@ on: jobs: update-lib: name: Check libraries - uses: canonical/observability/.github/workflows/update-libs.yaml@main + uses: canonical/observability/.github/workflows/charm-update-libs.yaml@main secrets: inherit + diff --git a/lib/charms/prometheus_k8s/v0/prometheus_scrape.py b/lib/charms/prometheus_k8s/v0/prometheus_scrape.py index e4297aa1..118b4372 100644 --- a/lib/charms/prometheus_k8s/v0/prometheus_scrape.py +++ b/lib/charms/prometheus_k8s/v0/prometheus_scrape.py @@ -362,7 +362,7 @@ def _on_scrape_targets_changed(self, event): # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 42 +LIBPATCH = 43 PYDEPS = ["cosl"] @@ -764,7 +764,7 @@ def _validate_relation_by_interface_and_direction( actual_relation_interface = relation.interface_name if actual_relation_interface != expected_relation_interface: raise RelationInterfaceMismatchError( - relation_name, expected_relation_interface, actual_relation_interface + relation_name, expected_relation_interface, actual_relation_interface or "None" ) if expected_relation_role == RelationRole.provides: @@ -857,7 +857,7 @@ class MonitoringEvents(ObjectEvents): class MetricsEndpointConsumer(Object): """A Prometheus based Monitoring service.""" - on = MonitoringEvents() + on = MonitoringEvents() # pyright: ignore def __init__(self, charm: CharmBase, relation_name: str = DEFAULT_RELATION_NAME): """A Prometheus based Monitoring service. @@ -1294,7 +1294,7 @@ def _resolve_dir_against_charm_path(charm: CharmBase, *path_elements: str) -> st class MetricsEndpointProvider(Object): """A metrics endpoint for Prometheus.""" - on = MetricsEndpointProviderEvents() + on = MetricsEndpointProviderEvents() # pyright: ignore def __init__( self, @@ -1836,14 +1836,16 @@ def _set_prometheus_data(self, event): return jobs = [] + _type_convert_stored( - self._stored.jobs + self._stored.jobs # pyright: ignore ) # list of scrape jobs, one per relation for relation in self.model.relations[self._target_relation]: targets = self._get_targets(relation) if targets and relation.app: jobs.append(self._static_scrape_job(targets, relation.app.name)) - groups = [] + _type_convert_stored(self._stored.alert_rules) # list of alert rule groups + groups = [] + _type_convert_stored( + self._stored.alert_rules # pyright: ignore + ) # list of alert rule groups for relation in self.model.relations[self._alert_rules_relation]: unit_rules = self._get_alert_rules(relation) if unit_rules and relation.app: @@ -1895,7 +1897,7 @@ def set_target_job_data(self, targets: dict, app_name: str, **kwargs) -> None: jobs.append(updated_job) relation.data[self._charm.app]["scrape_jobs"] = json.dumps(jobs) - if not _type_convert_stored(self._stored.jobs) == jobs: + if not _type_convert_stored(self._stored.jobs) == jobs: # pyright: ignore self._stored.jobs = jobs def _on_prometheus_targets_departed(self, event): @@ -1947,7 +1949,7 @@ def remove_prometheus_jobs(self, job_name: str, unit_name: Optional[str] = ""): relation.data[self._charm.app]["scrape_jobs"] = json.dumps(jobs) - if not _type_convert_stored(self._stored.jobs) == jobs: + if not _type_convert_stored(self._stored.jobs) == jobs: # pyright: ignore self._stored.jobs = jobs def _job_name(self, appname) -> str: @@ -2126,7 +2128,7 @@ def set_alert_rule_data(self, name: str, unit_rules: dict, label_rules: bool = T groups.append(updated_group) relation.data[self._charm.app]["alert_rules"] = json.dumps({"groups": groups}) - if not _type_convert_stored(self._stored.alert_rules) == groups: + if not _type_convert_stored(self._stored.alert_rules) == groups: # pyright: ignore self._stored.alert_rules = groups def _on_alert_rules_departed(self, event): @@ -2176,7 +2178,7 @@ def remove_alert_rules(self, group_name: str, unit_name: str) -> None: json.dumps({"groups": groups}) if groups else "{}" ) - if not _type_convert_stored(self._stored.alert_rules) == groups: + if not _type_convert_stored(self._stored.alert_rules) == groups: # pyright: ignore self._stored.alert_rules = groups def _get_alert_rules(self, relation) -> dict: diff --git a/lib/charms/prometheus_k8s/v1/prometheus_remote_write.py b/lib/charms/prometheus_k8s/v1/prometheus_remote_write.py index 89658165..049c8faf 100644 --- a/lib/charms/prometheus_k8s/v1/prometheus_remote_write.py +++ b/lib/charms/prometheus_k8s/v1/prometheus_remote_write.py @@ -46,7 +46,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 2 +LIBPATCH = 3 PYDEPS = ["cosl"] @@ -211,7 +211,7 @@ def _validate_relation_by_interface_and_direction( actual_relation_interface = relation.interface_name if actual_relation_interface != expected_relation_interface: raise RelationInterfaceMismatchError( - relation_name, expected_relation_interface, actual_relation_interface + relation_name, expected_relation_interface, actual_relation_interface or "None" ) if expected_relation_role == RelationRole.provides: @@ -394,7 +394,7 @@ def __init__(self, *args): ``` """ - on = PrometheusRemoteWriteConsumerEvents() + on = PrometheusRemoteWriteConsumerEvents() # pyright: ignore def __init__( self, @@ -458,7 +458,7 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None: self.on.endpoints_changed.emit(relation_id=event.relation.id) def _handle_endpoints_changed(self, event: RelationEvent) -> None: - if self._charm.unit.is_leader(): + if self._charm.unit.is_leader() and event.app is not None: ev = json.loads(event.relation.data[event.app].get("event", "{}")) if ev: @@ -591,7 +591,7 @@ def __init__(self, *args): name to differentiate between "incoming" and "outgoing" remote write interactions is necessary. """ - on = PrometheusRemoteWriteProviderEvents() + on = PrometheusRemoteWriteProviderEvents() # pyright: ignore def __init__( self, diff --git a/pyproject.toml b/pyproject.toml index 1fc3f107..b90567ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,36 +36,6 @@ per-file-ignores = {"tests/*" = ["D100","D101","D102","D103"]} [tool.ruff.pydocstyle] convention = "google" -[tool.mypy] -pretty = true -python_version = 3.8 -mypy_path = "$MYPY_CONFIG_FILE_DIR/src:$MYPY_CONFIG_FILE_DIR/lib" -follow_imports = "normal" -warn_redundant_casts = true -warn_unused_ignores = true -warn_unused_configs = true -show_traceback = true -show_error_codes = true -namespace_packages = true -explicit_package_bases = true -check_untyped_defs = true -allow_redefinition = true -no_implicit_optional = false - - -# Ignore libraries that do not have type hint nor stubs -[[tool.mypy.overrides]] -module = ["pytest.*", "pytest_operator.*", "prometheus_api_client.*", "deepdiff.*"] -ignore_missing_imports = true - -[[tool.mypy.overrides]] -module = ["charms.grafana_k8s.*", "charms.alertmanager_k8s.*", "charms.observability_libs.*", "charms.traefik_k8s.*"] -follow_imports = "silent" - -[[tool.mypy.overrides]] -module = ["ops.*"] -follow_imports = "skip" - [tool.pyright] extraPaths = ["lib"] pythonVersion = "3.8" diff --git a/src/charm.py b/src/charm.py index 3df92580..2c118dc3 100755 --- a/src/charm.py +++ b/src/charm.py @@ -385,9 +385,9 @@ def _on_server_cert_changed(self, _): def _is_cert_available(self) -> bool: return ( self.cert_handler.enabled - and self.cert_handler.cert - and self.cert_handler.key - and self.cert_handler.ca + and (self.cert_handler.cert is not None) + and (self.cert_handler.key is not None) + and (self.cert_handler.ca is not None) ) def _is_tls_ready(self) -> bool: @@ -408,31 +408,31 @@ def _update_cert(self): # Save the workload certificates self.container.push( CERT_PATH, - self.cert_handler.cert, + self.cert_handler.cert, # pyright: ignore make_dirs=True, ) self.container.push( KEY_PATH, - self.cert_handler.key, + self.cert_handler.key, # pyright: ignore make_dirs=True, ) # Save the CA among the trusted CAs and trust it self.container.push( ca_cert_path, - self.cert_handler.ca, + self.cert_handler.ca, # pyright: ignore make_dirs=True, ) # FIXME with the update-ca-certificates machinery prometheus shouldn't need # CA_CERT_PATH. self.container.push( CA_CERT_PATH, - self.cert_handler.ca, + self.cert_handler.ca, # pyright: ignore make_dirs=True, ) # Repeat for the charm container. We need it there for prometheus client requests. ca_cert_path.parent.mkdir(exist_ok=True, parents=True) - ca_cert_path.write_text(self.cert_handler.ca) + ca_cert_path.write_text(self.cert_handler.ca) # pyright: ignore else: self.container.remove_path(CERT_PATH, recursive=True) self.container.remove_path(KEY_PATH, recursive=True) diff --git a/tox.ini b/tox.ini index 4ce84563..ff06063c 100644 --- a/tox.ini +++ b/tox.ini @@ -47,10 +47,7 @@ commands = description = Run static analysis checks deps = cosl - mypy - types-dataclasses - types-PyYAML - types-requests + pyright charm: -r{toxinidir}/requirements.txt lib: ops charm: responses==0.20.0 @@ -59,11 +56,11 @@ deps = integration: {[testenv:integration]deps} integration: pytest-operator==1.0.0b1 commands = - charm: mypy {[vars]src_path} {posargs} - lib: mypy --python-version 3.8 {[vars]lib_path} {posargs} + charm: pyright {[vars]src_path} {posargs} + lib: pyright --pythonversion 3.8 {[vars]lib_path} {posargs} lib: /usr/bin/env sh -c 'for m in $(git diff main --name-only {[vars]lib_path}); do if ! git diff main $m | grep -q "+LIBPATCH\|+LIBAPI"; then echo "You forgot to bump the version on $m!"; exit 1; fi; done' - unit: mypy {[vars]tst_path}/unit {posargs} - integration: mypy {[vars]tst_path}/integration {posargs} + unit: pyright {[vars]tst_path}/unit {posargs} + integration: pyright {[vars]tst_path}/integration {posargs} allowlist_externals = /usr/bin/env [testenv:unit]