From 8d12bc45a1ba386c2ad890e1f006701ef5929d30 Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 8 Nov 2023 11:49:21 +0100 Subject: [PATCH] Loops in kubeconfig and namespace for discord bot. --- .../DiscordBotContainerRecipe.cs | 4 ++++ .../DiscordBotStartupConfig.cs | 4 +++- Tools/BiblioTech/Configuration.cs | 6 ++++++ Tools/BiblioTech/DeploymentsFilesMonitor.cs | 13 ++++++++++++- Tools/BiblioTech/Program.cs | 4 ++-- Tools/CodexNetDeployer/Deployer.cs | 3 ++- 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs b/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs index c65dd9c..a598d78 100644 --- a/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs +++ b/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs @@ -16,12 +16,16 @@ namespace CodexDiscordBotPlugin AddEnvVar("SERVERNAME", config.ServerName); AddEnvVar("ADMINROLE", config.AdminRoleName); AddEnvVar("ADMINCHANNELNAME", config.AdminChannelName); + AddEnvVar("KUBECONFIG", "/opt/kubeconfig.yaml"); + AddEnvVar("KUBENAMESPACE", config.KubeNamespace); if (!string.IsNullOrEmpty(config.DataPath)) { AddEnvVar("DATAPATH", config.DataPath); AddVolume(config.DataPath, 1.GB()); } + + AddVolume(name: "kubeconfig", mountPath: "/opt/kubeconfig.yaml", subPath: "kubeconfig.yaml", secret: "codex-dist-tests-app-kubeconfig"); } } } diff --git a/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotStartupConfig.cs b/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotStartupConfig.cs index bb00a8d..e590cfa 100644 --- a/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotStartupConfig.cs +++ b/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotStartupConfig.cs @@ -2,13 +2,14 @@ { public class DiscordBotStartupConfig { - public DiscordBotStartupConfig(string name, string token, string serverName, string adminRoleName, string adminChannelName) + public DiscordBotStartupConfig(string name, string token, string serverName, string adminRoleName, string adminChannelName, string kubeNamespace) { Name = name; Token = token; ServerName = serverName; AdminRoleName = adminRoleName; AdminChannelName = adminChannelName; + KubeNamespace = kubeNamespace; } public string Name { get; } @@ -16,6 +17,7 @@ public string ServerName { get; } public string AdminRoleName { get; } public string AdminChannelName { get; } + public string KubeNamespace { get; } public string? DataPath { get; set; } } } diff --git a/Tools/BiblioTech/Configuration.cs b/Tools/BiblioTech/Configuration.cs index 10e134e..eb58870 100644 --- a/Tools/BiblioTech/Configuration.cs +++ b/Tools/BiblioTech/Configuration.cs @@ -19,6 +19,12 @@ namespace BiblioTech [Uniform("admin-channel-name", "ac", "ADMINCHANNELNAME", true, "Name of the Discord server channel where admin commands are allowed.")] public string AdminChannelName { get; set; } = "admin-channel"; + [Uniform("kube-config", "kc", "KUBECONFIG", true, "Path to Kubeconfig file. Use a Kubeconfig with read-only access.")] + public string KubeConfigFile { get; set; } = "null"; + + [Uniform("kube-namespace", "kn", "KUBENAMESPACE", true, "Kubernetes namespace.")] + public string KubeNamespace { get; set; } = string.Empty; + public string EndpointsPath { get diff --git a/Tools/BiblioTech/DeploymentsFilesMonitor.cs b/Tools/BiblioTech/DeploymentsFilesMonitor.cs index 9278574..0092034 100644 --- a/Tools/BiblioTech/DeploymentsFilesMonitor.cs +++ b/Tools/BiblioTech/DeploymentsFilesMonitor.cs @@ -28,7 +28,7 @@ namespace BiblioTech try { var deploy = JsonConvert.DeserializeObject(str); - if (deploy != null) + if (IsDeploymentOk(deploy)) { var targetFile = Path.Combine(Program.Config.EndpointsPath, Guid.NewGuid().ToString().ToLowerInvariant() + ".json"); File.WriteAllText(targetFile, str); @@ -59,6 +59,17 @@ namespace BiblioTech return false; } + private bool IsDeploymentOk(CodexDeployment? deploy) + { + if (deploy == null) return false; + if (deploy.CodexInstances == null) return false; + if (!deploy.CodexInstances.Any()) return false; + if (!deploy.CodexInstances.All(i => i.Containers != null && i.Info != null)) return false; + if (deploy.GethDeployment == null) return false; + if (deploy.GethDeployment.Containers == null) return false; + return true; + } + private void LoadDeployments() { var path = Program.Config.EndpointsPath; diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index ecd9147..4bb1175 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -41,10 +41,10 @@ namespace BiblioTech ProjectPlugin.Load(); var entryPoint = new EntryPoint(new ConsoleLog(), new KubernetesWorkflow.Configuration( - kubeConfigFile: null, + kubeConfigFile: Config.KubeConfigFile, operationTimeout: TimeSpan.FromMinutes(5), retryDelay: TimeSpan.FromSeconds(10), - kubernetesNamespace: "not-applicable"), "datafiles"); + kubernetesNamespace: Config.KubeNamespace), "datafiles"); var ci = entryPoint.CreateInterface(); diff --git a/Tools/CodexNetDeployer/Deployer.cs b/Tools/CodexNetDeployer/Deployer.cs index ff2dd6f..60415ac 100644 --- a/Tools/CodexNetDeployer/Deployer.cs +++ b/Tools/CodexNetDeployer/Deployer.cs @@ -131,7 +131,8 @@ namespace CodexNetDeployer token: config.DiscordBotToken, serverName: config.DiscordBotServerName, adminRoleName: config.DiscordBotAdminRoleName, - adminChannelName: config.DiscordBotAdminChannelName) + adminChannelName: config.DiscordBotAdminChannelName, + kubeNamespace: config.KubeNamespace) { DataPath = config.DiscordBotDataPath });