Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into typehints-lazydictview
Browse files Browse the repository at this point in the history
  • Loading branch information
nolar committed Oct 4, 2019
2 parents 0eaee56 + 312ba46 commit 4330354
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 70 deletions.
17 changes: 10 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ env:
- MINIKUBE_WANTREPORTERRORPROMPT=false
- MINIKUBE_IN_STYLE=true
- MINIKUBE_HOME=$HOME
- MINIKUBE_VERSION=1.0.1
- MINIKUBE_VERSION=latest
matrix:
- KUBERNETES_VERSION=1.14.0 CLIENT=yes # only one "yes" is enough
- KUBERNETES_VERSION=1.14.0 CLIENT=no
- KUBERNETES_VERSION=1.13.0 CLIENT=no
- KUBERNETES_VERSION=1.12.0 CLIENT=no
# - KUBERNETES_VERSION=1.11.10 # Minikube fails on CRI preflight checks
# - KUBERNETES_VERSION=1.10.13 # CRDs require spec.version, which fails on 1.14
- KUBERNETES_VERSION=latest CLIENT=yes # only one "yes" is enough
- KUBERNETES_VERSION=latest CLIENT=no
- KUBERNETES_VERSION=v1.16.0 CLIENT=no
- KUBERNETES_VERSION=v1.15.0 CLIENT=no
- KUBERNETES_VERSION=v1.14.0 CLIENT=no
- KUBERNETES_VERSION=v1.13.0 CLIENT=no
- KUBERNETES_VERSION=v1.12.0 CLIENT=no
# - KUBERNETES_VERSION=v1.11.10 # Minikube fails on CRI preflight checks
# - KUBERNETES_VERSION=v1.10.13 # CRDs require spec.version, which fails on 1.14

