-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add/Remove handlers without code changes (#316)
## Motivation and Context (Why the change? What's the scenario?) The current KM Service definition requires editing and rebuilding code/images in order to add/remove pipeline handlers. This PR allows to add/remove handlers simply by editing the configuration files, providing the required assembly without the need to rebuild. ## High level description (Approach, Design) * Remove from KM Builder the hardcoded list of handlers used for the service. The list remains only for the serverless option. * Add to service configuration the list of handlers to load. * Us reflection to load handlers from assemblies.
- Loading branch information
Showing
31 changed files
with
909 additions
and
541 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
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
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,60 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Reflection; | ||
using Microsoft.KernelMemory.Configuration; | ||
using Microsoft.KernelMemory.Pipeline; | ||
|
||
namespace Microsoft.KernelMemory.Handlers; | ||
|
||
internal static class HandlerTypeLoader | ||
{ | ||
internal static bool TryGetHandlerType(HandlerConfig config, [NotNullWhen(true)] out Type? handlerType) | ||
{ | ||
handlerType = null; | ||
|
||
// If part of the config is empty, the handler is disabled | ||
if (string.IsNullOrEmpty(config.Class) || string.IsNullOrEmpty(config.Assembly)) | ||
{ | ||
return false; | ||
} | ||
|
||
// Search the assembly in a few directories | ||
var path = string.Empty; | ||
var assemblyFilePaths = new HashSet<string> | ||
{ | ||
config.Assembly, | ||
Path.Join(Environment.CurrentDirectory, config.Assembly), | ||
Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), config.Assembly), | ||
}; | ||
|
||
foreach (var p in assemblyFilePaths) | ||
{ | ||
if (!File.Exists(p)) { continue; } | ||
|
||
path = p; | ||
break; | ||
} | ||
|
||
// Check if the assembly exists | ||
if (string.IsNullOrEmpty(path)) | ||
{ | ||
throw new ConfigurationException($"Handler assembly not found: {config.Assembly}"); | ||
} | ||
|
||
Assembly assembly = Assembly.LoadFrom(path); | ||
|
||
// IPipelineStepHandler | ||
handlerType = assembly.GetType(config.Class); | ||
|
||
if (!typeof(IPipelineStepHandler).IsAssignableFrom(handlerType)) | ||
{ | ||
throw new ConfigurationException($"Invalid handler definition: `{config.Class}` class doesn't implement interface {nameof(IPipelineStepHandler)}"); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,28 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
namespace Microsoft.KernelMemory.Configuration; | ||
|
||
public class HandlerConfig | ||
{ | ||
/// <summary> | ||
/// .NET assembly containing the handler class | ||
/// </summary> | ||
public string Assembly { get; set; } | ||
|
||
/// <summary> | ||
/// .NET class in the assembly, containing the handler logic | ||
/// </summary> | ||
public string Class { get; set; } | ||
|
||
public HandlerConfig() | ||
{ | ||
this.Assembly = string.Empty; | ||
this.Class = string.Empty; | ||
} | ||
|
||
public HandlerConfig(string assembly, string className) | ||
{ | ||
this.Assembly = assembly; | ||
this.Class = className; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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
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
Oops, something went wrong.