Skip to content

feat(api): add IPAddressClass.spec.loadBalancerClass - #18

Merged
Timofei Larkin (lllamnyp) merged 1 commit into
mainfrom
feat/ipaddressclass-loadbalancerclass
Nov 26, 2026
Merged

Timofei Larkin (lllamnyp) merged 1 commit into
mainfrom
feat/ipaddressclass-loadbalancerclass

Conversation

@lllamnyp

Copy link
Copy Markdown
Member

The ip-address-claims proposal (cozystack/community#35) gives a class the loadBalancerClass that a consuming Service must carry, so the Service reaches the load-balancer implementation that owns the class's pool. IPAddressClassSpec has no such field. A consumer that reads spec.loadBalancerClass from the class, such as the EndpointAttachment controller in cozystack/cozystack#4324, always reads empty, and every Service it renders goes to the cluster's default implementation.

Changes

  • API. IPAddressClass.spec.loadBalancerClass, an optional string with a minimum length of 1. It is the spec.loadBalancerClass a Service consuming a claim of this class must carry. Unset means the cluster's default implementation, and such a Service carries none. The core never sets it on a Service. The CRD and deepcopy are regenerated with make manifests generate.
  • Immutability. The field cannot be changed, set on a class created without it, or removed. A spec-level CEL transition rule enforces this:
    has(self.loadBalancerClass) == has(oldSelf.loadBalancerClass) && (!has(self.loadBalancerClass) || self.loadBalancerClass == oldSelf.loadBalancerClass).
    A field-level self == oldSelf, as on provisioner, runs only when both the old and the new object have the field, so it would let the field be added or removed.
  • Auto-claim. A Service is now claimed only when its spec.loadBalancerClass equals its class's, with both unset counting as equal. A mismatch with the marked auto-claim class is skipped silently. A mismatch with a class the Service names through local.sdn.cozystack.io/ip-address-class is a LoadBalancerClassMismatch warning event.
  • Docs. The class field is in docs/design.md §2. §7 obligation 5 says a driver does not associate a Service whose loadBalancerClass differs from its claim's class's. §10 and the README describe the new auto-claim rule. The schema table in docs/operating.md lists the field under the unreleased version.

Why immutable

Kubernetes makes a Service's own spec.loadBalancerClass immutable once it is set. Suppose a class's value changed after Services were rendered for it. Those Services would keep the old value and stay with the old implementation, which no longer owns the pool, and nothing could move them short of deleting and recreating them. Unsetting the field has the same effect, and so does setting it on a class that has served claims. Recreating the class to change the field makes that cost visible. Reviewers who prefer a mutable field, for example to let a consumer recreate its Services after a change, can drop the rule. Nothing in the core depends on it.

Behaviour to know

  • Services with a loadBalancerClass. Auto-claim used to skip every Service that set spec.loadBalancerClass. A Service that sets one now gets a claim when its class names the same value.
  • Missing named class. When the class named by a Service's ip-address-class annotation does not exist, the core used to create a Pending claim for it. It now records a ClassNotFound warning on the Service and claims nothing, because the class's loadBalancerClass cannot be checked. The Service is claimed once the class is created: class events already requeue qualifying Services.
  • Existing objects. Existing classes leave the field unset, so they behave as before for Services without a loadBalancerClass. Existing auto-claims are untouched, because a Service that already names a claim never qualifies again. As with every API change, the CRDs must be reapplied (make install-crds), since Helm never upgrades crds/.

Tests

  • test/crd (new, envtest): runs the CRD's validation against a real kube-apiserver. Creating with the field unset or set is admitted; creating with an empty string is refused. Setting it on a class without it, changing it, and removing it are refused. Rewriting the same value, and changing another field with the value kept or still unset, are admitted. With the rule disabled, the three refusals fail.
  • internal/controller: auto-claim across both-unset, equal, class-only, Service-only and different values, each with the marked class and with a class named by the Service. The cases also check that only the named class records the mismatch warning. A further test covers the missing named class.

make test and golangci-lint run pass.

A class names the spec.loadBalancerClass that a Service consuming one of
its claims must carry, so the Service reaches the load-balancer
implementation that owns the class's pool. Unset means the cluster's
default implementation. The field is immutable, and it cannot be set or
unset after creation either: a Service's own loadBalancerClass cannot
change once set, so a new value would strand every Service already
rendered for the class. A spec-level CEL transition rule enforces this,
because a field-level self == oldSelf does not run when the field is
added or removed.

Auto-claim now claims a Service only when its loadBalancerClass equals
its class's, with both unset counting as equal, so a class served by a
non-default implementation can be the auto-claim class. It used to skip
every Service that set a loadBalancerClass. A mismatch with a class the
Service names through the annotation, or a named class that does not
exist, is reported as a warning event.

Assisted-by: LLM
Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c3b8197c-6bb8-444b-9958-741a3f3fd5ce
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM. The field has the shape and meaning the EndpointAttachment controller in cozystack/cozystack#4324 already reads (a string at spec.loadBalancerClass, unset means the default implementation), and classes without it keep their current behaviour.

)

// 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.

}
}

func TestAutoClaimWaitsForClassNamedByService(t *testing.T) {

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) This pins that nothing is claimed while the named class is missing, but not the other half of the documented behaviour: the Service is claimed once the class is created. Creating otherClassName after the first reconcile, checking that servicesForClass returns the Service, and reconciling again would pin it.

@lllamnyp
Timofei Larkin (lllamnyp) merged commit eabedc4 into main Nov 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants