Skip to content

Commit

Permalink
Correction in the flow of Pod and VMI deletion
Browse files Browse the repository at this point in the history
Signed-off-by: Vatsal Parekh <[email protected]>
  • Loading branch information
Vatsal Parekh committed Dec 3, 2019
1 parent 48118c3 commit caef634
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
53 changes: 28 additions & 25 deletions pkg/virt-api/rest/subresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,15 @@ func (app *SubresourceAPIApp) RestartVMRequestHandler(request *restful.Request,
return
}
}
if bodyStruct.GracePeriodSeconds != nil {
if *bodyStruct.GracePeriodSeconds > 0 {
response.WriteError(http.StatusForbidden, fmt.Errorf("For force restart, only gracePeriod=0 is supported for now"))
return
} else if *bodyStruct.GracePeriodSeconds < 0 {
response.WriteError(http.StatusForbidden, fmt.Errorf("gracePeriod has to be greater or equal to 0"))
return
}
}

vm, code, err := app.fetchVirtualMachine(name, namespace)
if err != nil {
Expand Down Expand Up @@ -461,31 +470,6 @@ func (app *SubresourceAPIApp) RestartVMRequestHandler(request *restful.Request,
return
}

// Only force restart is supported for now
// Check if bodyMap["graceperiod"] is not empty because gracePeriodint64 can be 0 if that is empty
if bodyStruct.GracePeriodSeconds != nil {
if *bodyStruct.GracePeriodSeconds == 0 {
vmiPodname, err := app.findPod(namespace, vmi)
if err != nil {
response.WriteError(http.StatusForbidden, err)
}
// set termincationGracePeriod and delete the VMI pod to trigger a forced restart
err = app.virtCli.CoreV1().Pods(namespace).Delete(vmiPodname, &k8smetav1.DeleteOptions{GracePeriodSeconds: bodyStruct.GracePeriodSeconds})
if err != nil {
response.WriteError(http.StatusForbidden, err)
} else {
response.WriteHeader(http.StatusAccepted)
}
return
} else if *bodyStruct.GracePeriodSeconds > 0 {
response.WriteError(http.StatusForbidden, fmt.Errorf("For force restart, only gracePeriod=0 is supported for now"))
return
} else if *bodyStruct.GracePeriodSeconds < 0 {
response.WriteError(http.StatusForbidden, fmt.Errorf("gracePeriod has to be greater or equal to 0"))
return
}
}

bodyString, err := getChangeRequestJson(vm,
v1.VirtualMachineStateChangeRequest{Action: v1.StopRequest, UID: &vmi.UID},
v1.VirtualMachineStateChangeRequest{Action: v1.StartRequest})
Expand All @@ -505,6 +489,25 @@ func (app *SubresourceAPIApp) RestartVMRequestHandler(request *restful.Request,
return
}

// Only force restart with GracePeriodSeconds=0 is supported for now
// Here we are deleting the Pod because CRDs don't support gracePeriodSeconds at the moment
if bodyStruct.GracePeriodSeconds != nil {
if *bodyStruct.GracePeriodSeconds == 0 {
vmiPodname, err := app.findPod(namespace, vmi)
if err != nil {
response.WriteError(http.StatusForbidden, err)
}
// set termincationGracePeriod and delete the VMI pod to trigger a forced restart
err = app.virtCli.CoreV1().Pods(namespace).Delete(vmiPodname, &k8smetav1.DeleteOptions{GracePeriodSeconds: bodyStruct.GracePeriodSeconds})
if err != nil {
response.WriteError(http.StatusForbidden, err)
} else {
response.WriteHeader(http.StatusAccepted)
}
return
}
}

response.WriteHeader(http.StatusAccepted)
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/virt-api/rest/subresource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ var _ = Describe("VirtualMachineInstance Subresources", func() {
),
)

server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("PATCH", "/apis/kubevirt.io/v1alpha3/namespaces/default/virtualmachines/testvm"),
ghttp.RespondWithJSONEncoded(http.StatusOK, vm),
),
)

server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/api/v1/namespaces/default/pods"),
Expand Down

0 comments on commit caef634

Please sign in to comment.