Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To make the reconcile function more robust. #63

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,106 +128,110 @@ class VirtualMachineReconciler(val client: KubernetesClient) : Reconciler<Virtua
resource: VirtualMachine?,
context: Context<VirtualMachine>?
): UpdateControl<VirtualMachine> {
val vm = resource ?: return UpdateControl.noUpdate()
val vmClient = newVMClient(vm.spec.platform)
if (vm.spec.deleted) {
val exist = runBlocking {
try {
if (vmClient.getVM(vm.status.uuid).isSuccess) {
vmClient.deleteVM(vm.status.uuid).getOrThrow()
true
} else {
try {
val vm = resource ?: return UpdateControl.noUpdate()
val vmClient = newVMClient(vm.spec.platform)
if (vm.spec.deleted) {
val exist = runBlocking {
try {
if (vmClient.getVM(vm.status.uuid).isSuccess) {
vmClient.deleteVM(vm.status.uuid).getOrThrow()
true
} else {
false
}
} catch (e: NullPointerException) {
false
}
} catch (e: NullPointerException) {
false
}
return if (exist) {
UpdateControl.noUpdate<VirtualMachine>().rescheduleAfter(1000L)
} else {
UpdateControl.noUpdate()
}
}
return if (exist) {
UpdateControl.noUpdate<VirtualMachine>().rescheduleAfter(1000L)
} else {
UpdateControl.noUpdate()
}
}



if (vm.status == null) {
val vmModelResult = runBlocking { vmClient.getVMByName(vm.spec.name, vm.spec.getVmExtraInfo().applyId) }
if (vmModelResult.isSuccess) {
vm.status = vmModelResult.getOrThrow().toCrdStatus()
return UpdateControl.patchStatus(vm).rescheduleAfter(1000L)
} else {
val vmApply = VmApply.id(vm.spec.getVmExtraInfo().applyId)
if (vmApply == null || !vmApply.isApproved()) {
return UpdateControl.noUpdate<VirtualMachine>().rescheduleAfter(1000L)
}
val vmModel = runBlocking {
if (createVmProcessMutex.tryLock(vm)) {
try {
vmClient.createVM(vm.spec.toCreateVmOptions()).getOrThrow()
} finally {
createVmProcessMutex.unlock(vm)
if (vm.status == null) {
val vmModelResult = runBlocking { vmClient.getVMByName(vm.spec.name, vm.spec.getVmExtraInfo().applyId) }
if (vmModelResult.isSuccess) {
vm.status = vmModelResult.getOrThrow().toCrdStatus()
return UpdateControl.patchStatus(vm).rescheduleAfter(1000L)
} else {
val vmApply = VmApply.id(vm.spec.getVmExtraInfo().applyId)
if (vmApply == null || !vmApply.isApproved()) {
return UpdateControl.noUpdate<VirtualMachine>().rescheduleAfter(1000L)
}
val vmModel = runBlocking {
if (createVmProcessMutex.tryLock(vm)) {
try {
vmClient.createVM(vm.spec.toCreateVmOptions()).getOrThrow()
} finally {
createVmProcessMutex.unlock(vm)
}
} else {
null
}
}
return if (vmModel != null) {
vm.status = vmModel.toCrdStatus()
UpdateControl.patchStatus(vm).rescheduleAfter(1000L)
} else {
null
UpdateControl.noUpdate<VirtualMachine>().rescheduleAfter(1000L)
}
}
return if (vmModel != null) {
vm.status = vmModel.toCrdStatus()
UpdateControl.patchStatus(vm).rescheduleAfter(1000L)
} else {
UpdateControl.noUpdate<VirtualMachine>().rescheduleAfter(1000L)
}
}
}

assert(vm.status != null)

// check if all done
val vmApply = VmApply.id(vm.spec.getVmExtraInfo().applyId)
if (vmApply != null && !vmApply.done) {
val done = vmKubeClient
.inNamespace(vmApply.namespaceName())
.list()
.items
.all {
val extraInfo = it.spec.getVmExtraInfo()
extraInfo.initial && it.status != null
assert(vm.status != null)

// check if all done
val vmApply = VmApply.id(vm.spec.getVmExtraInfo().applyId)
if (vmApply != null && !vmApply.done) {
val done = vmKubeClient
.inNamespace(vmApply.namespaceName())
.list()
.items
.all {
val extraInfo = it.spec.getVmExtraInfo()
extraInfo.initial && it.status != null
}
if (done) {
vmApply.done = true
mysql.vmApplyList.update(vmApply)
}
if (done) {
vmApply.done = true
mysql.vmApplyList.update(vmApply)
}
}

val vmUuid = vm.status.uuid
val vmUuid = vm.status.uuid

runBlocking {
val vmModel = vmClient.getVM(vmUuid).getOrNull()
if (vmModel == null) {
vm.spec = vm.spec.copy(deleted = true)
} else {
vm.status = vmModel.toCrdStatus()
runBlocking {
val vmModel = vmClient.getVM(vmUuid).getOrNull()
if (vmModel == null) {
vm.spec = vm.spec.copy(deleted = true)
} else {
vm.status = vmModel.toCrdStatus()
}
}
}

// check the power
if (vm.spec.powerState == VirtualMachineModel.PowerState.PoweredOn) {
if (vm.status.powerState != VirtualMachineModel.PowerState.PoweredOn) {
runBlocking {
vmClient.powerOnSync(vmUuid)
// check the power
if (vm.spec.powerState == VirtualMachineModel.PowerState.PoweredOn) {
if (vm.status.powerState != VirtualMachineModel.PowerState.PoweredOn) {
runBlocking {
vmClient.powerOnSync(vmUuid)
}
}
}
} else if (vm.spec.powerState == VirtualMachineModel.PowerState.PoweredOff) {
if (vm.status.powerState != VirtualMachineModel.PowerState.PoweredOff) {
runBlocking {
vmClient.powerOffSync(vmUuid)
} else if (vm.spec.powerState == VirtualMachineModel.PowerState.PoweredOff) {
if (vm.status.powerState != VirtualMachineModel.PowerState.PoweredOff) {
runBlocking {
vmClient.powerOffSync(vmUuid)
}
}
}
}

return UpdateControl.patchStatus(vm).rescheduleAfter(1000L)
return UpdateControl.patchStatus(vm).rescheduleAfter(1000L)
} catch (e: Throwable) {
return UpdateControl.noUpdate()
}
}

override fun cleanup(resource: VirtualMachine?, context: Context<VirtualMachine>?): DeleteControl {
Expand Down
Loading