server: Fix RunPodSandbox error path
When RunPodSandbox fails after calling s.addSandbox(sb),
we're left with a sandbox in s.state.sandboxes while the
sandbox is not created.
We fix that by adding removeSandbox() to the deferred cleanup
call
@@ -282,19 +282,20 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
hostname: hostname,
}
- s.addSandbox(sb)
- if err = s.podIDIndex.Add(id); err != nil {
- return nil, err
- }
-
defer func() {
if err != nil {
+ s.removeSandbox(id)
if err2 := s.podIDIndex.Delete(id); err2 != nil {
logrus.Warnf("couldn't delete pod id %s from idIndex", id)
}
}
}()
+ s.addSandbox(sb)
+ if err = s.podIDIndex.Add(id); err != nil {
+ return nil, err
+ }
+
for k, v := range annotations {
g.AddAnnotation(k, v)
}
