Class VSphereHostSelection
java.lang.Object
org.jenkinsci.plugins.vsphere.tools.VSphereHostSelection
Pure, yavijava-free logic for picking a "best" ESXi host out of a
cluster's members, given per-host stats and an optional admin-supplied
allow-list. Kept free of vSphere API types so it can be unit-tested
without a live vCenter connection.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classDescribes one candidate host's name, availability and current load, as needed to decide whether/how favourably it can be used for a new clone.static final classA candidate host with its availability score (0 worst .. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringExplicit "no host selection" override for a template/build-step'shostSelectionMode, distinct from leaving it blank (which means "inherit the cloud-level default" - seeresolveMode(java.lang.String, java.lang.String)). -
Method Summary
Modifier and TypeMethodDescriptionstatic StringexcludedBecause(VSphereHostSelection.HostCandidate candidate, Set<String> allowList) WhyfilterCandidates(java.util.List<org.jenkinsci.plugins.vsphere.tools.VSphereHostSelection.HostCandidate>, java.util.Set<java.lang.String>)drops this host, or null if it keeps it.filterByVmSize(List<VSphereHostSelection.HostCandidate> candidates, boolean requireCores, Integer vmCpus, boolean requireMemory, Integer vmMemoryMB) Optionally drops candidates that are physically too small for the VM about to be created: withrequireCores, hosts with fewer physical CPU cores thanvmCpus; withrequireMemory, hosts with less total physical RAM thanvmMemoryMB.filterCandidates(List<VSphereHostSelection.HostCandidate> candidates, Set<String> allowList) Filters candidates down to ones that are usable (connected, not in maintenance mode) and, if the allow-list is non-empty, whose name is in it.listOf(VSphereHostSelection.HostCandidate... candidates) Convenience: same as callingArrays.asList(T...)for tests.parseAllowList(String hostSelectionCandidatesCsv) Parses a comma-separated allow-list of host names, trimming whitespace and discarding empty entries.parseAllowListOrNull(String hostSelectionCandidatesCsv) LikeparseAllowList(java.lang.String), but preserves the "not set at all" case as null instead of collapsing it to an empty set - needed so a blankhostSelectionCandidatesAsStringcan mean "inherit the cloud-level default" (seeresolveCandidates(java.util.Set<java.lang.String>, java.util.Set<java.lang.String>)) while a deliberately non-blank-but-hostless value (e.g.pickBest(List<VSphereHostSelection.HostCandidate> candidates, HostWeights weights) The most available candidate according torank(java.util.List<org.jenkinsci.plugins.vsphere.tools.VSphereHostSelection.HostCandidate>, org.jenkinsci.plugins.vsphere.tools.HostWeights), or null if none has statistics.pickLeastLoaded(List<VSphereHostSelection.HostCandidate> candidates) Picks the candidate with the lowest load fraction, excluding any candidate whose stats are unavailable.static List<VSphereHostSelection.ScoredHost> rank(List<VSphereHostSelection.HostCandidate> candidates, HostWeights weights) Ranks the candidates that have usage statistics, most available first (equally scored ones keep their incoming order).resolveCandidates(Set<String> cloudDefault, Set<String> override) Resolves a template/build-step'shostSelectionCandidatesagainst its cloud's default: null (never set at this level) defers tocloudDefault; any explicitly-set value - including an empty set, meaning "explicitly no restriction" - wins outright.static StringresolveMode(String cloudDefault, String override) Resolves a template/build-step'shostSelectionModeagainst its cloud's default: blank/null defers tocloudDefault;HOST_SELECTION_MODE_NONEexplicitly disables host selection regardless of the cloud default; any other value (a real mode) wins outright.static StringsizeShortfall(VSphereHostSelection.HostCandidate candidate, boolean requireCores, Integer vmCpus, boolean requireMemory, boolean requireAvailableMemory, Integer vmMemoryMB) As above, and withrequireAvailableMemoryalso drops hosts that do not have at leastvmMemoryMBof memory free right now (so the new VM would not be swapped by the hypervisor), or whose memory usage is unknown.static StringsizeShortfall(VSphereHostSelection.HostCandidate candidate, boolean requireCores, Integer vmCpus, boolean requireMemory, Integer vmMemoryMB) WhyfilterByVmSize(java.util.List<org.jenkinsci.plugins.vsphere.tools.VSphereHostSelection.HostCandidate>, boolean, java.lang.Integer, boolean, java.lang.Integer)drops this host, or null if it keeps it.static StringtoAllowListString(Set<String> hosts) The inverse ofparseAllowListOrNull(java.lang.String): renders null as "" (inherit), an empty set as a single comma (a visible, non-blank marker for "explicitly no restriction" that round-trips back throughparseAllowListOrNull(java.lang.String)to an empty - not null - set), and anything else as the plain comma-separated form.static StringtoCsv(Collection<String> hosts) Joins a collection of host names back into the comma-separated form used by the classic config UI textbox.
-
Field Details
-
HOST_SELECTION_MODE_NONE
Explicit "no host selection" override for a template/build-step'shostSelectionMode, distinct from leaving it blank (which means "inherit the cloud-level default" - seeresolveMode(java.lang.String, java.lang.String)).- See Also:
-
-
Method Details
-
parseAllowList
Parses a comma-separated allow-list of host names, trimming whitespace and discarding empty entries. Returns an empty (not null) set when the input is null/blank, meaning "no restriction". -
toCsv
Joins a collection of host names back into the comma-separated form used by the classic config UI textbox. Returns "" (not null) for a null/empty input, so this is safe to use directly as a form field's current value. -
parseAllowListOrNull
LikeparseAllowList(java.lang.String), but preserves the "not set at all" case as null instead of collapsing it to an empty set - needed so a blankhostSelectionCandidatesAsStringcan mean "inherit the cloud-level default" (seeresolveCandidates(java.util.Set<java.lang.String>, java.util.Set<java.lang.String>)) while a deliberately non-blank-but-hostless value (e.g. a lone comma) can still mean "explicitly override to no restriction". Blank/whitespace-only input (including a single space) is treated as "not set"; anything else is parsed normally, which may still yield an empty (non-null) set. -
toAllowListString
The inverse ofparseAllowListOrNull(java.lang.String): renders null as "" (inherit), an empty set as a single comma (a visible, non-blank marker for "explicitly no restriction" that round-trips back throughparseAllowListOrNull(java.lang.String)to an empty - not null - set), and anything else as the plain comma-separated form. -
resolveMode
Resolves a template/build-step'shostSelectionModeagainst its cloud's default: blank/null defers tocloudDefault;HOST_SELECTION_MODE_NONEexplicitly disables host selection regardless of the cloud default; any other value (a real mode) wins outright. -
resolveCandidates
Resolves a template/build-step'shostSelectionCandidatesagainst its cloud's default: null (never set at this level) defers tocloudDefault; any explicitly-set value - including an empty set, meaning "explicitly no restriction" - wins outright. -
filterCandidates
public static List<VSphereHostSelection.HostCandidate> filterCandidates(List<VSphereHostSelection.HostCandidate> candidates, Set<String> allowList) Filters candidates down to ones that are usable (connected, not in maintenance mode) and, if the allow-list is non-empty, whose name is in it. An empty/null allow-list means "consider every usable host". -
excludedBecause
public static String excludedBecause(VSphereHostSelection.HostCandidate candidate, Set<String> allowList) WhyfilterCandidates(java.util.List<org.jenkinsci.plugins.vsphere.tools.VSphereHostSelection.HostCandidate>, java.util.Set<java.lang.String>)drops this host, or null if it keeps it. For logging the reasoning behind a placement. -
filterByVmSize
public static List<VSphereHostSelection.HostCandidate> filterByVmSize(List<VSphereHostSelection.HostCandidate> candidates, boolean requireCores, Integer vmCpus, boolean requireMemory, Integer vmMemoryMB) Optionally drops candidates that are physically too small for the VM about to be created: withrequireCores, hosts with fewer physical CPU cores thanvmCpus; withrequireMemory, hosts with less total physical RAM thanvmMemoryMB. Compares absolute capacity, not current free resources, so sites that oversubscribe (swap, hyperthreads, ...) can simply leave both off. A host whose relevant capacity is unknown (0) does not satisfy a requirement that is switched on, and a VM size that is unknown (null) disables the corresponding check. -
sizeShortfall
public static String sizeShortfall(VSphereHostSelection.HostCandidate candidate, boolean requireCores, Integer vmCpus, boolean requireMemory, Integer vmMemoryMB) WhyfilterByVmSize(java.util.List<org.jenkinsci.plugins.vsphere.tools.VSphereHostSelection.HostCandidate>, boolean, java.lang.Integer, boolean, java.lang.Integer)drops this host, or null if it keeps it. For logging the reasoning behind a placement. -
sizeShortfall
public static String sizeShortfall(VSphereHostSelection.HostCandidate candidate, boolean requireCores, Integer vmCpus, boolean requireMemory, boolean requireAvailableMemory, Integer vmMemoryMB) As above, and withrequireAvailableMemoryalso drops hosts that do not have at leastvmMemoryMBof memory free right now (so the new VM would not be swapped by the hypervisor), or whose memory usage is unknown. UnlikerequireMemory, which compares the RAM installed, this looks at the host's current usage, which changes by the minute. -
rank
public static List<VSphereHostSelection.ScoredHost> rank(List<VSphereHostSelection.HostCandidate> candidates, HostWeights weights) Ranks the candidates that have usage statistics, most available first (equally scored ones keep their incoming order). Hosts without statistics are left out.With
defaultweights, a host scores 1 minus itsload fraction, which reproducespickLeastLoaded(java.util.List<org.jenkinsci.plugins.vsphere.tools.VSphereHostSelection.HostCandidate>). Otherwise the score is the weighted average of four measures, each between 0 and 1: free CPU and free memory as a share of the host's own capacity, and free CPU (MHz) and free memory (MB) relative to the best of the candidates being compared. -
pickBest
public static VSphereHostSelection.HostCandidate pickBest(List<VSphereHostSelection.HostCandidate> candidates, HostWeights weights) The most available candidate according torank(java.util.List<org.jenkinsci.plugins.vsphere.tools.VSphereHostSelection.HostCandidate>, org.jenkinsci.plugins.vsphere.tools.HostWeights), or null if none has statistics. -
pickLeastLoaded
public static VSphereHostSelection.HostCandidate pickLeastLoaded(List<VSphereHostSelection.HostCandidate> candidates) Picks the candidate with the lowest load fraction, excluding any candidate whose stats are unavailable. Returns null if no candidate has usable stats. -
listOf
public static List<VSphereHostSelection.HostCandidate> listOf(VSphereHostSelection.HostCandidate... candidates) Convenience: same as callingArrays.asList(T...)for tests.
-