Commit 114d062
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 <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 83936dd commit 114d062
3 files changed
Lines changed: 178 additions & 151 deletions
| 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 | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | 33 | | |
35 | 34 | | |
36 | 35 | | |
| |||
1666 | 1665 | | |
1667 | 1666 | | |
1668 | 1667 | | |
1669 | | - | |
| 1668 | + | |
1670 | 1669 | | |
1671 | | - | |
1672 | | - | |
1673 | | - | |
1674 | | - | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
1675 | 1674 | | |
1676 | | - | |
1677 | | - | |
1678 | | - | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
1679 | 1678 | | |
1680 | | - | |
1681 | | - | |
1682 | | - | |
1683 | | - | |
| 1679 | + | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
1684 | 1683 | | |
1685 | | - | |
1686 | | - | |
1687 | | - | |
1688 | | - | |
1689 | | - | |
1690 | | - | |
1691 | | - | |
1692 | | - | |
1693 | | - | |
1694 | | - | |
1695 | | - | |
1696 | | - | |
1697 | | - | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
| 1694 | + | |
| 1695 | + | |
1698 | 1696 | | |
1699 | | - | |
1700 | | - | |
1701 | | - | |
1702 | 1697 | | |
1703 | 1698 | | |
1704 | | - | |
1705 | | - | |
1706 | | - | |
1707 | | - | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
1708 | 1702 | | |
1709 | 1703 | | |
1710 | 1704 | | |
| |||
1730 | 1724 | | |
1731 | 1725 | | |
1732 | 1726 | | |
1733 | | - | |
1734 | | - | |
| 1727 | + | |
1735 | 1728 | | |
1736 | | - | |
1737 | | - | |
1738 | | - | |
1739 | | - | |
1740 | | - | |
1741 | | - | |
1742 | | - | |
1743 | | - | |
1744 | | - | |
1745 | | - | |
1746 | | - | |
1747 | | - | |
1748 | | - | |
1749 | | - | |
1750 | | - | |
1751 | | - | |
1752 | | - | |
1753 | | - | |
1754 | | - | |
1755 | | - | |
| 1729 | + | |
| 1730 | + | |
| 1731 | + | |
| 1732 | + | |
| 1733 | + | |
| 1734 | + | |
| 1735 | + | |
| 1736 | + | |
1756 | 1737 | | |
1757 | | - | |
1758 | | - | |
1759 | | - | |
1760 | | - | |
1761 | | - | |
| 1738 | + | |
| 1739 | + | |
| 1740 | + | |
| 1741 | + | |
| 1742 | + | |
| 1743 | + | |
| 1744 | + | |
1762 | 1745 | | |
1763 | | - | |
1764 | | - | |
1765 | | - | |
| 1746 | + | |
| 1747 | + | |
| 1748 | + | |
1766 | 1749 | | |
1767 | | - | |
1768 | | - | |
1769 | | - | |
1770 | | - | |
1771 | | - | |
1772 | | - | |
1773 | | - | |
| 1750 | + | |
| 1751 | + | |
| 1752 | + | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
1774 | 1758 | | |
1775 | | - | |
1776 | | - | |
| 1759 | + | |
| 1760 | + | |
| 1761 | + | |
| 1762 | + | |
| 1763 | + | |
1777 | 1764 | | |
1778 | 1765 | | |
1779 | 1766 | | |
| |||
1800 | 1787 | | |
1801 | 1788 | | |
1802 | 1789 | | |
1803 | | - | |
| 1790 | + | |
1804 | 1791 | | |
1805 | | - | |
1806 | | - | |
1807 | | - | |
1808 | | - | |
1809 | | - | |
1810 | | - | |
1811 | | - | |
1812 | | - | |
| 1792 | + | |
| 1793 | + | |
| 1794 | + | |
| 1795 | + | |
1813 | 1796 | | |
1814 | | - | |
1815 | | - | |
1816 | | - | |
| 1797 | + | |
| 1798 | + | |
1817 | 1799 | | |
1818 | 1800 | | |
1819 | 1801 | | |
| |||
3321 | 3303 | | |
3322 | 3304 | | |
3323 | 3305 | | |
3324 | | - | |
3325 | | - | |
3326 | | - | |
3327 | | - | |
3328 | | - | |
3329 | | - | |
3330 | | - | |
| 3306 | + | |
3331 | 3307 | | |
3332 | | - | |
3333 | | - | |
3334 | | - | |
3335 | | - | |
| 3308 | + | |
| 3309 | + | |
| 3310 | + | |
| 3311 | + | |
3336 | 3312 | | |
3337 | | - | |
3338 | | - | |
| 3313 | + | |
| 3314 | + | |
3339 | 3315 | | |
3340 | 3316 | | |
3341 | 3317 | | |
| |||
3412 | 3388 | | |
3413 | 3389 | | |
3414 | 3390 | | |
3415 | | - | |
3416 | | - | |
3417 | | - | |
3418 | | - | |
3419 | | - | |
3420 | | - | |
3421 | | - | |
3422 | | - | |
3423 | | - | |
3424 | | - | |
3425 | | - | |
3426 | | - | |
| 3391 | + | |
3427 | 3392 | | |
3428 | | - | |
3429 | | - | |
3430 | | - | |
3431 | | - | |
3432 | | - | |
3433 | | - | |
3434 | | - | |
| 3393 | + | |
| 3394 | + | |
| 3395 | + | |
| 3396 | + | |
| 3397 | + | |
| 3398 | + | |
| 3399 | + | |
| 3400 | + | |
3435 | 3401 | | |
3436 | | - | |
3437 | | - | |
| 3402 | + | |
| 3403 | + | |
| 3404 | + | |
| 3405 | + | |
| 3406 | + | |
3438 | 3407 | | |
3439 | 3408 | | |
3440 | 3409 | | |
| |||
0 commit comments