fyl080801/k8s-adapter

k8s-adapter

โ˜… 0Forks 0TypeScriptGitHub โ†—Compare

README

K8s Adapter - Monorepo

This is a Keystone 6 headless CMS with integrated Kubernetes Informer functionality, organized as a monorepo for better separation of concerns.

๐Ÿ—๏ธ Monorepo Structure

This project is organized as an npm workspace with two packages:

packages/
โ”œโ”€โ”€ schema/          # Keystone list definitions (data models)
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ schema.ts    # User, Post, Tag, SyncState lists
โ”‚       โ””โ”€โ”€ index.ts     # Export point
โ””โ”€โ”€ core/            # Main application (K8s integration + API)
    โ”œโ”€โ”€ src/
    โ”‚   โ”œโ”€โ”€ api/         # RESTful API routes
    โ”‚   โ”œโ”€โ”€ k8s/         # Kubernetes Informer integration
    โ”‚   โ”œโ”€โ”€ lib/         # Utilities (MongoDB, etc.)
    โ”‚   โ”œโ”€โ”€ middleware/  # Express middleware
    โ”‚   โ””โ”€โ”€ models/      # Mongoose models for K8s resources
    โ”œโ”€โ”€ keystone.ts      # Entry point
    โ””โ”€โ”€ auth.ts          # Authentication config

Package Overview

@k8s-adapter/schema

  • Contains all Keystone CMS list definitions
  • Defines data structure (User, Post, Tag, SyncState)
  • Pure TypeScript with no runtime dependencies on core logic
  • Can be imported independently for schema validation

@k8s-adapter/core

  • Main application with Keystone server
  • Kubernetes Informer integration
  • RESTful API for K8s resources
  • MongoDB models and connections
  • Imports schema from @k8s-adapter/schema

๐Ÿš€ Quick Start

# Install dependencies (installs for all packages)
npm install

# Start development server
npm run dev

# Build for production
npm run build

# Start production server
npm start

Visit the Admin UI at http://localhost:3000

๐Ÿ“š Documentation

All detailed documentation is organized in the docs/ folder:

Essential Reading

Package Documentation

Logging Documentation

๐ŸŽฏ Project Overview

This application serves two purposes:

  1. Keystone CMS: Provides Admin UI for content management (Users, Posts, Tags)
  2. K8s Resource Sync: Real-time synchronization of Kubernetes resources to MongoDB with RESTful API access

Architecture

  • Dual Database Design:

    • SQLite (Prisma) for Keystone's internal data
    • MongoDB for Kubernetes resource storage
  • Monorepo Benefits:

    • Clear separation between schema definitions and business logic
    • Schema can be versioned and shared independently
    • Easier to test schema changes in isolation
    • Better code organization and maintainability
  • Real-time Sync:

    • Full sync on startup
    • Kubernetes Informer for real-time watch
    • RESTful API at /api/v1

๐Ÿ“– Quick Start Guide

1. Install Dependencies

npm install

This installs dependencies for all packages in the monorepo.

2. Configure Environment

Create .env file in the root:

MONGODB_URI=mongodb://localhost:27017/k8s-resources

# Optional: Logging configuration
LOG_LEVEL=info                    # debug, info, warn, error
ENABLE_FILE_LOGGING=false         # Enable file logging with daily rotation
LOG_DIR=./logs                    # Directory for log files
ENABLE_LOG_COLORS=true            # Enable colored console output

3. Start Development Server

npm run dev

This starts the core package's development server.

4. Test API Endpoints

# Health check
curl http://localhost:3000/api/v1/health

# List all pods
curl http://localhost:3000/api/v1/pods

# Get deployments by namespace
curl http://localhost:3000/api/v1/deployments?namespace=default

๐Ÿ› ๏ธ Development Commands

npm run dev          # Start Keystone dev server (core package)
npm run build        # Build for production (core package)
npm start            # Start production server (core package)
npm run db:generate  # Regenerate Prisma client
npm run test:api     # Test K8s API endpoints
npm run clean        # Clean all packages

Package-Specific Commands

# Schema package
npm run dev --workspace=packages/schema  # Watch schema changes
npm run build --workspace=packages/schema  # Build schema

# Core package
npm run dev --workspace=packages/core    # Start dev server
npm run build --workspace=packages/core  # Build core

๐Ÿ—๏ธ Working with the Monorepo

Adding New Fields to Schema

  1. Edit packages/schema/src/schema.ts
  2. The schema package will be automatically imported by the core package
  3. No need to restart - Keystone will hot-reload the schema

Modifying Core Logic

  1. Edit files in packages/core/src/
  2. Changes will hot-reload in development mode
  3. Schema is imported from @k8s-adapter/schema

Building Packages

The schema package must be built before it can be imported:

# Build schema package
npm run build --workspace=packages/schema

# Core package uses workspace references
# No build step needed if developing

๐ŸŽฏ Supported K8s Resources

All resources use official TypeScript types from @kubernetes/client-node:

  • โœ… Pod
  • โœ… Deployment
  • โœ… Service
  • โœ… Node
  • โœ… ConfigMap
  • โœ… Secret
  • โœ… DaemonSet
  • โœ… StatefulSet
  • โœ… Ingress
  • โœ… PersistentVolumeClaim
  • โœ… Event

๐Ÿ”Œ Adding New Resources

The project uses a generic, configuration-driven architecture:

  1. Create Mongoose model in packages/core/src/models/
  2. Register in packages/core/src/k8s/types.ts
  3. That's it! Automatically gets:
    • Full sync on startup
    • Real-time Informer watch
    • RESTful API endpoints

See docs/guides/ADD_NEW_RESOURCES.md for detailed instructions.

๐Ÿ“– Additional Documentation

See docs/README.md for complete documentation index.

๐Ÿ™‹ Troubleshooting

See CLAUDE.md for common issues and solutions.

Common Monorepo Issues

Schema changes not reflected:

  • Ensure the schema package is built: npm run build --workspace=packages/schema
  • Restart the dev server

Import errors:

  • Run npm install to ensure workspace links are set up correctly
  • Check that packages/schema/package.json has the correct name

Last Updated: 2026-01-07

Issues