Files
Entkube/src/EntKube.Web/Services/IKubernetesClientFactory.cs
2026-05-21 16:57:50 +02:00

57 lines
2.6 KiB
C#

namespace EntKube.Web.Services;
/// <summary>
/// Abstraction over Kubernetes API operations that CnpgService needs.
/// Allows testing orchestration logic without real K8s clusters.
///
/// Implementations use the k8s .NET client or kubectl under the hood,
/// applying manifests (CNPG Cluster CRDs, Backup CRDs, Secrets) and
/// executing SQL via kubectl exec against the primary pod.
/// </summary>
public interface IKubernetesClientFactory
{
/// <summary>
/// Applies a YAML manifest to a Kubernetes cluster using the given kubeconfig.
/// Equivalent to "kubectl apply -f -" with the manifest piped in.
/// </summary>
Task ApplyManifestAsync(string manifest, string kubeconfig, CancellationToken ct = default);
/// <summary>
/// Deletes a specific Kubernetes resource by kind/name/namespace.
/// Equivalent to "kubectl delete {kind} {name} -n {namespace}".
/// </summary>
Task DeleteManifestAsync(string kind, string name, string ns, string kubeconfig, CancellationToken ct = default);
/// <summary>
/// Ensures a namespace exists, creating it if it doesn't.
/// Equivalent to "kubectl create namespace {ns} --dry-run=client -o yaml | kubectl apply -f -".
/// </summary>
Task EnsureNamespaceAsync(string ns, string kubeconfig, CancellationToken ct = default);
/// <summary>
/// Gets the JSON output of a kubectl command. Used for querying cluster/pod status.
/// </summary>
Task<string> GetJsonAsync(string resource, string ns, string kubeconfig, string labelSelector = "", CancellationToken ct = default);
/// <summary>
/// Executes a SQL statement against the CNPG primary pod.
/// Uses kubectl exec to connect to the primary and run psql.
/// </summary>
Task ExecuteSqlAsync(string clusterName, string ns, string sql, string kubeconfig, CancellationToken ct = default);
/// <summary>
/// Executes a MongoDB script against the primary pod via kubectl exec + mongosh.
/// The primary pod is the first StatefulSet member: {clusterName}-0.
/// When username and password are provided, mongosh connects with SCRAM credentials.
/// </summary>
Task ExecuteMongoAsync(string clusterName, string ns, string script, string kubeconfig,
string? username = null, string? password = null, CancellationToken ct = default);
/// <summary>
/// Reads a single key from a Kubernetes Secret and returns the decoded value.
/// Returns null if the secret or key does not exist.
/// </summary>
Task<string?> GetSecretValueAsync(string secretName, string key, string ns, string kubeconfig,
CancellationToken ct = default);
}