Skip to content

Commit

Permalink
Create k8s and k8s-worker charms
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess committed Dec 5, 2023
1 parent e9ef5e1 commit 4f199f7
Show file tree
Hide file tree
Showing 19 changed files with 517 additions and 74 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ on:

jobs:
unit-tests:
strategy:
matrix:
charm: ["k8s", "k8s-worker"]
uses: canonical/operator-workflows/.github/workflows/test.yaml@main
secrets: inherit
with:
self-hosted-runner: false
working-directory: ${{ matrix.charm }}
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
File renamed without changes.
48 changes: 48 additions & 0 deletions charms/k8s-worker/src/charm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

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

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

"""Charm the service.
Refer to the following post for a quick-start guide that will help you
develop a new k8s charm using the Operator Framework:
https://discourse.charmhub.io/t/4208
"""

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.
32 changes: 32 additions & 0 deletions charms/k8s-worker/tests/unit/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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")
120 changes: 120 additions & 0 deletions charms/k8s-worker/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

[tox]
skipsdist=True
skip_missing_interpreters = True
envlist = lint, unit, static, coverage-report

[vars]
src_path = {toxinidir}/src/
tst_path = {toxinidir}/tests/
;lib_path = {toxinidir}/lib/charms/operator_name_with_underscores
all_path = {[vars]src_path} {[vars]tst_path}

[testenv]
setenv =
PYTHONPATH = {toxinidir}:{toxinidir}/lib:{[vars]src_path}
PYTHONBREAKPOINT=ipdb.set_trace
PY_COLORS=1
passenv =
PYTHONPATH
CHARM_BUILD_DIR
MODEL_SETTINGS

[testenv:format]
description = Apply coding style standards to code
deps =
black
isort
commands =
isort {[vars]all_path}
black {[vars]all_path}

[testenv:lint]
description = Check code against coding style standards
deps =
black
codespell
flake8<6.0.0
flake8-builtins
flake8-copyright<6.0.0
flake8-docstrings>=1.6.0
flake8-docstrings-complete>=1.0.3
flake8-test-docs>=1.0
mypy
pep8-naming
pydocstyle>=2.10
pylint
pyproject-flake8<6.0.0
pytest
pytest-asyncio
pytest-operator
requests
types-PyYAML
types-requests
-r{toxinidir}/requirements.txt
commands =
pydocstyle {[vars]src_path}
# uncomment the following line if this charm owns a lib
# codespell {[vars]lib_path}
codespell {toxinidir} --skip {toxinidir}/.git --skip {toxinidir}/.tox \
--skip {toxinidir}/build --skip {toxinidir}/lib --skip {toxinidir}/venv \
--skip {toxinidir}/.mypy_cache --skip {toxinidir}/icon.svg
# pflake8 wrapper supports config from pyproject.toml
pflake8 {[vars]all_path} --ignore=W503
isort --check-only --diff {[vars]all_path}
black --check --diff {[vars]all_path}
mypy {[vars]all_path}
pylint {[vars]all_path}

[testenv:unit]
description = Run unit tests
deps =
coverage[toml]
pytest
-r{toxinidir}/requirements.txt
commands =
coverage run --source={[vars]src_path} \
-m pytest --ignore={[vars]tst_path}integration -v --tb native -s {posargs}
coverage report

[testenv:coverage-report]
description = Create test coverage report
deps =
coverage[toml]
pytest
-r{toxinidir}/requirements.txt
commands =
coverage report

[testenv:static]
description = Run static analysis tests
deps =
bandit[toml]
-r{toxinidir}/requirements.txt
commands =
bandit -c {toxinidir}/pyproject.toml -r {[vars]src_path} {[vars]tst_path}

[testenv:integration]
description = Run integration tests
deps =
juju
pytest
pytest-asyncio
pytest-operator
-r{toxinidir}/requirements.txt
commands =
pytest -v --tb native --ignore={[vars]tst_path}unit --log-cli-level=INFO -s {posargs}

[testenv:src-docs]
allowlist_externals=sh
setenv =
PYTHONPATH = {toxinidir}:{toxinidir}/lib:{[vars]src_path}
description = Generate documentation for src
deps =
lazydocs
-r{toxinidir}/requirements.txt
commands =
; can't run lazydocs directly due to needing to run it on src/* which produces an invocation error in tox
sh generate-src-docs.sh
13 changes: 13 additions & 0 deletions charms/k8s/charmcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
# This file configures Charmcraft.
# See https://juju.is/docs/sdk/charmcraft-config for guidance.

type: charm
bases:
- build-on:
- name: ubuntu
channel: "22.04"
run-on:
- name: ubuntu
channel: "22.04"
2 changes: 0 additions & 2 deletions metadata.yaml → charms/k8s/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

name: k8s
display-name: Kubernetes

# (Required)
summary: A machine charm for K8s
docs: https://discourse.charmhub.io
issues: https://github.com/canonical/k8s-operator/issues
Expand Down
1 change: 1 addition & 0 deletions charms/k8s/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ops >= 2.2.0
File renamed without changes.
2 changes: 2 additions & 0 deletions charms/k8s/tests/unit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import ops
import ops.testing
import pytest

from charm import K8sCharm


Expand Down
Loading

0 comments on commit 4f199f7

Please sign in to comment.