Skip to content

Commit

Permalink
Prepare GH workflow actions for new charm (#2)
Browse files Browse the repository at this point in the history
* Retitle inclusive Naming check action

* remove inclusive-name tests as its included with unit-tests

* don't require a self-hosted runner

* Simplify charm and tests to easily pass

* Conform to required linters

* Run integration tests on lxd for the time being

* simplify charm and testing

* disable zap tests

* Create k8s and k8s-worker charms

* bake in a default .licenserc.yaml

* split matrix call into separate workflow calls with unique workflow names

* ensure each charm gets a publish and auto-update-libs job

* Address review comments
  • Loading branch information
addyess authored Dec 8, 2023
1 parent 9c3d4b8 commit 5cdacf2
Show file tree
Hide file tree
Showing 33 changed files with 915 additions and 301 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/auto-update-libs-k8s-worker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Auto-update K8s-worker charm libraries

on:
schedule:
- cron: "0 1 * * *"

jobs:
auto-update-libs:
uses: canonical/operator-workflows/.github/workflows/auto_update_charm_libs.yaml@main
secrets: inherit
with:
working-directory: ./charms/k8s-worker/
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Auto-update charm libraries
name: Auto-update K8s charm libraries

on:
schedule:
Expand All @@ -8,3 +8,5 @@ jobs:
auto-update-libs:
uses: canonical/operator-workflows/.github/workflows/auto_update_charm_libs.yaml@main
secrets: inherit
with:
working-directory: ./charms/k8s/
9 changes: 0 additions & 9 deletions .github/workflows/inclusive_naming_check.yaml

This file was deleted.

12 changes: 3 additions & 9 deletions .github/workflows/integration_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ jobs:
uses: canonical/operator-workflows/.github/workflows/integration_test.yaml@main
secrets: inherit
with:
chaos-app-label: app.kubernetes.io/name=indico
provider: lxd
juju-channel: 3.1/stable
chaos-enabled: false
chaos-experiments: pod-delete
load-test-enabled: false
load-test-run-args: "-e LOAD_TEST_HOST=localhost"
zap-before-command: "curl -H \"Host: indico.local\" http://localhost/bootstrap --data-raw 'csrf_token=00000000-0000-0000-0000-000000000000&first_name=admin&last_name=admin&email=admin%40admin.com&username=admin&password=lunarlobster&confirm_password=lunarlobster&affiliation=Canonical'"
zap-enabled: true
zap-cmd-options: '-T 60 -z "-addoninstall jython" --hook "/zap/wrk/tests/zap/hook.py"'
zap-target: localhost
zap-target-port: 80
zap-rules-file-name: "zap_rules.tsv"
zap-enabled: false
trivy-fs-enabled: true
trivy-image-config: "trivy.yaml"
15 changes: 15 additions & 0 deletions .github/workflows/publish-k8s-worker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Publish K8s-worker to edge

on:
workflow_dispatch:
push:
branches:
- main

jobs:
publish-to-edge:
uses: canonical/operator-workflows/.github/workflows/publish_charm.yaml@main
secrets: inherit
with:
channel: latest/edge
working-directory: ./charms/k8s-worker/
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish to edge
name: Publish K8s to edge

on:
workflow_dispatch:
Expand All @@ -12,3 +12,4 @@ jobs:
secrets: inherit
with:
channel: latest/edge
working-directory: ./charms/k8s/
12 changes: 12 additions & 0 deletions .github/workflows/tests-k8s-worker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Tests K8s Worker

on:
pull_request:

jobs:
unit-tests:
uses: canonical/operator-workflows/.github/workflows/test.yaml@main
secrets: inherit
with:
self-hosted-runner: false
working-directory: ./charms/k8s-worker/
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: Tests K8s

on:
pull_request:
Expand All @@ -7,3 +7,6 @@ jobs:
unit-tests:
uses: canonical/operator-workflows/.github/workflows/test.yaml@main
secrets: inherit
with:
self-hosted-runner: false
working-directory: ./charms/k8s/
47 changes: 47 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
header:
- license:
copyright-owner: Canonical Ltd.
content: |
Copyright [year] [owner]
Licensed under the Apache2.0. See LICENSE file in charm source for details.
paths:
- 'lib/**'
comment: on-failure
- license:
spdx-id: Apache-2.0
copyright-owner: Canonical Ltd.
content: |
Copyright [year] [owner]
See LICENSE file for licensing details.
paths:
- '**'
paths-ignore:
- '.github/**'
- '**/.gitkeep'
- '**/*.cfg'
- '**/*.conf'
- '**/*.ini'
- '**/*.j2'
- '**/*.json'
- '**/*.md'
- '**/*.rule'
- '**/*.tmpl'
- '**/*.txt'
- '**/*.xml'
- '.codespellignore'
- '.dockerignore'
- '.flake8'
- '.jujuignore'
- '.gitignore'
- '.licenserc.yaml'
- '.trivyignore'
- '.woke.yaml'
- '.woke.yml'
- 'CODEOWNERS'
- 'icon.svg'
- 'LICENSE'
- 'pyproject.toml'
- 'trivy.yaml'
- 'zap_rules.tsv'
- 'lib/**'
comment: on-failure
File renamed without changes.
27 changes: 27 additions & 0 deletions charms/k8s-worker/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
# This file populates the Overview on Charmhub.
# See https://juju.is/docs/sdk/metadata-reference for a checklist and guidance.

name: k8s-worker
display-name: Kubernetes Worker
summary: A machine charm for a K8s Worker
docs: https://discourse.charmhub.io
issues: https://github.com/canonical/k8s-operator/issues
maintainers:
- https://launchpad.net/~containers
source: https://github.com/canonical/k8s-operator

assumes:
- juju >= 3.1

description: |
A machine charm which operates a Kubernetes worker.
This charm installs and operates a Kubernetes worker via the k8s snap. It exposes
relations to co-operate with other kubernetes components
This charm provides the following running components:
* kube-proxy
* kubelet
* containerd
73 changes: 73 additions & 0 deletions charms/k8s-worker/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[tool.bandit]
exclude_dirs = ["/venv/"]
[tool.bandit.assert_used]
skips = ["*/*test.py", "*/test_*.py", "*tests/*.py"]

# Testing tools configuration
[tool.coverage.run]
branch = true

# Formatting tools configuration
[tool.black]
line-length = 99
target-version = ["py38"]

[tool.coverage.report]
show_missing = true

# Linting tools configuration
[tool.flake8]
max-line-length = 99
max-doc-length = 99
max-complexity = 10
exclude = [".git", "__pycache__", ".tox", "build", "dist", "*.egg_info", "venv"]
select = ["E", "W", "F", "C", "N", "R", "D", "H"]
# Ignore W503, E501 because using black creates errors with this
# Ignore D107 Missing docstring in __init__
ignore = ["W503", "E501", "D107"]
# D100, D101, D102, D103: Ignore missing docstrings in tests
per-file-ignores = ["tests/*:D100,D101,D102,D103,D104,D205,D212,D415"]
docstring-convention = "google"

[tool.isort]
line_length = 99
profile = "black"

[tool.mypy]
ignore_missing_imports = true
explicit_package_bases = true
namespace_packages = true

[tool.pylint]
disable = "wrong-import-order,redefined-outer-name"

[tool.pytest.ini_options]
minversion = "6.0"
log_cli_level = "INFO"

# Linting tools configuration
[tool.ruff]
line-length = 99
select = ["E", "W", "F", "C", "N", "D", "I001"]
extend-ignore = [
"D203",
"D204",
"D213",
"D215",
"D400",
"D404",
"D406",
"D407",
"D408",
"D409",
"D413",
]
ignore = ["E501", "D107"]
extend-exclude = ["__pycache__", "*.egg_info"]
per-file-ignores = {"tests/*" = ["D100","D101","D102","D103","D104"]}

[tool.ruff.mccabe]
max-complexity = 10

[tool.codespell]
skip = "build,lib,venv,icon.svg,.tox,.git,.mypy_cache,.ruff_cache,.coverage"
File renamed without changes.
49 changes: 49 additions & 0 deletions charms/k8s-worker/src/charm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3

# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

# Learn more at: https://juju.is/docs/sdk

"""K8s-worker Charm.
A machine charm which operates a Kubernetes worker.
This charm installs and operates a Kubernetes worker via the k8s snap. It exposes
relations to co-operate with other kubernetes components.
"""


import logging

import ops

# Log messages can be retrieved using juju debug-log
logger = logging.getLogger(__name__)

VALID_LOG_LEVELS = ["info", "debug", "warning", "error", "critical"]


class K8sWorkerCharm(ops.CharmBase):
"""Charm the service."""

def __init__(self, *args):
"""Construct.
Args:
args: Arguments passed to the CharmBase parent constructor.
"""
super().__init__(*args)
self.framework.observe(self.on.update_status, self._on_update_status)

def _on_update_status(self, _event: ops.UpdateStatusEvent):
"""Handle update-status event.
Args:
_event: event triggering the handler.
"""
self.unit.status = ops.ActiveStatus("Ready")


if __name__ == "__main__": # pragma: nocover
ops.main.main(K8sWorkerCharm)
File renamed without changes.
33 changes: 33 additions & 0 deletions charms/k8s-worker/tests/unit/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

# Learn more about testing at: https://juju.is/docs/sdk/testing

# pylint: disable=duplicate-code,missing-function-docstring
"""Unit tests."""


import ops
import ops.testing
import pytest

from charm import K8sWorkerCharm


@pytest.fixture()
def harness():
harness = ops.testing.Harness(K8sWorkerCharm)
harness.begin()
yield harness
harness.cleanup()


def test_config_changed_invalid(harness):
# Trigger a config-changed event with an unknown-config option
with pytest.raises(ValueError):
harness.update_config({"unknown-config": "foobar"})


def test_update_status(harness):
harness.charm.on.update_status.emit()
assert harness.model.unit.status == ops.ActiveStatus("Ready")
Loading

0 comments on commit 5cdacf2

Please sign in to comment.