there is Mediaplan CustomDefinitionResource: CustomResourceDefinition apiVersion:…
- User
there is Mediaplan CustomDefinitionResource:
CustomResourceDefinition
apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: name: mediaplans.crd.idom.project spec: group: crds.idom.project scope: Namespaced names: plural: mediaplans singular: mediaplan kind: Mediaplan shortNames: - mediap versions:
- name: v1alpha1 served: true storage: true schema: openAPIV3Schema: type: object properties: apiVersion: type: string description: ‘A unique version for the Kubernetes API’ kind: type: string description: ‘Type of resource’ metadata: type: object properties: name: type: string description: ‘Name of the resource’ required: - name spec: type: object properties: # scheduling schedule: type: string description: ‘Scheduling format for the mediaplan’ # channel metadata channel: type: object properties: name: type: string description: ‘Name of the channel’ guid: type: string description: ‘GUID of the channel’ id: type: integer description: ‘ID of the channel’ link: type: string description: ‘Link for the channel’ messagingSettings: type: object properties: # oneof: topic or files # generation topics topics: type: array items: type: object properties: code: type: string description: ‘Code for the topic’ weight: type: integer description: ‘Generation weight for the topic’ default: 1 # or choose files from s3 files: type: array items: type: object properties: uri: type: string description: ‘URI for the file’ # agent settings agentSettings: type: object properties: # count totalCount: type: integer description: ‘Total number of agents for the mediaplan’ default: 10 # credential comparison credentials: type: array items: type: object properties: key: type: string description: ‘Key for the credential’ value: type: string description: ‘Value for the credential’ # mimicry comparison mimicry: type: array items: type: object properties: key: type: string description: ‘Key for the mimicry option’ value: type: string description: ‘Value for the mimicry option’ # common mediaplan settings commonSettings: type: object properties: # output pkg generation coefficient # define, how many packages will be generated for one incoming message generationCoefficient: type: number description: ‘Output package generation coefficient’ default: # total packages count totalMessageCount: type: integer description: ‘Total number of messages for the mediaplan’ # estimated time for different messaging options estimatedLeadTime: type: string description: ‘Estimated lead time for the mediaplan’ # disable mimicry options (for agents) disableMimicryOptions: type: boolean description: ‘Disables mimicry options for agents’ # use image for packages didImageUsedForGeneration: type: boolean description: ‘Indicates if an image was used for package generation’
please show a sample of a python web application, based on fastapi, kubernetes, opentelemetry with jaeger support, pydantic models, which serve CRUD operations for Mediaplan CustomResourceDefinition
- Assistant
Here's a sample of a Python web application that uses FastAPI, Kubernetes, OpenTelemetry with Jaeger support, and pydantic models to serve CRUD operations for the Mediaplan CustomResourceDefinition.
from fastapi import FastAPI, HTTPException from kubernetes import client, config from opentelemetry import trace from opentelemetry.exporter import jaeger from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor from opentelemetry.instrumentation.kubernetes import KubernetesInstrumentor from opentelemetry.instrumentation.requests import RequestsInstrumentor from pydantic import BaseModel app = FastAPI() # Initialize OpenTelemetry with Jaeger exporter trace.set_tracer_provider(jaeger.JaegerSpanExporter(service_name="mediaplan-api")) KubernetesInstrumentor().instrument() RequestsInstrumentor().instrument() FastAPIInstrumentor().instrument_app(app) # Load kubernetes configuration config.load_kube_config() # Define pydantic model for Mediaplan class Mediaplan(BaseModel): schedule: str channel: dict messagingSettings: dict agentSettings: dict commonSettings: dict # Define kubernetes API client custom_api = client.CustomObjectsApi() # Define CRUD operations for Mediaplan CustomResourceDefinition @app.post("/mediaplans") def create_mediaplan(mediaplan: Mediaplan): namespace = "default" name = "example-mediaplan" crd = { "apiVersion": "crds.idom.project/v1alpha1", "kind": "Mediaplan", "metadata": {"name": name}, "spec": mediaplan, } try: custom_api.create_namespaced_custom_object( "crds.idom.project", "v1alpha1", namespace, "mediaplans", crd, ) except client.rest.ApiException as e: raise HTTPException(status_code=e.status, detail=e.reason) @app.get("/mediaplans/{name}") def read_mediaplan(name: str): namespace = "default" try: mediaplan = custom_api.get_namespaced_custom_object( "crds.idom.project", "v1alpha1", namespace, "mediaplans", name ) except client.rest.ApiException as e: raise HTTPException(status_code=e.status, detail=e.reason) return mediaplan @app.put("/mediaplans/{name}") def update_mediaplan(name: str, mediaplan: Mediaplan): namespace = "default" try: existing_mediaplan = custom_api.get_namespaced_custom_object( "crds.idom.project", "v1alpha1", namespace, "mediaplans", name ) except client.rest.ApiException as e: raise HTTPException(status_code=e.status, detail=e.reason) existing_mediaplan["spec"] = mediaplan try: custom_api.replace_namespaced_custom_object( "crds.idom.project", "v1alpha1", namespace, "mediaplans", name, existing_mediaplan, ) except client.rest.ApiException as e: raise HTTPException(status_code=e.status, detail=e.reason) @app.delete("/mediaplans/{name}") def delete_mediaplan(name: str): namespace = "default" try: custom_api.delete_namespaced_custom_object( "crds.idom.project", "v1alpha1", namespace, "mediaplans", name ) except client.rest.ApiException as e: raise HTTPException(status_code=e.status, detail=e.reason)This web application defines four endpoints to support CRUD operations for the Mediaplan CustomResourceDefinition. It uses pydantic models to validate incoming Mediaplan data and the Kubernetes API client to perform CRUD operations on the Mediaplan CustomResourceDefinition. It also uses OpenTelemetry with Jaeger support to trace requests and instrumentations for FastAPI, Kubernetes, and requests.