Skip to content

Commit

Permalink
sc
Browse files Browse the repository at this point in the history
Signed-off-by: Valeriy Khorunzhin <[email protected]>
  • Loading branch information
Valeriy Khorunzhin committed Nov 14, 2024
1 parent 077ff0a commit 4282322
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 60 deletions.
4 changes: 4 additions & 0 deletions api/core/v1alpha2/vdcondition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func (s SnapshottingReason) String() string {
return string(s)
}

func (s StorageClassReadyReason) String() string {
return string(s)
}

const (
// DatasourceReady indicates that the datasource is ready for use, allowing the import process to start.
DatasourceReady DatasourceReadyReason = "DatasourceReady"
Expand Down
16 changes: 0 additions & 16 deletions images/virtualization-artifact/pkg/controller/service/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,10 @@ import (
"unicode"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
virtv1 "kubevirt.io/api/core/v1"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
)

func GetCondition(condType string, conds []metav1.Condition) (metav1.Condition, bool) {
for _, cond := range conds {
if cond.Type == condType {
return cond, true
}
}

return metav1.Condition{}, false
}

func SetCondition(cond metav1.Condition, conditions *[]metav1.Condition) {
meta.SetStatusCondition(conditions, cond)
}

func CapitalizeFirstLetter(s string) string {
if s == "" {
return ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,20 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (r
return reconcile.Result{Requeue: true}, nil
}

storageClassReadyCondition, ok := service.GetCondition(vdcondition.StorageClassReadyType, vd.Status.Conditions)
storageClassReadyCondition, ok := conditions.GetCondition(vdcondition.StorageClassReadyType, vd.Status.Conditions)
if !ok {
return reconcile.Result{Requeue: true}, fmt.Errorf("condition %s not found", vdcondition.StorageClassReadyType)
}

if readyCondition.Status != metav1.ConditionTrue && storageClassReadyCondition.Status != metav1.ConditionTrue {
readyCondition.Status = metav1.ConditionFalse
readyCondition.Reason = vdcondition.StorageClassNotReady
readyCondition.Message = "Storage class is not ready, please read the StorageClassReady condition state."
service.SetCondition(readyCondition, &vd.Status.Conditions)
readyCB := conditions.
NewConditionBuilder(vdcondition.ReadyType).
Generation(vd.Generation).
Status(metav1.ConditionFalse).
Reason(vdcondition.StorageClassNotReady).
Message("Storage class is not ready, please read the StorageClassReady condition state.")

conditions.SetCondition(readyCB, &vd.Status.Conditions)
}

if readyCondition.Status != metav1.ConditionTrue && storageClassReadyCondition.Status != metav1.ConditionTrue && vd.Status.StorageClassName != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ var _ = Describe("LifeCycleHandler Run", func() {
"Check on LifeCycle.Cleanup after spec changes",
func(args cleanupAfterSpecChangeTestArgs) {
var sourcesMock SourcesMock
args.ReadyCondition.Type = vdcondition.ReadyType
args.ReadyCondition.Type = vdcondition.ReadyType.String()
cleanUpCalled := false
vd := virtv2.VirtualDisk{
Status: virtv2.VirtualDiskStatus{
StorageClassName: "",
Conditions: []metav1.Condition{
args.ReadyCondition,
{
Type: vdcondition.DatasourceReadyType,
Type: vdcondition.DatasourceReadyType.String(),
Status: metav1.ConditionTrue,
},
{
Type: vdcondition.StorageClassReadyType,
Type: vdcondition.StorageClassReadyType.String(),
Status: metav1.ConditionTrue,
},
},
Expand Down Expand Up @@ -115,7 +115,7 @@ var _ = Describe("LifeCycleHandler Run", func() {
cleanupAfterSpecChangeTestArgs{
ReadyCondition: metav1.Condition{
Status: metav1.ConditionTrue,
Reason: vdcondition.Ready,
Reason: vdcondition.Ready.String(),
},
SpecChanged: true,
ExpectCleanup: false,
Expand All @@ -126,7 +126,7 @@ var _ = Describe("LifeCycleHandler Run", func() {
cleanupAfterSpecChangeTestArgs{
ReadyCondition: metav1.Condition{
Status: metav1.ConditionUnknown,
Reason: vdcondition.Lost,
Reason: vdcondition.Lost.String(),
},
SpecChanged: true,
ExpectCleanup: false,
Expand All @@ -137,8 +137,8 @@ var _ = Describe("LifeCycleHandler Run", func() {
DescribeTable(
"Verification that LifeCycle.CleanUp is called after the StorageClassReady status becomes false",
func(args cleanupAfterScNotReadyTestArgs) {
args.ReadyCondition.Type = vdcondition.ReadyType
args.StorageClassReadyCondition.Type = vdcondition.StorageClassReadyType
args.ReadyCondition.Type = vdcondition.ReadyType.String()
args.StorageClassReadyCondition.Type = vdcondition.StorageClassReadyType.String()
var sourcesMock SourcesMock
cleanUpCalled := false
vd := virtv2.VirtualDisk{
Expand All @@ -148,7 +148,7 @@ var _ = Describe("LifeCycleHandler Run", func() {
args.ReadyCondition,
args.StorageClassReadyCondition,
{
Type: vdcondition.DatasourceReadyType,
Type: vdcondition.DatasourceReadyType.String(),
Status: metav1.ConditionTrue,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,15 @@ func NewStorageClassReadyHandler(diskService DiskService) *StorageClassReadyHand
}

func (h StorageClassReadyHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) {
condition, ok := service.GetCondition(vdcondition.StorageClassReadyType, vd.Status.Conditions)
if !ok {
condition = metav1.Condition{
Type: vdcondition.StorageClassReadyType,
Status: metav1.ConditionUnknown,
Reason: conditions.ReasonUnknown.String(),
}
}
cb := conditions.NewConditionBuilder(vdcondition.StorageClassReadyType).Generation(vd.Generation)

defer func() { service.SetCondition(condition, &vd.Status.Conditions) }()
defer func() { conditions.SetCondition(cb, &vd.Status.Conditions) }()

if vd.DeletionTimestamp != nil {
condition.Status = metav1.ConditionUnknown
condition.Reason = conditions.ReasonUnknown.String()
condition.Message = ""
cb.
Status(metav1.ConditionUnknown).
Reason(conditions.ReasonUnknown).
Message("")
return reconcile.Result{}, nil
}

Expand Down Expand Up @@ -95,17 +89,20 @@ func (h StorageClassReadyHandler) Handle(ctx context.Context, vd *virtv2.Virtual

switch {
case sc != nil:
condition.Status = metav1.ConditionTrue
condition.Reason = vdcondition.StorageClassReady
condition.Message = ""
cb.
Status(metav1.ConditionTrue).
Reason(vdcondition.StorageClassReady).
Message("")
case hasNoStorageClassInSpec:
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.StorageClassNotFound
condition.Message = "The default storage class was not found in cluster. Please specify the storage class name in the virtual disk specification."
cb.
Status(metav1.ConditionFalse).
Reason(vdcondition.StorageClassNotFound).
Message("The default storage class was not found in cluster. Please specify the storage class name in the virtual disk specification.")
default:
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.StorageClassNotFound
condition.Message = fmt.Sprintf("Storage class %q not found", *vd.Spec.PersistentVolumeClaim.StorageClass)
cb.
Status(metav1.ConditionFalse).
Reason(vdcondition.StorageClassNotFound).
Message(fmt.Sprintf("Storage class %q not found", *vd.Spec.PersistentVolumeClaim.StorageClass))
}

return reconcile.Result{}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"

"github.com/deckhouse/virtualization-controller/pkg/controller/service"
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition"
Expand All @@ -39,7 +39,7 @@ var _ = Describe("StorageClassHandler Run", func() {
_, err := handler.Handle(context.TODO(), args.VirtualDisk)

Expect(err).To(BeNil())
condition, ok := service.GetCondition(vdcondition.StorageClassReadyType, args.VirtualDisk.Status.Conditions)
condition, ok := conditions.GetCondition(vdcondition.StorageClassReadyType, args.VirtualDisk.Status.Conditions)
Expect(ok).To(BeTrue())
Expect(condition.Status).To(Equal(args.ExpectedCondition.Status))
Expect(condition.Reason).To(Equal(args.ExpectedCondition.Reason))
Expand All @@ -53,7 +53,7 @@ var _ = Describe("StorageClassHandler Run", func() {
StorageClassInExistedPVC: nil,
ExpectedCondition: metav1.Condition{
Status: metav1.ConditionFalse,
Reason: vdcondition.StorageClassNotFound,
Reason: vdcondition.StorageClassNotFound.String(),
},
ExpectedStorageClassInStatus: "",
},
Expand All @@ -66,7 +66,7 @@ var _ = Describe("StorageClassHandler Run", func() {
StorageClassInExistedPVC: ptr.To("sc"),
ExpectedCondition: metav1.Condition{
Status: metav1.ConditionTrue,
Reason: vdcondition.StorageClassReady,
Reason: vdcondition.StorageClassReady.String(),
},
ExpectedStorageClassInStatus: "sc",
},
Expand All @@ -79,7 +79,7 @@ var _ = Describe("StorageClassHandler Run", func() {
StorageClassInExistedPVC: nil,
ExpectedCondition: metav1.Condition{
Status: metav1.ConditionTrue,
Reason: vdcondition.StorageClassReady,
Reason: vdcondition.StorageClassReady.String(),
},
ExpectedStorageClassInStatus: "sc",
},
Expand All @@ -92,7 +92,7 @@ var _ = Describe("StorageClassHandler Run", func() {
StorageClassInExistedPVC: nil,
ExpectedCondition: metav1.Condition{
Status: metav1.ConditionTrue,
Reason: vdcondition.StorageClassReady,
Reason: vdcondition.StorageClassReady.String(),
},
ExpectedStorageClassInStatus: "sc",
},
Expand All @@ -105,7 +105,7 @@ var _ = Describe("StorageClassHandler Run", func() {
StorageClassInExistedPVC: nil,
ExpectedCondition: metav1.Condition{
Status: metav1.ConditionFalse,
Reason: vdcondition.StorageClassNotFound,
Reason: vdcondition.StorageClassNotFound.String(),
},
ExpectedStorageClassInStatus: "statusSC",
},
Expand All @@ -118,7 +118,7 @@ var _ = Describe("StorageClassHandler Run", func() {
StorageClassInExistedPVC: ptr.To("pvcSC"),
ExpectedCondition: metav1.Condition{
Status: metav1.ConditionTrue,
Reason: vdcondition.StorageClassReady,
Reason: vdcondition.StorageClassReady.String(),
},
ExpectedStorageClassInStatus: "pvcSC",
},
Expand All @@ -131,7 +131,7 @@ var _ = Describe("StorageClassHandler Run", func() {
StorageClassInExistedPVC: ptr.To("pvcSC"),
ExpectedCondition: metav1.Condition{
Status: metav1.ConditionTrue,
Reason: vdcondition.StorageClassReady,
Reason: vdcondition.StorageClassReady.String(),
},
ExpectedStorageClassInStatus: "pvcSC",
},
Expand All @@ -144,7 +144,7 @@ var _ = Describe("StorageClassHandler Run", func() {
StorageClassInExistedPVC: nil,
ExpectedCondition: metav1.Condition{
Status: metav1.ConditionTrue,
Reason: vdcondition.StorageClassReady,
Reason: vdcondition.StorageClassReady.String(),
},
ExpectedStorageClassInStatus: "specSC",
},
Expand Down

0 comments on commit 4282322

Please sign in to comment.