deploy
deploy
The deploy module is responsible for defining and orchestrating the deployment of the Code Buddy application onto a Kubernetes cluster. It provides a set of declarative Kubernetes manifests and a convenience script for setting up a local development environment using Kind.
This module focuses on the infrastructure layer, ensuring Code Buddy can be reliably deployed, configured, and accessed.
Module Overview
The deploy module consists of:
- Kubernetes Manifests: YAML files that define the various Kubernetes resources required to run Code Buddy (e.g., Deployments, Services, ConfigMaps, Secrets, Ingress).
- Kind Setup Script: A shell script (
kind-setup.sh) to automate the creation of a local Kubernetes cluster and the deployment of Code Buddy within it, including an NGINX Ingress controller.
Since this module primarily contains declarative configuration files and a shell script, traditional code call graphs and execution flows are not applicable. The execution flow is driven by kubectl apply -f commands and the Kubernetes control plane.
Kubernetes Deployment Architecture
Code Buddy is deployed as a single container within a Kubernetes Pod, managed by a Deployment. Configuration is externalized into ConfigMap and Secret resources. Network access is managed by a Service and an Ingress.
graph TD
subgraph External Access
User[User/Browser] --> Ingress[Ingress: codebuddy.local]
end
subgraph Kubernetes Cluster
Ingress --> Service[Service: codebuddy (Port 80)]
Service --> Deployment[Deployment: codebuddy]
Deployment --> Pod[Pod: codebuddy]
Pod --> Container[Container: codebuddy:latest]
ConfigMap[ConfigMap: codebuddy-config] --> Container
Secret[Secret: codebuddy-secrets] --> Container
ServiceAccount[ServiceAccount: codebuddy] --> Pod
end
Key Kubernetes Components
The deploy/kubernetes directory contains the following manifest files:
configmap.yaml
Defines a ConfigMap named codebuddy-config. This resource stores non-sensitive configuration data as key-value pairs, which are then injected as environment variables into the codebuddy container.
GROK_MODEL: Specifies the AI model to be used (e.g., "grok-3-latest").MAX_COST: Sets a maximum cost limit for AI interactions.RATE_LIMIT_MAX: Defines the maximum number of requests allowed within a certain period for rate limiting.CODEBUDDY_TZ: (Commented out) An optional timezone override for the Docker sandbox.
Contribution Note: To change these application-level settings, modify this file and re-apply it to the cluster (kubectl apply -f configmap.yaml).
secret.yaml
Defines a Secret named codebuddy-secrets of type Opaque. This resource stores sensitive configuration data, also injected as environment variables into the codebuddy container.
grok-api-key: The API key for accessing the Grok AI service.jwt-secret: The secret key used for signing and verifying JSON Web Tokens (JWTs) for authentication.
Contribution Note: Crucially, the placeholder values "REPLACE_ME" must be updated with actual secrets before deploying to any environment. For production, consider using a secret management solution (e.g., HashiCorp Vault, Kubernetes External Secrets) rather than committing secrets directly. For local Kind development, you can edit this file or use kubectl edit secret codebuddy-secrets.
rbac.yaml
Defines Role-Based Access Control (RBAC) resources for the Code Buddy application:
ServiceAccount:codebuddy- The identity under which the Code Buddy Pod runs.Role:codebuddy- Grants permissions togetandwatchConfigMapsandSecretswithin the namespace. While the currentDeploymentconfiguration injects these values directly, thisRoleprovides a foundation for scenarios where the application might need to dynamically read these resources at runtime.RoleBinding:codebuddy- Binds thecodebuddyServiceAccountto thecodebuddyRole.
Contribution Note: If the application's runtime needs to interact with other Kubernetes API resources, the Role definition would need to be extended.
deployment.yaml
Defines the core Deployment resource for the Code Buddy application:
name: codebuddy: The name of the deployment.replicas: 1: Specifies a single replica of the Code Buddy application.image: codebuddy:latest: The Docker image to use for the Code Buddy container.ports: Exposes container ports3000(main HTTP server) and3001(gateway/websocket).env: Environment variables are injected fromcodebuddy-secrets(forGROK_API_KEY,JWT_SECRET) andcodebuddy-config(forGROK_MODEL,MAX_COST,RATE_LIMIT_MAX).NODE_ENVis set toproduction.livenessProbe: An HTTP GET probe to/livezon port3000to determine if the application is running and healthy.readinessProbe: An HTTP GET probe to/readyzon port3000to determine if the application is ready to serve traffic.resources: Defines CPU and memoryrequestsandlimitsfor the container, ensuring resource predictability and preventing resource exhaustion.
Contribution Note: Developers can modify this file to update the image tag, adjust resource allocations, or fine-tune health check parameters.
service.yaml
Defines a Service named codebuddy of type ClusterIP:
selector: app: codebuddy: Targets pods with theapp: codebuddylabel (which matches theDeployment).ports:http: Maps internal cluster port80to the container'stargetPort: 3000.gateway: Maps internal cluster port3001to the container'stargetPort: 3001.
This Service provides a stable internal IP address and DNS name (codebuddy) for other services within the cluster to communicate with the Code Buddy application.
ingress.yaml
Defines an Ingress resource named codebuddy:
annotations:nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
These NGINX-specific annotations are crucial for handling potentially long-lived connections or extended processing times, common in AI interactions.
rules:host: codebuddy.local: Specifies that this Ingress rule applies to requests for thecodebuddy.localhostname.path: /: Routes all traffic for the root path to thecodebuddyservice on port80.
Contribution Note: For production deployments, the host should be updated to a publicly resolvable domain name.
Local Development with Kind (kind-setup.sh)
The kind-setup.sh script provides a convenient way to set up a local Kubernetes development environment for Code Buddy using Kind (Kubernetes in Docker).
Purpose
This script automates the following steps:
- Kind Cluster Creation: Creates a new Kind cluster with specific port mappings to expose the Ingress controller.
- NGINX Ingress Controller Installation: Deploys the NGINX Ingress controller, which is required for the
Ingressresource to function. - Manifest Application: Applies all Kubernetes manifests from the
deploy/kubernetesdirectory to the newly created cluster.
Usage
To set up your local Code Buddy development environment:
bash deploy/kubernetes/kind-setup.sh [CLUSTER_NAME]
CLUSTER_NAMEis optional. If not provided, the cluster will be namedcodebuddy-dev.
Example:
bash deploy/kubernetes/kind-setup.sh
Execution Flow
kind create cluster: A Kind cluster is created. The configuration includesextraPortMappingsto expose host ports8080(for HTTP) and8443(for HTTPS) to the cluster's internal ports80and443, respectively. This allows you to access the Ingress from your host machine vialocalhost:8080.kubectl apply -f .../deploy.yaml: The NGINX Ingress controller is installed into theingress-nginxnamespace.kubectl wait ...: The script waits for the NGINX Ingress controller pods to be ready before proceeding.kubectl apply -f ...: All Kubernetes manifests (configmap.yaml,secret.yaml,rbac.yaml,deployment.yaml,service.yaml,ingress.yaml) are applied to the cluster in the specified order.
Accessing Code Buddy Locally
After the script completes, Code Buddy will be accessible via http://localhost:8080.
Important: The ingress.yaml specifies host: codebuddy.local. For your browser to resolve codebuddy.local to localhost, you will need to add an entry to your host's hosts file (e.g., /etc/hosts on Linux/macOS, C:\Windows\System32\drivers\etc\hosts on Windows):
127.0.0.1 codebuddy.local
Then, navigate to http://codebuddy.local:8080 in your browser.
Contributing and Maintenance
- Updating Configuration: For non-sensitive settings, modify
configmap.yaml. For sensitive data, updatesecret.yaml(or usekubectl edit secret codebuddy-secrets). - Application Updates: To deploy a new version of the Code Buddy application, update the
imagetag indeployment.yamland re-apply the manifest. - Resource Tuning: Adjust
resources(CPU/memory) indeployment.yamlbased on performance monitoring. - Ingress Rules: Modify
ingress.yamlto change the hostname, add more paths, or configure additional Ingress features. - Local Development: Rerun
kind-setup.shto recreate a clean development environment, or usekubectl apply -ffor individual manifest changes.