Skip to content

Commit

Permalink
refactor: deliminate containerMeta
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Sun <allensun.shl@alibaba-inc.com>
  • Loading branch information
allencloud committed May 11, 2018
1 parent 188acff commit 17d4408
Show file tree
Hide file tree
Showing 23 changed files with 383 additions and 414 deletions.
83 changes: 44 additions & 39 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,38 @@ func (s *Server) createContainer(ctx context.Context, rw http.ResponseWriter, re
func (s *Server) getContainer(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]

meta, err := s.ContainerMgr.Get(ctx, name)
c, err := s.ContainerMgr.Get(ctx, name)
if err != nil {
return err
}

container := types.ContainerJSON{
ID: meta.ID,
Name: meta.Name,
Image: meta.Config.Image,
Created: meta.Created,
State: meta.State,
Config: meta.Config,
HostConfig: meta.HostConfig,
Snapshotter: meta.Snapshotter,
GraphDriver: &types.GraphDriverData{
Name: meta.Snapshotter.Name,
Data: meta.Snapshotter.Data,
},
var netSettings *types.NetworkSettings
if c.NetworkSettings != nil {
netSettings = &types.NetworkSettings{
Networks: c.NetworkSettings.Networks,
}
}

if meta.NetworkSettings != nil {
container.NetworkSettings = &types.NetworkSettings{
Networks: meta.NetworkSettings.Networks,
}
mounts := []types.MountPoint{}
for _, mp := range c.Mounts {
mounts = append(mounts, *mp)
}

container.Mounts = []types.MountPoint{}
for _, mp := range meta.Mounts {
container.Mounts = append(container.Mounts, *mp)
container := types.ContainerJSON{
ID: c.ID,
Name: c.Name,
Image: c.Config.Image,
Created: c.Created,
State: c.State,
Config: c.Config,
HostConfig: c.HostConfig,
Snapshotter: c.Snapshotter,
GraphDriver: &types.GraphDriverData{
Name: c.Snapshotter.Name,
Data: c.Snapshotter.Data,
},
Mounts: mounts,
NetworkSettings: netSettings,
}

return EncodeResponse(rw, http.StatusOK, container)
Expand All @@ -99,41 +102,43 @@ func (s *Server) getContainers(ctx context.Context, rw http.ResponseWriter, req
All: httputils.BoolValue(req, "all"),
}

metas, err := s.ContainerMgr.List(ctx, func(meta *mgr.ContainerMeta) bool {
cons, err := s.ContainerMgr.List(ctx, func(c *mgr.Container) bool {
return true
}, option)
if err != nil {
return err
}

containerList := make([]types.Container, 0, len(metas))
containerList := make([]types.Container, 0, len(cons))

for _, m := range metas {
status, err := m.FormatStatus()
for _, c := range cons {
status, err := c.FormatStatus()
if err != nil {
return err
}

t, err := time.Parse(utils.TimeLayout, m.Created)
t, err := time.Parse(utils.TimeLayout, c.Created)
if err != nil {
return err
}

container := types.Container{
ID: m.ID,
Names: []string{m.Name},
Image: m.Config.Image,
Command: strings.Join(m.Config.Cmd, " "),
Status: status,
Created: t.UnixNano(),
Labels: m.Config.Labels,
HostConfig: m.HostConfig,
var netSettings *types.ContainerNetworkSettings
if c.NetworkSettings != nil {
netSettings = &types.ContainerNetworkSettings{
Networks: c.NetworkSettings.Networks,
}
}

if m.NetworkSettings != nil {
container.NetworkSettings = &types.ContainerNetworkSettings{
Networks: m.NetworkSettings.Networks,
}
container := types.Container{
ID: c.ID,
Names: []string{c.Name},
Image: c.Config.Image,
Command: strings.Join(c.Config.Cmd, " "),
Status: status,
Created: t.UnixNano(),
Labels: c.Config.Labels,
HostConfig: c.HostConfig,
NetworkSettings: netSettings,
}

containerList = append(containerList, container)
Expand Down
4 changes: 2 additions & 2 deletions apis/server/image_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func (s *Server) removeImage(ctx context.Context, rw http.ResponseWriter, req *h
return err
}

containers, err := s.ContainerMgr.List(ctx, func(meta *mgr.ContainerMeta) bool {
return meta.Image == image.ID
containers, err := s.ContainerMgr.List(ctx, func(c *mgr.Container) bool {
return c.Image == image.ID
}, &mgr.ContainerListOption{All: true})
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions ctrd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ func (c *Client) createContainer(ctx context.Context, ref, id string, container
img, err := wrapperCli.client.GetImage(ctx, ref)
if err != nil {
if errdefs.IsNotFound(err) {
return errors.Wrap(errtypes.ErrNotfound, "image")
return errors.Wrapf(errtypes.ErrNotfound, "image %s", ref)
}
return errors.Wrapf(err, "failed to get image: %s", ref)
return errors.Wrapf(err, "failed to get image %s", ref)
}

logrus.Infof("success to get image: %s, container id: %s", img.Name(), id)
logrus.Infof("success to get image %s, container id %s", img.Name(), id)

// create container
specOptions := []oci.SpecOpts{
Expand All @@ -375,13 +375,13 @@ func (c *Client) createContainer(ctx context.Context, ref, id string, container

// check snapshot exist or not.
if _, err := c.GetSnapshot(ctx, id); err != nil {
return errors.Wrapf(err, "failed to create container, id: %s", id)
return errors.Wrapf(err, "failed to create container %s", id)
}
options = append(options, containerd.WithSnapshot(id))

nc, err := wrapperCli.client.NewContainer(ctx, id, options...)
if err != nil {
return errors.Wrapf(err, "failed to create container, id: %s", id)
return errors.Wrapf(err, "failed to create container %s", id)
}

defer func() {
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewDaemon(cfg *config.Config) *Daemon {
Buckets: []meta.Bucket{
{
Name: meta.MetaJSONFile,
Type: reflect.TypeOf(mgr.ContainerMeta{}),
Type: reflect.TypeOf(mgr.Container{}),
},
},
})
Expand Down
Loading

0 comments on commit 17d4408

Please sign in to comment.