2023-03-27 12:49:34 +00:00
|
|
|
|
using CodexDistTestCore.Config;
|
|
|
|
|
using k8s.Models;
|
|
|
|
|
|
2023-04-10 07:05:27 +00:00
|
|
|
|
namespace CodexDistTestCore.Metrics
|
2023-03-27 12:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
public class K8sPrometheusSpecs
|
|
|
|
|
{
|
|
|
|
|
public const string ContainerName = "dtest-prom";
|
|
|
|
|
public const string ConfigFilepath = "/etc/prometheus/prometheus.yml";
|
2023-03-27 14:24:04 +00:00
|
|
|
|
private const string dockerImage = "thatbenbierens/prometheus-envconf:latest";
|
2023-03-27 12:49:34 +00:00
|
|
|
|
private const string portName = "prom-1";
|
2023-03-27 14:24:04 +00:00
|
|
|
|
private readonly string config;
|
|
|
|
|
|
2023-03-28 11:43:25 +00:00
|
|
|
|
public K8sPrometheusSpecs(int servicePort, int prometheusNumber, string config)
|
2023-03-27 14:24:04 +00:00
|
|
|
|
{
|
|
|
|
|
ServicePort = servicePort;
|
2023-03-28 11:43:25 +00:00
|
|
|
|
PrometheusNumber = prometheusNumber;
|
2023-03-27 14:24:04 +00:00
|
|
|
|
this.config = config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int ServicePort { get; }
|
2023-03-28 11:43:25 +00:00
|
|
|
|
public int PrometheusNumber { get; }
|
2023-03-27 12:49:34 +00:00
|
|
|
|
|
|
|
|
|
public string GetDeploymentName()
|
|
|
|
|
{
|
2023-03-28 11:43:25 +00:00
|
|
|
|
return "test-prom" + PrometheusNumber;
|
2023-03-27 12:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public V1Deployment CreatePrometheusDeployment()
|
|
|
|
|
{
|
|
|
|
|
var deploymentSpec = new V1Deployment
|
|
|
|
|
{
|
|
|
|
|
ApiVersion = "apps/v1",
|
2023-04-10 07:05:27 +00:00
|
|
|
|
Metadata = new V1ObjectMeta
|
2023-03-27 12:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
Name = GetDeploymentName(),
|
|
|
|
|
NamespaceProperty = K8sCluster.K8sNamespace
|
|
|
|
|
},
|
|
|
|
|
Spec = new V1DeploymentSpec
|
|
|
|
|
{
|
|
|
|
|
Replicas = 1,
|
|
|
|
|
Selector = new V1LabelSelector
|
|
|
|
|
{
|
|
|
|
|
MatchLabels = CreateSelector()
|
|
|
|
|
},
|
|
|
|
|
Template = new V1PodTemplateSpec
|
|
|
|
|
{
|
|
|
|
|
Metadata = new V1ObjectMeta
|
|
|
|
|
{
|
|
|
|
|
Labels = CreateSelector()
|
|
|
|
|
},
|
|
|
|
|
Spec = new V1PodSpec
|
|
|
|
|
{
|
|
|
|
|
Containers = new List<V1Container>
|
|
|
|
|
{
|
|
|
|
|
new V1Container
|
|
|
|
|
{
|
|
|
|
|
Name = ContainerName,
|
|
|
|
|
Image = dockerImage,
|
|
|
|
|
Ports = new List<V1ContainerPort>
|
|
|
|
|
{
|
|
|
|
|
new V1ContainerPort
|
|
|
|
|
{
|
|
|
|
|
ContainerPort = 9090,
|
|
|
|
|
Name = portName
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-27 14:24:04 +00:00
|
|
|
|
Env = new List<V1EnvVar>
|
2023-03-27 12:49:34 +00:00
|
|
|
|
{
|
2023-03-27 14:24:04 +00:00
|
|
|
|
new V1EnvVar
|
|
|
|
|
{
|
|
|
|
|
Name = "PROM_CONFIG",
|
|
|
|
|
Value = config
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-27 12:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return deploymentSpec;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-27 14:24:04 +00:00
|
|
|
|
public V1Service CreatePrometheusService()
|
2023-03-27 12:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
var serviceSpec = new V1Service
|
|
|
|
|
{
|
|
|
|
|
ApiVersion = "v1",
|
|
|
|
|
Metadata = new V1ObjectMeta
|
|
|
|
|
{
|
2023-03-28 11:43:25 +00:00
|
|
|
|
Name = "codex-prom-service" + PrometheusNumber,
|
2023-03-27 12:49:34 +00:00
|
|
|
|
NamespaceProperty = K8sCluster.K8sNamespace
|
|
|
|
|
},
|
|
|
|
|
Spec = new V1ServiceSpec
|
|
|
|
|
{
|
|
|
|
|
Type = "NodePort",
|
|
|
|
|
Selector = CreateSelector(),
|
|
|
|
|
Ports = new List<V1ServicePort>
|
|
|
|
|
{
|
|
|
|
|
new V1ServicePort
|
|
|
|
|
{
|
2023-03-28 11:43:25 +00:00
|
|
|
|
Name = "prom-service" + PrometheusNumber,
|
2023-03-27 12:49:34 +00:00
|
|
|
|
Protocol = "TCP",
|
|
|
|
|
Port = 9090,
|
|
|
|
|
TargetPort = portName,
|
2023-03-27 14:24:04 +00:00
|
|
|
|
NodePort = ServicePort
|
2023-03-27 12:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return serviceSpec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, string> CreateSelector()
|
|
|
|
|
{
|
|
|
|
|
return new Dictionary<string, string> { { "test-prom", "dtest-prom" } };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|