[occm] Tag load balancers with cluster identity to prevent name collisions#3103
[occm] Tag load balancers with cluster identity to prevent name collisions#3103enginrect wants to merge 4 commits intokubernetes:masterfrom
Conversation
|
Welcome @enginrect! |
|
Hi @enginrect. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Hi @kayrus @stephenfin @zetaab — first-time contributor here. This PR addresses the long-standing cross-cluster LB collision issue (refs #2241, #2571, #2624) with an additive, backward-compatible Could one of you take a look and add Thanks! |
…sions OCCM constructs Octavia load balancer names as kube_service_<cluster-name>_<namespace>_<service>. When two Kubernetes clusters share the same OpenStack project and use the same --cluster-name (default "kubernetes"), services with identical namespace/name produce identical load balancer names. Octavia does not enforce uniqueness on load balancer names, so OCCM's first-time name-based lookup can adopt and overwrite a load balancer that actually belongs to a different cluster (see issues kubernetes#2241, kubernetes#2571, kubernetes#2624). This commit adds a stable Kubernetes cluster identifier - the UID of the kube-system namespace - as a load balancer tag of the form kube_cluster_id_<uid>. getLoadbalancerByName now ignores load balancers that carry a cluster-id tag for a different cluster and falls back to the legacy behaviour for load balancers without any cluster-id tag, so existing deployments keep working unchanged. Pre-existing load balancers gain the new tag during the next reconciliation. The cluster UID is read once at controller-manager start-up via the kube-system namespace; failure to read it (RBAC denial, missing namespace) is non-fatal and disables the safeguard, falling back to legacy name-based lookup. The cloud-controller-manager ClusterRole and the helm chart gain "get" on namespaces. Made-with: Cursor
123ffe4 to
bbf5f4f
Compare
1b0cbe1 to
9bb737f
Compare
The previous commit added a "get" on "namespaces" rule to the Helm chart's ClusterRole template. chart-testing requires a version bump on any chart modification. Bumping the patch version since the change is additive and backward-compatible.
9bb737f to
b620ccf
Compare
|
Quick update: I've rebased on top of master (which now has the SHA-pinned actions from #3100) and bumped the helm chart patch version to 2.35.1 — |
| // identifier on OpenStack load balancer tags. May be empty if the lookup | ||
| // failed or RBAC does not allow it; in that case OCCM falls back to the | ||
| // legacy name-based load balancer identification. | ||
| clusterUID string |
There was a problem hiding this comment.
clusterUID can be defined only in LoadBalancer struct, please remove it from here.
There was a problem hiding this comment.
Good catch — thanks! You're absolutely right, that was a cohesion miss on my part. clusterUID is only consumed by the LB code path so it shouldn't pollute the global OpenStack struct. Removed in 56756ea; the field now lives only on LoadBalancer where it belongs.
| klog.V(1).Info("Claiming to support LoadBalancer") | ||
|
|
||
| return &LbaasV2{LoadBalancer{secret, network, lb, os.lbOpts, os.kclient, os.eventRecorder}}, true | ||
| return &LbaasV2{LoadBalancer{secret, network, lb, os.lbOpts, os.kclient, os.eventRecorder, os.clusterUID}}, true |
There was a problem hiding this comment.
you can set clusterUID value here with:
clusterID := fetchClusterUID(os.kclient)There was a problem hiding this comment.
Thank you, that's a much cleaner pattern. Fetching lazily inside LoadBalancer() keeps the kube-system namespace lookup out of the global Initialize() path and limits the change to the LB construction site. As a nice side effect, clusters that disable LB now skip the lookup entirely. Done in 56756ea.
| opts := loadbalancers.ListOpts{ | ||
| Name: name, | ||
| } |
There was a problem hiding this comment.
you can filter by tags using ListOpts.Tags. filterLoadBalancersByClusterID doesn't make sense.
UPD: please ignore this comment
| // balancer with a matching name that belongs to a different Kubernetes | ||
| // cluster (different cluster-id tag). The lookup is treated as NotFound | ||
| // so OCCM creates a new load balancer instead of stealing an existing one. | ||
| eventLBStolen = "LoadBalancerNameCollision" |
There was a problem hiding this comment.
Great question — and the honest answer is: no, it's not used anywhere, sorry about the leftover. The original intent was to emit a Warning event via eventRecorder from getLoadbalancerByName when we drop a foreign-tagged LB, but plumbing the event recorder and *v1.Service into that free-standing function felt out of scope for this PR (the existing warning log already covers operator visibility), so I dropped the emission and forgot to remove the constant. Cleaned up in 56756ea.
- Remove duplicate clusterUID field from the OpenStack struct so the identifier lives only on the LoadBalancer struct that actually uses it (better cohesion). - Drop fetchClusterUID() out of Initialize() and call it lazily inside the LoadBalancer() factory instead. Clusters that disable LB now skip the kube-system namespace lookup entirely, and the change touches only the LB construction path. - Remove the unused eventLBStolen constant. It was a leftover from an earlier draft that intended to emit a Warning event from getLoadbalancerByName(); plumbing the eventRecorder + *v1.Service into that free-standing function felt out of scope, so the emission was dropped but the constant was left behind.
|
Thanks for the review @kayrus! Pushed the changes in 56756ea:
|
|
/ok-to-test |
The pull-cloud-provider-openstack-check prow job runs golangci-lint v2.3.1 with staticcheck enabled, which flags fake.NewSimpleClientset as SA1019 (deprecated). TestFetchClusterUID, added earlier in this PR, used the deprecated function. Swap it for fake.NewClientset; the signature is identical (objects ...runtime.Object) and the unit tests still pass.
|
The check job was tripping on SA1019 because the new Verified locally:
|
What this PR does / why we need it:
OCCM identifies an existing Octavia load balancer for a Service by name on
the first reconcile (via
getLoadbalancerByName). The name formatkube_service_<cluster-name>_<namespace>_<service>defaults to a<cluster-name>ofkubernetes, so two Kubernetes clusters in the sameOpenStack project that happen to use the default cluster-name and have
Services with identical namespace/name produce identical load balancer
names. Octavia does not enforce uniqueness of names, so OCCM in cluster B
ends up adopting and overwriting cluster A's load balancer. This has been
reported repeatedly (see #2241, #2571, #2624) and the standing guidance
"set a unique
--cluster-name" is correct but does not actually defendagainst the failure mode.
This PR adds a stable Kubernetes cluster identifier - the UID of the
kube-systemnamespace - as a load balancer tag of the formkube_cluster_id_<uid>. Lookup behaviour:kube_cluster_id_<our-uid>tag are kept.kube_cluster_id_*tag fall back to the legacybehaviour (preserves existing deployments and externally-created LBs).
kube_cluster_id_*tags are treated asNotFound, with a warning. OCCM will then create its own load balancer
rather than overwriting one that belongs to another cluster.
The cluster UID is read once at controller-manager start-up. If the
lookup fails (RBAC denial, missing namespace, etc.) the safeguard is
disabled and OCCM falls back to the legacy name-based behaviour, so the
change is strictly additive. Pre-existing load balancers also gain the
kube_cluster_id_*tag during the next reconciliation.Which issue this PR fixes(if applicable):
fixes #3102
Special notes for reviewers:
kube_cluster_id_*tag keep the previousbehaviour. They are tagged on the next successful reconcile.
loadbalancer.openstack.org/load-balancer-idannotation (i.e. onevery reconcile after the first one) go through
GetLoadbalancerByID,which is unaffected.
getonnamespacesis added to both the manifestClusterRole (
manifests/controller-manager/cloud-controller-manager-roles.yaml)and the helm chart (
charts/openstack-cloud-controller-manager/templates/clusterrole.yaml).If the verb is unavailable the safeguard simply degrades to the legacy
behaviour with a warning log; OCCM does not refuse to start.
already gated by
svcConf.supportLBTagsand behaves as before on olderclouds.
TestFilterLoadBalancersByClusterIDcovers the matching, legacy,foreign-only, and mixed cases.
TestFetchClusterUIDcovers happy path and graceful degradation(missing namespace, forbidden) with a fake clientset.
How to verify manually:
go test ./pkg/openstack/...A reproduction of the original failure mode (two clusters in the same
project, same
--cluster-name, same Service ns/name) is described in#3102.
Release note: