Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ All kinds live in the `local.sdn.cozystack.io/v1alpha1` API group.

| storage | addresses | scope | role |
|---|---|---|---|
| `StorageClass` | `IPAddressClass` | cluster | which pool, which driver (`spec.provisioner`), which reclaim policy |
| `StorageClass` | `IPAddressClass` | cluster | which pool, which driver (`spec.provisioner`), which reclaim policy, which load-balancer implementation (`spec.loadBalancerClass`) |
| `PersistentVolume` | `IPAddress` | cluster | the address itself, with a `claimRef`, a reclaim policy, and a `fromClass`/`providerRef` source union |
| `PersistentVolumeClaim` | `IPAddressClaim` | namespaced | "give me one" — the whole tenant-facing API |

Expand Down Expand Up @@ -76,6 +76,9 @@ core controller as follows:
reconciles live Service assignments against the ledger and sets the
`Conflict` phase when an address is held by a Service its binding does not
authorize, and `Lost` when the backing allocation disappears.
A Service consuming a claim carries its class's `spec.loadBalancerClass`
(none when the class leaves it unset), so it reaches the load-balancer
implementation that owns the class's pool. The field is immutable.

## Auto-claim for LoadBalancer Services

Expand All @@ -96,9 +99,12 @@ nothing:
owner. To keep the address, remove the claim's owner reference; it is an
ordinary claim from then on.
- Skipped: Services that already name a claim, set
`local.sdn.cozystack.io/auto-claim: "false"`, set
`spec.loadBalancerClass` or `spec.loadBalancerIP`, carry a MetalLB pool,
pin or sharing annotation, or already have an address.
`local.sdn.cozystack.io/auto-claim: "false"`, set `spec.loadBalancerIP`,
carry a MetalLB pool, pin or sharing annotation, or already have an
address, and Services whose `spec.loadBalancerClass` differs from the
class's (both unset counts as a match). A Service that names its class
with the annotation gets a warning event for a mismatch or a missing
class.

Apart from auto-claim, the core controller never touches Services. It
never parses class parameters and never puts a packet on a wire.
Expand Down
13 changes: 13 additions & 0 deletions api/v1alpha1/ipaddressclass_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
)

// IPAddressClassSpec defines the desired state of IPAddressClass.
// +kubebuilder:validation:XValidation:rule="has(self.loadBalancerClass) == has(oldSelf.loadBalancerClass) && (!has(self.loadBalancerClass) || self.loadBalancerClass == oldSelf.loadBalancerClass)",message="loadBalancerClass is immutable",fieldPath=".loadBalancerClass"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(non-blocking) Because the field cannot be set after creation, a class created before this release can never take a non-empty value. For a driver that fronts a non-default instance (metallb-iad with a non-empty --lb-class), obligation 5 in docs/design.md then has no in-place fix: the class has to be recreated, and it is held until its addresses are gone. One line about this under "Upgrading the CRDs" in docs/operating.md would save an operator the surprise.

