Skip to content

Commit 9e37904

Browse files
authored
Merge pull request #11 from ucloud/feature/exporter
support redis exporter
2 parents 1e949f4 + f3a8c1b commit 9e37904

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ replace (
3737
// resolve it correctly.
3838
github.com/prometheus/prometheus => github.com/prometheus/prometheus v0.0.0-20190525122359-d20e84d0fb64
3939
)
40+
41+
go 1.13

pkg/apis/redis/v1alpha1/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,7 @@ const (
7171

7272
JobTypeBackup = "backup"
7373
JobTypeRestore = "restore"
74+
75+
PrometheusExporterPortNumber = 9100
76+
PrometheusExporterTelemetryPath = "/metrics"
7477
)

pkg/apis/redis/v1alpha1/default.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ func (in *DistributedRedisCluster) Validate() {
3636
if in.Spec.Resources == nil || in.Spec.Resources.Size() == 0 {
3737
in.Spec.Resources = defaultResource()
3838
}
39+
40+
mon := in.Spec.Monitor
41+
if mon != nil {
42+
if mon.Prometheus == nil {
43+
mon.Prometheus = &PrometheusSpec{}
44+
}
45+
if mon.Prometheus.Port == 0 {
46+
mon.Prometheus.Port = PrometheusExporterPortNumber
47+
}
48+
if in.Spec.Annotations == nil {
49+
in.Spec.Annotations = make(map[string]string)
50+
}
51+
52+
in.Spec.Annotations["prometheus.io/scrape"] = "true"
53+
in.Spec.Annotations["prometheus.io/path"] = PrometheusExporterTelemetryPath
54+
in.Spec.Annotations["prometheus.io/port"] = fmt.Sprintf("%d", mon.Prometheus.Port)
55+
}
3956
}
4057

4158
func defaultResource() *v1.ResourceRequirements {

pkg/apis/redis/v1alpha1/distributedrediscluster_types.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,53 @@ type DistributedRedisClusterSpec struct {
2929
Storage *RedisStorage `json:"storage,omitempty"`
3030
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
3131
PasswordSecret *corev1.LocalObjectReference `json:"rootPasswordSecret,omitempty"`
32+
Monitor *AgentSpec `json:"monitor,omitempty"`
33+
}
34+
35+
type AgentSpec struct {
36+
Image string `json:"image,omitempty"`
37+
Prometheus *PrometheusSpec `json:"prometheus,omitempty"`
38+
// Arguments to the entrypoint.
39+
// The docker image's CMD is used if this is not provided.
40+
// Variable references $(VAR_NAME) are expanded using the container's environment. If a variable
41+
// cannot be resolved, the reference in the input string will be unchanged. The $(VAR_NAME) syntax
42+
// can be escaped with a double $$, ie: $$(VAR_NAME). Escaped references will never be expanded,
43+
// regardless of whether the variable exists or not.
44+
// Cannot be updated.
45+
// More info: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
46+
// +optional
47+
Args []string `json:"args,omitempty"`
48+
// List of environment variables to set in the container.
49+
// Cannot be updated.
50+
// +optional
51+
// +patchMergeKey=name
52+
// +patchStrategy=merge
53+
Env []corev1.EnvVar `json:"env,omitempty" patchStrategy:"merge" patchMergeKey:"name"`
54+
// Compute Resources required by exporter container.
55+
// Cannot be updated.
56+
// More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/
57+
// +optional
58+
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
59+
// Security options the pod should run with.
60+
// More info: https://kubernetes.io/docs/concepts/policy/security-context/
61+
// More info: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
62+
// +optional
63+
SecurityContext *corev1.SecurityContext `json:"securityContext,omitempty"`
64+
}
65+
66+
type PrometheusSpec struct {
67+
// Port number for the exporter side car.
68+
Port int32 `json:"port,omitempty"`
69+
70+
// Namespace of Prometheus. Service monitors will be created in this namespace.
71+
Namespace string `json:"namespace,omitempty"`
72+
// Labels are key value pairs that is used to select Prometheus instance via ServiceMonitor labels.
73+
// +optional
74+
Labels map[string]string `json:"labels,omitempty"`
75+
76+
// Interval at which metrics should be scraped
77+
Interval string `json:"interval,omitempty"`
78+
//Annotations map[string]string `json:"annotations,omitempty"`
3279
}
3380

3481
// RedisStorage defines the structure used to store the Redis Data

pkg/resources/statefulsets/statefulset.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func NewStatefulSetForCR(cluster *redisv1alpha1.DistributedRedisCluster, labels
4949
},
5050
Template: corev1.PodTemplateSpec{
5151
ObjectMeta: metav1.ObjectMeta{
52-
Labels: labels,
52+
Labels: labels,
53+
Annotations: cluster.Spec.Annotations,
5354
},
5455
Spec: corev1.PodSpec{
5556
Affinity: getAffinity(spec.Affinity, labels),
@@ -74,6 +75,9 @@ func NewStatefulSetForCR(cluster *redisv1alpha1.DistributedRedisCluster, labels
7475
ss.Spec.VolumeClaimTemplates[0].OwnerReferences = redisv1alpha1.DefaultOwnerReferences(cluster)
7576
}
7677
}
78+
if spec.Monitor != nil {
79+
ss.Spec.Template.Spec.Containers = append(ss.Spec.Template.Spec.Containers, redisExporterContainer(cluster, password))
80+
}
7781
return ss
7882
}
7983

@@ -215,6 +219,32 @@ func redisServerContainer(cluster *redisv1alpha1.DistributedRedisCluster, passwo
215219
return container
216220
}
217221

222+
func redisExporterContainer(cluster *redisv1alpha1.DistributedRedisCluster, password *corev1.EnvVar) corev1.Container {
223+
container := corev1.Container{
224+
Name: "exporter",
225+
Args: append([]string{
226+
fmt.Sprintf("--web.listen-address=:%v", cluster.Spec.Monitor.Prometheus.Port),
227+
fmt.Sprintf("--web.telemetry-path=%v", redisv1alpha1.PrometheusExporterTelemetryPath),
228+
}, cluster.Spec.Monitor.Args...),
229+
Image: cluster.Spec.Monitor.Image,
230+
ImagePullPolicy: corev1.PullIfNotPresent,
231+
Ports: []corev1.ContainerPort{
232+
{
233+
Name: "prom-http",
234+
Protocol: corev1.ProtocolTCP,
235+
ContainerPort: cluster.Spec.Monitor.Prometheus.Port,
236+
},
237+
},
238+
Env: cluster.Spec.Monitor.Env,
239+
Resources: cluster.Spec.Monitor.Resources,
240+
SecurityContext: cluster.Spec.Monitor.SecurityContext,
241+
}
242+
if password != nil {
243+
container.Env = append(container.Env, *password)
244+
}
245+
return container
246+
}
247+
218248
func volumeMounts() []corev1.VolumeMount {
219249
return []corev1.VolumeMount{
220250
{

0 commit comments

Comments
 (0)