From d924494f561bb878a2b3a7ce438d87ecb934b5fb Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Fri, 1 Nov 2019 13:03:34 -0700 Subject: Initial commit on compatible API Signed-off-by: Jhon Honce Create service command Use cd cmd/service && go build . $ systemd-socket-activate -l 8081 cmd/service/service & $ curl http://localhost:8081/v1.24/images/json Signed-off-by: Jhon Honce Correct Makefile Signed-off-by: Jhon Honce Two more stragglers Signed-off-by: Jhon Honce Report errors back as http headers Signed-off-by: Jhon Honce Split out handlers, updated output Output aligned to docker structures Signed-off-by: Jhon Honce Refactored routing, added more endpoints and types * Encapsulated all the routing information in the handler_* files. * Added more serviceapi/types, including podman additions. See Info Signed-off-by: Jhon Honce Cleaned up code, implemented info content * Move Content-Type check into serviceHandler * Custom 404 handler showing the url, mostly for debugging * Refactored images: better method names and explicit http codes * Added content to /info * Added podman fields to Info struct * Added Container struct Signed-off-by: Jhon Honce Add a bunch of endpoints containers: stop, pause, unpause, wait, rm images: tag, rmi, create (pull only) Signed-off-by: baude Add even more handlers * Add serviceapi/Error() to improve error handling * Better support for API return payloads * Renamed unimplemented to unsupported these are generic endpoints we don't intend to ever support. Swarm broken out since it uses different HTTP codes to signal that the node is not in a swarm. * Added more types * API Version broken out so it can be validated in the future Signed-off-by: Jhon Honce Refactor to introduce ServiceWriter Signed-off-by: Jhon Honce populate pods endpoints /libpod/pods/.. exists, kill, pause, prune, restart, remove, start, stop, unpause Signed-off-by: baude Add components to Version, fix Error body Signed-off-by: Jhon Honce Add images pull output, fix swarm routes * docker-py tests/integration/api_client_test.py pass 100% * docker-py tests/integration/api_image_test.py pass 4/16 + Test failures include services podman does not support Signed-off-by: Jhon Honce pods endpoint submission 2 add create and others; only top and stats is left. Signed-off-by: baude Update pull image to work from empty registry Signed-off-by: Jhon Honce pod create and container create first pass at pod and container create. the container create does not quite work yet but it is very close. pod create needs a partial rewrite. also broken off the DELETE (rm/rmi) to specific handler funcs. Signed-off-by: baude Add docker-py demos, GET .../containers/json * Update serviceapi/types to reflect libpod not podman * Refactored removeImage() to provide non-streaming return Signed-off-by: Jhon Honce create container part2 finished minimal config needed for create container. started demo.py for upcoming talk Signed-off-by: baude Stop server after honoring request * Remove casting for method calls * Improve WriteResponse() * Update Container API type to match docker API Signed-off-by: Jhon Honce fix namespace assumptions cleaned up namespace issues with libpod. Signed-off-by: baude wip Signed-off-by: baude Add sliding window when shutting down server * Added a Timeout rather than closing down service on each call * Added gorilla/schema dependency for Decode'ing query parameters * Improved error handling * Container logs returned and multiplexed for stdout and stderr * .../containers/{name}/logs?stdout=True&stderr=True * Container stats * .../containers/{name}/stats Signed-off-by: Jhon Honce Improve error handling * Add check for at least one std stream required for /containers/{id}/logs * Add check for state in /containers/{id}/top * Fill in more fields for /info * Fixed error checking in service start code Signed-off-by: Jhon Honce get rest of image tests for pass Signed-off-by: baude linting our content Signed-off-by: baude more linting Signed-off-by: baude more linting Signed-off-by: baude pruning Signed-off-by: baude [CI:DOCS]apiv2 pods migrate from using args in the url to using a json struct in body for pod create. Signed-off-by: baude fix handler_images prune prune's api changed slightly to deal with filters. Signed-off-by: baude [CI:DOCS]enabled base container create tests enabling the base container create tests which allow us to get more into the stop, kill, etc tests. many new tests now pass. Signed-off-by: baude serviceapi errors: append error message to API message I dearly hope this is not breaking any other tests but debugging "Internal Server Error" is not helpful to any user. In case, it breaks tests, we can rever the commit - that's why it's a small one. Signed-off-by: Valentin Rothberg serviceAPI: add containers/prune endpoint Signed-off-by: Valentin Rothberg add `service` make target Also remove the non-functional sub-Makefile. Signed-off-by: Valentin Rothberg add make targets for testing the service * `sudo make run-service` for running the service. * `DOCKERPY_TEST="tests/integration/api_container_test.py::ListContainersTest" \ make run-docker-py-tests` for running a specific tests. Run all tests by leaving the env variable empty. Signed-off-by: Valentin Rothberg Split handlers and server packages The files were split to help contain bloat. The api/server package will contain all code related to the functioning of the server while api/handlers will have all the code related to implementing the end points. api/server/register_* will contain the methods for registering endpoints. Additionally, they will have the comments for generating the swagger spec file. See api/handlers/version.go for a small example handler, api/handlers/containers.go contains much more complex handlers. Signed-off-by: Jhon Honce [CI:DOCS]enabled more tests Signed-off-by: baude [CI:DOCS]libpod endpoints small refactor for libpod inclusion and began adding endpoints. Signed-off-by: baude Implement /build and /events * Include crypto libraries for future ssh work Signed-off-by: Jhon Honce [CI:DOCS]more image implementations convert from using for to query structs among other changes including new endpoints. Signed-off-by: baude [CI:DOCS]add bindings for golang Signed-off-by: baude [CI:DOCS]add volume endpoints for libpod create, inspect, ls, prune, and rm Signed-off-by: baude [CI:DOCS]apiv2 healthcheck enablement wire up container healthchecks for the api. Signed-off-by: baude [CI:DOCS]Add mount endpoints via the api, allow ability to mount a container and list container mounts. Signed-off-by: baude [CI:DOCS]Add search endpoint add search endpoint with golang bindings Signed-off-by: baude [CI:DOCS]more apiv2 development misc population of methods, etc Signed-off-by: baude rebase cleanup and epoch reset Signed-off-by: baude [CI:DOCS]add more network endpoints also, add some initial error handling and convenience functions for standard endpoints. Signed-off-by: baude [CI:DOCS]use helper funcs for bindings use the methods developed to make writing bindings less duplicative and easier to use. Signed-off-by: baude [CI:DOCS]add return info for prereview begin to add return info and status codes for errors so that we can review the apiv2 Signed-off-by: baude [CI:DOCS]first pass at adding swagger docs for api Signed-off-by: baude --- pkg/bindings/containers.go | 137 ++++++++++++++++++++++++++++++++++++++++++++ pkg/bindings/generate.go | 4 ++ pkg/bindings/healthcheck.go | 19 ++++++ pkg/bindings/info.go | 3 + pkg/bindings/mount.go | 26 +++++++++ pkg/bindings/network.go | 37 ++++++++++++ pkg/bindings/play.go | 3 + pkg/bindings/pods.go | 128 +++++++++++++++++++++++++++++++++++++++++ pkg/bindings/search.go | 39 +++++++++++++ pkg/bindings/version.go | 3 + pkg/bindings/volumes.go | 60 +++++++++++++++++++ 11 files changed, 459 insertions(+) create mode 100644 pkg/bindings/containers.go create mode 100644 pkg/bindings/generate.go create mode 100644 pkg/bindings/healthcheck.go create mode 100644 pkg/bindings/info.go create mode 100644 pkg/bindings/mount.go create mode 100644 pkg/bindings/network.go create mode 100644 pkg/bindings/play.go create mode 100644 pkg/bindings/pods.go create mode 100644 pkg/bindings/search.go create mode 100644 pkg/bindings/version.go create mode 100644 pkg/bindings/volumes.go (limited to 'pkg/bindings') diff --git a/pkg/bindings/containers.go b/pkg/bindings/containers.go new file mode 100644 index 000000000..cd0b09767 --- /dev/null +++ b/pkg/bindings/containers.go @@ -0,0 +1,137 @@ +package bindings + +import ( + "fmt" + "net/http" + "strconv" + + "github.com/containers/libpod/cmd/podman/shared" + "github.com/containers/libpod/libpod" +) + +func (c Connection) ListContainers(filter []string, last int, size, sync bool) ([]shared.PsContainerOutput, error) { // nolint:typecheck + images := []shared.PsContainerOutput{} + params := make(map[string]string) + params["last"] = strconv.Itoa(last) + params["size"] = strconv.FormatBool(size) + params["sync"] = strconv.FormatBool(sync) + response, err := c.newRequest(http.MethodGet, "/containers/json", nil, params) + if err != nil { + return images, err + } + return images, response.Process(nil) +} + +func (c Connection) PruneContainers() ([]string, error) { + var ( + pruned []string + ) + response, err := c.newRequest(http.MethodPost, "/containers/prune", nil, nil) + if err != nil { + return pruned, err + } + return pruned, response.Process(nil) +} + +func (c Connection) RemoveContainer(nameOrID string, force, volumes bool) error { + params := make(map[string]string) + params["force"] = strconv.FormatBool(force) + params["vols"] = strconv.FormatBool(volumes) + response, err := c.newRequest(http.MethodDelete, fmt.Sprintf("/containers/%s", nameOrID), nil, params) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) InspectContainer(nameOrID string, size bool) (*libpod.InspectContainerData, error) { + params := make(map[string]string) + params["size"] = strconv.FormatBool(size) + response, err := c.newRequest(http.MethodGet, fmt.Sprintf("/containers/%s/json", nameOrID), nil, params) + if err != nil { + return nil, err + } + inspect := libpod.InspectContainerData{} + return &inspect, response.Process(&inspect) +} + +func (c Connection) KillContainer(nameOrID string, signal int) error { + params := make(map[string]string) + params["signal"] = strconv.Itoa(signal) + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/containers/%s/kill", nameOrID), nil, params) + if err != nil { + return err + } + return response.Process(nil) + +} +func (c Connection) ContainerLogs() {} +func (c Connection) PauseContainer(nameOrID string) error { + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/containers/%s/pause", nameOrID), nil, nil) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) RestartContainer(nameOrID string, timeout int) error { + // TODO how do we distinguish between an actual zero value and not wanting to change the timeout value + params := make(map[string]string) + params["timeout"] = strconv.Itoa(timeout) + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/containers/%s/restart", nameOrID), nil, params) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) StartContainer(nameOrID, detachKeys string) error { + params := make(map[string]string) + if len(detachKeys) > 0 { + params["detachKeys"] = detachKeys + } + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/containers/%s/start", nameOrID), nil, params) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) ContainerStats() {} +func (c Connection) ContainerTop() {} + +func (c Connection) UnpauseContainer(nameOrID string) error { + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/containers/%s/unpause", nameOrID), nil, nil) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) WaitContainer(nameOrID string) error { + _, err := http.Post(c.makeEndpoint(fmt.Sprintf("containers/%s/wait", nameOrID)), "application/json", nil) + return err +} + +func (c Connection) ContainerExists(nameOrID string) (bool, error) { + response, err := http.Get(c.makeEndpoint(fmt.Sprintf("/containers/%s/exists", nameOrID))) + if err != nil { + return false, err + } + if response.StatusCode == http.StatusOK { + return true, nil + } + return false, nil +} + +func (c Connection) StopContainer(nameOrID string, timeout int) error { + // TODO we might need to distinguish whether a timeout is desired; a zero, the int + // zero value is valid; what do folks want to do? + params := make(map[string]string) + params["t"] = strconv.Itoa(timeout) + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/containers/%s/stop", nameOrID), nil, params) + if err != nil { + return err + } + return response.Process(nil) +} diff --git a/pkg/bindings/generate.go b/pkg/bindings/generate.go new file mode 100644 index 000000000..534909062 --- /dev/null +++ b/pkg/bindings/generate.go @@ -0,0 +1,4 @@ +package bindings + +func (c Connection) GenerateKube() {} +func (c Connection) GenerateSystemd() {} diff --git a/pkg/bindings/healthcheck.go b/pkg/bindings/healthcheck.go new file mode 100644 index 000000000..32515e332 --- /dev/null +++ b/pkg/bindings/healthcheck.go @@ -0,0 +1,19 @@ +package bindings + +import ( + "fmt" + "net/http" + + "github.com/containers/libpod/libpod" +) + +func (c Connection) RunHealthCheck(nameOrID string) (*libpod.HealthCheckStatus, error) { + var ( + status libpod.HealthCheckStatus + ) + response, err := c.newRequest(http.MethodGet, fmt.Sprintf("/containers/%s/runhealthcheck", nameOrID), nil, nil) + if err != nil { + return nil, err + } + return &status, response.Process(&status) +} diff --git a/pkg/bindings/info.go b/pkg/bindings/info.go new file mode 100644 index 000000000..5f318d652 --- /dev/null +++ b/pkg/bindings/info.go @@ -0,0 +1,3 @@ +package bindings + +func (c Connection) Info() {} diff --git a/pkg/bindings/mount.go b/pkg/bindings/mount.go new file mode 100644 index 000000000..2e3d6d7f6 --- /dev/null +++ b/pkg/bindings/mount.go @@ -0,0 +1,26 @@ +package bindings + +import ( + "fmt" + "net/http" +) + +func (c Connection) MountContainer(nameOrID string) (string, error) { + var ( + path string + ) + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/containers/%s/mount", nameOrID), nil, nil) + if err != nil { + return path, err + } + return path, response.Process(&path) +} + +func (c Connection) GetMountedContainerPaths() (map[string]string, error) { + mounts := make(map[string]string) + response, err := c.newRequest(http.MethodGet, "/containers/showmounted", nil, nil) + if err != nil { + return mounts, err + } + return mounts, response.Process(&mounts) +} diff --git a/pkg/bindings/network.go b/pkg/bindings/network.go new file mode 100644 index 000000000..383615e5d --- /dev/null +++ b/pkg/bindings/network.go @@ -0,0 +1,37 @@ +package bindings + +import ( + "fmt" + "net/http" + + "github.com/containernetworking/cni/libcni" +) + +func (c Connection) CreateNetwork() {} +func (c Connection) InspectNetwork(nameOrID string) (map[string]interface{}, error) { + n := make(map[string]interface{}) + response, err := c.newRequest(http.MethodGet, fmt.Sprintf("/networks/%s/json", nameOrID), nil, nil) + if err != nil { + return n, err + } + return n, response.Process(&n) +} + +func (c Connection) RemoveNetwork(nameOrID string) error { + response, err := c.newRequest(http.MethodDelete, fmt.Sprintf("/networks/%s", nameOrID), nil, nil) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) ListNetworks() ([]*libcni.NetworkConfigList, error) { + var ( + netList []*libcni.NetworkConfigList + ) + response, err := c.newRequest(http.MethodGet, "/networks/json", nil, nil) + if err != nil { + return netList, err + } + return netList, response.Process(&netList) +} diff --git a/pkg/bindings/play.go b/pkg/bindings/play.go new file mode 100644 index 000000000..a9dee82b1 --- /dev/null +++ b/pkg/bindings/play.go @@ -0,0 +1,3 @@ +package bindings + +func (c Connection) PlayKube() {} diff --git a/pkg/bindings/pods.go b/pkg/bindings/pods.go new file mode 100644 index 000000000..eac9d2ef5 --- /dev/null +++ b/pkg/bindings/pods.go @@ -0,0 +1,128 @@ +package bindings + +import ( + "fmt" + "net/http" + "strconv" + + "github.com/containers/libpod/libpod" +) + +func (c Connection) CreatePod() error { + // TODO + return ErrNotImplemented +} + +func (c Connection) PodExists(nameOrID string) (bool, error) { + response, err := http.Get(c.makeEndpoint(fmt.Sprintf("/pods/%s/exists", nameOrID))) + if err != nil { + return false, err + } + return response.StatusCode == http.StatusOK, err +} + +func (c Connection) InspectPod(nameOrID string) (*libpod.PodInspect, error) { + inspect := libpod.PodInspect{} + response, err := c.newRequest(http.MethodGet, fmt.Sprintf("/pods/%s/json", nameOrID), nil, nil) + if err != nil { + return &inspect, err + } + return &inspect, response.Process(&inspect) +} + +func (c Connection) KillPod(nameOrID string, signal int) error { + params := make(map[string]string) + params["signal"] = strconv.Itoa(signal) + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/pods/%s/kill", nameOrID), nil, params) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) PausePod(nameOrID string) error { + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/pods/%s/pause", nameOrID), nil, nil) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) PrunePods(force bool) error { + params := make(map[string]string) + params["force"] = strconv.FormatBool(force) + response, err := c.newRequest(http.MethodPost, "/pods/prune", nil, params) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) ListPods(filters []string) (*[]libpod.PodInspect, error) { + var ( + inspect []libpod.PodInspect + ) + params := make(map[string]string) + // TODO I dont remember how to do this for []string{} + // FIXME + //params["filters"] = strconv.FormatBool(force) + response, err := c.newRequest(http.MethodPost, "/pods/json", nil, params) + if err != nil { + return &inspect, err + } + return &inspect, response.Process(&inspect) +} + +func (c Connection) RestartPod(nameOrID string) error { + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/pods/%s/restart", nameOrID), nil, nil) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) RemovePod(nameOrID string, force bool) error { + params := make(map[string]string) + params["force"] = strconv.FormatBool(force) + response, err := c.newRequest(http.MethodDelete, fmt.Sprintf("/pods/%s", nameOrID), nil, params) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) StartPod(nameOrID string) error { + response, err := c.newRequest(http.MethodDelete, fmt.Sprintf("/pods/%s/start", nameOrID), nil, nil) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) PodStats() error { + // TODO + return ErrNotImplemented +} + +func (c Connection) StopPod(nameOrID string, timeout int) error { + params := make(map[string]string) + params["t"] = strconv.Itoa(timeout) + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/pods/%s/stop", nameOrID), nil, params) + if err != nil { + return err + } + return response.Process(nil) +} + +func (c Connection) PodTop() error { + // TODO + return ErrNotImplemented // nolint:typecheck +} + +func (c Connection) UnpausePod(nameOrID string) error { + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/pods/%s/unpause", nameOrID), nil, nil) + if err != nil { + return err + } + return response.Process(nil) +} diff --git a/pkg/bindings/search.go b/pkg/bindings/search.go new file mode 100644 index 000000000..0f462357c --- /dev/null +++ b/pkg/bindings/search.go @@ -0,0 +1,39 @@ +package bindings + +import ( + "net/http" + "strconv" + + "github.com/containers/libpod/libpod/image" +) + +type ImageSearchFilters struct { + Automated bool `json:"automated"` + Official bool `json:"official"` + Stars int `json:"stars"` +} + +// TODO This method can be concluded when we determine how we want the filters to work on the +// API end +func (i *ImageSearchFilters) ToMapJSON() string { + return "" +} + +func (c Connection) SearchImages(term string, limit int, filters *ImageSearchFilters) ([]image.SearchResult, error) { + var ( + searchResults []image.SearchResult + ) + params := make(map[string]string) + params["term"] = term + if limit > 0 { + params["limit"] = strconv.Itoa(limit) + } + if filters != nil { + params["filters"] = filters.ToMapJSON() + } + response, err := c.newRequest(http.MethodGet, "/images/search", nil, params) + if err != nil { + return searchResults, nil + } + return searchResults, response.Process(&searchResults) +} diff --git a/pkg/bindings/version.go b/pkg/bindings/version.go new file mode 100644 index 000000000..c833a644c --- /dev/null +++ b/pkg/bindings/version.go @@ -0,0 +1,3 @@ +package bindings + +func (c Connection) Version() {} diff --git a/pkg/bindings/volumes.go b/pkg/bindings/volumes.go new file mode 100644 index 000000000..27e6f9efa --- /dev/null +++ b/pkg/bindings/volumes.go @@ -0,0 +1,60 @@ +package bindings + +import ( + "fmt" + "net/http" + "strconv" + + "github.com/containers/libpod/libpod" + "github.com/containers/libpod/pkg/api/handlers" +) + +func (c Connection) CreateVolume(config handlers.VolumeCreateConfig) (string, error) { + var ( + volumeID string + ) + response, err := c.newRequest(http.MethodPost, "/volumes/create", nil, nil) + if err != nil { + return volumeID, err + } + return volumeID, response.Process(&volumeID) +} + +func (c Connection) InspectVolume(nameOrID string) (*libpod.InspectVolumeData, error) { + var ( + inspect libpod.InspectVolumeData + ) + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/volumes/%s/json", nameOrID), nil, nil) + if err != nil { + return &inspect, err + } + return &inspect, response.Process(&inspect) +} + +func (c Connection) ListVolumes() error { + // TODO + // The API side of things for this one does a lot in main and therefore + // is not implemented yet. + return ErrNotImplemented // nolint:typecheck +} + +func (c Connection) PruneVolumes() ([]string, error) { + var ( + pruned []string + ) + response, err := c.newRequest(http.MethodPost, "/volumes/prune", nil, nil) + if err != nil { + return pruned, err + } + return pruned, response.Process(&pruned) +} + +func (c Connection) RemoveVolume(nameOrID string, force bool) error { + params := make(map[string]string) + params["force"] = strconv.FormatBool(force) + response, err := c.newRequest(http.MethodPost, fmt.Sprintf("/volumes/prune", nameOrID), nil, params) + if err != nil { + return err + } + return response.Process(nil) +} -- cgit v1.2.3-54-g00ecf