Files
unkinben d3fb5dcd1a
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/pr/pre-commit Pipeline failed
ci/woodpecker/pr/test Pipeline failed
Scaffold kea-operator: CRDs, controllers, config rendering, REST API, CI
Replace the ISC dhcpd PXE-boot VM with a Kea DHCP Kubernetes operator, modelled
on bind-operator. The operator renders kea-dhcp4 config from CRs and runs an HA
pair of kea-dhcp4 + kea-ctrl-agent servers behind an anycast Service.

- add KeaCluster/KeaSubnet/KeaClientClass/KeaAPI CRDs (group kea.unkin.net)
- render deterministic kea-dhcp4.conf + kea-ctrl-agent.conf into a ConfigMap and
  roll the StatefulSet via a config-hash annotation; best-effort hot-reload via
  the kea-ctrl-agent REST channel
- run HA hot-standby (memfile leases) with stable per-peer DNS identity from a
  StatefulSet; expose an anycast LoadBalancer Service for PureLB
- represent the full legacy dhcpd config: 198.18.13-17.0/24 pools, pool-less
  198.18.25.0/24, and the Legacy/UEFI-64 PXE arch classes (option 93)
- add the KeaAPI-spawned REST service: Terraform-friendly CRUD over subnet and
  client-class CRs (stable IDs, PUT upsert, 404 drift, bearer-token auth)
- add Makefile (patch/minor/major tag targets), distroless operator/api images,
  an AlmaLinux+EPEL kea workload image, and woodpecker CI with k8s resources +
  serviceAccountName on every step
- unit tests for config rendering, controller reconcile/config-hash, and the API

Claude-Session: https://claude.ai/code/session_01JUoARVdmhxKQHyyyp1pxeT
2026-08-02 18:42:46 +10:00

73 lines
2.2 KiB
Go

package controller
import (
"context"
"fmt"
"net"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
v1alpha1 "git.unkin.net/unkin/kea-operator/api/v1alpha1"
)
// KeaSubnetReconciler validates KeaSubnet CRs and maintains their status. The
// actual kea config is rendered by the KeaCluster controller, which watches
// subnets and re-renders on change.
type KeaSubnetReconciler struct {
client.Client
Scheme *runtime.Scheme
}
// +kubebuilder:rbac:groups=kea.unkin.net,resources=keasubnets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=kea.unkin.net,resources=keasubnets/status,verbs=get;update;patch
func (r *KeaSubnetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var subnet v1alpha1.KeaSubnet
if err := r.Get(ctx, req.NamespacedName, &subnet); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if _, _, err := net.ParseCIDR(subnet.Spec.Subnet); err != nil {
subnet.Status.Phase = "Invalid"
setReady(&subnet.Status.Conditions, subnet.Generation, false, "InvalidCIDR", err.Error())
_ = r.Status().Update(ctx, &subnet)
return ctrl.Result{}, nil
}
for _, p := range subnet.Spec.Pools {
if err := validatePool(p); err != nil {
subnet.Status.Phase = "Invalid"
setReady(&subnet.Status.Conditions, subnet.Generation, false, "InvalidPool", err.Error())
_ = r.Status().Update(ctx, &subnet)
return ctrl.Result{}, nil
}
}
subnet.Status.Phase = "Ready"
subnet.Status.AssignedID = subnet.Spec.ID
subnet.Status.ObservedGeneration = subnet.Generation
setReady(&subnet.Status.Conditions, subnet.Generation, true, "Validated", "subnet accepted")
if err := r.Status().Update(ctx, &subnet); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func validatePool(p string) error {
// Accept "start - end" (with or without spaces) or a single address.
fields := splitPool(p)
for _, f := range fields {
if net.ParseIP(f) == nil {
return fmt.Errorf("invalid pool address %q in %q", f, p)
}
}
return nil
}
func (r *KeaSubnetReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.KeaSubnet{}).
Complete(r)
}