-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update "triggers" information for the function apps (#112)
* Update "triggers" information for the function apps * updating function trigger using new buildctl updatejson command * Adding buildNumber in the functionApp trigger patch json payload * Adding buildNumber in the functionApp trigger patch json payload * Updating the method name to include triggers name to it
- Loading branch information
1 parent
06fc670
commit 36cd4c4
Showing
12 changed files
with
347 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Kudu.Core.Functions | ||
{ | ||
using Newtonsoft.Json; | ||
|
||
public class CodeSpec | ||
{ | ||
[JsonProperty(PropertyName = "packageRef")] | ||
public PackageReference PackageRef { get; set; } | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace Kudu.Core.Functions | ||
{ | ||
public class FunctionTriggerProvider | ||
{ | ||
/// <summary> | ||
/// Returns the function triggers from function.json files | ||
/// </summary> | ||
/// <param name="providerName">The the returns json string differs as per the format expected based upon providers. | ||
/// e.g. for "KEDA" the json string is the serialzed string of IEnumerable<ScaleTrigger>object</param> | ||
/// <param name="functionzipFilePath">The functions file path</param> | ||
/// <returns>The josn string of triggers in function.json</returns> | ||
public static T GetFunctionTriggers<T>(string providerName, string functionzipFilePath) | ||
{ | ||
if (string.IsNullOrWhiteSpace(providerName)) | ||
{ | ||
return default; | ||
} | ||
|
||
switch (providerName.ToLower()) | ||
{ | ||
case "keda": | ||
var functionTriggerProvider = new KedaFunctionTriggerProvider(); | ||
return (T)functionTriggerProvider.GetFunctionTriggers(functionzipFilePath); | ||
default: | ||
functionTriggerProvider = new KedaFunctionTriggerProvider(); | ||
return (T)functionTriggerProvider.GetFunctionTriggers(functionzipFilePath); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Kudu.Core.Functions | ||
{ | ||
/// <summary> | ||
/// Returns "<see cref="IEnumerable<ScaleTrigger>"/> for KEDA scalers" | ||
/// </summary> | ||
public class KedaFunctionTriggerProvider | ||
{ | ||
public IEnumerable<ScaleTrigger> GetFunctionTriggers(string zipFilePath) | ||
{ | ||
if (!File.Exists(zipFilePath)) | ||
{ | ||
return null; | ||
} | ||
|
||
List<ScaleTrigger> kedaScaleTriggers = new List<ScaleTrigger>(); | ||
using (var zip = ZipFile.OpenRead(zipFilePath)) | ||
{ | ||
var entries = zip.Entries | ||
.Where(e => IsFunctionJson(e.FullName)); | ||
|
||
foreach (var entry in entries) | ||
{ | ||
using (var stream = entry.Open()) | ||
{ | ||
using (var reader = new StreamReader(stream)) | ||
{ | ||
var functionTriggers = ParseFunctionJson(GetFunctionName(entry), reader.ReadToEnd()); | ||
if (functionTriggers?.Any() == true) | ||
{ | ||
kedaScaleTriggers.AddRange(functionTriggers); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
bool IsFunctionJson(string fullName) | ||
{ | ||
return fullName.EndsWith(Constants.FunctionsConfigFile) && | ||
fullName.Count(c => c == '/' || c == '\\') == 1; | ||
} | ||
|
||
return kedaScaleTriggers; | ||
} | ||
|
||
public IEnumerable<ScaleTrigger> ParseFunctionJson(string functionName, string functionJson) | ||
{ | ||
var json = JObject.Parse(functionJson); | ||
if (json.TryGetValue("disabled", out JToken value)) | ||
{ | ||
string stringValue = value.ToString(); | ||
if (!bool.TryParse(stringValue, out bool disabled)) | ||
{ | ||
string expandValue = System.Environment.GetEnvironmentVariable(stringValue); | ||
disabled = string.Equals(expandValue, "1", StringComparison.OrdinalIgnoreCase) || | ||
string.Equals(expandValue, "true", StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
if (disabled) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
var excluded = json.TryGetValue("excluded", out value) && (bool)value; | ||
if (excluded) | ||
{ | ||
return null; | ||
} | ||
|
||
var triggers = new List<ScaleTrigger>(); | ||
foreach (JObject binding in (JArray)json["bindings"]) | ||
{ | ||
var type = (string)binding["type"]; | ||
if (type.EndsWith("Trigger", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var scaleTrigger = new ScaleTrigger | ||
{ | ||
Type = type, | ||
Metadata = new Dictionary<string, string>() | ||
}; | ||
foreach (var property in binding) | ||
{ | ||
if (property.Value.Type == JTokenType.String) | ||
{ | ||
scaleTrigger.Metadata.Add(property.Key, property.Value.ToString()); | ||
} | ||
} | ||
|
||
scaleTrigger.Metadata.Add("functionName", functionName); | ||
triggers.Add(scaleTrigger); | ||
} | ||
} | ||
|
||
return triggers; | ||
} | ||
|
||
private static string GetFunctionName(ZipArchiveEntry zipEnetry) | ||
{ | ||
if (string.IsNullOrWhiteSpace(zipEnetry?.FullName)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
return zipEnetry.FullName.Split('/').Length == 2 ? zipEnetry.FullName.Split('/')[0] : zipEnetry.FullName.Split('\\')[0]; | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Kudu.Core.Functions | ||
{ | ||
using Newtonsoft.Json; | ||
|
||
public class PackageReference | ||
{ | ||
[JsonProperty(PropertyName = "buildVersion")] | ||
public string BuildVersion { get; set; } | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Kudu.Core.Functions | ||
{ | ||
public class PatchAppJson | ||
{ | ||
[JsonProperty(PropertyName = "spec")] | ||
public PatchSpec PatchSpec { get; set; } | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Kudu.Core.Functions | ||
{ | ||
public class PatchSpec | ||
{ | ||
[JsonProperty(PropertyName = "triggerOptions")] | ||
public TriggerOptions TriggerOptions { get; set; } | ||
|
||
[JsonProperty(PropertyName = "code")] | ||
public CodeSpec Code { get; set; } | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
|
||
namespace Kudu.Core.Functions | ||
{ | ||
public class ScaleTrigger | ||
{ | ||
[JsonProperty(PropertyName = "type")] | ||
public string Type { get; set; } | ||
|
||
[JsonProperty(PropertyName = "metadata")] | ||
public IDictionary<string, string> Metadata { get; set; } | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
|
||
namespace Kudu.Core.Functions | ||
{ | ||
public class TriggerOptions | ||
{ | ||
[JsonProperty(PropertyName = "triggers")] | ||
public IEnumerable<ScaleTrigger> Triggers { get; set; } | ||
|
||
[JsonProperty(PropertyName = "pollingInterval")] | ||
public int? PollingInterval { get; set; } | ||
|
||
[JsonProperty(PropertyName = "cooldownPeriod")] | ||
public int? cooldownPeriod { get; set; } | ||
} | ||
} |
Oops, something went wrong.