containerized-data-importer/pkg/controller/dataimportcron-conditions.go
Arnon Gilboa 41f7c89187
[release-v1.57] Add new Prometheus alerts and label existing alerts (#3041)
Manual backport of #2998 & #3038

- CDINoDefaultStorageClass - not having a default SC is surely not an
OpenShift error, as admins may prefer their cluster users to only use
explicit SC names. However, in the CDI context when DV is created with
default SC but default does not exist, we will fire an error event and
the PVC will be Pending for the default SC, so when there are such
Pending PVCs we will fire an alert.

- CDIDefaultStorageClassDegraded - when the default SC does not support
CSI/Snapshot clone (smart clone) or does not have ReadWriteMany access
mode (for live migration).

- CDIStorageProfilesIncomplete - add storageClass and provisioner
labels.

- CDIDataImportCronOutdated - add dataImportCron namespace and name
labels.

Also:
* Rename the metric kubevirt_cdi_storageprofile_status to
kubevirt_cdi_storageprofile_info since it always reports value 1,
where the label values provide the details about the storage class and
storage profile.
* Add snapshot manifests for tests and deploy snapshot CRDs in the hpp
destructive lane

Signed-off-by: Arnon Gilboa <agilboa@redhat.com>
2024-01-03 20:29:20 +01:00

87 lines
2.9 KiB
Go

/*
Copyright 2021 The CDI Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
limitations under the License.
See the License for the specific language governing permissions and
*/
package controller
import (
"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
)
const (
noDigest = "NoDigest"
noImport = "NoImport"
outdated = "Outdated"
scheduled = "ImportScheduled"
inProgress = "ImportProgressing"
upToDate = "UpToDate"
)
func updateDataImportCronCondition(cron *cdiv1.DataImportCron, conditionType cdiv1.DataImportCronConditionType, status corev1.ConditionStatus, message, reason string) {
if conditionType == cdiv1.DataImportCronUpToDate {
gaugeVal := float64(0)
if status != corev1.ConditionTrue {
gaugeVal = 1
}
DataImportCronOutdatedGauge.With(getPrometheusCronLabels(client.ObjectKeyFromObject(cron))).Set(gaugeVal)
}
if condition := FindDataImportCronConditionByType(cron, conditionType); condition != nil {
updateConditionState(&condition.ConditionState, status, message, reason)
} else {
condition = &cdiv1.DataImportCronCondition{Type: conditionType}
updateConditionState(&condition.ConditionState, status, message, reason)
cron.Status.Conditions = append(cron.Status.Conditions, *condition)
}
}
// FindDataImportCronConditionByType finds DataImportCronCondition by condition type
func FindDataImportCronConditionByType(cron *cdiv1.DataImportCron, conditionType cdiv1.DataImportCronConditionType) *cdiv1.DataImportCronCondition {
for i, condition := range cron.Status.Conditions {
if condition.Type == conditionType {
return &cron.Status.Conditions[i]
}
}
return nil
}
func updateConditionState(condition *cdiv1.ConditionState, status corev1.ConditionStatus, message, reason string) {
conditionStatusUpdated := condition.Status != status
conditionUpdated := conditionStatusUpdated || condition.Message != message || condition.Reason != reason
if conditionUpdated {
now := metav1.Now()
if conditionStatusUpdated {
condition.Status = status
condition.LastTransitionTime = now
}
condition.LastHeartbeatTime = now
condition.Reason = reason
condition.Message = message
}
}
func getPrometheusCronLabels(cron types.NamespacedName) prometheus.Labels {
return prometheus.Labels{
prometheusNsLabel: cron.Namespace,
prometheusCronNameLabel: cron.Name,
}
}