Point HA peer URLs at per-pod ClusterIP Services
## Why
kea-dhcp4 crash-loops at HA hook load: kea 2.6's HA hook parses each peer url host as an IP literal and never resolves DNS, so the StatefulSet headless hostnames are rejected ("Failed to convert string to address ..."). Verified in-cluster that only an IP works (short name, FQDN both fail; `kea-dhcp4 -t` does not exercise this, which is why the v0.1.3 wait did not catch it). Pod IPs cannot be baked into the config because they change on restart and would roll-loop the StatefulSet via the config hash.
## How
- create one ClusterIP Service per HA peer, selecting the pod by its statefulset.kubernetes.io/pod-name label, with publishNotReadyAddresses so peers are routable during bootstrap
- render each HA peer url as its peer Service ClusterIP (a stable IP literal, safe in the config hash); reconcile Services before the ConfigMap and requeue until the ClusterIPs are allocated
This commit is contained in:
@@ -43,12 +43,15 @@ func (r *KeaClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
|
||||
return ctrl.Result{}, client.IgnoreNotFound(err)
|
||||
}
|
||||
|
||||
if err := r.reconcileConfigMap(ctx, &cluster); err != nil {
|
||||
return r.fail(ctx, &cluster, "ConfigError", err)
|
||||
}
|
||||
// Services first: the per-pod ClusterIP Services must exist (and have their
|
||||
// ClusterIPs allocated) before the ConfigMap is rendered, because the HA
|
||||
// peer URLs baked into kea-dhcp4.conf are those stable ClusterIPs.
|
||||
if err := r.reconcileServices(ctx, &cluster); err != nil {
|
||||
return r.fail(ctx, &cluster, "ServiceError", err)
|
||||
}
|
||||
if err := r.reconcileConfigMap(ctx, &cluster); err != nil {
|
||||
return r.fail(ctx, &cluster, "ConfigError", err)
|
||||
}
|
||||
sts, err := r.reconcileStatefulSet(ctx, &cluster)
|
||||
if err != nil {
|
||||
return r.fail(ctx, &cluster, "WorkloadError", err)
|
||||
@@ -118,16 +121,24 @@ func (r *KeaClusterReconciler) buildInput(ctx context.Context, c *v1alpha1.KeaCl
|
||||
}
|
||||
}
|
||||
|
||||
peers, err := r.peers(ctx, c)
|
||||
if err != nil {
|
||||
return kea.RenderInput{}, err
|
||||
}
|
||||
return kea.RenderInput{
|
||||
Cluster: *c,
|
||||
Subnets: subnets,
|
||||
ClientClasses: classes,
|
||||
Peers: r.peers(c),
|
||||
Peers: peers,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// peers returns stable HA peer identities (DNS only, no pod IPs).
|
||||
func (r *KeaClusterReconciler) peers(c *v1alpha1.KeaCluster) []kea.Peer {
|
||||
// peers builds the HA peer list. Each URL points at the peer's per-pod ClusterIP
|
||||
// Service address (a stable IP literal): kea 2.6's HA hook parses the peer URL
|
||||
// host as an IP and never resolves DNS, so hostnames are rejected. Using the
|
||||
// stable ClusterIP (not the pod IP) also keeps the config hash stable across
|
||||
// pod restarts, so the StatefulSet does not roll-loop.
|
||||
func (r *KeaClusterReconciler) peers(ctx context.Context, c *v1alpha1.KeaCluster) ([]kea.Peer, error) {
|
||||
replicas := int32(1)
|
||||
if c.Spec.Replicas != nil {
|
||||
replicas = *c.Spec.Replicas
|
||||
@@ -147,13 +158,22 @@ func (r *KeaClusterReconciler) peers(c *v1alpha1.KeaCluster) []kea.Peer {
|
||||
case i == 1:
|
||||
role = "secondary"
|
||||
}
|
||||
svcName := peerServiceName(c.Name, int(i))
|
||||
var svc corev1.Service
|
||||
if err := r.Get(ctx, types.NamespacedName{Namespace: c.Namespace, Name: svcName}, &svc); err != nil {
|
||||
return nil, fmt.Errorf("peer service %s: %w", svcName, err)
|
||||
}
|
||||
ip := svc.Spec.ClusterIP
|
||||
if ip == "" || ip == corev1.ClusterIPNone {
|
||||
return nil, fmt.Errorf("peer service %s has no ClusterIP allocated yet", svcName)
|
||||
}
|
||||
peers = append(peers, kea.Peer{
|
||||
Name: fmt.Sprintf("server%d", i),
|
||||
URL: peerDNS(c.Name, c.Namespace, int(i)),
|
||||
URL: peerURL(ip),
|
||||
Role: role,
|
||||
})
|
||||
}
|
||||
return peers
|
||||
return peers, nil
|
||||
}
|
||||
|
||||
func (r *KeaClusterReconciler) reconcileConfigMap(ctx context.Context, c *v1alpha1.KeaCluster) error {
|
||||
@@ -184,7 +204,7 @@ func (r *KeaClusterReconciler) reconcileConfigMap(ctx context.Context, c *v1alph
|
||||
}
|
||||
|
||||
func (r *KeaClusterReconciler) reconcileServices(ctx context.Context, c *v1alpha1.KeaCluster) error {
|
||||
// Headless service for stable per-pod DNS (HA peer URLs, ctrl-agent).
|
||||
// Headless service for stable per-pod DNS (ctrl-agent discovery).
|
||||
headless := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: headlessName(c.Name), Namespace: c.Namespace}}
|
||||
if _, err := ctrl.CreateOrUpdate(ctx, r.Client, headless, func() error {
|
||||
headless.Labels = commonLabels(c.Name)
|
||||
@@ -199,6 +219,31 @@ func (r *KeaClusterReconciler) reconcileServices(ctx context.Context, c *v1alpha
|
||||
return err
|
||||
}
|
||||
|
||||
// Per-pod ClusterIP Services: one stable IP per HA peer. Kea's HA hook needs
|
||||
// an IP literal for each peer URL, and a ClusterIP survives pod restarts, so
|
||||
// it is safe to bake into the (roll-triggering) config hash. Not-ready
|
||||
// addresses are published so peers are routable during HA bootstrap.
|
||||
replicas := int32(1)
|
||||
if c.Spec.Replicas != nil {
|
||||
replicas = *c.Spec.Replicas
|
||||
}
|
||||
for i := int32(0); i < replicas; i++ {
|
||||
ord := int(i)
|
||||
peerSvc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: peerServiceName(c.Name, ord), Namespace: c.Namespace}}
|
||||
if _, err := ctrl.CreateOrUpdate(ctx, r.Client, peerSvc, func() error {
|
||||
peerSvc.Labels = commonLabels(c.Name)
|
||||
peerSvc.Spec.Type = corev1.ServiceTypeClusterIP
|
||||
peerSvc.Spec.PublishNotReadyAddresses = true
|
||||
peerSvc.Spec.Selector = map[string]string{statefulSetPodNameLabel: podName(c.Name, ord)}
|
||||
peerSvc.Spec.Ports = []corev1.ServicePort{
|
||||
{Name: "ctrl", Port: kea.CtrlAgentPort, TargetPort: intstrFromInt(kea.CtrlAgentPort), Protocol: corev1.ProtocolTCP},
|
||||
}
|
||||
return ctrl.SetControllerReference(c, peerSvc, r.Scheme)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Anycast DHCP service (LoadBalancer via PureLB by default).
|
||||
svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: serviceName(c.Name), Namespace: c.Namespace}}
|
||||
_, err := ctrl.CreateOrUpdate(ctx, r.Client, svc, func() error {
|
||||
|
||||
Reference in New Issue
Block a user