Skip to content

Commit

Permalink
Merge pull request #322 from anispate/decom_metric
Browse files Browse the repository at this point in the history
Fixing the certman_operator_certificate_valid_duration_days metrics
  • Loading branch information
openshift-merge-bot[bot] authored Dec 12, 2024
2 parents a18be0c + 89fdc88 commit 44ce1d6
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions pkg/localmetrics/localmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,33 +181,18 @@ func AddCertificateIssuance(action string) {

// UpdateCertValidDuration updates the Prometheus metric for certificate validity duration.
func UpdateCertValidDuration(kubeClient client.Client, cert *x509.Certificate, now time.Time, clusterName, namespace string) {
if kubeClient == nil {
// If kubeClient is nil, set the metric value based on the certificate's expiration date
if cert != nil {
diff := cert.NotAfter.Sub(now)
days := math.Max(0, math.Round(diff.Hours()/24))
MetricCertValidDuration.With(prometheus.Labels{
"cn": cert.Subject.CommonName,
"cluster": clusterName,
}).Set(days)
} else {
MetricCertValidDuration.With(prometheus.Labels{
"cn": clusterName,
"cluster": clusterName,
}).Set(0)
}
return
}
var days float64
var cn string

// Check if the cluster is decommissioned
if isClusterDecommissioned(kubeClient, clusterName, namespace) {
logger.Info("Cluster is decommissioned, skipping UpdateCertValidDuration", "clusterName", clusterName)
return
// If kubeClient is available, check for decommissioning
if kubeClient != nil {
if isClusterDecommissioned(kubeClient, clusterName, namespace) {
logger.Info("Cluster is decommissioned, skipping metric update", "clusterName", clusterName)
return
}
}

// Set metric based on certificate expiration date if available
var days float64
var cn string
// Calculate days from certificate
if cert != nil {
diff := cert.NotAfter.Sub(now)
days = math.Max(0, math.Round(diff.Hours()/24))
Expand Down

0 comments on commit 44ce1d6

Please sign in to comment.