Strategy to unprovision old applicationtype versions in Service Fabric

非 Y 不嫁゛ 提交于 2019-12-03 20:33:27

问题


Is there a way to set some sort of configuration on the cluster to remove service fabric application type versions? Like only keep the last 5 versions or something?

For example i have CI/CD deploying new versions of a service fabric app to our cluster, it leaves a bunch of application version types in the cluster. Is there a way to automatically unprovision them over time or only keep a certain number of versions?


回答1:


There are two options that cross my mind -

  • Specify UnregisterUnusedApplicationVersionsAfterUpgrade = $true when you execute Deploy-FabricApplication.ps1. This parameter indicates whether to unregister any unused application versions that exist after an upgrade is finished.

  • Add custom script into your release defintion, deployment script or whereever you want that will resolve all the deployed app types and unprovision those ones that you think are obsolete. Here is the command that you will need to use - Unregister-ServiceFabricApplicationType. Here is some example of the script that unregisters all the app types except running ones -

    #resolve all app types
    $appTypes = Get-ServiceFabricApplicationType
    foreach($appType in $appTypes)
    {
       #try to find the match with any of installed applications
       $match = Get-ServiceFabricApplication -ApplicationTypeName $appType.ApplicationTypeName | Where-Object {$_.ApplicationTypeVersion -eq $appType.ApplicationTypeVersion}
       if(!$match)
       {
           Write-Host "Deleting $($appType.ApplicationTypeName) $($appType.ApplicationTypeVersion)"
           Unregister-ServiceFabricApplicationType -ApplicationTypeName $appType.ApplicationTypeName -ApplicationTypeVersion $appType.ApplicationTypeVersion -Force -Confirm
       }    
    }
    



回答2:


I expanded on Kiyrl's answer keeping the currently deployed version + n history'

#resolve all app types
$appTypes = Get-ServiceFabricApplicationType
$deployedAppArray = @()
foreach($appType in $appTypes){    
    #try to find the match with any of installed applications
    $match = Get-ServiceFabricApplication -ApplicationTypeName $appType.ApplicationTypeName | Where-Object {$_.ApplicationTypeVersion -eq $appType.ApplicationTypeVersion}
    if(!$match)
    {
        $oldApp = new-object psobject -property @{
            ApplicationTypeName = $appType.ApplicationTypeName
            ApplicationTypeVersion  = $appType.ApplicationTypeVersion 
        }
        $deployedAppArray += $oldApp
    }    
}

$countToKeep = 2 # keeps this many in addition + currently deployed
$uniqueAppTypes = $deployedAppArray | Group-Object "ApplicationTypeName" | Where-Object { $_.Count -gt $countToKeep } | Select-Object -ExpandProperty Name

foreach($appType in $uniqueAppTypes){ 
    $versionsToRemove = $deployedAppArray | Where-Object {$_.ApplicationTypeName -eq $appType}
    $toRemoveCount =  $versionsToRemove.Length - $countToKeep
    $versionsToRemove = $versionsToRemove | Select-Object -First $toRemoveCount

    foreach($appToRemove in $versionsToRemove){
        Write-Host "Removing $($appToRemove.ApplicationTypeName) $($appToRemove.ApplicationTypeVersion)"
        Unregister-ServiceFabricApplicationType -ApplicationTypeName $appToRemove.ApplicationTypeName -ApplicationTypeVersion $appToRemove.ApplicationTypeVersion -Force
    }
}


来源:https://stackoverflow.com/questions/45467800/strategy-to-unprovision-old-applicationtype-versions-in-service-fabric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!