From 9056d353f6659186bd2ffa2db337bccc22f5dd27 Mon Sep 17 00:00:00 2001 From: Michael Wan Date: Tue, 24 Apr 2018 22:58:51 -0400 Subject: [PATCH] refactor: if store container info to disk failed return errors Signed-off-by: Michael Wan --- apis/server/network_bridge.go | 12 +++++----- apis/server/router.go | 6 ++--- daemon/mgr/container.go | 44 +++++++++++++++++++++++++++-------- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/apis/server/network_bridge.go b/apis/server/network_bridge.go index d4b78f44b..d9352e408 100644 --- a/apis/server/network_bridge.go +++ b/apis/server/network_bridge.go @@ -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 } @@ -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) @@ -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 { diff --git a/apis/server/router.go b/apis/server/router.go index 33bdfe338..e649df374 100644 --- a/apis/server/router.go +++ b/apis/server/router.go @@ -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()) diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index 9739d1ec0..42d27d2f9 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -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. @@ -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) @@ -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 } @@ -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 } @@ -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 } @@ -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 @@ -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 } @@ -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 @@ -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 @@ -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 } @@ -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