Skip to content

Commit 0991b54

Browse files
Refactor server architecture with multi-backend support and LibP2P in… (#154)
Refactor server architecture with multi-backend support, LibP2P integration, and resource reconciliation Major architectural changes: - Backend abstraction layer supporting HTTP, gRPC, CoAP, and LibP2P transports - Client refactoring with automatic backend fallback and selection - Server handler registry pattern for better modularity - LibP2P P2P networking with relay, DHT, and NAT traversal - Kubernetes-style resource reconciliation with drift detection - Subscription improvements with defensive nil checking Resource reconciliation system: - Resource and ResourceDefinition types for declarative infrastructure - Automatic drift detection and correction mechanism - JSON Schema-based resource validation - Namespaced resource management with metadata (labels, annotations) - Resource status tracking with generation counters - Reconciliation actions: create, update, delete, noop - Field-level change tracking for spec, status, and metadata - Handler specification for executor-based reconciliation Backend improvements: - Unified backend interface for HTTP (Gin), gRPC, CoAP, and LibP2P - Multi-backend client with automatic failover - DHT-based peer discovery for LibP2P - Circuit relay v2 for NAT traversal - Server manager for coordinating multiple backend servers Client enhancements: - Modular client architecture with per-domain clients - Automatic backend selection and fallback - LibP2P client with DHT caching - Support for HTTP, gRPC, CoAP, and LibP2P transports Server refactoring: - Handler registry pattern for better organization - Moved handlers to domain-specific packages - Resource handler for managing ResourceDefinitions and Resources - Improved controller architecture - Better test isolation and mocking Database layer: - ResourceDatabase interface with PostgreSQL implementation - Resource and ResourceDefinition CRUD operations - Namespace and kind-based queries - JSON/JSONB column support for flexible schemas - Database migration support for resource tables LibP2P/P2P features: - P2P relay server with CLI integration (colonies p2p relay) - Identity generation (colonies p2p generate) - DHT peer discovery with caching - AutoNAT and hole punching support - Bootstrap peer management Resource management features: - Declarative resource specifications with desired state - Custom Resource Definition (CRD) support - Schema validation with required fields, types, and enums - Nested object and array validation - Helper methods for GetAPIVersion, Diff, ValidateAgainstRD - Resource arrays and JSON conversion utilities Bug fixes: - Fixed nil pointer panics in subscription cleanup (Gin and LibP2P) - Fixed DHT cache to only store active peers - Fixed database test isolation with unique table prefixes - Fixed TestSearchLogs timing boundary issue with PostgreSQL NOW() - Removed APIVersion field from Resource struct (moved to ResourceDefinition) - Updated hardcoded peer IDs to current server identity Testing improvements: - Fixed controller test database conflicts - Added DatabaseMock resource interface methods - Improved test utilities and mocking - Better test isolation with unique database prefixes - Added comprehensive backend tests - Added resource validation test coverage - Added reconciliation and diff testing Core domain improvements: - Resource type with Kind, Metadata, Spec, Status fields - ResourceDefinition with Group, Version, Names, Scope, Handler - ValidationSchema for JSON Schema validation - Reconciliation type for tracking old/new state with diffs - ResourceDiff with field-level change tracking - FunctionSpec integration for reconciliation workflows CLI enhancements: - New 'colonies p2p' subcommand for relay and identity management - Improved server startup with backend selection - Better configuration management Documentation: - Added Container Building Guide with multi-platform instructions - Docker buildx setup and QEMU configuration - Multi-architecture build examples (amd64, arm64) - CI/CD integration examples for GitHub Actions and GitLab - Troubleshooting guide for buildx issues
1 parent 9e057f6 commit 0991b54

3,565 files changed

Lines changed: 394149 additions & 182776 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
run: make
4848

4949
- name: Test
50-
run: TZ=Europe/Stockholm make github_test
50+
run: TZ=Europe/Stockholm make test
5151
env:
5252
AWS_S3_ENDPOINT: localhost:9000
5353
AWS_S3_ACCESSKEY: minioadmin

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ coverage.txt
88
*.sw*
99
pkg/database/postgresql/*.sw*
1010
pkg/database/*.sw*
11-
pkg/server/*.sw*
11+
pkg/service/*.sw*
1212
pkg/core/*.sw*
1313
pkg/security/*.sw*
1414
pkg/logging/*.sw*

CLAUDE.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ make github_test # Run tests without grc (for CI)
1919

2020
### Development Environment
2121
```bash
22-
source devenv # Set up development environment
23-
colonies dev # Start development server
24-
make startdb # Start PostgreSQL TimescaleDB container
22+
docker-compose up -d # Start Colonies server with dependencies (TimescaleDB, MinIO)
23+
docker-compose down # Stop all services
24+
docker-compose logs -f # View logs
2525
```
2626

2727
### Coverage
@@ -34,13 +34,13 @@ ColonyOS is a distributed meta-orchestrator framework that creates compute conti
3434

3535
### Core Components
3636
- **Colony**: A distributed runtime environment consisting of networked Executors
37-
- **Process**: A computational workload defined by a FunctionSpec, with states (WAITING, RUNNING, SUCCESS, FAILED)
38-
- **Executor**: Distributed microservices that execute processes, can run anywhere on the Internet
37+
- **Process**: A computational workload defined by a FunctionSpec, with states (WAITING, RUNNING, SUCCESS, FAILED)
38+
- **Executor**: Distributed workers that pull and execute processes, can run anywhere on the Internet
3939
- **FunctionSpec**: Specification defining what computation to run and execution conditions
4040

4141
### Key Packages
4242
- `pkg/core/`: Core domain models (Process, Executor, Colony, FunctionSpec)
43-
- `pkg/server/`: HTTP RPC server implementation
43+
- `pkg/service/`: HTTP RPC service implementation
4444
- `pkg/client/`: Go SDK for Colonies API
4545
- `pkg/database/postgresql/`: PostgreSQL database layer with TimescaleDB support
4646
- `pkg/security/`: Zero-trust security protocol implementation

Dockerfile

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
1-
FROM alpine
1+
# Build stage
2+
FROM golang:1.24-alpine AS builder
3+
4+
WORKDIR /build
5+
6+
# Copy go mod files
7+
COPY go.mod go.sum ./
8+
RUN go mod download
9+
10+
# Copy source code
11+
COPY . .
12+
13+
# Get build variables
14+
ARG VERSION
15+
ARG BUILDTIME
16+
ENV VERSION=${VERSION}
17+
ENV BUILDTIME=${BUILDTIME}
18+
19+
# Build the binary for the target architecture
20+
RUN CGO_ENABLED=0 go build \
21+
-ldflags="-s -w -X 'main.BuildVersion=${VERSION}' -X 'main.BuildTime=${BUILDTIME}'" \
22+
-o /colonies \
23+
./cmd/main.go
24+
25+
# Runtime stage
26+
FROM alpine:latest
227

328
WORKDIR /
4-
COPY ./bin/colonies /bin
29+
30+
# Copy the binary from builder
31+
COPY --from=builder /colonies /bin/colonies
532

633
CMD ["colonies", "server", "start"]

Makefile

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ all: build
22
.PHONY: all build
33

44
BUILD_IMAGE ?= colonyos/colonies
5-
PUSH_IMAGE ?= colonyos/colonies:v1.8.19
5+
PUSH_IMAGE ?= colonyos/colonies:v1.9.0
66

77
VERSION := $(shell git rev-parse --short HEAD)
88
BUILDTIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
@@ -17,10 +17,19 @@ build:
1717
@GOOS=js GOARCH=wasm go build -o ./lib/libcryptolib.wasm internal/cryptolib.wasm/cryptolib.go
1818

1919
container:
20-
docker build -t $(BUILD_IMAGE) .
20+
@echo "Building container for local architecture..."
21+
docker build --build-arg VERSION=$(VERSION) --build-arg BUILDTIME=$(BUILDTIME) -t $(BUILD_IMAGE) .
22+
23+
container-multiplatform:
24+
@echo "Building multiplatform container (amd64, arm64)..."
25+
docker buildx build --platform linux/amd64,linux/arm64 --build-arg VERSION=$(VERSION) --build-arg BUILDTIME=$(BUILDTIME) -t $(BUILD_IMAGE) .
26+
27+
container-multiplatform-push:
28+
@echo "Building and pushing multiplatform container (amd64, arm64)..."
29+
docker buildx build --platform linux/amd64,linux/arm64 --build-arg VERSION=$(VERSION) --build-arg BUILDTIME=$(BUILDTIME) -t $(BUILD_IMAGE) -t $(PUSH_IMAGE) --push .
2130

2231
push:
23-
docker tag $(BUILD_IMAGE) $(PUSH_IMAGE)
32+
docker tag $(BUILD_IMAGE) $(PUSH_IMAGE)
2433
docker push $(BUILD_IMAGE)
2534
docker push $(PUSH_IMAGE)
2635

@@ -32,23 +41,6 @@ build_cryptolib_ubuntu_2020:
3241
cd buildtools; ./build_cryptolib_ubuntu.sh
3342

3443
test:
35-
@cd tests/reliability; grc go test -v --race
36-
@cd internal/crypto; grc go test -v --race
37-
@cd pkg/core; grc go test -v --race
38-
@cd pkg/database/postgresql; grc go test -v --race
39-
@cd pkg/rpc; grc go test -v --race
40-
@cd pkg/security; grc go test -v --race
41-
@cd pkg/security/crypto; grc go test -v --race
42-
@cd pkg/security/validator; grc go test -v --race
43-
@cd pkg/server; grc go test -v --race
44-
@cd pkg/scheduler; grc go test -v --race
45-
@cd pkg/parsers; grc go test -v --race
46-
@cd pkg/utils; grc go test -v --race
47-
@cd pkg/cluster; grc go test -v --race
48-
@cd pkg/cron; grc go test -v --race
49-
@cd pkg/fs; go test -v --race
50-
51-
github_test:
5244
@cd tests/reliability; go test -v --race
5345
@cd internal/crypto; go test -v --race
5446
@cd pkg/core; go test -v --race
@@ -57,7 +49,30 @@ github_test:
5749
@cd pkg/security; go test -v --race
5850
@cd pkg/security/crypto; go test -v --race
5951
@cd pkg/security/validator; go test -v --race
52+
@cd pkg/backends/gin; go test -v --race
53+
@cd pkg/backends/grpc; go test -v --race
54+
@cd pkg/backends/libp2p; go test -v --race
55+
@cd pkg/client/gin; go test -v --race
56+
@cd pkg/client/grpc; go test -v --race
57+
@cd pkg/client/libp2p; go test -v --race
6058
@cd pkg/server; go test -v --race
59+
@cd pkg/server/controllers; go test -v --race
60+
@cd pkg/server/handlers/attribute; go test -v --race
61+
@cd pkg/server/handlers/colony; go test -v --race
62+
@cd pkg/server/handlers/cron; go test -v --race
63+
@cd pkg/server/handlers/executor; go test -v --race
64+
@cd pkg/server/handlers/file; go test -v --race
65+
@cd pkg/server/handlers/function; go test -v --race
66+
@cd pkg/server/handlers/generator; go test -v --race
67+
@cd pkg/server/handlers/log; go test -v --race
68+
@cd pkg/server/handlers/process; go test -v --race
69+
@cd pkg/server/handlers/processgraph; go test -v --race
70+
@cd pkg/server/handlers/security; go test -v --race
71+
@cd pkg/server/handlers/server; go test -v --race
72+
@cd pkg/server/handlers/snapshot; go test -v --race
73+
@cd pkg/server/handlers/user; go test -v --race
74+
@cd pkg/server/handlers/realtime; go test -v --race
75+
@cd pkg/server/utils; go test -v --race
6176
@cd pkg/scheduler; go test -v --race
6277
@cd pkg/parsers; go test -v --race
6378
@cd pkg/utils; go test -v --race
@@ -72,3 +87,10 @@ install:
7287

7388
startdb:
7489
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=rFcLGNkgsNtksg6Pgtn9CumL4xXBQ7 --restart unless-stopped timescale/timescaledb:latest-pg16
90+
91+
nukedb:
92+
@echo "Nuking TimescaleDB containers and volumes..."
93+
@docker stop $$(docker ps -aq --filter ancestor=timescale/timescaledb:latest-pg16) 2>/dev/null || true
94+
@docker rm $$(docker ps -aq --filter ancestor=timescale/timescaledb:latest-pg16) 2>/dev/null || true
95+
@docker volume rm $$(docker volume ls -q --filter dangling=true) 2>/dev/null || true
96+
@echo "TimescaleDB containers and volumes destroyed"

0 commit comments

Comments
 (0)