python:
- "3.7"
Expand Down
8 changes: 5 additions & 3 deletions kopf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@
run,
create_tasks, # deprecated
)
from kopf.structs.hierarchies import (
from kopf.structs.bodies import (
build_object_reference,
build_owner_reference,
)
from kopf.toolkits.hierarchies import (
adopt,
label,
harmonize_naming,
adjust_namespace,
build_object_reference,
build_owner_reference,
append_owner_reference,
remove_owner_reference,
)
Expand Down
4 changes: 2 additions & 2 deletions kopf/clients/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from kopf import config
from kopf.clients import auth
from kopf.structs import hierarchies
from kopf.structs import bodies

logger = logging.getLogger(__name__)

Expand All @@ -30,7 +30,7 @@ async def post_event(*, obj=None, ref=None, type, reason, message=''):
if obj is None and ref is None:
raise TypeError("One of obj= and ref= is required for a posted event. Got none.")
if ref is None:
ref = hierarchies.build_object_reference(obj)
ref = bodies.build_object_reference(obj)

now = datetime.datetime.utcnow()

Expand Down
4 changes: 2 additions & 2 deletions kopf/engines/posting.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

from kopf import config
from kopf.clients import events
from kopf.structs import bodies
from kopf.structs import dicts
from kopf.structs import hierarchies

# Logging and event-posting can happen cross-thread: e.g. in sync-executors.
# We have to remember our main event-loop with the queue consumer, to make
Expand Down Expand Up @@ -69,7 +69,7 @@ def enqueue(ref, type, reason, message):

def event(objs, *, type, reason, message=''):
for obj in dicts.walk(objs):
ref = hierarchies.build_object_reference(obj)
ref = bodies.build_object_reference(obj)
enqueue(ref=ref, type=type, reason=reason, message=message)


Expand Down
1 change: 1 addition & 0 deletions kopf/on.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
def creation_handler(**kwargs):
pass
This module is a part of the framework's public interface.
"""

# TODO: add cluster=True support (different API methods)
Expand Down
41 changes: 41 additions & 0 deletions kopf/structs/bodies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
All the structures coming from/to the Kubernetes API.
"""


def build_object_reference(body):
"""
Construct an object reference for the events.
Keep in mind that some fields can be absent: e.g. ``namespace``
for cluster resources, or e.g. ``apiVersion`` for ``kind: Node``, etc.
"""
ref = dict(
apiVersion=body.get('apiVersion'),
kind=body.get('kind'),
name=body.get('metadata', {}).get('name'),
uid=body.get('metadata', {}).get('uid'),
namespace=body.get('metadata', {}).get('namespace'),
)
return {key: val for key, val in ref.items() if val}


def build_owner_reference(body):
"""
Construct an owner reference object for the parent-children relationships.
The structure needed to link the children objects to the current object as a parent.
See https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/
Keep in mind that some fields can be absent: e.g. ``namespace``
for cluster resources, or e.g. ``apiVersion`` for ``kind: Node``, etc.
"""
ref = dict(
controller=True,
blockOwnerDeletion=True,
apiVersion=body.get('apiVersion'),
kind=body.get('kind'),
name=body.get('metadata', {}).get('name'),
uid=body.get('metadata', {}).get('uid'),
)
return {key: val for key, val in ref.items() if val}
10 changes: 10 additions & 0 deletions kopf/testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
Helper tools to test the Kopf-based operators.
This module is a part of the framework's public interface.
"""
from kopf.toolkits.runner import KopfRunner

__all__ = [
'KopfRunner',
]
8 changes: 0 additions & 8 deletions kopf/testing/__init__.py

This file was deleted.

13 changes: 13 additions & 0 deletions kopf/toolkits/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
Toolkits to improve the developer experience in the context of Kopf.
They are not needed to use the framework or to run the operator
(unlike all other packages), but they can make the development
of the operators much easier.
Some things can be considered as the clients' responsibilities
rather than the operator framework's responsibilities.
In that case, the decision point is whether the functions work
"in the context of Kopf" at least to some extent
(e.g. by using its contextual information of the current handler).
"""
43 changes: 3 additions & 40 deletions kopf/structs/hierarchies.py → kopf/toolkits/hierarchies.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,18 @@
"""
All the functions to properly build the object hierarchies.
"""
from kopf.structs import bodies
from kopf.structs import dicts


def build_object_reference(body):
"""
Construct an object reference for the events.
Keep in mind that some fields can be absent: e.g. ``namespace``
for cluster resources, or e.g. ``apiVersion`` for ``kind: Node``, etc.
"""
ref = dict(
apiVersion=body.get('apiVersion'),
kind=body.get('kind'),
name=body.get('metadata', {}).get('name'),
uid=body.get('metadata', {}).get('uid'),
namespace=body.get('metadata', {}).get('namespace'),
)
return {key: val for key, val in ref.items() if val}


def build_owner_reference(body):
"""
Construct an owner reference object for the parent-children relationships.
The structure needed to link the children objects to the current object as a parent.
See https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/
Keep in mind that some fields can be absent: e.g. ``namespace``
for cluster resources, or e.g. ``apiVersion`` for ``kind: Node``, etc.
"""
ref = dict(
controller=True,
blockOwnerDeletion=True,
apiVersion=body.get('apiVersion'),
kind=body.get('kind'),
name=body.get('metadata', {}).get('name'),
uid=body.get('metadata', {}).get('uid'),
)
return {key: val for key, val in ref.items() if val}


def append_owner_reference(objs, owner):
"""
Append an owner reference to the resource(s), if it is not yet there.
Note: the owned objects are usually not the one being processed,
so the whole body can be modified, no patches are needed.
"""
owner = build_owner_reference(owner)
owner = bodies.build_owner_reference(owner)
for obj in dicts.walk(objs):
refs = obj.setdefault('metadata', {}).setdefault('ownerReferences', [])
matching = [ref for ref in refs if ref.get('uid') == owner.get('uid')]
Expand All @@ -64,7 +27,7 @@ def remove_owner_reference(objs, owner):
Note: the owned objects are usually not the one being processed,
so the whole body can be modified, no patches are needed.
"""
owner = build_owner_reference(owner)
owner = bodies.build_owner_reference(owner)
for obj in dicts.walk(objs):
refs = obj.setdefault('metadata', {}).setdefault('ownerReferences', [])
matching = [ref for ref in refs if ref.get('uid') == owner.get('uid')]
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions tests/hierarchies/test_owner_referencing.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ def test_removal_distinguishes_by_uid():
def test_adopting(mocker):
# These methods are tested in their own tests.
# We just check that they are called at all.
append_owner_ref = mocker.patch('kopf.structs.hierarchies.append_owner_reference')
harmonize_naming = mocker.patch('kopf.structs.hierarchies.harmonize_naming')
adjust_namespace = mocker.patch('kopf.structs.hierarchies.adjust_namespace')
label = mocker.patch('kopf.structs.hierarchies.label')
append_owner_ref = mocker.patch('kopf.toolkits.hierarchies.append_owner_reference')
harmonize_naming = mocker.patch('kopf.toolkits.hierarchies.harmonize_naming')
adjust_namespace = mocker.patch('kopf.toolkits.hierarchies.adjust_namespace')
label = mocker.patch('kopf.toolkits.hierarchies.label')

obj = Mock()
kopf.adopt(obj, owner=OWNER, nested=['template'])
Expand Down
2 changes: 1 addition & 1 deletion tools/kubernetes-client.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ set -x

if [[ "${CLIENT:-}" = "yes" ]] ; then
# FIXME: See https://github.com/kubernetes-client/python/issues/866
pip install 'kubernetes<10.0.0'
pip install 'kubernetes>=10.0.1'
fi
13 changes: 10 additions & 3 deletions tools/minikube-for-travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
set -eu
set -x

curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v${KUBERNETES_VERSION}/bin/linux/amd64/kubectl
: ${KUBERNETES_VERSION:=latest}
: ${MINIKUBE_VERSION:=latest}

if [[ "${KUBERNETES_VERSION}" == latest ]] ; then
KUBERNETES_VERSION=$( curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt )
fi

curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/"${KUBERNETES_VERSION}"/bin/linux/amd64/kubectl
chmod +x kubectl
sudo mv kubectl /usr/local/bin/

curl -Lo minikube https://storage.googleapis.com/minikube/releases/v${MINIKUBE_VERSION}/minikube-linux-amd64
curl -Lo minikube https://storage.googleapis.com/minikube/releases/"${MINIKUBE_VERSION}"/minikube-linux-amd64
chmod +x minikube
sudo mv minikube /usr/local/bin/

Expand All @@ -18,6 +25,6 @@ sudo minikube start \
--vm-driver=none \
--extra-config=apiserver.authorization-mode=RBAC \
--extra-config=apiserver.runtime-config=events.k8s.io/v1beta1=false \
--kubernetes-version=v${KUBERNETES_VERSION}
--kubernetes-version="${KUBERNETES_VERSION}"

sudo chown -R travis: /home/travis/.minikube/

0 comments on commit 4330354

Please sign in to comment.