Skip to content

Commit edfaa5d

Browse files
author
derricheng
committed
Add retry mechanism while ContainedInspectContainer return NotFound
1 parent 1b17268 commit edfaa5d

File tree

1 file changed

+58
-47
lines changed

1 file changed

+58
-47
lines changed

pkg/gc/flannel_gc.go

+58-47
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (gc *flannelGC) Run() {
106106
if err := gc.cleanupVeth(); err != nil {
107107
glog.Errorf("failed cleanup links: %v", err)
108108
}
109-
}, *flagFlannelGCInterval*3, gc.quit)
109+
}, *flagFlannelGCInterval*2, gc.quit)
110110
}
111111

112112
func (gc *flannelGC) cleanupIP() error {
@@ -195,52 +195,63 @@ func (gc *flannelGC) cleanupVeth() error {
195195
}
196196

197197
func (gc *flannelGC) shouldCleanup(cid string) bool {
198-
if os.Getenv("CONTAINERD_HOST") != "" {
199-
if c, err := gc.dockerCli.ContainedInspectContainer(cid); err != nil {
200-
if stausErr, ok := status.FromError(err); ok {
201-
if stausErr.Code() == codes.NotFound {
202-
glog.Infof("container %s not found", cid)
203-
return true
204-
}
205-
glog.Warningf("Error inspect container %s: %v", cid, err)
206-
} else {
207-
glog.Warningf("Error inspect container %s: %v", cid, err)
208-
}
209-
} else {
210-
if c != nil && (c.State == criapi.PodSandboxState_SANDBOX_NOTREADY) {
211-
pod, err := gc.kubeCli.CoreV1().Pods(c.Annotations[SandboxNamespace]).Get(context.Background(), c.Annotations[SandboxName], metav1.GetOptions{})
212-
if err != nil {
213-
if apierrors.IsNotFound(err) {
214-
return true
215-
}
216-
glog.Errorf("failed to get pod %s", fmt.Sprintf("%s/%s", c.Annotations[SandboxNamespace], c.Annotations[SandboxName]))
217-
return false
218-
}
219-
for _, status := range pod.Status.ContainerStatuses {
220-
if status.State.Waiting != nil || status.State.Running != nil {
221-
return false
222-
}
223-
}
224-
glog.Infof("container %s exited %s", c.Id, c.State.String())
225-
return true
226-
}
227-
}
228-
return false
229-
}
230-
if c, err := gc.dockerCli.DockerInspectContainer(cid); err != nil {
231-
if _, ok := err.(docker.ContainerNotFoundError); ok {
232-
glog.Infof("container %s not found", cid)
233-
return true
234-
} else {
235-
glog.Warningf("Error inspect container %s: %v", cid, err)
236-
}
237-
} else {
238-
if c.State != nil && (c.State.Status == ContainerExited || c.State.Status == ContainerDead) {
239-
glog.Infof("container %s(%s) exited %s", c.ID, c.Name, c.State.Status)
240-
return true
241-
}
242-
}
243-
return false
198+
if os.Getenv("CONTAINERD_HOST") != "" {
199+
for i := 0; i < 2; i++ {
200+
if c, err := gc.dockerCli.ContainedInspectContainer(cid); err != nil {
201+
if statusErr, ok := status.FromError(err); ok {
202+
if statusErr.Code() == codes.NotFound {
203+
glog.Infof("container %s not found (attempt %d)", cid, i+1)
204+
if i == 1 {
205+
return true
206+
}
207+
} else {
208+
glog.Warningf("Error inspect container %s: %v", cid, err)
209+
return false
210+
}
211+
} else {
212+
glog.Warningf("Error inspect container %s: %v", cid, err)
213+
return false
214+
}
215+
} else {
216+
if c != nil && (c.State == criapi.PodSandboxState_SANDBOX_NOTREADY) {
217+
pod, err := gc.kubeCli.CoreV1().Pods(c.Annotations[SandboxNamespace]).Get(context.Background(), c.Annotations[SandboxName], metav1.GetOptions{})
218+
if err != nil {
219+
if apierrors.IsNotFound(err) {
220+
return true
221+
}
222+
glog.Errorf("failed to get pod %s", fmt.Sprintf("%s/%s", c.Annotations[SandboxNamespace], c.Annotations[SandboxName]))
223+
return false
224+
}
225+
for _, status := range pod.Status.ContainerStatuses {
226+
if status.State.Waiting != nil || status.State.Running != nil {
227+
return false
228+
}
229+
}
230+
glog.Infof("container %s exited %s", c.Id, c.State.String())
231+
return true
232+
}
233+
return false
234+
}
235+
if i == 0 {
236+
time.Sleep(3 * time.Second)
237+
}
238+
}
239+
}
240+
241+
if c, err := gc.dockerCli.DockerInspectContainer(cid); err != nil {
242+
if _, ok := err.(docker.ContainerNotFoundError); ok {
243+
glog.Infof("container %s not found", cid)
244+
return true
245+
} else {
246+
glog.Warningf("Error inspect container %s: %v", cid, err)
247+
}
248+
} else {
249+
if c.State != nil && (c.State.Status == ContainerExited || c.State.Status == ContainerDead) {
250+
glog.Infof("container %s(%s) exited %s", c.ID, c.Name, c.State.Status)
251+
return true
252+
}
253+
}
254+
return false
244255
}
245256

246257
func removeLeakyIPFile(ipFile, containerId string) {

0 commit comments

Comments
 (0)