diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-04-16 10:52:58 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-16 10:52:58 -0700 |
commit | 5def21140038fc34cee9707d3069bf52adc24577 (patch) | |
tree | 2582da76814b9eb636f11070b00a2dbb596754a5 /pkg/api/handlers | |
parent | 8c4d4b58eee5e6d759199c36560af738aadc521d (diff) | |
parent | ba430bfe5ef65d5aa5ffa1fef0087da76aafcc35 (diff) | |
download | podman-5def21140038fc34cee9707d3069bf52adc24577.tar.gz podman-5def21140038fc34cee9707d3069bf52adc24577.tar.bz2 podman-5def21140038fc34cee9707d3069bf52adc24577.zip |
Merge pull request #5842 from baude/v2bloat2
podman v2 remove bloat v2
Diffstat (limited to 'pkg/api/handlers')
-rw-r--r-- | pkg/api/handlers/compat/containers.go | 197 | ||||
-rw-r--r-- | pkg/api/handlers/compat/swagger.go | 4 | ||||
-rw-r--r-- | pkg/api/handlers/compat/unsupported.go | 4 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/containers_create.go | 4 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/pods.go | 3 | ||||
-rw-r--r-- | pkg/api/handlers/swagger/swagger.go (renamed from pkg/api/handlers/swagger.go) | 19 | ||||
-rw-r--r-- | pkg/api/handlers/types.go | 196 | ||||
-rw-r--r-- | pkg/api/handlers/utils/containers.go | 14 | ||||
-rw-r--r-- | pkg/api/handlers/utils/errors.go | 26 |
9 files changed, 220 insertions, 247 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 +} diff --git a/pkg/api/handlers/compat/swagger.go b/pkg/api/handlers/compat/swagger.go index f1aabf987..ce83aa32f 100644 --- a/pkg/api/handlers/compat/swagger.go +++ b/pkg/api/handlers/compat/swagger.go @@ -1,7 +1,7 @@ package compat import ( - "github.com/containers/libpod/pkg/api/handlers/utils" + "github.com/containers/libpod/pkg/domain/entities" "github.com/containers/storage/pkg/archive" ) @@ -10,7 +10,7 @@ import ( type swagCtrCreateResponse struct { // in:body Body struct { - utils.ContainerCreateResponse + entities.ContainerCreateResponse } } diff --git a/pkg/api/handlers/compat/unsupported.go b/pkg/api/handlers/compat/unsupported.go index d9c3c3f49..55660882f 100644 --- a/pkg/api/handlers/compat/unsupported.go +++ b/pkg/api/handlers/compat/unsupported.go @@ -4,6 +4,8 @@ import ( "fmt" "net/http" + "github.com/containers/libpod/pkg/domain/entities" + "github.com/containers/libpod/pkg/api/handlers/utils" log "github.com/sirupsen/logrus" ) @@ -13,5 +15,5 @@ func UnsupportedHandler(w http.ResponseWriter, r *http.Request) { log.Infof("Request Failed: %s", msg) utils.WriteJSON(w, http.StatusInternalServerError, - utils.ErrorModel{Message: msg}) + entities.ErrorModel{Message: msg}) } diff --git a/pkg/api/handlers/libpod/containers_create.go b/pkg/api/handlers/libpod/containers_create.go index 38a341a89..f64132d55 100644 --- a/pkg/api/handlers/libpod/containers_create.go +++ b/pkg/api/handlers/libpod/containers_create.go @@ -4,6 +4,8 @@ import ( "encoding/json" "net/http" + "github.com/containers/libpod/pkg/domain/entities" + "github.com/containers/libpod/libpod" "github.com/containers/libpod/pkg/api/handlers/utils" "github.com/containers/libpod/pkg/specgen" @@ -29,6 +31,6 @@ func CreateContainer(w http.ResponseWriter, r *http.Request) { utils.InternalServerError(w, err) return } - response := utils.ContainerCreateResponse{ID: ctr.ID()} + response := entities.ContainerCreateResponse{ID: ctr.ID()} utils.WriteJSON(w, http.StatusCreated, response) } diff --git a/pkg/api/handlers/libpod/pods.go b/pkg/api/handlers/libpod/pods.go index 81cab1ede..92556bb61 100644 --- a/pkg/api/handlers/libpod/pods.go +++ b/pkg/api/handlers/libpod/pods.go @@ -74,8 +74,9 @@ func PodInspect(w http.ResponseWriter, r *http.Request) { utils.Error(w, "Something went wrong", http.StatusInternalServerError, err) return } + report := entities.PodInspectReport{ - PodInspect: podData, + InspectPodData: podData, } utils.WriteResponse(w, http.StatusOK, report) } diff --git a/pkg/api/handlers/swagger.go b/pkg/api/handlers/swagger/swagger.go index 33a9fdd58..ba97a4755 100644 --- a/pkg/api/handlers/swagger.go +++ b/pkg/api/handlers/swagger/swagger.go @@ -1,9 +1,10 @@ -package handlers +package swagger import ( "github.com/containers/libpod/libpod" "github.com/containers/libpod/libpod/define" "github.com/containers/libpod/libpod/image" + "github.com/containers/libpod/pkg/api/handlers" "github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/inspect" "github.com/docker/docker/api/types" @@ -14,7 +15,7 @@ import ( type swagHistory struct { // in:body Body struct { - HistoryResponse + handlers.HistoryResponse } } @@ -23,7 +24,7 @@ type swagHistory struct { type swagImageInspect struct { // in:body Body struct { - ImageInspect + handlers.ImageInspect } } @@ -45,7 +46,7 @@ type swagLibpodImagesImportResponse struct { // swagger:response DocsLibpodImagesPullResponse type swagLibpodImagesPullResponse struct { // in:body - Body LibpodImagesPullReport + Body handlers.LibpodImagesPullReport } // Delete response @@ -77,14 +78,14 @@ type swagLibpodInspectImageResponse struct { // swagger:response DocsContainerPruneReport type swagContainerPruneReport struct { // in: body - Body []ContainersPruneReport + Body []handlers.ContainersPruneReport } // Prune containers // swagger:response DocsLibpodPruneResponse type swagLibpodContainerPruneReport struct { // in: body - Body []LibpodContainersPruneReport + Body []handlers.LibpodContainersPruneReport } // Inspect container @@ -101,7 +102,7 @@ type swagContainerInspectResponse struct { type swagContainerTopResponse struct { // in:body Body struct { - ContainerTopOKBody + handlers.ContainerTopOKBody } } @@ -110,7 +111,7 @@ type swagContainerTopResponse struct { type swagPodTopResponse struct { // in:body Body struct { - PodTopOKBody + handlers.PodTopOKBody } } @@ -153,6 +154,6 @@ type swagInspectVolumeResponse struct { type swagImageTreeResponse struct { // in:body Body struct { - ImageTreeResponse + handlers.ImageTreeResponse } } diff --git a/pkg/api/handlers/types.go b/pkg/api/handlers/types.go index 0fe6ae6a7..f402da064 100644 --- a/pkg/api/handlers/types.go +++ b/pkg/api/handlers/types.go @@ -5,12 +5,9 @@ import ( "encoding/json" "fmt" "strconv" - "strings" "time" "github.com/containers/image/v5/manifest" - "github.com/containers/libpod/libpod" - "github.com/containers/libpod/libpod/define" "github.com/containers/libpod/libpod/events" libpodImage "github.com/containers/libpod/libpod/image" "github.com/containers/libpod/pkg/domain/entities" @@ -146,10 +143,6 @@ type PodCreateConfig struct { Share string `json:"share"` } -type ErrorModel struct { - Message string `json:"message"` -} - type Event struct { dockerEvents.Message } @@ -378,195 +371,6 @@ func ImageDataToImageInspect(ctx context.Context, l *libpodImage.Image) (*ImageI } -func LibpodToContainer(l *libpod.Container, sz bool) (*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 &Container{docker.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, - }, - docker.ContainerCreateConfig{}, - }, nil -} - -func LibpodToContainerJSON(l *libpod.Container, sz bool) (*docker.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 := docker.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 := dockerContainer.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 := docker.GraphDriverData{} - if err := json.Unmarshal(g, &graphDriver); err != nil { - return nil, err - } - - cb := docker.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 := dockerContainer.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 := []docker.MountPoint{} - if err := json.Unmarshal(m, &mounts); err != nil { - return nil, err - } - - networkSettingsDefault := docker.DefaultNetworkSettings{ - EndpointID: "", - Gateway: "", - GlobalIPv6Address: "", - GlobalIPv6PrefixLen: 0, - IPAddress: "", - IPPrefixLen: 0, - IPv6Gateway: "", - MacAddress: l.Config().StaticMAC.String(), - } - - networkSettings := docker.NetworkSettings{ - NetworkSettingsBase: docker.NetworkSettingsBase{}, - DefaultNetworkSettings: networkSettingsDefault, - Networks: nil, - } - - c := docker.ContainerJSON{ - ContainerJSONBase: &cb, - Mounts: mounts, - Config: &config, - NetworkSettings: &networkSettings, - } - return &c, nil -} - // portsToPortSet converts libpods exposed ports to dockers structs func portsToPortSet(input map[string]struct{}) (nat.PortSet, error) { ports := make(nat.PortSet) diff --git a/pkg/api/handlers/utils/containers.go b/pkg/api/handlers/utils/containers.go index d1107f67c..bc247c4ae 100644 --- a/pkg/api/handlers/utils/containers.go +++ b/pkg/api/handlers/utils/containers.go @@ -6,22 +6,14 @@ import ( "time" "github.com/containers/libpod/cmd/podman/shared" - createconfig "github.com/containers/libpod/pkg/spec" - "github.com/containers/libpod/libpod" "github.com/containers/libpod/libpod/define" + "github.com/containers/libpod/pkg/domain/entities" + createconfig "github.com/containers/libpod/pkg/spec" "github.com/gorilla/schema" "github.com/pkg/errors" ) -// ContainerCreateResponse is the response struct for creating a container -type ContainerCreateResponse struct { - // ID of the container created - ID string `json:"Id"` - // Warnings during container creation - Warnings []string `json:"Warnings"` -} - func WaitContainer(w http.ResponseWriter, r *http.Request) (int32, error) { var ( err error @@ -77,7 +69,7 @@ func CreateContainer(ctx context.Context, w http.ResponseWriter, runtime *libpod return } - response := ContainerCreateResponse{ + response := entities.ContainerCreateResponse{ ID: ctr.ID(), Warnings: []string{}} diff --git a/pkg/api/handlers/utils/errors.go b/pkg/api/handlers/utils/errors.go index 8d499f40b..aafc64353 100644 --- a/pkg/api/handlers/utils/errors.go +++ b/pkg/api/handlers/utils/errors.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/containers/libpod/libpod/define" + "github.com/containers/libpod/pkg/domain/entities" "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -20,7 +21,7 @@ var ( func Error(w http.ResponseWriter, apiMessage string, code int, err error) { // Log detailed message of what happened to machine running podman service log.Infof("Request Failed(%s): %s", http.StatusText(code), err.Error()) - em := ErrorModel{ + em := entities.ErrorModel{ Because: (errors.Cause(err)).Error(), Message: err.Error(), ResponseCode: code, @@ -73,29 +74,6 @@ func BadRequest(w http.ResponseWriter, key string, value string, err error) { Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, e) } -type ErrorModel struct { - // API root cause formatted for automated parsing - // example: API root cause - Because string `json:"cause"` - // human error message, formatted for a human to read - // example: human error message - Message string `json:"message"` - // http response code - ResponseCode int `json:"response"` -} - -func (e ErrorModel) Error() string { - return e.Message -} - -func (e ErrorModel) Cause() error { - return errors.New(e.Because) -} - -func (e ErrorModel) Code() int { - return e.ResponseCode -} - // UnsupportedParameter logs a given param by its string name as not supported. func UnSupportedParameter(param string) { log.Infof("API parameter %q: not supported", param) |