Skip to content

Commit

Permalink
Merge pull request #23 from OmegaVVeapon/silent-handlers
Browse files Browse the repository at this point in the history
Use only on.event handlers
  • Loading branch information
OmegaVVeapon authored Jun 24, 2021
2 parents b800878 + 7784186 commit 92b237d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 42 deletions.
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,6 @@ If you are looking to use this image with the [Grafana Helm chart](https://githu
* Set `.Values.sidecar.image.repository` to `omegavveapon/kopf-k8s-sidecar`
* Set `.Values.sidecar.image.tag` to the latest tag in [Releases](https://github.com/OmegaVVeapon/kopf-k8s-sidecar/releases)

### RBAC permissions

Provide `patch` permissions for `configmaps` and `secrets` in the `values.yaml`

```
rbac:
- extraClusterRoleRules: []
+ extraClusterRoleRules:
+ - apiGroups: [""] # "" indicates the core API group
+ resources: ["configmaps", "secrets"]
+ verbs: ["patch"]
```
The operator needs to `patch` resources to add a `kopf.zalando.org/last-handled-configuration` annotation which is used to handle [change detection](https://kopf.readthedocs.io/en/stable/configuration/?highlight=last-handled-configuration#change-detection).

## Configuration Environment Variables

| Variable | Required? | Default | Description |
Expand Down
4 changes: 4 additions & 0 deletions app/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def resource_is_desired(body, **_):

return sidecar_settings.RESOURCE in (kind, 'both')

def resource_is_cru(event, **_):
"""Returns true if the resource was created, resumed or updated in the on.event handler"""
return not resource_is_deleted(event)

def resource_is_deleted(event, **_):
"""Returns true if the resource was deleted in the on.event handler"""
return event['type'] == 'DELETED'
9 changes: 6 additions & 3 deletions app/io_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,22 @@ def delete_file(body, kind, logger):

for filename in body['data'].keys():
filepath = get_filepath(filename, folder, kind, body)
logger.info("[DELETE:%s] Deleting file %s.", kind, filepath)
logger.info("[DELETED:%s] Deleting file %s.", kind, filepath)
try:
os.remove(filepath)
except FileNotFoundError:
logger.error("[DELETE:%s] %s not found.", kind, filepath)
logger.error("[DELETED:%s] %s not found.", kind, filepath)
except OSError as e:
logger.error(e)

def write_file(event, body, kind, logger):
"""
Write contents to the desired filepath if they have changed.
"""
event = event.upper()
if event is None:
event = 'RESUMED'
else:
event = event.upper()

folder = get_folder(body['metadata'])
create_folder(folder, logger)
Expand Down
24 changes: 7 additions & 17 deletions app/sidecar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import contextlib
import sidecar_settings
from io_helpers import write_file, delete_file
from conditions import label_is_satisfied, resource_is_desired, resource_is_deleted
from conditions import *
from list_mode import one_run
import kopf

Expand All @@ -23,14 +23,11 @@ def startup_tasks(settings: kopf.OperatorSettings, logger, **_):
# Disable k8s event logging
settings.posting.enabled = False

@kopf.on.resume('', 'v1', 'configmaps', when=kopf.all_([label_is_satisfied, resource_is_desired]))
@kopf.on.create('', 'v1', 'configmaps', when=kopf.all_([label_is_satisfied, resource_is_desired]))
@kopf.on.update('', 'v1', 'configmaps', when=kopf.all_([label_is_satisfied, resource_is_desired]))
@kopf.on.resume('', 'v1', 'secrets', when=kopf.all_([label_is_satisfied, resource_is_desired]))
@kopf.on.create('', 'v1', 'secrets', when=kopf.all_([label_is_satisfied, resource_is_desired]))
@kopf.on.update('', 'v1', 'secrets', when=kopf.all_([label_is_satisfied, resource_is_desired]))
def cru_fn(body, reason, logger, **_):
write_file(reason, body, body['kind'], logger)
# Event types when not 'DELETED' can be 'ADDED', 'MODIFIED' and None. The latter being when the resource already existed when the operator started
@kopf.on.event('', 'v1', 'configmaps', when=kopf.all_([label_is_satisfied, resource_is_desired, resource_is_created]))
@kopf.on.event('', 'v1', 'secrets', when=kopf.all_([label_is_satisfied, resource_is_desired, resource_is_created]))
def cru_fn(body, event, logger, **_):
write_file(event['type'], body, body['kind'], logger)

@kopf.on.event('', 'v1', 'configmaps', when=kopf.all_([label_is_satisfied, resource_is_desired, resource_is_deleted]))
@kopf.on.event('', 'v1', 'secrets', when=kopf.all_([label_is_satisfied, resource_is_desired, resource_is_deleted]))
Expand All @@ -45,13 +42,6 @@ def kopf_thread(
asyncio.set_event_loop(loop)
with contextlib.closing(loop):

# Since we're using an embedded operator we can't rely on CLI options to configure the logger
# we have to do it here, before we start the operator
# kopf.configure(
# debug=sidecar_settings.DEBUG,
# verbose=sidecar_settings.VERBOSE
# )

# The Grafana Helm chart doesn't even use liveness probes for the sidecar... worth enabling?
# liveness_endpoint = "http://0.0.0.0:8080/healthz"

Expand All @@ -77,7 +67,7 @@ def main():
stop_flag = threading.Event()
thread = threading.Thread(target=kopf_thread, kwargs=dict(
stop_flag=stop_flag,
ready_flag=ready_flag,
ready_flag=ready_flag
))
thread.start()
ready_flag.wait()
Expand Down
9 changes: 1 addition & 8 deletions examples/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ metadata:
rules:
- apiGroups: [""]
resources: ["configmaps", "secrets"]
verbs: ["get", "watch", "list", "patch"]
# Framework: posting the events about the handlers progress/errors.
# - apiGroups: [events.k8s.io]
# resources: [events]
# verbs: [create]
# - apiGroups: [""]
# resources: [events]
# verbs: [create]
verbs: ["get", "watch", "list"]
---
apiVersion: v1
kind: ServiceAccount
Expand Down

0 comments on commit 92b237d

Please sign in to comment.