diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..f29cda1 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,207 @@ +# Contributing to Coder Kubernetes Template + +Thank you for your interest in contributing! This guide will help you get started with development and testing. + +## ๐Ÿš€ Development Setup + +### Prerequisites + +1. **Tools Required:** + - [Terraform](https://developer.hashicorp.com/terraform/downloads) (latest version) + - [Git](https://git-scm.com/) + - Access to a Kubernetes cluster + - [kubectl](https://kubernetes.io/docs/tasks/tools/) configured for your cluster + +2. **For Testing:** + - [Coder CLI](https://coder.com/docs/v2/latest/install) + - Access to a Coder instance (for integration testing) + +### Local Development + +1. **Clone the repository:** + ```bash + git clone https://github.com/Frantche/coder-template-kubernetes.git + cd coder-template-kubernetes + ``` + +2. **Validate the template locally:** + ```bash + ./validate.sh + ``` + +3. **Make your changes:** + - Template configuration: Edit `template-kubernetes/main.tf` + - Documentation: Update relevant README files + - CI/CD: Modify `.github/workflows/coder-template-update.yaml` + +## ๐Ÿงช Testing + +### Local Validation + +The repository includes a validation script that checks: +- Terraform formatting (`terraform fmt -check`) +- Terraform configuration validity (`terraform validate`) + +```bash +# Run all local tests +./validate.sh +``` + +### Manual Terraform Testing + +```bash +cd template-kubernetes + +# Initialize Terraform +terraform init + +# Check formatting +terraform fmt -check + +# Validate configuration +terraform validate + +# Plan deployment (requires proper variables) +terraform plan +``` + +### Integration Testing + +For full integration testing, you'll need access to a Coder instance: + +1. **Set up Coder CLI:** + ```bash + coder login + ``` + +2. **Push template for testing:** + ```bash + coder templates push test-kubernetes -d template-kubernetes + ``` + +3. **Create a test workspace:** + ```bash + coder create test-workspace -t test-kubernetes + ``` + +4. **Clean up test resources:** + ```bash + coder delete test-workspace + coder templates delete test-kubernetes + ``` + +## ๐Ÿ”„ GitHub Actions Testing + +The repository includes automated testing via GitHub Actions: + +### Pull Request Testing +- Automatically triggered on PRs to `main` branch +- Creates temporary template and workspace for validation +- Cleans up resources automatically + +### Setting Up Testing Environment + +If you want to test the GitHub Actions workflow: + +1. **Fork the repository** +2. **Add required secrets** to your fork: + - `CODER_SESSION_TOKEN`: Your Coder instance session token +3. **Update workflow configuration** (if needed): + - Edit `access_url` in `.github/workflows/coder-template-update.yaml` + - Modify `namespace` parameter if using different namespace + +## ๐Ÿ“ Contribution Guidelines + +### Code Style + +- **Terraform**: Follow standard Terraform formatting (`terraform fmt`) +- **Documentation**: Use clear, concise language with proper markdown formatting +- **Commit Messages**: Use conventional commit format when possible + +### Pull Request Process + +1. **Create a feature branch:** + ```bash + git checkout -b feature/your-feature-name + ``` + +2. **Make your changes** with appropriate tests + +3. **Run local validation:** + ```bash + ./validate.sh + ``` + +4. **Commit your changes:** + ```bash + git add . + git commit -m "feat: description of your changes" + ``` + +5. **Push and create PR:** + ```bash + git push origin feature/your-feature-name + ``` + +6. **Create a Pull Request** with: + - Clear description of changes + - Any breaking changes noted + - Testing steps performed + +### What We Look For + +- โœ… **Working Code**: Changes should not break existing functionality +- โœ… **Documentation**: Update relevant documentation for any changes +- โœ… **Testing**: Include appropriate tests or validation +- โœ… **Compatibility**: Ensure backward compatibility when possible + +## ๐Ÿ› Reporting Issues + +When reporting issues, please include: + +1. **Template Version**: Commit SHA or version information +2. **Environment**: Kubernetes version, Coder version, etc. +3. **Steps to Reproduce**: Clear steps to reproduce the issue +4. **Expected vs Actual Behavior**: What you expected vs what happened +5. **Logs**: Relevant error messages or logs + +## ๐Ÿ’ก Feature Requests + +Feature requests are welcome! Please: + +1. **Check existing issues** to avoid duplicates +2. **Describe the use case** and why it would be valuable +3. **Provide implementation ideas** if you have them +4. **Consider backward compatibility** implications + +## ๐Ÿ”ง Development Tips + +### Common Development Tasks + +1. **Adding new parameters:** + - Add `coder_parameter` data source in `main.tf` + - Update template README documentation + - Test with different parameter values + +2. **Modifying resource configuration:** + - Update Kubernetes resources in `main.tf` + - Ensure labels and annotations follow Coder conventions + - Test resource creation and cleanup + +3. **Updating dependencies:** + - Renovate automatically manages Terraform provider versions + - For Docker image updates, modify the `image` field and update renovate comment + +### Debugging Tips + +- **Template Issues**: Use `coder templates push --debug` for verbose output +- **Workspace Issues**: Check `coder show ` and Kubernetes pod logs +- **CI/CD Issues**: Review GitHub Actions logs and Coder template logs + +## ๐Ÿ“ž Getting Help + +- **Issues**: Create a GitHub issue for bugs or questions +- **Discussions**: Use GitHub Discussions for general questions +- **Coder Community**: Join the [Coder Discord](https://discord.gg/coder) for real-time help + +Thank you for contributing to make this template better for everyone! ๐ŸŽ‰ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..870c1ab --- /dev/null +++ b/README.md @@ -0,0 +1,224 @@ +# Coder Kubernetes Template + +A [Coder](https://coder.com/) template for creating cloud development environments running on Kubernetes. This template provisions development workspaces as Kubernetes pods with persistent storage, code-server access, and configurable resources. + +## ๐Ÿš€ Quick Start + +1. **Prerequisites:** + - A Kubernetes cluster with appropriate permissions + - [Coder](https://coder.com/docs/v2/latest/install) installed and configured + - kubectl access to your Kubernetes cluster + +2. **Deploy the template:** + ```bash + coder templates push kubernetes -d template-kubernetes + ``` + +3. **Create a workspace:** + ```bash + coder create my-dev-workspace -t kubernetes + ``` + +## ๐Ÿ“ Project Structure + +``` +โ”œโ”€โ”€ .github/ +โ”‚ โ””โ”€โ”€ workflows/ +โ”‚ โ””โ”€โ”€ coder-template-update.yaml # CI/CD pipeline for testing and publishing +โ”œโ”€โ”€ template-kubernetes/ +โ”‚ โ”œโ”€โ”€ README.md # Template-specific documentation +โ”‚ โ””โ”€โ”€ main.tf # Terraform configuration for the template +โ”œโ”€โ”€ renovate.json # Dependency update configuration +โ”œโ”€โ”€ validate.sh # Local validation script +โ”œโ”€โ”€ CONTRIBUTING.md # Contribution guidelines and setup +โ””โ”€โ”€ README.md # This file +``` + +### Key Files Explained + +- **`template-kubernetes/main.tf`**: The core Terraform configuration defining the Coder template, including Kubernetes resources, parameters, and workspace configuration +- **`.github/workflows/coder-template-update.yaml`**: Automated testing and publishing pipeline +- **`validate.sh`**: Local validation script for checking Terraform formatting and configuration +- **`renovate.json`**: Configuration for Renovate bot to automatically update dependencies (Terraform providers, Docker images) +- **`CONTRIBUTING.md`**: Comprehensive guide for contributors including setup, testing, and development workflows + +## ๐Ÿ”„ Dependency Management + +This repository uses [Renovate](https://renovatebot.com/) for automated dependency updates. + +### Renovate Configuration + +The `renovate.json` file configures: +- **Auto-merge**: Automatically merges updates when all checks pass +- **Squash strategy**: Uses squash commits for cleaner history +- **Auto-merge comment**: Adds clear indication of automated merges + +### Managed Dependencies + +Renovate automatically monitors and updates: + +1. **Terraform Providers**: + - Coder provider (`coder/coder`) + - Kubernetes provider (`hashicorp/kubernetes`) + +2. **Docker Images**: + - Development image: `ghcr.io/frantche/coder-full` + - Updates tracked via special comment in `main.tf` + +3. **GitHub Actions**: + - Action versions in workflow files + - Ensures security and feature updates + +### Update Process + +1. Renovate creates PRs for available updates +2. GitHub Actions automatically test the changes +3. If tests pass, changes are auto-merged +4. New template version is published to Coder + +This ensures dependencies stay current while maintaining stability through automated testing. + +## ๐Ÿ› ๏ธ Template Features + +- **Kubernetes Pod Workspaces**: Each workspace runs as a Kubernetes pod +- **Persistent Storage**: Home directory persisted via PersistentVolumeClaim +- **Code-Server**: Web-based VS Code editor accessible through Coder dashboard +- **Configurable Resources**: Adjustable CPU, memory, and disk size +- **Docker Support**: Docker-in-Docker for containerized development +- **Authentication Options**: Support for both in-cluster and kubeconfig authentication + +## ๐Ÿ”ง Configuration Options + +The template supports several parameters that can be configured when creating workspaces: + +- **CPU**: 2, 4, 6, or 8 cores +- **Memory**: 2, 4, 6, or 8 GB RAM +- **Home Disk Size**: Configurable persistent storage (1-99999 GB) +- **Namespace**: Target Kubernetes namespace for workspace deployment + +## ๐Ÿƒโ€โ™‚๏ธ GitHub Actions Workflow + +This repository includes a sophisticated CI/CD pipeline that automatically tests and publishes the Coder template. + +### Workflow Overview + +The workflow is defined in `.github/workflows/coder-template-update.yaml` and consists of two main jobs: + +#### ๐Ÿงช Pull Request Testing (`test` job) + +**Trigger**: Pull requests to `main` branch + +**Steps**: +1. **Environment Setup** + - Checkout repository code + - Install Terraform (latest version) + - Setup Coder CLI with authentication + +2. **Terraform Validation** + ```bash + terraform init + terraform fmt -check -diff + terraform validate + ``` + +3. **Live Template Testing** + - Pushes template with unique suffix: `kubernetes-ci-{commit-sha}` + - Uses commit SHA as version name + - Deployed with `activate=false` to avoid affecting production + +4. **Workspace Deployment Test** + - Creates test workspace: `workspace-ci-{commit-sha}` + - Configures with standard parameters (2 CPU, 2GB RAM, 10GB disk) + - Verifies successful deployment with `coder show` + +5. **Automatic Cleanup** + - Deletes test workspace and template + - Runs even if previous steps fail (`if: always()`) + +#### ๐Ÿš€ Production Publishing (`publish` job) + +**Trigger**: Pushes to `main` branch (after PR merge) + +**Steps**: +1. **Environment Setup** (same as test job) +2. **Template Publishing** + - Pushes template with production name: `kubernetes` + - Uses commit SHA as version identifier + - Sets `activate=true` to make it the default version + - Includes commit message as version description + +### Configuration + +#### Required Secrets +- `CODER_SESSION_TOKEN`: Authentication token for your Coder instance + - Generate via: `coder tokens create --lifetime 87600h` + - Add to repository secrets in GitHub + +#### Environment Variables +- `TEMPLATE_NAME`: Set to "kubernetes" (the production template name) +- Coder instance URL: Currently set to `https://coder.frantchenco.page` + +#### Default Parameters +The workflow deploys test workspaces with these parameters: +- `namespace`: `coder-workspace` +- `cpu`: `2` +- `memory`: `2` +- `home_disk_size`: `10` + +### Workflow Benefits + +- โœ… **Automated Testing**: Every PR is tested with real Coder deployment +- โœ… **Safe Deployment**: Test resources are isolated and automatically cleaned up +- โœ… **Version Control**: Each deployment tagged with commit SHA +- โœ… **Zero Downtime**: New versions published without affecting existing workspaces +- โœ… **Rollback Ready**: Previous versions remain available in Coder + +### Monitoring Workflow + +You can monitor the workflow by: +1. **GitHub Actions Tab**: View live logs and status +2. **Coder Dashboard**: Check for test templates/workspaces during PR testing +3. **Template Versions**: Verify new versions appear after merge + +### Customizing the Workflow + +To adapt this workflow for your environment: + +1. **Update Coder URL**: Change `access_url` in the workflow file +2. **Modify Parameters**: Adjust default workspace parameters as needed +3. **Change Namespace**: Update the `namespace` variable +4. **Add Validation**: Include additional validation steps if required + +## ๐Ÿงช Testing + +### Local Validation +```bash +# Run local Terraform validation +./validate.sh +``` + +### Manual Testing +1. Fork this repository +2. Set up the required secrets in your GitHub repository +3. Create a pull request to trigger the test workflow +4. Review the workflow logs to ensure successful deployment + +## ๐Ÿ“š Documentation + +- [Template README](template-kubernetes/README.md) - Detailed template documentation +- [Coder Documentation](https://coder.com/docs/) - Official Coder documentation +- [Kubernetes Provider](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs) - Terraform Kubernetes provider docs + +## ๐Ÿค Contributing + +See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to this project. + +## ๐Ÿ“„ License + +This project is open source. Please check the repository for license details. + +## ๐Ÿ†˜ Support + +- Create an [issue](https://github.com/Frantche/coder-template-kubernetes/issues) for bug reports or feature requests +- Check [Coder Community](https://github.com/coder/coder/discussions) for general Coder questions +- Review the [troubleshooting guide](template-kubernetes/README.md) in the template documentation \ No newline at end of file diff --git a/template-kubernetes/README.md b/template-kubernetes/README.md index 7aa42ff..fa095e1 100644 --- a/template-kubernetes/README.md +++ b/template-kubernetes/README.md @@ -5,11 +5,45 @@ tags: [cloud, kubernetes] icon: /icon/k8s.png --- -# Getting started +# Kubernetes Development Template -This template creates a pod running the `codercom/enterprise-base:ubuntu` image. +This template creates a pod running a custom development image (`ghcr.io/frantche/coder-full`) with Docker support, code-server, and persistent storage for cloud-native development. -## Authentication +## โœจ Features + +- **Full Development Environment**: Pre-configured with development tools +- **Docker-in-Docker**: Build and run containers within your workspace +- **Code-Server**: Web-based VS Code editor +- **Persistent Storage**: Home directory persisted across workspace sessions +- **Resource Configuration**: Configurable CPU, memory, and disk resources +- **Anti-Affinity**: Workspaces distributed across nodes for better performance + +## ๐Ÿš€ Quick Start + +1. Set the required variables when pushing the template +2. Create a workspace with your preferred resource configuration +3. Access your development environment through code-server or SSH + +## ๐Ÿ”ง Configuration Parameters + +This template supports the following configurable parameters: + +### CPU Allocation +- **Options**: 2, 4, 6, or 8 cores +- **Default**: 2 cores +- **Mutable**: Yes (can be changed after workspace creation) + +### Memory Allocation +- **Options**: 2, 4, 6, or 8 GB +- **Default**: 2 GB +- **Mutable**: Yes (can be changed after workspace creation) + +### Home Disk Size +- **Range**: 1-99999 GB +- **Default**: 10 GB +- **Mutable**: No (set at workspace creation time) + +## ๐Ÿ” Authentication This template can authenticate using in-cluster authentication, or using a kubeconfig local to the Coder host. For additional authentication options, consult the [Kubernetes provider @@ -60,12 +94,14 @@ Then start the Coder host with `serviceAccountName: coder` in the pod spec. You may want to deploy workspaces on a cluster outside of the Coder control plane. Refer to the [Coder docs](https://coder.com/docs/v2/latest/platforms/kubernetes/additional-clusters) to learn how to modify your template to authenticate against external clusters. -## Namespace +## ๐Ÿ“ฆ Namespace -The target namespace in which the pod will be deployed is defined via the `coder_workspace` +The target namespace in which the pod will be deployed is defined via the `namespace` variable. The namespace must exist prior to creating workspaces. -## Persistence +**Important**: Ensure the namespace exists and has appropriate RBAC permissions before deploying workspaces. + +## ๐Ÿ’พ Persistence The `/home/coder` directory in this example is persisted via the attached PersistentVolumeClaim. Any data saved outside of this directory will be wiped when the workspace stops. @@ -91,8 +127,53 @@ resource "coder_agent" "main" { } ``` -## code-server +## ๐Ÿ’ป code-server `code-server` is installed via the `startup_script` argument in the `coder_agent` resource block. The `coder_app` resource is defined to access `code-server` through -the dashboard UI over `localhost:13337`. \ No newline at end of file +the dashboard UI over `localhost:13337`. + +### Features +- **Web-based VS Code**: Full VS Code experience in your browser +- **Extensions Support**: Install VS Code extensions as needed +- **File Explorer**: Access to your entire workspace filesystem +- **Integrated Terminal**: Built-in terminal access + +## ๐Ÿณ Docker Support + +This template includes Docker-in-Docker support, allowing you to: +- Build and run containers within your workspace +- Use Docker Compose for multi-container applications +- Access the Docker daemon directly + +## ๐Ÿ“Š Monitoring + +The workspace includes several built-in monitoring metrics: +- CPU usage (workspace and host) +- Memory usage (workspace and host) +- Disk usage for home directory +- Load average on the host + +## ๐Ÿ” Troubleshooting + +### Common Issues + +**Workspace won't start:** +- Check namespace exists and has proper permissions +- Verify PersistentVolume storage class is available +- Review Kubernetes pod logs for startup errors + +**Code-server not accessible:** +- Wait for the startup script to complete (check agent logs) +- Verify port 13337 is not blocked +- Check the healthcheck status in Coder dashboard + +**Docker not working:** +- Ensure the container runs with privileged mode +- Check if Docker daemon started successfully in workspace logs + +### Getting Help + +- Check workspace logs in Coder dashboard +- Review Kubernetes pod logs: `kubectl logs -n ` +- Verify agent connectivity in Coder dashboard \ No newline at end of file