Skip to content

Commit

Permalink
Passing function app triggers string from KuduLite to buildctl comman…
Browse files Browse the repository at this point in the history
…d tool (#114)

* Json patch method name change and converting Json patch to string

* Correcting build version for the function trigger json patch

* Double quote escape and printing buildctl output

* Trigger type for KEDA scaler
  • Loading branch information
SatishRanjan authored Apr 9, 2020
1 parent 36cd4c4 commit 6d9a031
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
38 changes: 36 additions & 2 deletions Kudu.Core/Functions/KedaFunctionTriggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bool IsFunctionJson(string fullName)
return fullName.EndsWith(Constants.FunctionsConfigFile) &&
fullName.Count(c => c == '/' || c == '\\') == 1;
}

return kedaScaleTriggers;
}

Expand Down Expand Up @@ -85,7 +85,7 @@ public IEnumerable<ScaleTrigger> ParseFunctionJson(string functionName, string f
{
var scaleTrigger = new ScaleTrigger
{
Type = type,
Type = GetKedaTriggerType(type),
Metadata = new Dictionary<string, string>()
};
foreach (var property in binding)
Expand Down Expand Up @@ -113,5 +113,39 @@ private static string GetFunctionName(ZipArchiveEntry zipEnetry)

return zipEnetry.FullName.Split('/').Length == 2 ? zipEnetry.FullName.Split('/')[0] : zipEnetry.FullName.Split('\\')[0];
}

private static string GetKedaTriggerType(string triggerType)
{
if (string.IsNullOrEmpty(triggerType))
{
throw new ArgumentNullException(nameof(triggerType));
}

triggerType = triggerType.ToLower();

switch (triggerType)
{
case "queuetrigger":
return "azure-queue";

case "kafkatrigger":
return "kafka";

case "blobtrigger":
return "azure-blob";

case "servicebustrigger":
return "azure-servicebus";

case "eventhubtrigger":
return "azure-eventhub";

case "rabbitmqtrigger":
return "rabbitmq";

default:
return triggerType;
}
}
}
}
2 changes: 1 addition & 1 deletion Kudu.Core/Infrastructure/DockerContainerRestartTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void RequestContainerRestart(IEnvironment environment, string reas
//Only for function apps functionTriggers will be non-null/non-empty
if (functionTriggers?.Any() == true)
{
K8SEDeploymentHelper.UpdateFunctionApp(appName, functionTriggers, buildNumber);
K8SEDeploymentHelper.UpdateFunctionAppTriggers(appName, functionTriggers, $"{buildNumber}|{appName}");
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Kudu.Core/K8SE/BuildCtlArgumentsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal static void AddAppPropertyValueArgument(StringBuilder args, string appP
args.AppendFormat(" -appPropertyValue {0}", appPropertyValue);
}

internal static void AddJsonToPatchValueArgument(StringBuilder args, string jsonToPatch)
internal static void AddFunctionTriggersJsonToPatchValueArgument(StringBuilder args, string jsonToPatch)
{
args.AppendFormat(" -jsonToPatch {0}", jsonToPatch);
}
Expand Down
10 changes: 7 additions & 3 deletions Kudu.Core/K8SE/K8SEDeploymentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,20 @@ public static void UpdateFunctionAppTriggers(string appName, IEnumerable<ScaleTr
var cmd = new StringBuilder();
BuildCtlArgumentsHelper.AddBuildCtlCommand(cmd, "updatejson");
BuildCtlArgumentsHelper.AddAppNameArgument(cmd, appName);
BuildCtlArgumentsHelper.AddFunctionAppTriggerToPatchValueArgument(cmd, functionAppPatchJson);
BuildCtlArgumentsHelper.AddFunctionTriggersJsonToPatchValueArgument(cmd, functionAppPatchJson);
RunBuildCtlCommand(cmd.ToString(), "Updating function app triggers...");
}

private static string RunBuildCtlCommand(string args, string msg)
{
var escapedArgs = args.Replace("\"", "\\\"");
Console.WriteLine($"{msg} : {args}");
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{args}\"",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
Expand All @@ -94,6 +95,7 @@ private static string RunBuildCtlCommand(string args, string msg)
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
Console.WriteLine($"buildctl output:\n {output}");
process.WaitForExit();

if (string.IsNullOrEmpty(error))
Expand Down Expand Up @@ -147,7 +149,9 @@ private static string GetFunctionAppPatchJson(IEnumerable<ScaleTrigger> function
}
};

return JsonConvert.SerializeObject(patchAppJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
var patchJson = JsonConvert.SerializeObject(patchAppJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
var patchJsonString = JsonConvert.ToString(patchJson);
return patchJsonString;
}
}
}

0 comments on commit 6d9a031

Please sign in to comment.