Skip to content

Commit

Permalink
fix(host): add startPodLock for pod instance
Browse files Browse the repository at this point in the history
  • Loading branch information
zexi committed Nov 14, 2024
1 parent 8457991 commit c95edd9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/hostman/guestman/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"

runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
Expand Down Expand Up @@ -216,12 +217,15 @@ type sPodGuestInstance struct {
containers map[string]*sContainer
startStat *startStatHelper
expectedStatus *PodExpectedStatus

startPodLock sync.Mutex
}

func newPodGuestInstance(id string, man *SGuestManager) PodInstance {
p := &sPodGuestInstance{
sBaseGuestInstance: newBaseGuestInstance(id, man, computeapi.HYPERVISOR_POD),
containers: make(map[string]*sContainer),
startPodLock: sync.Mutex{},
}
es, err := NewPodExpectedStatus(p.HomeDir(), computeapi.VM_UNKNOWN)
if err != nil {
Expand Down Expand Up @@ -737,6 +741,9 @@ func (s *sPodGuestInstance) ShouldRestartPodOnCrash() bool {
}

func (s *sPodGuestInstance) startPod(ctx context.Context, userCred mcclient.TokenCredential) (*computeapi.PodStartResponse, error) {
s.startPodLock.Lock()
defer s.startPodLock.Unlock()

retries := 3
sec := 5 * time.Second
var err error
Expand Down
5 changes: 5 additions & 0 deletions pkg/hostman/guestman/pod_sync_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ func (m *SGuestManager) syncContainerLoopIteration(plegCh chan *pleg.PodLifecycl
log.Infof("pod %s is dirty shutdown, waiting it to started", podMan.GetName())
return
}
podInstance := podMan.(*sPodGuestInstance)
if e.Type == pleg.ContainerStarted {
// 防止读取 podMan.GetCRIId 还没有刷新的问题
podInstance.startPodLock.Lock()
defer podInstance.startPodLock.Unlock()

log.Infof("pod container started: %s", jsonutils.Marshal(e))
ctrId := e.Data.(string)
if ctrId == podMan.GetCRIId() {
Expand Down

0 comments on commit c95edd9

Please # to comment.