implement pv last phase transition time

This commit is contained in:
Roman Bednar 2023-07-20 11:08:48 +02:00
parent 1b4de065df
commit aa65bab426
2 changed files with 34 additions and 0 deletions

View File

@ -41,6 +41,14 @@ func DropDisabledSpecFields(pvSpec *api.PersistentVolumeSpec, oldPVSpec *api.Per
} }
} }
// DropDisabledStatusFields removes disabled fields from the pv status.
// This should be called from PrepareForUpdate for all resources containing a pv status.
func DropDisabledStatusFields(oldStatus, newStatus *api.PersistentVolumeStatus) {
if !utilfeature.DefaultFeatureGate.Enabled(features.PersistentVolumeLastPhaseTransitionTime) && oldStatus.LastPhaseTransitionTime.IsZero() {
newStatus.LastPhaseTransitionTime = nil
}
}
func hasNodeExpansionSecrets(oldPVSpec *api.PersistentVolumeSpec) bool { func hasNodeExpansionSecrets(oldPVSpec *api.PersistentVolumeSpec) bool {
if oldPVSpec == nil || oldPVSpec.CSI == nil { if oldPVSpec == nil || oldPVSpec.CSI == nil {
return false return false

View File

@ -19,6 +19,9 @@ package persistentvolume
import ( import (
"context" "context"
"fmt" "fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/kubernetes/pkg/features"
"k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
@ -66,6 +69,12 @@ func (persistentvolumeStrategy) PrepareForCreate(ctx context.Context, obj runtim
pv := obj.(*api.PersistentVolume) pv := obj.(*api.PersistentVolume)
pv.Status = api.PersistentVolumeStatus{} pv.Status = api.PersistentVolumeStatus{}
if utilfeature.DefaultFeatureGate.Enabled(features.PersistentVolumeLastPhaseTransitionTime) {
pv.Status.Phase = api.VolumePending
now := nowFunc()
pv.Status.LastPhaseTransitionTime = &now
}
pvutil.DropDisabledSpecFields(&pv.Spec, nil) pvutil.DropDisabledSpecFields(&pv.Spec, nil)
} }
@ -134,11 +143,28 @@ func (persistentvolumeStatusStrategy) GetResetFields() map[fieldpath.APIVersion]
return fields return fields
} }
var nowFunc = metav1.Now
// PrepareForUpdate sets the Spec field which is not allowed to be changed when updating a PV's Status // PrepareForUpdate sets the Spec field which is not allowed to be changed when updating a PV's Status
func (persistentvolumeStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { func (persistentvolumeStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
newPv := obj.(*api.PersistentVolume) newPv := obj.(*api.PersistentVolume)
oldPv := old.(*api.PersistentVolume) oldPv := old.(*api.PersistentVolume)
newPv.Spec = oldPv.Spec newPv.Spec = oldPv.Spec
if utilfeature.DefaultFeatureGate.Enabled(features.PersistentVolumeLastPhaseTransitionTime) {
switch {
case oldPv.Status.Phase == newPv.Status.Phase && newPv.Status.LastPhaseTransitionTime == nil:
// phase didn't change, preserve the existing transition time if set
newPv.Status.LastPhaseTransitionTime = oldPv.Status.LastPhaseTransitionTime
case oldPv.Status.Phase != newPv.Status.Phase && (newPv.Status.LastPhaseTransitionTime == nil || newPv.Status.LastPhaseTransitionTime.Equal(oldPv.Status.LastPhaseTransitionTime)):
// phase changed and client didn't set or didn't change the transition time
now := nowFunc()
newPv.Status.LastPhaseTransitionTime = &now
}
}
pvutil.DropDisabledStatusFields(&oldPv.Status, &newPv.Status)
} }
func (persistentvolumeStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { func (persistentvolumeStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {