Skip to content

Commit

Permalink
Merge pull request #1203 from HusterWan/zr/fix-disconnect-info
Browse files Browse the repository at this point in the history
refactor: if store container info to disk failed return errors
  • Loading branch information
allencloud authored Apr 25, 2018
2 parents e4a33e2 + 9056d35 commit c3d217a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
12 changes: 6 additions & 6 deletions apis/server/network_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func (s *Server) listNetwork(ctx context.Context, rw http.ResponseWriter, req *h
}

func (s *Server) getNetwork(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
id := mux.Vars(req)["id"]

network, err := s.NetworkMgr.Get(ctx, name)
network, err := s.NetworkMgr.Get(ctx, id)
if err != nil {
return err
}
Expand All @@ -68,9 +68,9 @@ func (s *Server) getNetwork(ctx context.Context, rw http.ResponseWriter, req *ht
}

func (s *Server) deleteNetwork(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
id := mux.Vars(req)["id"]

if err := s.NetworkMgr.Remove(ctx, name); err != nil {
if err := s.NetworkMgr.Remove(ctx, id); err != nil {
return err
}
rw.WriteHeader(http.StatusNoContent)
Expand Down Expand Up @@ -108,9 +108,9 @@ func (s *Server) disconnectNetwork(ctx context.Context, rw http.ResponseWriter,
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

name := mux.Vars(req)["name"]
id := mux.Vars(req)["id"]

return s.ContainerMgr.DisconnectContainerFromNetwork(ctx, network.Container, name, network.Force)
return s.ContainerMgr.Disconnect(ctx, network.Container, id, network.Force)
}

func buildNetworkInspectResp(n *networktypes.Network) *types.NetworkInspectResp {
Expand Down
6 changes: 3 additions & 3 deletions apis/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func initRoute(s *Server) http.Handler {

s.addRoute(r, http.MethodGet, "/networks", s.listNetwork)
s.addRoute(r, http.MethodPost, "/networks/create", s.createNetwork)
s.addRoute(r, http.MethodGet, "/networks/{name:.*}", s.getNetwork)
s.addRoute(r, http.MethodDelete, "/networks/{name:.*}", s.deleteNetwork)
s.addRoute(r, http.MethodGet, "/networks/{id:.*}", s.getNetwork)
s.addRoute(r, http.MethodDelete, "/networks/{id:.*}", s.deleteNetwork)
s.addRoute(r, http.MethodPost, "/networks/{id:.*}/connect", s.connectToNetwork)
s.addRoute(r, http.MethodPost, "/networks/{name:.*}/disconnect", s.disconnectNetwork)
s.addRoute(r, http.MethodPost, "/networks/{id:.*}/disconnect", s.disconnectNetwork)

// metrics
r.Path(versionMatcher + "/metrics").Methods(http.MethodGet).Handler(prometheus.Handler())
Expand Down
44 changes: 34 additions & 10 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ type ContainerMgr interface {
// Connect is used to connect a container to a network.
Connect(ctx context.Context, name string, networkIDOrName string, epConfig *types.EndpointSettings) error

// DisconnectContainerFromNetwork disconnects the given container from
// Disconnect disconnects the given container from
// given network
DisconnectContainerFromNetwork(ctx context.Context, containerName, networkName string, force bool) error
Disconnect(ctx context.Context, containerName, networkName string, force bool) error
}

// ContainerManager is the default implement of interface ContainerMgr.
Expand Down Expand Up @@ -528,7 +528,10 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
defer container.Unlock()

// store disk
container.Write(mgr.Store)
if err := container.Write(mgr.Store); err != nil {
logrus.Errorf("failed to update meta: %v", err)
return nil, err
}

// add to collection
mgr.NameToID.Put(name, id)
Expand Down Expand Up @@ -753,7 +756,12 @@ func (mgr *ContainerManager) Pause(ctx context.Context, name string) error {
}

c.meta.State.Status = types.StatusPaused
c.Write(mgr.Store)

if err := c.Write(mgr.Store); err != nil {
logrus.Errorf("failed to update meta: %v", err)
return err
}

return nil
}

Expand Down Expand Up @@ -784,7 +792,12 @@ func (mgr *ContainerManager) Unpause(ctx context.Context, name string) error {
}

c.meta.State.Status = types.StatusRunning
c.Write(mgr.Store)

if err := c.Write(mgr.Store); err != nil {
logrus.Errorf("failed to update meta: %v", err)
return err
}

return nil
}

Expand Down Expand Up @@ -863,7 +876,11 @@ func (mgr *ContainerManager) Rename(ctx context.Context, oldName, newName string
mgr.NameToID.Put(newName, c.ID())

c.meta.Name = newName
c.Write(mgr.Store)

if err := c.Write(mgr.Store); err != nil {
logrus.Errorf("failed to update meta: %v", err)
return err
}

return nil
}
Expand Down Expand Up @@ -966,7 +983,7 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty

// store disk.
if updateErr == nil {
c.Write(mgr.Store)
updateErr = c.Write(mgr.Store)
}

return updateErr
Expand Down Expand Up @@ -1093,7 +1110,10 @@ func (mgr *ContainerManager) Upgrade(ctx context.Context, name string, config *t
}

// Works fine, store new container info to disk.
c.Write(mgr.Store)
if err := c.Write(mgr.Store); err != nil {
logrus.Errorf("failed to update meta: %v", err)
return err
}

return nil
}
Expand Down Expand Up @@ -1216,9 +1236,9 @@ func (mgr *ContainerManager) Connect(ctx context.Context, name string, networkID
return c.Write(mgr.Store)
}

// DisconnectContainerFromNetwork disconnects the given container from
// Disconnect disconnects the given container from
// given network
func (mgr *ContainerManager) DisconnectContainerFromNetwork(ctx context.Context, containerName, networkName string, force bool) error {
func (mgr *ContainerManager) Disconnect(ctx context.Context, containerName, networkName string, force bool) error {
c, err := mgr.container(containerName)
if err != nil {
// TODO(ziren): if force is true, force delete endpoint
Expand Down Expand Up @@ -1271,6 +1291,7 @@ func (mgr *ContainerManager) DisconnectContainerFromNetwork(ctx context.Context,
// update container meta json
if err := c.Write(mgr.Store); err != nil {
logrus.Errorf("failed to update meta: %v", err)
return err
}

return nil
Expand Down Expand Up @@ -1509,7 +1530,9 @@ func (mgr *ContainerManager) markStoppedAndRelease(c *Container, m *ctrd.Message
// update meta
if err := c.Write(mgr.Store); err != nil {
logrus.Errorf("failed to update meta: %v", err)
return err
}

return nil
}

Expand All @@ -1532,6 +1555,7 @@ func (mgr *ContainerManager) exitedAndRelease(id string, m *ctrd.Message) error
c.meta.State.Status = types.StatusExited
if err := c.Write(mgr.Store); err != nil {
logrus.Errorf("failed to update meta: %v", err)
return err
}

// send exit event to monitor
Expand Down

0 comments on commit c3d217a

Please sign in to comment.