Присоединяйся
Регистрация: Using the logger argument to provide visibility into the operator's actions within the cluster logs.
In the context of Kopf (Kubernetes Operator Pythonic Framework) , a "feature" typically refers to a handler that reacts to events on a Custom Resource Definition (CRD) . Handler Feature for Kopf (Part 2)
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
: Using kopf.adopt(child_object) to link the lifecycle of the new resource to its parent, ensuring clean deletions.
: Using decorators like @kopf.on.create , @kopf.on.update , or @kopf.on.delete to define behavior.
Вы в первый раз зашли с помощью социальной сети. Если у вас уже есть профиль на нашем сайте, то вы можете связать ее с соц. сетью. Иначе укажите ваш электронный адрес и зарегистрируйтесь как новый пользователь.