Restrict tenant-controlled fields on infra LoadBalancer Services #422
[APPROVALNOTIFIER] This PR is NOT APPROVED
This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign rmohr for approval. For more information see the Code Review Process.
The full list of commands accepted by this bot can be found here.
<details open>
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
</details>
<!-- META={"approvers":["rmohr"]} -->
Thanks for picking this up — the allowlist and the clean-up on update match what we had in mind. A few points inline: three about behaviour and a nit.
The service controller in k8s.io/cloud-provider skips Services that set a class: wantsLoadBalancer (controllers/service/controller.go, v0.37.0 as vendored here) returns false when spec.loadBalancerClass is set, so EnsureLoadBalancer/UpdateLoadBalancer are never called for them. The class can't reach the infra Service, and a tenant following the README to pick a backend with loadBalancerClass gets no infra Service at all.
Unless I'm missing another path, I'd drop the class propagation and that advice; if a backend choice is needed, an operator option that sets the class on every infra Service keeps it with the infra side.
This replaces every annotation on the infra Service, including ones written in the infra cluster — MetalLB sets metallb.io/ip-allocated-from-pool on each Service it assigns, and operators may annotate these Services too. Each tenant-side reconcile would remove them and the infra controller write them back.
Removing only the keys the tenant Service carries that are not allowlisted (or recording the mirrored keys on the infra Service) would still clean Services from older builds without touching the infra side's.
With a hosted control plane the provider's log is on the infra side, so the tenant cluster administrator never sees these warnings — their Service just gets a different address, or no external IPs. A Warning event on the tenant Service (a recorder on the tenant client in Initialize) would tell them why. Same for the loadBalancerIP case below.
Nit: strings.Contains also matches e.g. notkubernetes.io/x. Comparing the key's prefix (== "kubernetes.io" or HasSuffix(".kubernetes.io"), same for k8s.io) is exact.
Addressed in d2a7fd8ba by tracking mirrored keys in an infra-only annotation and reconciling only owned keys. Tests cover preserving MetalLB/operator annotations, tenant changes/removal, allowlist revocation and idempotency. Legacy Services have no provenance: the first reconcile conservatively cleans keys whose values still match the tenant, then records ownership. Historical keys no longer matching require operator audit/manual cleanup; identical independently authored annotations remain ambiguous. These migration limits are documented rather than promising complete automatic cleanup. Added an e2e preservation scenario (compiled locally; live execution pending CI).
Addressed in d2a7fd8ba. Removed tenant class propagation and the direct-call test that bypassed the service controller. README and PR description now explain that tenant loadBalancerClass opts out of the standard controller, rather than selecting the infra backend. Existing infra classes are preserved on reconciliation; an operator-configured infra class is outside this fix.
Addressed in d2a7fd8ba. Cloud.Initialize now creates a recorder with the tenant client builder, using the existing service-controller identity and shutting down the broadcaster on stop. Both creation and reconciliation emit tenant Service Warning events for ignored externalIPs/loadBalancerIP. Tests verify tenant-client event delivery, both reconciliation entrypoints, and no warning for permitted IP requests; the new e2e scenario checks the tenant event too.
Addressed in d2a7fd8ba by removing the blanket namespace ban altogether. That ban also prevented operators from explicitly allowing legitimate service.beta.kubernetes.io provider keys. Only the ownership annotation, kubectl last-applied configuration and the separately validated legacy source-range annotation are excluded. Tests cover notkubernetes.io/x, a provider-specific Kubernetes-prefixed key, and exclusion of reserved metadata even when allowlisted.
The new recorder uses the tenant client under the service-controller identity, but the repository's packaged tenant RBAC (config/rbac/kccm_role.yaml:24-44) grants this controller access to services/nodes and no access to events. With the default manager.yaml deployment (and also when the builder reuses the CCM credentials), every Event write will be forbidden, so the promised LoadBalancerIPIgnored/ExternalIPsIgnored warnings will never reach the tenant Service. Add the required event permissions for the identity actually used here, or wire this to an already-authorized tenant event recorder.
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Tenant event recording lacks the required RBAC permissions, so warning events will be forbidden.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (1)</strong></summary>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
Restricts tenant-controlled fields mirrored to infrastructure LoadBalancer Services while preserving operator-owned state and adding validation, events, documentation, and tests.
Changes:
loadBalancerIP.| File | Summary |
|---|---|
| test/e2e/cloud_provider_kubevirt/load_balancer_test.go | Adds end-to-end reconciliation coverage. |
| README.md | Documents configuration, behavior, and migration guidance. |
| pkg/provider/loadbalancer.go | Implements restricted field reconciliation. |
| pkg/provider/loadbalancer_test.go | Extends LoadBalancer tests. |
| pkg/provider/loadbalancer_fields_test.go | Tests field validation and annotation ownership. |
| pkg/provider/cloud.go | Initializes tenant event recording. |
| pkg/provider/cloud_test.go | Tests configuration defaults. |
| pkg/provider/cloud_events_test.go | Tests tenant warning events. |
</details>
---
💡 <a href="/kubevirt/cloud-provider-kubevirt/new/main?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a code-review agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review?tool=webui#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>
Addressed in b0879bfe6 with a separate tenant-side event RBAC example (config/tenant-rbac/events.yaml), credential-specific documentation, and an e2e authorization check using the deployed CCM tenant kubeconfig. The current core Event recorder uses create and patch, so the supplemental ClusterRole grants those verbs across tenant namespaces. Operators must adjust its binding subject to their actual tenant identity. One distinction: config/rbac is applied alongside the provider in the infrastructure cluster, whereas manager.yaml uses an explicit tenant kubeconfig; adding permissions to the infra Role would not authorize tenant writes. Shared credentials use the tenant kubeconfig identity; per-controller credentials use service-controller. The initialization comment now makes that requirement explicit. Unit tests, lint and e2e compilation pass; live e2e remains for CI.
<!-- ccr-overview-v2 -->
Copilot review overview
🔵 Needs a closer look
Allowlisted annotation changes may leave stale values on infrastructure Services until another reconciliation trigger occurs.
Review effort: Lite
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (1)</strong></summary>
</details>
Older builds copied every tenant annotation onto the infra Service at creation, so on a Service created before the upgrade this record may come from the tenant rather than the provider. If a tenant on a cluster that hasn't been upgraded yet creates a Service with cloud-provider.kubevirt.io/tenant-annotation-keys set, then after the upgrade:
x (not JSON): reconcileAnnotations fails before anything is written, so the copied externalIPs and loadBalancerIP stay on the infra Service and every retry fails the same way.[]: the Service counts as tracked, the value-match cleanup is skipped, and annotations the old build copied (e.g. metallb.universe.tf/loadBalancerIPs) stay.["metallb.io/ip-allocated-from-pool"]: listed infra-side keys are deleted.I checked the three cases with a unit test against e61b0df (an infra Service built the way the old createLoadBalancerService did, then UpdateLoadBalancer). Without the planted key, the same Service is cleaned as expected.
Older builds only ever set provider-built label keys on the infra Service, so a label written together with the record (e.g. cloud-provider.kubevirt.io/annotation-ownership: v1) would separate a provider-written record from a copied one: without the label, ignore the record and take the legacy path. It may also be worth not letting an error in the annotation bookkeeping block the externalIPs/loadBalancerIP clean-up. For the README's upgrade audit: an infra Service from an older build that already carries cloud-provider.kubevirt.io/tenant-annotation-keys got it from the tenant.
Happy to push the test to a branch if it helps.
Thanks — the four points from the first round are addressed in e61b0df. One more about the upgrade path, inline.
Good catch, thanks. Fixed in 923578af0:
cloud-provider.kubevirt.io/annotation-ownership: v1, set on create and on every update that writes the record. Without the label, the record is ignored and the legacy value-match path runs.reconcileAnnotations no longer returns an error. An unreadable record falls back to the legacy path, so it can no longer block the externalIPs/loadBalancerIP cleanup.TestPlantedOwnershipRecordIgnored covers your three values (x, [], ["metallb.io/ip-allocated-from-pool"]) through UpdateLoadBalancer on an infra Service built like the old createLoadBalancerService.tenant-annotation-keys came from the tenant.If your test covers anything beyond that, feel free to push it.
This lookup uses the mirrored Service's namespace (infra.Namespace), but the kubeconfig Secret mounted by the deployed CCM is created in the default namespace (config/secret/kustomization.yaml:4-8 and config/manager/manager.yaml:53-58), while the mirrored Service uses the configured namespace: kvcluster (config/isolated/cloud-config:1-4). The new e2e test therefore fails before it can check authorization; fetch the Secret from the CCM Deployment's namespace (currently default).
<!-- ccr-overview-v2 -->
Copilot review overview
🟡 Changes recommended
Fix the e2e Secret namespace lookup and handle null ownership metadata without orphaning annotations.
Review effort: Lite
Findings: 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture> · 1 <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture>
<details open>
<summary><strong>Open (2)</strong></summary>
</details>
aimnbrs
qinqon
What this PR does
Before this PR:
The provider copied tenant Service annotations,
externalIPs,loadBalancerIPandhealthCheckNodePortinto an infra LoadBalancer Service. Source ranges were not forwarded, and only ports were reconciled.Unrestricted copying allows tenant input to influence infra controllers and shared address allocation. The impact depends on the infra load-balancer implementation, network and admission policy.
After this PR:
loadBalancer.allowedAnnotationspermits exact keys chosen by the infra operator.externalIPsand tenanthealthCheckNodePortare not copied.loadBalancerIPrequiresloadBalancer.allowTenantLoadBalancerIP: true.externalIPsare cleared.Cloud.Initialize.Tenant
loadBalancerClassis not propagated: the standard Kubernetes service controller skips Services with a class. It is not a way to select the backend for an infra mirror.Default configuration:
Why we need it and why it was done in this way
Tenant administrators may be less trusted than infrastructure operators. The provider must explicitly control what crosses that boundary.
The following tradeoffs were made:
The following alternatives were considered:
loadBalancerClasscannot work through the standard service controller. Any future infra-class selection should be an operator-controlled feature.Special notes for your reviewer
N/S
Checklist
Release note
``
release-note
``ACTION REQUIRED: Tenant LoadBalancer Service annotations and requested loadBalancerIP are no longer forwarded by default. Infrastructure operators must review existing mirrored Services for historical tenant annotations and address allocations, and explicitly configure any trusted annotation/IP permissions. Tenant externalIPs and healthCheckNodePort are no longer copied. Source ranges are validated and forwarded, subject to enforcement by the infrastructure implementation. Ignored IP requests emit Warning events on tenant Services. Tenant loadBalancerClass continues to opt out of the standard cloud-provider service controller; it is not propagated to infrastructure Services.