type IPAddressClassSpec struct {
// provisioner names the per-class driver that fulfils claims of this
// class, exactly as StorageClass.provisioner names a CSI driver. The
Expand All @@ -61,6 +62,18 @@ type IPAddressClassSpec struct {
// +optional
ReleaseDelay *metav1.Duration `json:"releaseDelay,omitempty"`

// loadBalancerClass is the spec.loadBalancerClass a Service consuming a
// claim of this class must carry, so that the Service reaches the
// load-balancer implementation that owns this class's pool. Unset means
// the cluster's default implementation: such a Service carries no
// loadBalancerClass. The core controller never sets it on a Service.
// Immutable, including setting or unsetting it after creation: a
// Service's own loadBalancerClass cannot change once set, so a new value
// would strand every Service already rendered for the class.
// +optional
// +kubebuilder:validation:MinLength=1
LoadBalancerClass *string `json:"loadBalancerClass,omitempty"`

// parameters is an opaque, driver-specific configuration blob (for
// example the address ranges to carve from). The core controller never
// interprets it.
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha1/well_known.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,7 @@ const (
ReasonAutoClaimed = "AutoClaimed"
ReasonAutoClaimNameTaken = "AutoClaimNameTaken"
ReasonMultipleAutoClaimClasses = "MultipleAutoClaimClasses"
// ReasonLoadBalancerClassMismatch is recorded when a Service names a
// class whose loadBalancerClass differs from its own.
ReasonLoadBalancerClassMismatch = "LoadBalancerClassMismatch"
)
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ spec:
spec:
description: IPAddressClassSpec defines the desired state of IPAddressClass.
properties:
loadBalancerClass:
description: |-
loadBalancerClass is the spec.loadBalancerClass a Service consuming a
claim of this class must carry, so that the Service reaches the
load-balancer implementation that owns this class's pool. Unset means
the cluster's default implementation: such a Service carries no
loadBalancerClass. The core controller never sets it on a Service.
Immutable, including setting or unsetting it after creation: a
Service's own loadBalancerClass cannot change once set, so a new value
would strand every Service already rendered for the class.
minLength: 1
type: string
parameters:
description: |-
parameters is an opaque, driver-specific configuration blob (for
Expand Down Expand Up @@ -91,6 +103,11 @@ spec:
required:
- provisioner
type: object
x-kubernetes-validations:
- fieldPath: .loadBalancerClass
message: loadBalancerClass is immutable
rule: has(self.loadBalancerClass) == has(oldSelf.loadBalancerClass)
&& (!has(self.loadBalancerClass) || self.loadBalancerClass == oldSelf.loadBalancerClass)
type: object
served: true
storage: true
Expand Down
27 changes: 22 additions & 5 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ StorageClass names a CSI driver.
provisioned `IPAddress`, where it takes effect. What each policy
actually reclaims — the API object, the ledger entry, backend state —
is spelled out in §6.
- `spec.loadBalancerClass` (immutable, including setting or unsetting it)
— the `spec.loadBalancerClass` a Service consuming a claim of this class
must carry, so the Service reaches the load-balancer implementation that
owns the class's pool. Unset means the cluster's default implementation,
and the Service carries none. Immutable because a Service's own
`loadBalancerClass` cannot change once set: a new value would strand
every Service already rendered for the class.
- `spec.parameters` — an opaque object (unknown fields preserved), never
read by the core. Shape is defined by the driver.
- The annotation `ipaddressclass.local.sdn.cozystack.io/is-default-class:
Expand Down Expand Up @@ -411,8 +418,10 @@ integration. Its obligations:
Service's *own* namespace — cross-namespace sharing is not a thing),
translate it into the backend's pin mechanism, maintain
`status.associatedTo`, and enforce that one claim serves one workload
at a time. Disassociation must leave the address `Bound` — reserved,
inert.
at a time. A Service whose `spec.loadBalancerClass` differs from its
claim's class's `spec.loadBalancerClass` reaches another implementation
and is not associated. Disassociation must leave the address `Bound` —
reserved, inert.
6. **Detect, and clear, conflicts.** If the backend hands the reserved
address to a workload the binding does not authorize, set phase
`Conflict` (and `Lost` if the backing reservation disappears). The
Expand Down Expand Up @@ -476,8 +485,7 @@ A Service qualifies when it is a live `type: LoadBalancer` Service that:
- carries no `local.sdn.cozystack.io/ip-address-claim` annotation,
- does not carry `local.sdn.cozystack.io/auto-claim: "false"` (drivers
stamp this on Services they create for their own bookkeeping),
- sets neither `spec.loadBalancerClass` (another implementation's) nor
`spec.loadBalancerIP` (asks for a specific address),
- does not set `spec.loadBalancerIP` (asks for a specific address),
- carries none of the backend's pool, pin or sharing annotations: for
MetalLB, `loadBalancerIPs`, `address-pool` and `allow-shared-ip` under
`metallb.io/` or `metallb.universe.tf/`. Such a Service has chosen its
Expand All @@ -490,7 +498,16 @@ The class is the one named by the Service's
`local.sdn.cozystack.io/ip-address-class` annotation, else the single class
marked `is-auto-claim-class`. With no marked class the Service is left
alone; with several, a warning event is recorded on it and nothing is
claimed.
claimed. A named class that does not exist is reported the same way, and
the Service is claimed once the class is created.

The Service's `spec.loadBalancerClass` must equal the class's, both unset
counting as equal; otherwise the Service reaches another load-balancer
implementation than the one owning the class's pool, and nothing is
claimed. The core cannot fix the Service, whose `loadBalancerClass` is
immutable once set. A mismatch with the marked class is silent, since that
class simply does not serve the Service; a mismatch with a class the
Service named is a warning event.

The core then creates an `IPAddressClaim` named after the Service, in the
Service's namespace, with that `className`, the family derived from the
Expand Down
2 changes: 1 addition & 1 deletion docs/operating.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ still set it, so every release states its schema changes:
| release | schema change |
|---|---|
| v0.1.0 | initial `IPAddress`, `IPAddressClaim`, `IPAddressClass` |
| unreleased | `IPAddressClass.spec.releaseDelay` added; `IPAddress` gains a core-set `Bound` condition |
| unreleased | `IPAddressClass.spec.releaseDelay` added; `IPAddress` gains a core-set `Bound` condition; immutable `IPAddressClass.spec.loadBalancerClass` added |
94 changes: 72 additions & 22 deletions internal/controller/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"slices"
"sort"
"strconv"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -66,23 +67,31 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, nil
}

className := svc.Annotations[localv1alpha1.ServiceClassAnnotation]
if className == "" {
var err error
className, err = r.autoClaimClassName(ctx, svc)
if err != nil || className == "" {
return ctrl.Result{}, err
class, err := r.serviceClass(ctx, svc)
if err != nil || class == nil {
return ctrl.Result{}, err
}
if !sameLoadBalancerClass(svc.Spec.LoadBalancerClass, class.Spec.LoadBalancerClass) {
// The Service reaches another load-balancer implementation than
// the one owning the class's pool. Left alone when the class is the
// marked one; reported when the Service named it.
if svc.Annotations[localv1alpha1.ServiceClassAnnotation] != "" {
r.Recorder.Eventf(svc, "Warning", localv1alpha1.ReasonLoadBalancerClassMismatch,
"Service loadBalancerClass %s does not match IPAddressClass %q loadBalancerClass %s",
describeLoadBalancerClass(svc.Spec.LoadBalancerClass), class.Name,
describeLoadBalancerClass(class.Spec.LoadBalancerClass))
}
return ctrl.Result{}, nil
}

claim := &localv1alpha1.IPAddressClaim{}
err := r.Get(ctx, types.NamespacedName{Namespace: svc.Namespace, Name: svc.Name}, claim)
err = r.Get(ctx, types.NamespacedName{Namespace: svc.Namespace, Name: svc.Name}, claim)
switch {
case apierrors.IsNotFound(err):
claim = &localv1alpha1.IPAddressClaim{
ObjectMeta: metav1.ObjectMeta{Namespace: svc.Namespace, Name: svc.Name},
Spec: localv1alpha1.IPAddressClaimSpec{
ClassName: className,
ClassName: class.Name,
Family: familyForService(svc),
},
}
Expand Down Expand Up @@ -132,17 +141,16 @@ var backendAddressInputs = []string{
}

// wantsAutoClaim reports whether a Service qualifies for auto-claim: a live
// LoadBalancer Service that names no claim, has not opted out, is not left
// to another load-balancer implementation, carries no backend pool or pin
// input, and has no address yet. The last rule keeps a Service addressed by
// LoadBalancer Service that names no claim, has not opted out, carries no
// backend pool or pin input, and has no address yet. Whether its
// loadBalancerClass matches the class's is checked once the class is known. The last rule keeps a Service addressed by
// other means — before auto-claim existed, or from an auto-assigning pool —
// from being moved to a new address.
func wantsAutoClaim(svc *corev1.Service) bool {
return svc.Spec.Type == corev1.ServiceTypeLoadBalancer &&
svc.DeletionTimestamp.IsZero() &&
svc.Annotations[localv1alpha1.ServiceClaimAnnotation] == "" &&
svc.Annotations[localv1alpha1.ServiceAutoClaimAnnotation] != "false" &&
svc.Spec.LoadBalancerClass == nil &&
svc.Spec.LoadBalancerIP == "" &&
!slices.ContainsFunc(backendAddressInputs, func(key string) bool {
_, ok := svc.Annotations[key]
Expand All @@ -151,33 +159,75 @@ func wantsAutoClaim(svc *corev1.Service) bool {
len(svc.Status.LoadBalancer.Ingress) == 0
}

// autoClaimClassName finds the single IPAddressClass marked for auto-claim.
// serviceClass returns the class a Service's auto-claim draws from: the one
// its class annotation names, else the single class marked for auto-claim.
// Nil means nothing is claimed for now. A named class that does not exist
// is recorded on the Service; creating it requeues the Service.
func (r *ServiceReconciler) serviceClass(ctx context.Context, svc *corev1.Service) (*localv1alpha1.IPAddressClass, error) {
name := svc.Annotations[localv1alpha1.ServiceClassAnnotation]
if name == "" {
return r.autoClaimClass(ctx, svc)
}
class := &localv1alpha1.IPAddressClass{}
err := r.Get(ctx, types.NamespacedName{Name: name}, class)
if apierrors.IsNotFound(err) {
r.Recorder.Eventf(svc, "Warning", localv1alpha1.ReasonClassNotFound,
"IPAddressClass %q not found", name)
return nil, nil
}
if err != nil {
return nil, err
}
return class, nil
}

// sameLoadBalancerClass reports whether two loadBalancerClass values are
// equal, both unset counting as equal.
func sameLoadBalancerClass(a, b *string) bool {
if a == nil || b == nil {
return a == b
}
return *a == *b
}

func describeLoadBalancerClass(v *string) string {
if v == nil {
return "(unset)"
}
return strconv.Quote(*v)
}

// autoClaimClass finds the single IPAddressClass marked for auto-claim.
// None means auto-claim is off and the Service is left alone; more than one
// is recorded on the Service and nothing is claimed.
func (r *ServiceReconciler) autoClaimClassName(ctx context.Context, svc *corev1.Service) (string, error) {
func (r *ServiceReconciler) autoClaimClass(ctx context.Context, svc *corev1.Service) (*localv1alpha1.IPAddressClass, error) {
classes := &localv1alpha1.IPAddressClassList{}
if err := r.List(ctx, classes); err != nil {
return "", err
return nil, err
}
// A deleted class is held only until its addresses are gone; drawing
// new claims into it would keep it from draining.
classes.Items = slices.DeleteFunc(classes.Items, classDeleting)
var marked []string
var marked []localv1alpha1.IPAddressClass
for _, c := range classes.Items {
if c.Annotations[localv1alpha1.IsAutoClaimClassAnnotation] == localv1alpha1.AnnotationTrue {
marked = append(marked, c.Name)
marked = append(marked, c)
}
}
switch len(marked) {
case 0:
return "", nil
return nil, nil
case 1:
return marked[0], nil
return &marked[0], nil
default:
sort.Strings(marked)
names := make([]string, 0, len(marked))
for _, c := range marked {
names = append(names, c.Name)
}
sort.Strings(names)
r.Recorder.Eventf(svc, "Warning", localv1alpha1.ReasonMultipleAutoClaimClasses,
"multiple IPAddressClasses marked for auto-claim: %v", marked)
return "", nil
"multiple IPAddressClasses marked for auto-claim: %v", names)
return nil, nil
}
}

Expand Down
Loading