Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 4039595

Browse files
committed
add pod's autoremove function
1 parent 10d54db commit 4039595

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

daemon/pod.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,17 @@ func (daemon *Daemon) CmdPodRun(job *engine.Job) error {
8686
if daemon.GetRunningPodNum() >= 1024 {
8787
return fmt.Errorf("Pod full, the maximum Pod is 1024!")
8888
}
89+
var autoremove bool = false
8990
podArgs := job.Args[0]
91+
if job.Args[1] == "yes" {
92+
autoremove = true
93+
}
9094

9195
podId := fmt.Sprintf("pod-%s", pod.RandStr(10, "alpha"))
9296

9397
glog.Info(podArgs)
9498

95-
code, cause, err := daemon.StartPod(podId, podArgs, "", nil, false, false, types.VM_KEEP_NONE)
99+
code, cause, err := daemon.StartPod(podId, podArgs, "", nil, false, autoremove, types.VM_KEEP_NONE)
96100
if err != nil {
97101
glog.Error(err.Error())
98102
return err
@@ -126,6 +130,7 @@ func (daemon *Daemon) CreatePod(podId, podArgs string, config interface{}, autor
126130
mypod := hypervisor.NewPod(podId, userPod)
127131
mypod.Handler.Handle = hyperHandlePodEvent
128132
mypod.Handler.Data = daemon
133+
mypod.Autoremove = autoremove
129134

130135
// store the UserPod into the db
131136
if err := daemon.WritePodToDB(podId, []byte(podArgs)); err != nil {
@@ -641,6 +646,10 @@ func hyperHandlePodEvent(vmResponse *types.VmResponse, data interface{},
641646
mypod.SetPodContainerStatus(vmResponse.Data.([]uint32))
642647
mypod.Vm = ""
643648
vm.Status = types.S_VM_IDLE
649+
if mypod.Autoremove == true {
650+
daemon.CleanPod(mypod.Id)
651+
return false
652+
}
644653
} else if vmResponse.Code == types.E_VM_SHUTDOWN {
645654
if mypod.Status == types.S_POD_RUNNING {
646655
mypod.Status = types.S_POD_SUCCEEDED

daemon/stop.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ func (daemon *Daemon) CmdPodStop(job *engine.Job) error {
3333
func (daemon *Daemon) StopPod(podId, stopVm string) (int, string, error) {
3434
glog.V(1).Infof("Prepare to stop the POD: %s", podId)
3535
// find the vm id which running POD, and stop it
36-
if daemon.PodList[podId].Status != types.S_POD_RUNNING {
36+
mypod, ok := daemon.PodList[podId]
37+
if !ok {
38+
glog.Errorf("Can not find pod(%s)", podId)
39+
return -1, "", fmt.Errorf("Can not find pod(%s)", podId)
40+
}
41+
if mypod.Status != types.S_POD_RUNNING {
3742
return -1, "", fmt.Errorf("The POD %s has aleady stopped, can not stop again!", podId)
3843
}
3944
vmid, err := daemon.GetPodVmByName(podId)
@@ -42,9 +47,9 @@ func (daemon *Daemon) StopPod(podId, stopVm string) (int, string, error) {
4247
}
4348
// we need to set the 'RestartPolicy' of the pod to 'never' if stop command is invoked
4449
// for kubernetes
45-
if daemon.PodList[podId].Type == "kubernetes" {
46-
daemon.PodList[podId].RestartPolicy = "never"
47-
if daemon.PodList[podId].Vm == "" {
50+
if mypod.Type == "kubernetes" {
51+
mypod.RestartPolicy = "never"
52+
if mypod.Vm == "" {
4853
return types.E_VM_SHUTDOWN, "", nil
4954
}
5055
}
@@ -53,7 +58,6 @@ func (daemon *Daemon) StopPod(podId, stopVm string) (int, string, error) {
5358
if !ok {
5459
return -1, "", fmt.Errorf("VM is not exist")
5560
}
56-
mypod, _ := daemon.PodList[podId]
5761

5862
vmResponse := vm.StopPod(mypod, stopVm)
5963

@@ -63,6 +67,9 @@ func (daemon *Daemon) StopPod(podId, stopVm string) (int, string, error) {
6367
if vmResponse.Code == types.E_VM_SHUTDOWN {
6468
daemon.RemoveVm(vmid)
6569
}
70+
if mypod.Autoremove == true {
71+
daemon.CleanPod(podId)
72+
}
6673

6774
return vmResponse.Code, vmResponse.Cause, nil
6875
}

utils/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
var (
1919
GITCOMMIT string = "0"
20-
VERSION string = "0.2.1"
20+
VERSION string = "0.3.0"
2121

2222
IAMSTATIC string = "true"
2323
INITSHA1 string = ""

0 commit comments

Comments
 (0)