Skip to content

Use DOTNET_STARTUP_HOOKS and Harmony to hotpatch UBT and UAT, and add support for Kubernetes-based UBA #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions UET/Redpoint.Uet.Patching.Runtime/ConsoleUetPatchLogging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Redpoint.Uet.Patching.Runtime
{
internal class ConsoleUetPatchLogging : IUetPatchLogging
{
public void LogInfo(string message)
{
Console.WriteLine($"Redpoint.Uet.Patching.Runtime [info ] {message}");
}

public void LogWarning(string message)
{
Console.WriteLine($"Redpoint.Uet.Patching.Runtime [warn ] {message}");
}

public void LogError(string message)
{
Console.WriteLine($"Redpoint.Uet.Patching.Runtime [error] {message}");
}
}
}
11 changes: 11 additions & 0 deletions UET/Redpoint.Uet.Patching.Runtime/IUetPatchLogging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Redpoint.Uet.Patching.Runtime
{
internal interface IUetPatchLogging
{
void LogInfo(string message);

void LogWarning(string message);

void LogError(string message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Redpoint.Uet.Patching.Runtime.Kubernetes
{
internal interface IKubernetesUbaConfig
{
string? Namespace { get; }
string? Context { get; }
string? SmbServer { get; }
string? SmbShare { get; }
string? SmbUsername { get; }
string? SmbPassword { get; }

public const ulong MemoryBytesPerCore = 1610612736uL;
}
}
106 changes: 106 additions & 0 deletions UET/Redpoint.Uet.Patching.Runtime/Kubernetes/KubernetesNodeState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
namespace Redpoint.Uet.Patching.Runtime.Kubernetes
{
using System;
using System.Collections.Generic;
using System.Linq;
using k8s.Models;

internal class KubernetesNodeState
{
public string? NodeId;
public V1Node? KubernetesNode;
public List<V1Pod> KubernetesPods = new List<V1Pod>();
public readonly List<KubernetesNodeWorker> AllocatedBlocks = new List<KubernetesNodeWorker>();

public ulong MemoryTotal
{
get
{
return KubernetesNode!.Status.Capacity["memory"].ToUInt64();
}
}

public ulong MemoryNonUba
{
get
{
return KubernetesPods
.Where(x => x.GetLabel("uba") != "true")
.SelectMany(x => x.Spec.Containers)
.Select(x => x?.Resources?.Requests != null && x.Resources.Requests.TryGetValue("memory", out var quantity) ? quantity.ToUInt64() : 0)
.Aggregate((a, b) => a + b);
}
}

public ulong MemoryAllocated
{
get
{
return AllocatedBlocks
.Select(x => (ulong)x.AllocatedCores * IKubernetesUbaConfig.MemoryBytesPerCore)
.DefaultIfEmpty((ulong)0)
.Aggregate((a, b) => a + b);
}
}

public ulong MemoryAvailable
{
get
{
var memory = MemoryTotal;
memory -= MemoryNonUba;
memory -= MemoryAllocated;
return Math.Max(0, memory);
}
}

public double CoresTotal
{
get
{
return Math.Floor(KubernetesNode!.Status.Capacity["cpu"].ToDouble());
}
}

public double CoresNonUba
{
get
{
return KubernetesPods
.Where(x => x.GetLabel("uba") != "true")
.SelectMany(x => x.Spec.Containers)
.Select(x => x?.Resources?.Requests != null && x.Resources.Requests.TryGetValue("cpu", out var quantity) ? quantity.ToDouble() : 0)
.Sum();
}
}

public double CoresAllocated
{
get
{
return AllocatedBlocks.Sum(x => x.AllocatedCores);
}
}

public int CoresAvailable
{
get
{
var cores = CoresTotal;
cores -= CoresNonUba;
cores -= CoresAllocated;
return (int)Math.Max(0, Math.Floor(cores));
}
}

public int CoresAllocatable
{
get
{
var realCoresAvailable = CoresAvailable;
var memoryConstrainedCoresAvailable = MemoryAvailable / IKubernetesUbaConfig.MemoryBytesPerCore;
return Math.Min(realCoresAvailable, (int)memoryConstrainedCoresAvailable);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Redpoint.Uet.Patching.Runtime.Kubernetes
{
using k8s.Models;

internal class KubernetesNodeWorker
{
public V1Pod? KubernetesPod;
public V1Service? KubernetesService;
public int AllocatedCores;
public string? UbaHost;
public int? UbaPort;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Redpoint.Uet.Patching.Runtime.Kubernetes
{
using System.Reflection;

internal class KubernetesUbaConfigFromHook : IKubernetesUbaConfig
{
private readonly object _ubaKubeConfig;

public KubernetesUbaConfigFromHook(object ubtHookObject)
{
var field = ubtHookObject.GetType().GetField("_ubaKubeConfig", BindingFlags.NonPublic | BindingFlags.Instance);
_ubaKubeConfig = field!.GetValue(ubtHookObject)!;
}

private string? GetPropertyValue(string property)
{
return _ubaKubeConfig.GetType()
.GetProperty(property, BindingFlags.Public | BindingFlags.Instance)
?.GetValue(_ubaKubeConfig) as string;
}

public string? Namespace => GetPropertyValue("Namespace");

public string? Context => GetPropertyValue("Context");

public string? SmbServer => GetPropertyValue("SmbServer");

public string? SmbShare => GetPropertyValue("SmbShare");

public string? SmbUsername => GetPropertyValue("SmbUsername");

public string? SmbPassword => GetPropertyValue("SmbPassword");
}
}
Loading
Loading