# Task Services

Task services are installable infrastructure components that provide specific functionality.
Provisioning includes 30+ task services for databases, orchestration, monitoring, and more.

## Categories

### Kubernetes & Container Orchestration

**kubernetes** - Complete Kubernetes cluster deployment
- Control plane setup
- Worker node pools
- CNI configuration (Calico, Cilium, Flannel)
- Addon management (metrics-server, ingress-nginx, cert-manager)

**containerd** - Container runtime configuration
- Systemd integration
- Storage driver configuration
- Runtime class support

**docker** - Docker engine installation
- Docker Compose integration
- Registry configuration

### Databases

**postgresql** - PostgreSQL database server
- Replication setup
- Backup automation
- Performance tuning

**mysql** - MySQL/MariaDB deployment
- Cluster configuration
- Backup strategies

**mongodb** - MongoDB database
- Replica sets
- Sharding configuration

**redis** - Redis in-memory store
- Persistence configuration
- Cluster mode

### Storage

**rook-ceph** - Cloud-native storage orchestrator
- Block storage (RBD)
- Object storage (S3-compatible)
- Shared filesystem (CephFS)

**minio** - S3-compatible object storage
- Distributed mode
- Versioning and lifecycle policies

### Monitoring & Observability

**prometheus** - Metrics collection and alerting
- Service discovery
- Alerting rules
- Long-term storage

**grafana** - Metrics visualization
- Dashboard provisioning
- Data source configuration

**loki** - Log aggregation system
- Log collection
- Query language

### Networking

**cilium** - eBPF-based networking and security
- Network policies
- Load balancing
- Service mesh capabilities

**calico** - Network policy engine
- BGP networking
- IP-in-IP tunneling

**nginx** - Web server and reverse proxy
- Load balancing
- TLS termination

### Security

**vault** - Secrets management (HashiCorp Vault)
- Secret storage
- Dynamic secrets
- Encryption as a service

**cert-manager** - TLS certificate automation
- Let's Encrypt integration
- Certificate renewal

## Task Service Definition

Task services are defined in `provisioning/extensions/taskservs/`:

```plaintext
taskservs/
└── kubernetes/
    ├── service.ncl           # Service schema
    ├── install.nu            # Installation script
    ├── configure.nu          # Configuration script
    ├── health-check.nu       # Health validation
    └── README.md
```

## Using Task Services

### Installation

```nickel
{
  task_services = [
    {
      name = "kubernetes",
      version = "v1.28.0",
      config = {
        control_plane = {nodes = 3, plan = 'medium},
        workers = [{name = "pool-1", nodes = 3, plan = 'large}],
        networking = {cni = 'cilium},
      }
    },
    {
      name = "prometheus",
      version = "latest",
      config = {retention = "30d", storage_size_gb = 100}
    }
  ]
}
```

### CLI Commands

```bash
# List available task services
provisioning taskserv list

# Show task service details
provisioning taskserv show kubernetes

# Install task service
provisioning taskserv install kubernetes

# Check task service health
provisioning taskserv health kubernetes

# Uninstall task service
provisioning taskserv uninstall kubernetes
```

## Custom Task Services

Create custom task services:

```plaintext
provisioning/extensions/taskservs/my-service/
├── service.ncl           # Service definition
├── install.nu            # Installation logic
├── configure.nu          # Configuration logic
├── health-check.nu       # Health checks
└── README.md
```

**service.ncl** schema:

```nickel
{
  name = "my-service",
  version = "1.0.0",
  description = "Custom service description",
  dependencies = ["kubernetes"],  # Optional dependencies
  config_schema = {
    port | Number | default = 8080,
    replicas | Number | default = 3,
  }
}
```

**install.nu** implementation:

```nushell
export def "taskserv install" [config: record] {
  # Installation logic
  print $"Installing ($config.name)..."

  # Deploy resources
  kubectl apply -f deployment.yaml

  {status: "installed"}
}
```

## Task Service Lifecycle

1. **Validation** - Check dependencies and configuration
2. **Installation** - Execute install script
3. **Configuration** - Apply service configuration
4. **Health Check** - Verify service is running
5. **Ready** - Service available for use

## Dependencies

Task services can declare dependencies:

```nickel
{
  name = "grafana",
  dependencies = ["prometheus"],  # Installed first
}
```

Provisioning automatically resolves dependency order.

## Health Checks

Each task service provides health validation:

```nushell
export def "taskserv health" [] {
  let pods = (kubectl get pods -l app=my-service -o json | from json)

  if ($pods.items | all | { | p $p.status.phase == "Running"}) {
    {status: "healthy"}
  } else {
    {status: "unhealthy", reason: "pods not running"}
  }
}
```

## Best Practices

1. **Define schemas** - Use Nickel schemas for task service configuration
2. **Declare dependencies** - Explicit dependency declaration
3. **Idempotent installs** - Installation should be repeatable
4. **Health checks** - Implement comprehensive health validation
5. **Version pinning** - Specify exact versions for reproducibility
6. **Document configuration** - Provide clear configuration examples

## References

- [Clusters](clusters.md) - Cluster orchestration
- [Batch Workflows](batch-workflows.md) - Multi-service deployments
- [Providers](providers.md) - Infrastructure providers
