Skip to content

Commit

Permalink
build-s2i-python-kopf v1.36.1 (#600)
Browse files Browse the repository at this point in the history
- Bump kopf to 1.36.1
- Fix async example to initalize api within async method
  • Loading branch information
jkupferer authored Jun 2, 2023
1 parent 3b7372c commit 558402c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
6 changes: 3 additions & 3 deletions build-s2i-python-kopf/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM registry.access.redhat.com/ubi8/python-39:latest
FROM registry.access.redhat.com/ubi9/python-311:latest

MAINTAINER Johnathan Kupferer <[email protected]>

ENV OPENSHIFT_CLIENT_VERSION=4.11.18 \
HELM_VERSION=3.10.0
ENV OPENSHIFT_CLIENT_VERSION=4.13.2 \
HELM_VERSION=3.12.0

LABEL io.k8s.description="Python Kopf - Kubernetes Operator Framework" \
io.k8s.display-name="Kopf Operator" \
Expand Down
2 changes: 1 addition & 1 deletion build-s2i-python-kopf/build-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ metadata:

parameters:
- name: FROM_IMAGE
value: registry.access.redhat.com/ubi8/python-39:latest
value: registry.access.redhat.com/ubi9/python-311:latest
- name: GIT_REPO
value: https://github.com/redhat-cop/containers-quickstarts.git
- name: NAME
Expand Down
26 changes: 18 additions & 8 deletions build-s2i-python-kopf/examples/kopf-simple/operator/operator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

import asyncio
import kopf
import kubernetes_asyncio
import os
Expand All @@ -9,13 +10,7 @@

operator_domain = os.environ.get('OPERATOR_DOMAIN', 'kopf-simple.example.com')
config_map_label = operator_domain + '/config'

if os.path.exists('/var/run/secrets/kubernetes.io/serviceaccount'):
kubernetes_asyncio.config.load_incluster_config()
else:
kubernetes_asyncio.config.load_kube_config()

core_v1_api = kubernetes_asyncio.client.CoreV1Api()
core_v1_api = None

def random_string(length=8, character_set=''):
'''
Expand Down Expand Up @@ -120,6 +115,12 @@ async def manage_secret_for_config_map(name, namespace, config_map, logger):
'''
Create secrets based on config_map.
'''
task = asyncio.create_task(
__manage_secret_for_config_map(name, namespace, config_map, logger)
)
await task

async def __manage_secret_for_config_map(name, namespace, config_map, logger):
config = load_config_map(config_map)
owner_reference = owner_reference_from_resource(config_map)
secret = await get_secret(name, namespace)
Expand All @@ -133,10 +134,19 @@ async def manage_secret_for_config_map(name, namespace, config_map, logger):
await update_config_map_status(name, namespace, config_map, secret)

@kopf.on.startup()
def configure(settings: kopf.OperatorSettings, **_):
async def configure(settings: kopf.OperatorSettings, **_):
global core_v1_api

# Disable scanning for Namespaces and CustomResourceDefinitions
settings.scanning.disabled = True

if os.path.exists('/var/run/secrets/kubernetes.io/serviceaccount'):
kubernetes_asyncio.config.load_incluster_config()
else:
await kubernetes_asyncio.config.load_kube_config()

core_v1_api = kubernetes_asyncio.client.CoreV1Api()

@kopf.on.create('', 'v1', 'configmaps', labels={config_map_label: kopf.PRESENT})
async def on_create_config_map(body, name, namespace, logger, **_):
logger.info("New app ConfigMap '%s'", name)
Expand Down
7 changes: 4 additions & 3 deletions build-s2i-python-kopf/install/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
kopf==1.36.0
kubernetes==25.3.0
kubernetes-asyncio==24.2.2
kopf==1.36.1
kubernetes==26.1.0
kubernetes-asyncio==24.2.3
openshift==0.13.1
pykube-ng==22.9.0
uvloop==0.17.0
2 changes: 1 addition & 1 deletion build-s2i-python-kopf/version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"v1.36.0"}
{"version":"v1.36.1"}

0 comments on commit 558402c

Please sign in to comment.