4.8 KiB
Skybridge FaaS Implementation Guide
This document explains the implementation of the Function-as-a-Service (FaaS) component in Skybridge, specifically focusing on the Docker runtime implementation that replaced the original mock implementation.
Overview
The Skybridge FaaS platform allows users to deploy and execute functions in isolated containers. The implementation consists of several key components:
- Function Management: CRUD operations for function definitions
- Execution Engine: Runtime backend for executing functions
- Repository Layer: Data persistence for functions and executions
- Services Layer: Business logic implementation
- API Layer: RESTful interface for managing functions
Docker Runtime Implementation
The original implementation contained a mock Docker runtime (faas/internal/runtime/docker/simple.go) that didn't actually interact with Docker. The new implementation provides real container execution capabilities.
Key Features Implemented
- Real Docker Client Integration: Uses the official Docker client library to communicate with the Docker daemon
- Container Lifecycle Management: Creates, starts, waits for, and cleans up containers
- Image Management: Pulls images when they don't exist locally
- Resource Limiting: Applies memory limits to containers
- Input/Output Handling: Passes input to functions and captures output
- Logging: Retrieves container logs for debugging
- Health Checks: Verifies Docker daemon connectivity
Implementation Details
Container Creation
The createContainer method creates a Docker container with the following configuration:
- Environment Variables: Function environment variables plus input data
- Resource Limits: Memory limits based on function configuration
- Attached Streams: STDOUT and STDERR for log capture
Function Execution Flow
- Container Creation: Creates a container from the function's Docker image
- Container Start: Starts the container execution
- Wait for Completion: Waits for the container to finish execution
- Result Collection: Gathers output, logs, and execution metadata
- Cleanup: Removes the container to free resources
Error Handling
The implementation includes comprehensive error handling:
- Connection Errors: Handles Docker daemon connectivity issues
- Container Errors: Manages container creation and execution failures
- Resource Errors: Handles resource constraint violations
- Graceful Cleanup: Ensures containers are cleaned up even on failures
Testing
Unit Tests
Unit tests are located in faas/test/integration/ and cover:
- Docker runtime health checks
- Container creation and execution
- Error conditions
Example Function
An example "Hello World" function is provided in faas/examples/hello-world/ to demonstrate:
- Function structure and implementation
- Docker image creation
- Local testing
- Deployment to Skybridge FaaS
Deployment
Prerequisites
- Docker daemon running and accessible
- Docker socket mounted to the FaaS service container (as shown in
docker-compose.yml) - Required permissions to access Docker
Configuration
The FaaS service reads configuration from environment variables:
FAAS_DEFAULT_RUNTIME: Should be set to "docker"- Docker socket path: Typically
/var/run/docker.sock
Security Considerations
The current implementation has basic security features:
- Container Isolation: Functions run in isolated containers
- Resource Limits: Prevents resource exhaustion
- Image Verification: Only pulls trusted images
For production use, consider implementing:
- Container user restrictions
- Network isolation
- Enhanced logging and monitoring
- Authentication and authorization for Docker operations
Performance Optimizations
Potential performance improvements include:
- Image Caching: Pre-pull commonly used images
- Container Pooling: Maintain a pool of ready containers
- Parallel Execution: Optimize concurrent function execution
- Resource Monitoring: Track and optimize resource usage
Future Enhancements
Planned enhancements include:
- Multiple Runtime Support: Add support for Podman and other container runtimes
- Advanced Resource Management: CPU quotas, disk limits
- Enhanced Monitoring: Detailed metrics and tracing
- Improved Error Handling: More granular error reporting
- Security Hardening: Additional security measures for container execution
API Usage
The FaaS API provides endpoints for:
- Function Management: Create, read, update, delete functions
- Deployment: Deploy functions to prepare for execution
- Execution: Execute functions synchronously or asynchronously
- Monitoring: View execution status, logs, and metrics
Refer to the API documentation endpoint (/api/docs) for detailed information.