Commit 2caad48
committed
vsphere: Cache REST API sessions to prevent excessive vCenter logouts
This commit fixes a critical issue where the machine-api-operator was
creating and destroying vCenter REST API sessions on every machine
reconciliation, causing excessive login/logout cycles that pollute
vCenter audit logs and create unnecessary session churn.
Root Cause:
The WithRestClient() and WithCachingTagsManager() wrapper functions
were creating new REST sessions, performing operations, and immediately
logging out on every invocation. With hundreds of machines reconciling
periodically, this created a constant stream of login/logout events.
Solution (inspired by cluster-api-provider-vsphere):
- Add TagManager field to Session struct to cache REST client
- Initialize and cache REST client during session creation (GetOrCreate)
- Validate both SOAP and REST session health before reusing cached sessions
- Add GetCachingTagsManager() helper for direct access to cached tag manager
- Update reconcileRegionAndZoneLabels() to use cached tag manager
- Update reconcileTags() to use cached tag manager
- Deprecate WithRestClient() and WithCachingTagsManager() for backward compatibility
Key Changes:
1. pkg/controller/vsphere/session/session.go:
- Added TagManager *tags.Manager field to Session struct
- Modified GetOrCreate() to create and cache REST client once
- Added dual session validation (SOAP + REST) before reusing sessions
- Added GetCachingTagsManager() method for direct access
- Deprecated old wrapper functions with migration guidance
2. pkg/controller/vsphere/reconciler.go:
- Updated reconcileRegionAndZoneLabels() to use GetCachingTagsManager()
- Updated reconcileTags() to use GetCachingTagsManager()
- Eliminated callback pattern in favor of direct access
Impact:
- Eliminates excessive vCenter login/logout cycles
- Reduces vCenter session churn from O(reconciliations) to O(1) per MAPI instance
- Improves performance by removing authentication overhead on every tag operation
- REST session now lives as long as SOAP session (until invalidation)
Reference Implementation:
https://github.com/kubernetes-sigs/cluster-api-provider-vsphere/blob/main/pkg/session/session.go
Backward Compatibility:
The deprecated wrapper functions are maintained with warning logs to support
existing test code. All production code paths now use the new pattern.
Fixes: Excessive vCenter logout events reported by customers
Signed-off-by: Claude Code Assistant <[email protected]>1 parent c8772d3 commit 2caad48
2 files changed
+91
-33
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
594 | 594 | | |
595 | 595 | | |
596 | 596 | | |
597 | | - | |
598 | | - | |
599 | | - | |
600 | | - | |
601 | | - | |
602 | | - | |
603 | | - | |
604 | | - | |
605 | | - | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
606 | 601 | | |
607 | 602 | | |
608 | 603 | | |
| |||
1599 | 1594 | | |
1600 | 1595 | | |
1601 | 1596 | | |
1602 | | - | |
1603 | | - | |
1604 | | - | |
1605 | | - | |
1606 | | - | |
1607 | | - | |
1608 | | - | |
1609 | | - | |
1610 | | - | |
1611 | | - | |
1612 | | - | |
1613 | | - | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
| 1610 | + | |
1614 | 1611 | | |
1615 | | - | |
1616 | | - | |
1617 | | - | |
1618 | | - | |
1619 | | - | |
1620 | | - | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
1621 | 1617 | | |
1622 | 1618 | | |
1623 | | - | |
1624 | | - | |
1625 | | - | |
1626 | 1619 | | |
1627 | | - | |
1628 | 1620 | | |
1629 | 1621 | | |
1630 | 1622 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
49 | 52 | | |
50 | 53 | | |
51 | 54 | | |
52 | 55 | | |
| 56 | + | |
53 | 57 | | |
54 | 58 | | |
55 | 59 | | |
| |||
80 | 84 | | |
81 | 85 | | |
82 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
83 | 91 | | |
84 | 92 | | |
85 | | - | |
| 93 | + | |
86 | 94 | | |
87 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
88 | 107 | | |
89 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
90 | 121 | | |
91 | 122 | | |
92 | 123 | | |
| |||
127 | 158 | | |
128 | 159 | | |
129 | 160 | | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
130 | 175 | | |
131 | 176 | | |
132 | 177 | | |
| |||
206 | 251 | | |
207 | 252 | | |
208 | 253 | | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
209 | 267 | | |
| 268 | + | |
210 | 269 | | |
211 | 270 | | |
212 | 271 | | |
| |||
223 | 282 | | |
224 | 283 | | |
225 | 284 | | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
226 | 291 | | |
| 292 | + | |
227 | 293 | | |
228 | 294 | | |
229 | 295 | | |
| |||
0 commit comments