This is a complete example of a Kubernetes Aggregate API server that implements custom Widget and Gadget resources with full CRUD operations using in-memory storage.
The MyTest API server demonstrates:
- Custom Resource Definitions:
WidgetandGadgetresources with their own specifications - In-Memory Storage: Thread-safe storage with mutex protection
- CRUD Operations: Create, Read, Update, Delete, and List operations
- Kubernetes Integration: Direct integration with Kubernetes API server framework
- Aggregate API: Extends Kubernetes API with custom resources
// Widget represents a custom resource
type Widget struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec WidgetSpec `json:"spec,omitempty"`
Status WidgetStatus `json:"status,omitempty"`
}
type WidgetSpec struct {
Name string `json:"name"`
Description string `json:"description"`
Size int32 `json:"size"`
}
type WidgetStatus struct {
Phase string `json:"phase,omitempty"`
}// Gadget represents a custom resource
type Gadget struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec GadgetSpec `json:"spec,omitempty"`
Status GadgetStatus `json:"status,omitempty"`
}
type GadgetSpec struct {
Type string `json:"type"`
Version string `json:"version"`
Enabled bool `json:"enabled"`
Priority int32 `json:"priority"`
}
type GadgetStatus struct {
State string `json:"state,omitempty"`
}Once deployed, the API server exposes these endpoints:
- Create:
POST /apis/things.myorg.io/v1alpha1/namespaces/{namespace}/widgets - Get:
GET /apis/things.myorg.io/v1alpha1/namespaces/{namespace}/widgets/{name} - Update:
PUT /apis/things.myorg.io/v1alpha1/namespaces/{namespace}/widgets/{name} - List:
GET /apis/things.myorg.io/v1alpha1/namespaces/{namespace}/widgets - Delete:
DELETE /apis/things.myorg.io/v1alpha1/namespaces/{namespace}/widgets/{name}
- Create:
POST /apis/things.myorg.io/v1alpha1/namespaces/{namespace}/gadgets - Get:
GET /apis/things.myorg.io/v1alpha1/namespaces/{namespace}/gadgets/{name} - Update:
PUT /apis/things.myorg.io/v1alpha1/namespaces/{namespace}/gadgets/{name} - List:
GET /apis/things.myorg.io/v1alpha1/namespaces/{namespace}/gadgets - Delete:
DELETE /apis/things.myorg.io/v1alpha1/namespaces/{namespace}/gadgets/{name}
# Set up complete development environment
make dev-setup
# Run quick end-to-end test
make quick-test
# Clean up when done
make dev-teardown-
Set up Kind cluster with cert-manager:
./deploy/kind/setup.sh
-
Deploy the API server:
./deploy/deploy.sh install
-
Verify deployment:
kubectl get apiservice v1alpha1.things.myorg.io kubectl get pods -n my-apiserver-system
-
Prerequisites: Ensure cert-manager is installed
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.18.2/cert-manager.yaml
-
Deploy the API server:
./deploy/deploy.sh install
# Show all available commands
make help
# Development workflow
make dev-setup # Set up complete dev environment
make dev-restart # Rebuild and redeploy
make dev-teardown # Clean up everything
# Building
make build # Build binary
make docker-build # Build Docker image
make release-build # Build release artifacts
# Testing
make test # Run all tests
make test-unit # Run unit tests only
make test-coverage # Run with coverage report
make quick-test # Quick end-to-end test
# Deployment
make deploy # Deploy to Kubernetes
make status # Check deployment status
make logs # Show API server logs-
Build the server:
go build -o mytest-apiserver . # or make build
-
Build Docker image:
docker build -t quay.io/zhujian/mytest-apiserver:dev . # or make docker-build
kubectl apply -f - <<EOF
apiVersion: things.myorg.io/v1alpha1
kind: Widget
metadata:
name: test-widget
namespace: default
spec:
name: "My Test Widget"
description: "A test widget for demonstration"
size: 42
EOFkubectl get widget test-widget -n default -o yamlkubectl patch widget test-widget -n default --type='merge' -p='{"spec":{"size":100}}'kubectl get widgets -n defaultkubectl delete widget test-widget -n defaultkubectl apply -f - <<EOF
apiVersion: things.myorg.io/v1alpha1
kind: Gadget
metadata:
name: test-gadget
namespace: default
spec:
type: "sensor"
version: "v1.0"
enabled: true
priority: 10
EOFkubectl get gadget test-gadget -n default -o yamlkubectl patch gadget test-gadget -n default --type='merge' -p='{"spec":{"priority":20}}'kubectl get gadgets -n defaultkubectl delete gadget test-gadget -n default-
APIService not Available: Check pod status and logs
kubectl get pods -n my-apiserver-system kubectl logs -f -n my-apiserver-system -l app=mytest-apiserver
-
Permission Errors: Ensure RBAC is properly configured
kubectl get clusterrole mytest-apiserver-auth-reader kubectl get clusterrolebinding mytest-apiserver-auth-reader
-
List Operation Fails: Fixed in latest version with proper interface implementation
# Run all tests
./test.sh
# Run only unit tests
./test.sh unit
# Run only integration tests
./test.sh integration
# Run tests with coverage report
./test.sh coverage# Test individual packages
go test -v ./pkg/apis/widgets/
go test -v ./pkg/apis/gadgets/
go test -v ./main_test.go
# Run with race detection
go test -race -v ./pkg/...# Run integration tests (requires build tag)
go test -tags=integration -v ./integration_test.goAfter deploying to Kind cluster, test the full workflow:
# Test Widget operations
kubectl apply -f - <<EOF
apiVersion: things.myorg.io/v1alpha1
kind: Widget
metadata:
name: test-widget
namespace: default
spec:
name: "Test Widget"
description: "Integration test widget"
size: 100
EOF
# Test Gadget operations
kubectl apply -f - <<EOF
apiVersion: things.myorg.io/v1alpha1
kind: Gadget
metadata:
name: test-gadget
namespace: default
spec:
type: "sensor"
version: "v1.0"
enabled: true
priority: 5
EOF
# Verify resources
kubectl get widgets,gadgetsThe test suite includes:
- Unit Tests: Storage operations, CRUD functionality, thread safety
- Integration Tests: Resource interactions, concurrent operations, lifecycle testing
- End-to-End Tests: Full Kubernetes API integration via kubectl
The project includes comprehensive GitHub Actions workflows:
- Code Quality: Format checking, linting, and vetting
- Testing: Unit tests, integration tests, race detection
- Coverage: Automated coverage reporting with Codecov
- Build: Multi-platform binary generation
- Security: Basic security scanning
- Multi-Architecture: Builds for linux/amd64 and linux/arm64
- Security: Container vulnerability scanning with Trivy
- Signing: Container signing with Cosign
- SBOM: Software Bill of Materials generation
- Registry: Automated push to Quay.io
- Automated Releases: Triggered by git tags (v*)
- Multi-Platform Binaries: Linux, macOS, Windows (amd64/arm64)
- Deployment Artifacts: Ready-to-use Kubernetes manifests
- Checksums: SHA256 verification files
- Container Images: Tagged and signed release images
- Vulnerability Scanning: Go modules, filesystem, containers
- Code Analysis: CodeQL, Gosec, Semgrep security analysis
- Dependency Checking: Automated dependency vulnerability detection
- Secrets Detection: Gitleaks and TruffleHog scanning
- Kubernetes Security: Kubesec and Polaris policy validation
- Daily Scans: Scheduled security monitoring
- Dependabot: Automated dependency updates
- Auto-deployment: Image tag updates in manifests
- Quality Gates: All tests must pass before merge
- Security Gates: Security scans block vulnerable releases
- Widget and Gadget resources with their own specifications
- Implements
runtime.Objectinterface with DeepCopy methods - Includes TypeMeta and ObjectMeta for Kubernetes integration
- Thread-safe storage with mutex protection for both resources
- Implements Create, Read, Update, Delete, and List operations
- Provides automatic metadata management (UID, timestamps, etc.)
- Implements multiple REST interfaces (Creater, Lister, Getter, etc.)
- Bridges between HTTP API and storage layer
- Provides namespace scoping and singular name support
- Configures generic API server with custom options
- Registers Widget API group and resources
- Handles authentication delegation and TLS
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β kubectl/curl βββββΆβ MyTest API βββββΆβ In-Memory β
β HTTP Client β β Server β β Storage β
βββββββββββββββββββ β (REST handlers) β β (Widget & β
β Widget/Gadget β β Gadget) β
ββββββββββββββββββββ βββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Kubernetes β
β API Aggregation β
ββββββββββββββββββββ
For production use, consider:
- Persistent Storage: Replace in-memory storage with etcd or database
- Authentication: Add proper authentication and authorization
- Validation: Implement comprehensive validation logic
- Monitoring: Add metrics and health checks
- High Availability: Deploy multiple replicas
- TLS: Proper certificate management
- RBAC: Define appropriate role-based access controls
k8s.io/apimachinery: Kubernetes API machinery and runtime typesk8s.io/apiserver: Kubernetes API server framework and utilities- Direct implementation without external frameworks for learning purposes
- β Multiple custom resources (Widget and Gadget) with spec and status
- β In-memory storage with thread safety for both resources
- β Full CRUD operations (Create, Read, Update, Delete, List)
- β Kubernetes API server integration
- β Authentication delegation
- β RBAC integration
- β Namespace scoping
- β API discovery and OpenAPI schema
- β Docker containerization
- β Kubernetes deployment manifests
- β Automated deployment scripts
- β Modular package structure
- β Kind cluster setup for easy testing
- β Automatic CA injection for TLS certificates
- β Comprehensive test suite with coverage reporting
- β Makefile for development automation
- β Complete CI/CD pipeline with GitHub Actions
- β Security scanning and vulnerability detection
- β Automated releases with multi-platform binaries
./deploy/deploy.sh uninstallkind delete cluster --name kind.
βββ main.go # API server main entry point
βββ main_test.go # Main package unit tests
βββ integration_test.go # Integration tests
βββ test.sh # Test runner script
βββ Makefile # Build and development automation
βββ go.mod # Go module definition
βββ Dockerfile # Container build file
βββ README.md # This file
βββ .github/ # GitHub configuration
β βββ workflows/ # GitHub Actions workflows
β β βββ ci.yml # Continuous Integration
β β βββ docker.yml # Docker build and push
β β βββ release.yml # Release automation
β β βββ security.yml # Security scanning
β βββ dependabot.yml # Dependency updates
β βββ markdown-link-check.json # Link checking config
βββ pkg/ # Go packages
β βββ apis/ # API resource definitions
β β βββ widgets/ # Widget resource implementation
β β β βββ widget.go # Widget types and storage
β β β βββ widget_test.go # Widget unit tests
β β βββ gadgets/ # Gadget resource implementation
β β βββ gadget.go # Gadget types and storage
β β βββ gadget_test.go # Gadget unit tests
β βββ common/ # Shared constants and utilities
βββ deploy/ # Deployment manifests
βββ deploy.sh # Automated deployment script
βββ README.md # Deployment documentation
βββ base/ # Core Kubernetes manifests
β βββ deploy.yaml # RBAC, Deployment, Service
β βββ apiservice.yaml # API registration
βββ certificates/ # TLS certificate setup
β βββ ca.yaml # Certificate Authority
β βββ issuer.yaml # cert-manager Issuer
β βββ cert.yaml # API server certificate
βββ kind/ # Kind cluster setup
βββ cluster-config.yaml # Kind cluster configuration
βββ setup.sh # Automated Kind setup