E2 | Der Kopf

: Using decorators like @kopf.on.create , @kopf.on.update , or @kopf.on.delete to define behavior.

: Using the logger argument to provide visibility into the operator's actions within the cluster logs. der kopf e2

A common second-stage feature in Kopf involves using the @kopf.on.create decorator to manage the lifecycle of a resource. This handler ensures that when a user creates a specific resource, the operator performs the necessary setup (like creating a Deployment or a Secret). : Using decorators like @kopf

Resume events keep firing · Issue #223 · nolar/kopf - GitHub This handler ensures that when a user creates

import kopf import kubernetes.client as k8s @kopf.on.create('example.com', 'v1', 'myresources') def create_fn(spec, name, namespace, logger, **kwargs): """ Feature: Handle the creation of a 'MyResource' object. This handler reacts when a new resource is added to the cluster. """ # 1. Extract values from the spec image = spec.get('image', 'nginx:latest') replicas = spec.get('replicas', 1) logger.info(f"Creating a deployment for {name} with image {image}") # 2. Logic to create a child resource (e.g., a Deployment) api = k8s.AppsV1Api() # Define the Deployment object deployment = { "apiVersion": "apps/v1", "kind": "Deployment", "metadata": {"name": name, "namespace": namespace}, "spec": { "replicas": replicas, "selector": {"matchLabels": {"app": name}}, "template": { "metadata": {"labels": {"app": name}}, "spec": { "containers": [{"name": "app", "image": image}] } } } } # 3. Apply the deployment to the cluster # Note: In a real operator, you should also set owner references # so the deployment is deleted when the CRD is deleted. kopf.adopt(deployment) api.create_namespaced_deployment(namespace=namespace, body=deployment) return {'status': 'Successfully initialized'} Use code with caution. Copied to clipboard Key Concepts in Stage 2

Understood
This website is using cookies, more details HERE