-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: dmitry.lopatin <[email protected]>
- Loading branch information
1 parent
e599b02
commit 11bbb7f
Showing
26 changed files
with
353 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
images/virtualization-artifact/pkg/controller/service/lock_service.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" | ||
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" | ||
) | ||
|
||
type LockService struct { | ||
client client.Client | ||
} | ||
|
||
func NewLockService( | ||
client client.Client, | ||
) *LockService { | ||
return &LockService{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (s LockService) LockVirtualDiskByVirtualImage(ctx context.Context, vd *virtv2.VirtualDisk) error { | ||
inUseCondition, ok := GetCondition(vdcondition.InUseType, vd.Status.Conditions) | ||
|
||
if !ok || inUseCondition.Status == metav1.ConditionUnknown { | ||
inUseCondition = metav1.Condition{ | ||
Type: vdcondition.InUseType, | ||
Reason: vdcondition.InUseByVirtualImage, | ||
Status: metav1.ConditionTrue, | ||
} | ||
|
||
SetCondition(inUseCondition, &vd.Status.Conditions) | ||
err := s.client.Status().Update(ctx, vd) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
if inUseCondition.Reason != vdcondition.InUseByVirtualImage { | ||
return fmt.Errorf("virtual disk %q already used by running virtual machine", vd.GetName()) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s LockService) UnlockVirtualDisk(ctx context.Context, vd *virtv2.VirtualDisk) error { | ||
inUseCondition, ok := GetCondition(vdcondition.InUseType, vd.Status.Conditions) | ||
if ok { | ||
inUseCondition = metav1.Condition{ | ||
Type: vdcondition.InUseType, | ||
Status: metav1.ConditionUnknown, | ||
} | ||
|
||
SetCondition(inUseCondition, &vd.Status.Conditions) | ||
err := s.client.Status().Update(ctx, vd) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s LockService) LockVirtualDiskByVirtualMachine(ctx context.Context, vd *virtv2.VirtualDisk) error { | ||
inUseCondition, ok := GetCondition(vdcondition.InUseType, vd.Status.Conditions) | ||
if !ok || inUseCondition.Status == metav1.ConditionUnknown { | ||
inUseCondition = metav1.Condition{ | ||
Type: vdcondition.InUseType, | ||
Reason: vdcondition.InUseByVirtualMachine, | ||
Status: metav1.ConditionTrue, | ||
} | ||
|
||
SetCondition(inUseCondition, &vd.Status.Conditions) | ||
err := s.client.Status().Update(ctx, vd) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
if inUseCondition.Reason != vdcondition.InUseByVirtualMachine { | ||
return fmt.Errorf("virtual disk %q already used by creating virtual disk", vd.GetName()) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.