Files
cozystack/hack
Aleksei Sviridkin ed22d251c8 feat(api): add EndpointAttachment to attach addresses to app endpoints (#4324)
## What this PR does

Implements `EndpointAttachment` (`local.sdn.cozystack.io/v1alpha1`,
namespaced) from the design proposal in
[cozystack/community#45](https://github.com/cozystack/community/pull/45):
a tenant-created resource that attaches one external address to one
tenant-facing Service of one managed application. It is the Network
load-balancing path (a dedicated address per attachment), complementary
to SNI exposure through the tenant Gateway, and it implements the
`loadBalancer` union member only. Nothing in it routes by hostname,
touches TLS, or reads chart values.

Two commits. The first updates address-controller and metallb-iad to
v0.3.0: the class's provisioner now reports, in
`IPAddressClass.status.loadBalancerClass` with an `Accepted` condition
for the class's generation, the `loadBalancerClass` a Service consuming
its claims must carry, and the MetalLB driver writes that status. The
second adds `EndpointAttachment`, which sets that class on every Service
and FloatingIP it renders and renders nothing new until the class is
accepted (`ClassNotAccepted`); a Service or FloatingIP already published
is kept if the class later loses acceptance. The FloatingIP retarget
check needs cozyplane v0.1.5 or later, which stamps
`metadata.generation` and `status.observedGeneration`.
[#5389](https://github.com/cozystack/cozystack/pull/5389) moves the
cozyplane pin to v0.1.5, and this PR merges after it; on an older
cozyplane both fields read 0 and the check passes as before.

**API** (`api/localsdn/v1alpha1/endpointattachment_types.go`, CRD
generated into the cozystack-controller chart). The kind lives in
`local.sdn.cozystack.io`, the CRD-served half of the sdn family the
address substrate and cozyplane already share, so stock `ResourceQuota`
counts still work. `spec` is required at the resource root, since
removing it would otherwise skip every rule nested inside it.
`spec.applicationRef` (group/kind/name, group defaulting to
`apps.cozystack.io`), `spec.endpoint.serviceName`, and
`spec.loadBalancer` with `className` | `claimName` (exclusive),
`family`, `ports`, `method` (`WholeIP` | `PortList`), `allowICMP`. CEL
freezes the address-identity fields (`applicationRef`, `className`,
`claimName`, `family`) and the member selection, requires exactly one
union member (written as a list so a `gateway` member can be added
without restructuring), and holds the static shape rules (`PortList`
needs `ports`, `allowICMP` is `PortList`-only). Everything cross-object
or dynamic is a condition. `status.phase` is a projection of the
`Resolved` and `Provisioned` conditions.

**Controller** (`internal/controller/endpointattachment`), one reconcile
in four steps:

1. Resolve. The application's HelmRelease is found by the three lineage
labels; the attachment gets a non-controller owner reference to it by
UID and the lineage labels stamped on itself. The endpoint Service must
carry the lineage triple matching `applicationRef` and
`internal.cozystack.io/tenantresource: "true"`; lineage alone is refused
(`EndpointLineageMismatch` / `EndpointNotTenantFacing`), as is a Service
rendered by another attachment, and a LoadBalancer Service a tenant
Kubernetes cluster's cloud-controller-manager creates for a Service
inside that cluster: it carries the cluster's lineage and the marker,
and is told apart by the `cluster.x-k8s.io/tenant-service-name` label
that controller writes. A same-name recreated application (different
HelmRelease UID) is reported as `ApplicationIncarnationChanged`, the
Service is withdrawn, and the owner reference is never rewritten.
2. Claim. `claimName` references an existing `IPAddressClaim` and writes
nothing to it; otherwise a claim is minted with `generateName` under the
attachment's controller reference. The substrate kinds are read as
unstructured, so the controller carries no module or CRD dependency on
it and reports `ClaimPending` where it is not installed.
`loadBalancerClass` is read from the claim's resolved `IPAddressClass`
status before the Service is created, only once the class's `Accepted`
condition is True for its current generation, and left empty when the
class reports the cluster default. The Service's `ipFamilyPolicy` and
`ipFamilies` follow the claim's family (`SingleStack` for IPv4 or IPv6,
`RequireDualStack` for Dual) and are reconciled on existing Services; a
primary family that differs, which the apiserver never changes, and a
create the cluster refuses are both reported as `RenderFailed`. An
address whose `IPAddress.status.associatedTo` names another live Service
gives `ClaimInUse` and withdraws this attachment's own Service, so two
attachments that both rendered before either association was recorded
leave one Service asking for the claim. A `family` conflicting with a
referenced claim gives `FamilyConflict`.
3. Render. One `type: LoadBalancer` Service via `generateName` (a prefix
derived from the attachment name and bounded to what the apiserver
accepts for a Service, which also bounds the minted claim's), identified
by controller owner reference only (a Service with the ownership label
and no reference is ignored, never adopted). It mirrors the endpoint's
selector and ports (or the `ports` subset; synthesized ports when the
endpoint has only the sentinel port), carries the
`local.sdn.cozystack.io/ip-address-claim` annotation,
`externalTrafficPolicy: Local`, and `allocateLoadBalancerNodePorts:
false` unless the platform says otherwise. It copies no labels or
annotations from the endpoint. An endpoint with no selector
(hand-managed endpoints) is refused with `NothingToPublish`, and the VM
placeholder is recognised by its shape (headless, only port 65535)
rather than by the port alone. When the cache shows no owned Service,
the controller rechecks through the API reader before creating one, and
collapses duplicates to the oldest. Re-mirroring is event-driven through
watches on Services, EndpointSlices, HelmReleases and, where served at
startup, the claim kind.
4. Report. `status.addresses` mirrors the claim's bound addresses (a
list), `status.serviceName` and `status.claimName` name the objects, and
`Provisioned=True` asserts address and Service wiring only; its message
says so, since reachability additionally depends on the CNI's policy
objects.

**VM datapath modes.** `method` renders the cozy-proxy Service contract
exactly (the `service.kubernetes.io/service-proxy-name: cozy-proxy`
label, `networking.cozystack.io/wholeIP`,
`networking.cozystack.io/allowICMP`, the sentinel port for whole-IP
without ports) and adds nothing beside it. Rendering is gated on a
platform declaration, not CNI detection: the controller flag
`--cozy-proxy-contract-implemented` is threaded from the system bundle,
which sets it exactly when it emits the cozy-proxy Package; without it a
method-bearing attachment renders no Service and reports
`DatapathContractUnavailable`, because a load balancer would still
attract the address while every proxy skipped the Service. Exclusivity
is keyed on the backend pod and counts every delegated Service in either
mode (attachments, chart Services rendered with `external: true`,
hand-made ones): a delegation no attachment renders (a chart Service
with `external: true`, or a hand-made one) always wins, since nothing
would ever withdraw it; between attachments the oldest wins, ties by
name. The loser reports `BackendAlreadyDelegated` and renders nothing
rather than fighting for the slot. cozy-proxy v0.4.0 programs IPv4 only
(one IPv4 nftables table, every address through `To4()`), so a method
attachment off a VPC with an `IPv6` or `Dual` claim renders no Service
and reports `FamilyUnsupportedByDatapath`; the cozyplane path still
serves IPv6 through the FloatingIP. `WholeIP` on an endpoint with more
than one ready backend keeps its Service and claim but selects nothing
(`MultipleBackends`), so a rolling update or live migration passes
through held-but-dark and recovers without churning the address.

**VMs in a VPC.** On a cozyplane VPC the VM's only interface is in the
VPC, so the launcher pod address a cozy-proxy Service maps to never
reaches it. When a backend pod carries the VM chart's
`sdn.cozystack.io/vpc` annotation and the cluster serves cozyplane's
`FloatingIP`, a `WholeIP` attachment owns a FloatingIP in its namespace
(`vpcRef` from the annotation, `target` from the
`sdn.cozystack.io/vpc-ip` address cozyplane's CNI stamps,
`addressClaimName` the attachment's claim) instead of a Service, and
`Provisioned` mirrors the FloatingIP's readiness. FloatingIP is read as
unstructured and only where served, so there is no dependency on the
cozyplane PR. Refusals there: `PortList` (`MethodUnsupportedInVPC`), a
`Dual` claim, since a FloatingIP wears one address
(`DualStackUnsupportedInVPC`), a VPC in another namespace
(`VPCNotInNamespace`), several distinct VPC addresses or a mix of VPC
and non-VPC pods (`MultipleBackends`, claim held), no VPC address
stamped yet (`WaitingForVPCAddress`), and a VPC backend where FloatingIP
is not served (`DatapathContractUnavailable`, no Service). A live
migration's two launcher pods share the VPC address and stay one
binding; a stopped VM keeps its FloatingIP. Since only a backend pod
says whether a VM is in a VPC, a `method` attachment that has published
nothing yet waits for one (`WaitingForBackend`), so an attachment
created before its VM starts never pins the claim to a cozy-proxy
Service first. On the kube-ovn VPC the VM keeps its pod-network
interface and joins VPC subnets through extra interfaces, so the
cozy-proxy path is unchanged there.

**Detach semantics.** An endpoint Service that disappears withdraws the
rendered Service, keeps the claim (minted or referenced), and reports
`Detached`; the same claim is reused when the endpoint returns. Deleting
an attachment collects its Service and its minted claim; a referenced
claim is untouched.

**Wiring.** The controller is registered in `cmd/cozystack-controller`
behind two flags (`--cozy-proxy-contract-implemented`,
`--endpoint-attachment-allocate-node-ports`), the chart exposes them as
`cozystackController.endpointAttachments.*`, the platform system bundle
derives both (cozy-proxy emitted, Hetzner RobotLB enabled), the
controller ClusterRole gains Service writes, the `endpointattachments`
kind, `ipaddressclaims` create/delete and `sdn.cozystack.io`
`floatingips` writes, and the tenant ClusterRoles get
`endpointattachments` beside `securitygroups` (full in
`cozy:tenant:base`, read in `cozy:tenant:view:base`, write in
`cozy:tenant:admin:base`).

**Tests.** `crdcel_test.go` compiles the shipped CRD and pins the create
and update admission matrix. `reconciler_test.go` (fake client) covers
the resolve/deny matrix over both label families, mirroring and port
subsetting, the minted-claim path, the two proof sequences (a
labelled-but-unowned Service is ignored and a same-name recreated
application is not re-acquired; a reserved claim holds across detachment
and moves to a different application), detach/reattach, the
sentinel-port rule, the capability gate, both modes, held-but-dark on
multiple backends, pod-keyed exclusivity, the substrate-absent path and
the family conflict. Admission tests also validate from the resource
root (removing or restoring `spec`). The reconciler tests cover a Dual
claim that is provisioned only once the Service carries both addresses,
and attachment names up to 253 characters, with dots or a leading digit,
on both address-selection paths. `floatingip_test.go` covers the
cozyplane path: the FloatingIP's spec and readiness mirroring, the claim
worn by its own Service not counting as in use, switching from and back
to a cozy-proxy Service, a kube-ovn-shaped backend keeping the Service,
each refusal, live migration, a stopped VM, re-pointing in place, detach
and name bounds. Chart unit tests pin the platform declaration on every
bundle variant, slim opt-in included, and the FloatingIP RBAC rule.
`hack/e2e-chainsaw/endpointattachment` carries the postgres hold/move
sequence, GC, the lineage refusal of an attachment pointing at another
application's Service, and the VM mode Test (re-scoped to a single
delegated Service per VM, since current cozy-proxy's rule installer
keeps one 1:1 slot per pod). It is registered: `hack/select-install.sh`
maps it to `cozystack.address-controller` and `cozystack.metallb-iad`,
with Postgres, VMInstance, VMDisk and cozy-proxy as its runtime
requirements, and `hack/select-e2e.sh` maps both substrate packages back
to it, since no other suite installs them. The e2e install enables both
packages, and `hack/e2e-post-install-prep.sh` creates the default
`IPAddressClass` on a dedicated, non-auto-assigning MetalLB pool
whenever metallb-iad is installed.
`hack/e2e-chainsaw/endpointattachment-vpc` covers the cozyplane
FloatingIP path (whole-IP published through a FloatingIP, `PortList` and
`Dual` refused) and stays parked as `chainsaw-test.yaml.disabled`,
because no e2e lane installs the cozyplane networking variant.

**Upgrade.** Additive: a new CRD, a new reconciler, new RBAC rules, no
migration. The system bundle now always passes the cozystack-controller
component an `endpointAttachments` values block, so the first upgrade
rolls the controller once with the new flags; the flags change nothing
until `EndpointAttachment` objects exist, and no existing object is
modified.

**Docs.** The user guide is on the website, in
[cozystack/website#825](https://github.com/cozystack/website/pull/825):
the resource, which Services can be attached, the lifecycle, the
conditions' exact meaning, the VM modes and the cozyplane path, and the
TLS posture (attaching a database does not turn its TLS on). It is a
cross-package guide, so under the documentation placement rules it lives
there rather than in `docs/`.

**Not in this PR, deliberately.** The `gateway` union member (a reserved
name; the CRD ships without it and nothing references it). A certSAN
write-back for the managed Kubernetes API. Discovery-engine address
write-back. The address-preserving migration from `external: true`. An
authoritative exclusivity gate inside cozy-proxy (the attachment-side
refusal is advisory); that wants an issue in cozystack/cozy-proxy.

### Screenshots

Not a UI change.

### Downstream repositories

`ApplicationDefinition.spec.services` now also decides which Services
tenants can attach an address to, which the trigger map routes to the
website's ApplicationDefinition page:
[cozystack/website#825](https://github.com/cozystack/website/pull/825)
documents that, and carries the user guide. Nothing else matched: the
cozy-proxy runtime contract is consumed with the same three literal
strings and nothing is renamed; the new CRD travels as opaque JSON in
the terraform provider; no platform `values.yaml` key, variant, asset or
node prerequisite changed. An issue in cozy-proxy for an authoritative
one-delegation-per-pod gate is worth a human decision rather than a
speculative filing.

- [ ] No downstream repository is affected by this change
- [x] [cozystack/website](https://github.com/cozystack/website) -
follow-up: https://github.com/cozystack/website/pull/825
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
feat(api): add EndpointAttachment (local.sdn.cozystack.io/v1alpha1), a tenant resource that attaches an external address, minted from an IPAddressClass or bound from a reserved IPAddressClaim, to one tenant-facing Service of a managed application by rendering an additive LoadBalancer Service that mirrors it; VM whole-IP and port-list modes render the cozy-proxy contract where the platform deploys it.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Added endpoint attachments to publish eligible application and VM
endpoints through dedicated load balancer addresses, with whole-IP or
selected-port exposure and IPv4, IPv6, or dual-stack support.
* Attachments can use a reserved address or request one through a
configured address class. Status shows endpoint resolution,
provisioning, assigned addresses, and attachment state.
* Tenant access now includes managing and inspecting endpoint
attachments. Whole-IP VM exposure is limited to one delegated backend,
and VPC support depends on the available network datapath.

* **Documentation**
* Added guidance on eligible endpoints, address lifecycle, status, and
exposure considerations.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-27 15:56:24 +03:00
..