Skip to content

Commit

Permalink
Added k8smetrics service monitor
Browse files Browse the repository at this point in the history
Signed-off-by: Kaustav Majumder <[email protected]>
  • Loading branch information
Kaustav Majumder committed Feb 20, 2024
1 parent 841c36a commit 938c3d7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
29 changes: 29 additions & 0 deletions controllers/ocsinitialization/ocsinitialization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ func (r *OCSInitializationReconciler) Reconcile(ctx context.Context, request rec
return reconcile.Result{}, err
}

err = r.reconcileK8sMetricsServiceMonitor(instance)
if err != nil {
r.Log.Error(err, "Failed to ensure k8sMetricsService Monitor")
return reconcile.Result{}, err
}

reason := ocsv1.ReconcileCompleted
message := ocsv1.ReconcileCompletedMessage
util.SetCompleteCondition(&instance.Status.Conditions, reason, message)
Expand Down Expand Up @@ -594,3 +600,26 @@ func (r *OCSInitializationReconciler) reconcilePrometheusProxyNetworkPolicy(init
r.Log.Info("Prometheus proxy network policy creation succeeded", "Name", promethuesProxyNetworkPolicy.Name)
return nil
}

func (r *OCSInitializationReconciler) reconcileK8sMetricsServiceMonitor(initialData *ocsv1.OCSInitialization) error {
var err error

k8sMetricsServiceMonitor := &promv1.ServiceMonitor{}
k8sMetricsServiceMonitor.Name = ""
k8sMetricsServiceMonitor.Namespace = initialData.Namespace

_, err = ctrl.CreateOrUpdate(r.ctx, r.Client, k8sMetricsServiceMonitor, func() error {
if err := ctrl.SetControllerReference(initialData, k8sMetricsServiceMonitor, r.Scheme); err != nil {
return err
}
k8sMetricsServiceMonitor.Spec = templates.K8sMetricsServiceMonitorTemplate.DeepCopy().Spec
return nil
})
if err != nil {
r.Log.Error(err, "Failed to create/update K8s Metrics Service Monitor")
return err
}
r.Log.Info("K8s Metrics Service Monitor creation succeeded", "Name", k8sMetricsServiceMonitor.Name)
return nil

}
70 changes: 70 additions & 0 deletions templates/k8smetricsservicemonitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package templates

import (
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var params = map[string][]string{
"match[]": {
"{__name__='kube_node_status_condition'}",
"{__name__='kube_persistentvolume_info'}",
"{__name__='kube_storageclass_info'}",
"{__name__='kube_persistentvolumeclaim_info'}",
"{__name__='kube_deployment_spec_replicas'}",
"{__name__='kube_pod_status_phase'}",
"{__name__='kubelet_volume_stats_capacity_bytes'}",
"{__name__='kubelet_volume_stats_used_bytes'}",
"{__name__='node_disk_read_time_seconds_total'}",
"{__name__='node_disk_write_time_seconds_total'}",
"{__name__='node_disk_reads_completed_total'}",
"{__name__='node_disk_writes_completed_total'}",
},
}

var K8sMetricsServiceMonitorTemplate = promv1.ServiceMonitor{
Spec: promv1.ServiceMonitorSpec{
Endpoints: []promv1.Endpoint{
{
Port: "web",
Path: "/federate",
Scheme: "https",
ScrapeTimeout: "1m",
Interval: "2m",
HonorLabels: true,
MetricRelabelConfigs: []*promv1.RelabelConfig{
{
Action: "labeldrop",
Regex: "prometheus_replica",
},
},
RelabelConfigs: []*promv1.RelabelConfig{
{
Action: "replace",
Regex: "prometheus-k8s-.*",
Replacement: "",
SourceLabels: []promv1.LabelName{
"pod",
},
TargetLabel: "pod",
},
},
TLSConfig: &promv1.TLSConfig{
SafeTLSConfig: promv1.SafeTLSConfig{
InsecureSkipVerify: true,
},
},
Params: params,
BearerTokenFile: "/var/run/secrets/kubernetes.io/serviceaccount/token",
},
},
NamespaceSelector: promv1.NamespaceSelector{
MatchNames: []string{"openshift-monitoring"},
},
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/component": "prometheus",
},
},
},
}

0 comments on commit 938c3d7

Please sign in to comment.