-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Don't allow adding of existing host to another zone, pod or cluster #13182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vishesh92
wants to merge
4
commits into
apache:4.20
Choose a base branch
from
shapeblue:fix-add-host-diff-location
base: 4.20
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,9 +19,11 @@ | |||||
| import static com.cloud.configuration.ConfigurationManagerImpl.MIGRATE_VM_ACROSS_CLUSTERS; | ||||||
| import static com.cloud.configuration.ConfigurationManagerImpl.SET_HOST_DOWN_TO_MAINTENANCE; | ||||||
|
|
||||||
| import java.net.InetAddress; | ||||||
| import java.net.URI; | ||||||
| import java.net.URISyntaxException; | ||||||
| import java.net.URLDecoder; | ||||||
| import java.net.UnknownHostException; | ||||||
| import java.util.ArrayList; | ||||||
| import java.util.Arrays; | ||||||
| import java.util.Collections; | ||||||
|
|
@@ -31,6 +33,7 @@ | |||||
| import java.util.List; | ||||||
| import java.util.Locale; | ||||||
| import java.util.Map; | ||||||
| import java.util.Objects; | ||||||
| import java.util.Random; | ||||||
| import java.util.stream.Collectors; | ||||||
|
|
||||||
|
|
@@ -798,6 +801,8 @@ private List<HostVO> discoverHostsFull(final Long dcId, final Long podId, Long c | |||||
| throw new InvalidParameterValueException(url + " is not a valid uri"); | ||||||
| } | ||||||
|
|
||||||
| checkForDuplicateHost(url); | ||||||
|
|
||||||
| final List<HostVO> hosts = new ArrayList<>(); | ||||||
| logger.info("Trying to add a new host at {} in data center {}", url, zone); | ||||||
| boolean isHypervisorTypeSupported = false; | ||||||
|
|
@@ -2281,6 +2286,29 @@ private HostVO getNewHost(StartupCommand[] startupCommands) { | |||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| protected void validateExistingHostLocationImmutable(final HostVO host, final boolean newHost, | ||||||
| final long dcId, final Long podId, final Long clusterId, final StartupCommand startup) { | ||||||
| if (newHost || host == null || host.getType() != Host.Type.Routing) { | ||||||
| return; | ||||||
| } | ||||||
| final long existingDcId = host.getDataCenterId(); | ||||||
| final Long existingPodId = host.getPodId(); | ||||||
| final Long existingClusterId = host.getClusterId(); | ||||||
| if (existingPodId == null || existingClusterId == null) { | ||||||
| return; | ||||||
| } | ||||||
| if (existingDcId == dcId && Objects.equals(existingPodId, podId) && Objects.equals(existingClusterId, clusterId)) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return; | ||||||
| } | ||||||
| final String identity = host.getUuid() != null ? host.getUuid() : host.getGuid(); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| final String ip = startup != null ? startup.getPrivateIpAddress() : "unknown"; | ||||||
| throw new InvalidParameterValueException( | ||||||
| String.format("Host %s (ip: %s) is already registered in [zone: %s, pod: %s, cluster: %s] and cannot " + | ||||||
| "be re-added or reconnected with [zone: %s, pod: %s, cluster: %s]. Zone, pod and " + | ||||||
| "cluster of an existing host are immutable.", | ||||||
| identity, ip, existingDcId, existingPodId, existingClusterId, dcId, podId, clusterId)); | ||||||
| } | ||||||
|
|
||||||
| protected HostVO createHostVO(final StartupCommand[] cmds, final ServerResource resource, final Map<String, String> details, List<String> hostTags, | ||||||
| final ResourceStateAdapter.Event stateEvent) { | ||||||
| boolean newHost = false; | ||||||
|
|
@@ -2356,6 +2384,8 @@ protected HostVO createHostVO(final StartupCommand[] cmds, final ServerResource | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| validateExistingHostLocationImmutable(host, newHost, dcId, podId, clusterId, startup); | ||||||
|
|
||||||
| host.setDataCenterId(dc.getId()); | ||||||
| host.setPodId(podId); | ||||||
| host.setClusterId(clusterId); | ||||||
|
|
@@ -2585,6 +2615,31 @@ private Host createHostAndAgent(final ServerResource resource, final Map<String, | |||||
| return host; | ||||||
| } | ||||||
|
|
||||||
| void checkForDuplicateHost(final String url) { | ||||||
| String hostIpOrName = null; | ||||||
| String ipAddress = null; | ||||||
| try { | ||||||
| hostIpOrName = new URI(UriUtils.encodeURIComponent(url)).getHost(); | ||||||
| if (StringUtils.isBlank(hostIpOrName)) { | ||||||
| return; | ||||||
| } | ||||||
| InetAddress ip = InetAddress.getByName(hostIpOrName); | ||||||
| ipAddress = ip.getHostAddress(); | ||||||
| } catch (final URISyntaxException | UnknownHostException ignore) { | ||||||
| // unparseable URL or unknown host - discoverer will reject it shortly anyway | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| if (StringUtils.isNotBlank(ipAddress)) { | ||||||
| final HostVO existingByIp = _hostDao.findByIp(ipAddress); | ||||||
| if (existingByIp != null) { | ||||||
| throw new InvalidParameterValueException(String.format( | ||||||
| "A host with IP address '%s' (%s) already exists (id: %s). Remove it before adding again.", | ||||||
| ipAddress, hostIpOrName, existingByIp)); | ||||||
| } | ||||||
| } | ||||||
|
vishesh92 marked this conversation as resolved.
|
||||||
| } | ||||||
|
|
||||||
| private Host createHostAndAgentDeferred(final ServerResource resource, final Map<String, String> details, final boolean old, final List<String> hostTags, final boolean forRebalance) { | ||||||
| HostVO host = null; | ||||||
| StartupCommand[] cmds = null; | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.