Skip to content

Commit

Permalink
batch and verify portgroup removal
Browse files Browse the repository at this point in the history
  • Loading branch information
sei-jmattson committed Nov 21, 2024
1 parent fc76028 commit 3142e8c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 57 deletions.
13 changes: 8 additions & 5 deletions src/TopoMojo.Hypervisor/vSphere/DistributedNetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,17 @@ config.defaultPortConfig is VMwareDVSPortSetting setting
return [.. list];
}

public override async Task<bool> RemovePortgroup(string pgReference)
public override async Task<PortGroupAllocation[]> RemovePortgroups(PortGroupAllocation[] pgs)
{
try
foreach (var pg in pgs)
{
await _client.Vim.Destroy_TaskAsync(pgReference.AsReference());
try
{
await _client.Vim.Destroy_TaskAsync(pg.Key.AsReference());
}
catch { }
}
catch { }
return true;
return pgs;
}

public override Task RemoveSwitch(string sw)
Expand Down
13 changes: 8 additions & 5 deletions src/TopoMojo.Hypervisor/vSphere/HostNetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,17 @@ public override async Task<PortGroupAllocation[]> LoadPortGroups()
return [.. list];
}

public override async Task<bool> RemovePortgroup(string pgReference)
public override async Task<PortGroupAllocation[]> RemovePortgroups(PortGroupAllocation[] pgs)
{
try
foreach (var pg in pgs.ToArray())
{
await _client.Vim.RemovePortGroupAsync(_client.Net, pgReference);
try
{
await _client.Vim.RemovePortGroupAsync(_client.Net, pg.Key);
}
catch { }
}
catch { }
return true;
return pgs;
}

public override async Task RemoveSwitch(string sw)
Expand Down
2 changes: 1 addition & 1 deletion src/TopoMojo.Hypervisor/vSphere/INetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface INetworkManager
Task RemoveSwitch(string sw);
Task<PortGroupAllocation> AddPortGroup(string sw, VmNet eth);
Task<PortGroupAllocation[]> AddPortGroups(string sw, VmNet[] eths);
Task<bool> RemovePortgroup(string pgReference);
Task<PortGroupAllocation[]> RemovePortgroups(PortGroupAllocation[] pgs);
Task<VmNetwork[]> GetVmNetworks(ManagedObjectReference managedObjectReference);
Task<PortGroupAllocation[]> LoadPortGroups();
void UpdateEthernetCardBacking(VirtualEthernetCard card, string portgroupName);
Expand Down
29 changes: 12 additions & 17 deletions src/TopoMojo.Hypervisor/vSphere/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,19 @@ public async Task Clean(string tag = null)
;

// find portgroups with no associated vm's
foreach (var pg in q.ToArray())
{
string id = pg.Net.Tag();

// if vm's still exist, skip
if (_vmCache.Values.Any(v => v.Name.EndsWith(id)))
continue;

_logger.LogDebug("try removing net {net}", pg.Net);
var vm_space = _vmCache.Values.Select(v => v.Name.Tag()).Distinct();
var pg_space = q.Select(p => p.Net.Tag()).Distinct();
var inactive = pg_space.Except(vm_space).ToArray();
var manifest = q.Where(p => inactive.Contains(p.Net.Tag())).ToArray();

if (RemovePortgroup(pg.Key).Result)
{
_pgAllocation.Remove(pg.Net);
_vlanManager.Deactivate(pg.Net);
var removed_pgs = RemovePortgroups(manifest).Result;
foreach (var pg in removed_pgs)
{
_pgAllocation.Remove(pg.Net);
_vlanManager.Deactivate(pg.Net);

if (_swAllocation.ContainsKey(pg.Switch))
_swAllocation[pg.Switch] -= 1;
}
if (_swAllocation.ContainsKey(pg.Switch))
_swAllocation[pg.Switch] -= 1;
}

foreach (var sw in _swAllocation.Keys.ToArray())
Expand Down Expand Up @@ -231,7 +226,7 @@ await AddPortGroup(sw, eth)
);
return [.. pgs];
}
public abstract Task<bool> RemovePortgroup(string pgReference);
public abstract Task<PortGroupAllocation[]> RemovePortgroups(PortGroupAllocation[] gps);
public abstract Task AddSwitch(string sw);
public abstract Task RemoveSwitch(string sw);

Expand Down
38 changes: 9 additions & 29 deletions src/TopoMojo.Hypervisor/vSphere/NsxNetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,38 +301,18 @@ config.defaultPortConfig is VMwareDVSPortSetting setting
return [.. list];
}

public override async Task<bool> RemovePortgroup(string pgReference)
public override async Task<PortGroupAllocation[]> RemovePortgroups(PortGroupAllocation[] pgs)
{
try
{
var pga = _pgAllocation.Values.FirstOrDefault(v => v.Key == pgReference);

if (pga == null || !pga.Net.Contains('#'))
return false;

await InitClient();
await InitClient();

var response = await _sddc.DeleteAsync(
$"{_apiUrl}/{_apiSegments}/{pga.Net.Replace("#", "%23")}"
);
// remove all
var tasks = pgs.Select(p => _sddc.DeleteAsync($"{_apiUrl}/{_apiSegments}/{p.Net.Replace("#", "%23")}")).ToArray();
Task.WaitAll(tasks);

if (!response.IsSuccessStatusCode)
{
_logger.LogWarning("error removing net: {net} {code} {reason} {content}",
pga.Net,
response.StatusCode,
response.ReasonPhrase,
await response.Content.ReadAsStringAsync()
);
}

return response.IsSuccessStatusCode;
}
catch (Exception ex)
{
_logger.LogWarning("error removing net: {message}", ex.Message);
}
return false;
// verify deletion
await Task.Delay(2000);
var existing = await LoadPortGroups();
return pgs.ExceptBy(existing.Select(e => e.Net), p => p.Net).ToArray();
}

public override Task RemoveSwitch(string sw)
Expand Down

0 comments on commit 3142e8c

Please sign in to comment.