diff options
author | Brent Baude <bbaude@redhat.com> | 2020-04-16 08:39:34 -0500 |
---|---|---|
committer | Brent Baude <bbaude@redhat.com> | 2020-04-16 12:04:46 -0500 |
commit | ba430bfe5ef65d5aa5ffa1fef0087da76aafcc35 (patch) | |
tree | 3554a8bc2a5add5feee1d008d594665f82279fb3 /pkg/api/handlers/compat/containers.go | |
parent | 8857ba20a0651e8fa71762da90d774e3aa290883 (diff) | |
download | podman-ba430bfe5ef65d5aa5ffa1fef0087da76aafcc35.tar.gz podman-ba430bfe5ef65d5aa5ffa1fef0087da76aafcc35.tar.bz2 podman-ba430bfe5ef65d5aa5ffa1fef0087da76aafcc35.zip |
podman v2 remove bloat v2
rid ourseleves of libpod references in v2 client
Signed-off-by: Brent Baude <bbaude@redhat.com>
Diffstat (limited to 'pkg/api/handlers/compat/containers.go')
-rw-r--r-- | pkg/api/handlers/compat/containers.go | 197 |
1 files changed, 195 insertions, 2 deletions
diff --git a/pkg/api/handlers/compat/containers.go b/pkg/api/handlers/compat/containers.go index 3f6aca502..239e41af4 100644 --- a/pkg/api/handlers/compat/containers.go +++ b/pkg/api/handlers/compat/containers.go @@ -2,6 +2,7 @@ package compat import ( "encoding/binary" + "encoding/json" "fmt" "net/http" "strconv" @@ -16,6 +17,9 @@ import ( "github.com/containers/libpod/pkg/api/handlers/utils" "github.com/containers/libpod/pkg/signal" "github.com/containers/libpod/pkg/util" + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" + "github.com/docker/go-connections/nat" "github.com/gorilla/schema" "github.com/pkg/errors" log "github.com/sirupsen/logrus" @@ -96,7 +100,7 @@ func ListContainers(w http.ResponseWriter, r *http.Request) { // TODO filters still need to be applied var list = make([]*handlers.Container, len(containers)) for i, ctnr := range containers { - api, err := handlers.LibpodToContainer(ctnr, query.Size) + api, err := LibpodToContainer(ctnr, query.Size) if err != nil { utils.InternalServerError(w, err) return @@ -126,7 +130,7 @@ func GetContainer(w http.ResponseWriter, r *http.Request) { utils.ContainerNotFound(w, name, err) return } - api, err := handlers.LibpodToContainerJSON(ctnr, query.Size) + api, err := LibpodToContainerJSON(ctnr, query.Size) if err != nil { utils.InternalServerError(w, err) return @@ -341,3 +345,192 @@ func LogsFromContainer(w http.ResponseWriter, r *http.Request) { } } } + +func LibpodToContainer(l *libpod.Container, sz bool) (*handlers.Container, error) { + imageId, imageName := l.Image() + + var ( + err error + sizeRootFs int64 + sizeRW int64 + state define.ContainerStatus + ) + + if state, err = l.State(); err != nil { + return nil, err + } + stateStr := state.String() + if stateStr == "configured" { + stateStr = "created" + } + + if sz { + if sizeRW, err = l.RWSize(); err != nil { + return nil, err + } + if sizeRootFs, err = l.RootFsSize(); err != nil { + return nil, err + } + } + + return &handlers.Container{Container: types.Container{ + ID: l.ID(), + Names: []string{fmt.Sprintf("/%s", l.Name())}, + Image: imageName, + ImageID: imageId, + Command: strings.Join(l.Command(), " "), + Created: l.CreatedTime().Unix(), + Ports: nil, + SizeRw: sizeRW, + SizeRootFs: sizeRootFs, + Labels: l.Labels(), + State: stateStr, + Status: "", + HostConfig: struct { + NetworkMode string `json:",omitempty"` + }{ + "host"}, + NetworkSettings: nil, + Mounts: nil, + }, + ContainerCreateConfig: types.ContainerCreateConfig{}, + }, nil +} + +func LibpodToContainerJSON(l *libpod.Container, sz bool) (*types.ContainerJSON, error) { + _, imageName := l.Image() + inspect, err := l.Inspect(sz) + if err != nil { + return nil, err + } + i, err := json.Marshal(inspect.State) + if err != nil { + return nil, err + } + state := types.ContainerState{} + if err := json.Unmarshal(i, &state); err != nil { + return nil, err + } + + // docker considers paused to be running + if state.Paused { + state.Running = true + } + + h, err := json.Marshal(inspect.HostConfig) + if err != nil { + return nil, err + } + hc := container.HostConfig{} + if err := json.Unmarshal(h, &hc); err != nil { + return nil, err + } + g, err := json.Marshal(inspect.GraphDriver) + if err != nil { + return nil, err + } + graphDriver := types.GraphDriverData{} + if err := json.Unmarshal(g, &graphDriver); err != nil { + return nil, err + } + + cb := types.ContainerJSONBase{ + ID: l.ID(), + Created: l.CreatedTime().String(), + Path: "", + Args: nil, + State: &state, + Image: imageName, + ResolvConfPath: inspect.ResolvConfPath, + HostnamePath: inspect.HostnamePath, + HostsPath: inspect.HostsPath, + LogPath: l.LogPath(), + Node: nil, + Name: fmt.Sprintf("/%s", l.Name()), + RestartCount: 0, + Driver: inspect.Driver, + Platform: "linux", + MountLabel: inspect.MountLabel, + ProcessLabel: inspect.ProcessLabel, + AppArmorProfile: inspect.AppArmorProfile, + ExecIDs: inspect.ExecIDs, + HostConfig: &hc, + GraphDriver: graphDriver, + SizeRw: inspect.SizeRw, + SizeRootFs: &inspect.SizeRootFs, + } + + stopTimeout := int(l.StopTimeout()) + + ports := make(nat.PortSet) + for p := range inspect.HostConfig.PortBindings { + splitp := strings.Split(p, "/") + port, err := nat.NewPort(splitp[0], splitp[1]) + if err != nil { + return nil, err + } + ports[port] = struct{}{} + } + + config := container.Config{ + Hostname: l.Hostname(), + Domainname: inspect.Config.DomainName, + User: l.User(), + AttachStdin: inspect.Config.AttachStdin, + AttachStdout: inspect.Config.AttachStdout, + AttachStderr: inspect.Config.AttachStderr, + ExposedPorts: ports, + Tty: inspect.Config.Tty, + OpenStdin: inspect.Config.OpenStdin, + StdinOnce: inspect.Config.StdinOnce, + Env: inspect.Config.Env, + Cmd: inspect.Config.Cmd, + Healthcheck: nil, + ArgsEscaped: false, + Image: imageName, + Volumes: nil, + WorkingDir: l.WorkingDir(), + Entrypoint: l.Entrypoint(), + NetworkDisabled: false, + MacAddress: "", + OnBuild: nil, + Labels: l.Labels(), + StopSignal: string(l.StopSignal()), + StopTimeout: &stopTimeout, + Shell: nil, + } + + m, err := json.Marshal(inspect.Mounts) + if err != nil { + return nil, err + } + mounts := []types.MountPoint{} + if err := json.Unmarshal(m, &mounts); err != nil { + return nil, err + } + + networkSettingsDefault := types.DefaultNetworkSettings{ + EndpointID: "", + Gateway: "", + GlobalIPv6Address: "", + GlobalIPv6PrefixLen: 0, + IPAddress: "", + IPPrefixLen: 0, + IPv6Gateway: "", + MacAddress: l.Config().StaticMAC.String(), + } + + networkSettings := types.NetworkSettings{ + NetworkSettingsBase: types.NetworkSettingsBase{}, + DefaultNetworkSettings: networkSettingsDefault, + Networks: nil, + } + + c := types.ContainerJSON{ + ContainerJSONBase: &cb, + Mounts: mounts, + Config: &config, + NetworkSettings: &networkSettings, + } + return &c, nil +} |