Skip to content

Commit

Permalink
get vmss by nodeTypeRef rather than name, this fixes #4708
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfredo Santamaria Gomez committed Jul 19, 2019
1 parent 94e745d commit 29b18d0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
58 changes: 43 additions & 15 deletions src/ServiceFabric/ServiceFabric/Commands/ServiceFabricCmdletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,33 +181,51 @@ private void InitializeAzureRmClients()

#region VMSS

protected VirtualMachineScaleSet GetVmss(string name)
protected VirtualMachineScaleSet GetVmss(string name, string clusterId)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new PSArgumentNullException("Invalid vmss name");
}

var result = ComputeClient.VirtualMachineScaleSets.List(ResourceGroupName);
if (result == null || !result.Any())
var vmssPages = ComputeClient.VirtualMachineScaleSets.List(ResourceGroupName);
if (vmssPages == null || !vmssPages.Any())
{
throw new PSArgumentException(string.Format(
ServiceFabricProperties.Resources.NoVMSSFoundInRG,
this.ResourceGroupName));
}

var vmss = result.FirstOrDefault(
vm => vm.Name.Equals(name, StringComparison.OrdinalIgnoreCase));

if (vmss == null)
do
{
throw new PSInvalidOperationException(
if (!vmssPages.Any())
{
break;
}

foreach (var vmss in vmssPages)
{
VirtualMachineScaleSetExtension sfExtension;
if (TryGetFabricVmExt(vmss.VirtualMachineProfile.ExtensionProfile?.Extensions, out sfExtension))
{
if (string.Equals(GetClusterIdFromExtension(sfExtension), clusterId, StringComparison.OrdinalIgnoreCase))
{
WriteVerboseWithTimestamp(string.Format("GetVmss: Found vmss {0} that corresponds to cluster id {1}", vmss.Id, clusterId));
string nodeTypeRef = GetNodeTypeRefFromExtension(sfExtension);
if (string.Equals(nodeTypeRef, name, StringComparison.OrdinalIgnoreCase))
{
return vmss;
}
}
}
}
} while (!string.IsNullOrEmpty(vmssPages.NextPageLink) &&
(vmssPages = this.ComputeClient.VirtualMachineScaleSets.ListNext(vmssPages.NextPageLink)) != null);

throw new PSInvalidOperationException(
string.Format(
ServiceFabricProperties.Resources.CannotFindVMSS,
ServiceFabricProperties.Resources.CannotFindVMSS,
name));
}

return vmss;
}

public bool TryGetFabricVmExt(IList<VirtualMachineScaleSetExtension> extensions, out VirtualMachineScaleSetExtension sfExtension)
Expand All @@ -230,12 +248,22 @@ public bool TryGetFabricVmExt(IList<VirtualMachineScaleSetExtension> extensions,

public string GetClusterIdFromExtension(VirtualMachineScaleSetExtension sfExtension)
{
JObject extSettings = (JObject)sfExtension.Settings;
string clusterEndpoint = (string)extSettings.SelectToken("clusterEndpoint");
string id = clusterEndpoint.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last();
string clusterEndpoint = GetSettingFromExtension(sfExtension, "clusterEndpoint");
string id = clusterEndpoint?.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last();
return id;
}

public string GetNodeTypeRefFromExtension(VirtualMachineScaleSetExtension sfExtension)
{
return GetSettingFromExtension(sfExtension, "nodeTypeRef");
}

internal string GetSettingFromExtension(VirtualMachineScaleSetExtension sfExtension, string settingName)
{
JObject extSettings = sfExtension.Settings as JObject;
return (string)extSettings.SelectToken(settingName);
}

#endregion

#region Key Vault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ public class UpdateAzureRmServiceFabricDurability : ServiceFabricClusterCmdlet

public override void ExecuteCmdlet()
{
var vmss = GetVmss(this.NodeType);
var cluster = GetCurrentCluster();
var vmss = GetVmss(this.NodeType, cluster.ClusterId);
VirtualMachineScaleSetExtension ext;
if (!TryGetFabricVmExt(vmss.VirtualMachineProfile.ExtensionProfile.Extensions, out ext))
{
throw new InvalidOperationException(string.Format(ServiceFabricProperties.Resources.SFExtensionNotFoundInVMSS, vmss.Name, vmss.Id));
}

var cluster = GetCurrentCluster();
var nodeType = GetNodeType(cluster, this.NodeType);
var oldDurabilityLevel = GetDurabilityLevel(nodeType.DurabilityLevel);
var newDurabilityLevel = this.DurabilityLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected virtual int Number
public override void ExecuteCmdlet()
{
var cluster = GetCurrentCluster();
var vmss = GetVmss(this.NodeType);
var vmss = GetVmss(this.NodeType, cluster.ClusterId);
var nodeType = GetNodeType(cluster, this.NodeType);
var durabilityLevel = (DurabilityLevel)Enum.Parse(typeof(DurabilityLevel), nodeType.DurabilityLevel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected VirtualMachineScaleSet GetPrimaryVmss()
var clusterRes = GetCurrentCluster();
var nodeTypes = clusterRes.NodeTypes;
var primaryNodeType = nodeTypes.Single(nt => nt.IsPrimary);
var vmss = GetVmss(primaryNodeType.Name);
var vmss = GetVmss(primaryNodeType.Name, clusterRes.ClusterId);
return vmss;
}
}
Expand Down

0 comments on commit 29b18d0

Please sign in to comment.