-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
podman http api client #51
Merged
Merged
Changes from 33 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
eeb5c43
podman http api client initial commit
towe75 8c7b915
Merge branch 'master' into f-http-api
towe75 951c53a
Implement ContainerStart http api call
towe75 e9d30e1
Implement ContailerDelete http api call
towe75 d0591f2
Switch podman repo to opensuse
towe75 de69df0
Implement ContainerInspect and SystemInfo http api calls
towe75 ab0bcba
Linter
towe75 5d363e6
License headers
towe75 4ac65cf
linter, fmt
towe75 6f05360
Remove separate go setup, rely on base image
towe75 b2c8e24
Makefile improvements
towe75 4ca9b04
implemented api v2 create container
towe75 819cd82
Merged SignalTask support and linter changes
towe75 84bb809
Improved tmpfs unittest
towe75 f06c899
Implement ContainerKill / SignalTask http api call
towe75 c9bb044
Fix SignalTask unittest
towe75 34807fd
Implement ContainerStats http api call
towe75 220228c
Code cleanup
towe75 745fa54
Removed varlink from tests
towe75 dac3c0d
Remove varlink code
towe75 c394552
Fix test, lint
towe75 d49b67f
Pull image before create container
towe75 4a5c6ee
Fix ContainerInstpect, adopt some loglevels
towe75 b2cb7a3
Fixed various bugs
towe75 d1eddc5
Comments
towe75 3241eb9
Merge branch 'master' into f-http-api
towe75 205f71e
Code review #51
towe75 48050ff
Code review #51
towe75 1e6ba5a
Change license headers to MPL-2
towe75 072b63b
Code review #51
towe75 7f6d2f2
Removed license headers completely
towe75 c952215
Code review #51
towe75 4e201ba
Added a ClientConfig struct with default values
towe75 f52f9ee
Review #51
towe75 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,11 @@ cd nomad-driver-podman | |
- Linux host with `podman` installed | ||
- For rootless containers you need a system supporting cgroup V2 and a few other things, follow [this tutorial](https://github.com/containers/libpod/blob/master/docs/tutorials/rootless_tutorial.md) | ||
|
||
You need a varlink enabled podman binary and a system socket activation unit, | ||
see https://podman.io/blogs/2019/01/16/podman-varlink.html. | ||
You need a 2.x podman binary and a system socket activation unit, | ||
see https://www.redhat.com/sysadmin/podmans-new-rest-api | ||
|
||
nomad agent, nomad-driver-podman and podman will reside on the same host, so you | ||
do not have to worry about the ssh aspects of podman varlink. | ||
do not have to worry about the ssh aspects of the podman api. | ||
|
||
Ensure that nomad can find the plugin, see [plugin_dir](https://www.nomadproject.io/docs/configuration/index.html#plugin_dir) | ||
|
||
|
@@ -334,22 +334,16 @@ GRUB_CMDLINE_LINUX_DEFAULT="quiet cgroup_enable=memory swapaccount=1 systemd.uni | |
|
||
`sudo update-grub` | ||
|
||
ensure that podman varlink is running | ||
``` | ||
$ systemctl --user status io.podman | ||
● io.podman.service - Podman Remote API Service | ||
Loaded: loaded (/usr/lib/systemd/user/io.podman.service; disabled; vendor preset: enabled) | ||
Active: active (running) since Wed 2020-07-01 16:01:41 EDT; 7s ago | ||
TriggeredBy: ● io.podman.socket | ||
Docs: man:podman-varlink(1) | ||
Main PID: 25091 (podman) | ||
Tasks: 29 (limit: 18808) | ||
Memory: 17.5M | ||
CPU: 184ms | ||
CGroup: /user.slice/user-1000.slice/[email protected]/io.podman.service | ||
├─25091 /usr/bin/podman varlink unix:/run/user/1000/podman/io.podman --timeout=60000 --cgroup-manager=systemd | ||
├─25121 /usr/bin/podman varlink unix:/run/user/1000/podman/io.podman --timeout=60000 --cgroup-manager=systemd | ||
└─25125 /usr/bin/podman | ||
ensure that podman socket is running | ||
``` | ||
$ systemctl --user status podman.socket | ||
* podman.socket - Podman API Socket | ||
Loaded: loaded (/usr/lib/systemd/user/podman.socket; disabled; vendor preset: disabled) | ||
Active: active (listening) since Sat 2020-10-31 19:21:29 CET; 22h ago | ||
Triggers: * podman.service | ||
Docs: man:podman-system-service(1) | ||
Listen: /run/user/1000/podman/podman.sock (Stream) | ||
CGroup: /user.slice/user-1000.slice/[email protected]/podman.socket | ||
``` | ||
|
||
ensure that you have a recent version of [crun](https://github.com/containers/crun/) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
type API struct { | ||
baseUrl string | ||
httpClient *http.Client | ||
logger hclog.Logger | ||
} | ||
|
||
type ClientConfig struct { | ||
SocketPath string | ||
HttpTimeout time.Duration | ||
} | ||
|
||
func DefaultClientConfig() ClientConfig { | ||
cfg := ClientConfig{ | ||
HttpTimeout: 60 * time.Second, | ||
} | ||
uid := os.Getuid() | ||
// are we root? | ||
if uid == 0 { | ||
cfg.SocketPath = "unix:/run/podman/podman.sock" | ||
} else { | ||
// not? then let's try the default per-user socket location | ||
cfg.SocketPath = fmt.Sprintf("unix:/run/user/%d/podman/podman.sock", uid) | ||
} | ||
return cfg | ||
} | ||
|
||
func NewClient(logger hclog.Logger, config ClientConfig) *API { | ||
ac := &API{ | ||
logger: logger, | ||
} | ||
|
||
baseUrl := config.SocketPath | ||
ac.logger.Debug("http baseurl", "url", baseUrl) | ||
ac.httpClient = &http.Client{ | ||
Timeout: config.HttpTimeout, | ||
} | ||
if strings.HasPrefix(baseUrl, "unix:") { | ||
ac.baseUrl = "http://u" | ||
path := strings.TrimPrefix(baseUrl, "unix:") | ||
ac.httpClient.Transport = &http.Transport{ | ||
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return net.Dial("unix", path) | ||
}, | ||
} | ||
} else { | ||
ac.baseUrl = baseUrl | ||
} | ||
|
||
return ac | ||
} | ||
|
||
func (c *API) Do(req *http.Request) (*http.Response, error) { | ||
res, err := c.httpClient.Do(req) | ||
return res, err | ||
} | ||
|
||
func (c *API) Get(ctx context.Context, path string) (*http.Response, error) { | ||
req, err := http.NewRequestWithContext(ctx, "GET", c.baseUrl+path, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c.Do(req) | ||
} | ||
|
||
func (c *API) Post(ctx context.Context, path string, body io.Reader) (*http.Response, error) { | ||
req, err := http.NewRequestWithContext(ctx, "POST", c.baseUrl+path, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
return c.Do(req) | ||
} | ||
|
||
func (c *API) Delete(ctx context.Context, path string) (*http.Response, error) { | ||
req, err := http.NewRequestWithContext(ctx, "DELETE", c.baseUrl+path, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return c.Do(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// ContainerCreate creates a new container | ||
func (c *API) ContainerCreate(ctx context.Context, create SpecGenerator) (ContainerCreateResponse, error) { | ||
|
||
response := ContainerCreateResponse{} | ||
|
||
jsonString, err := json.Marshal(create) | ||
if err != nil { | ||
return response, err | ||
} | ||
// fmt.Println(string(jsonString)) | ||
|
||
res, err := c.Post(ctx, "/v1.0.0/libpod/containers/create", bytes.NewBuffer(jsonString)) | ||
if err != nil { | ||
return response, err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
if res.StatusCode != http.StatusCreated { | ||
body, _ := ioutil.ReadAll(res.Body) | ||
return response, fmt.Errorf("unknown error, status code: %d: %s", res.StatusCode, body) | ||
} | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return response, err | ||
} | ||
err = json.Unmarshal(body, &response) | ||
if err != nil { | ||
return response, err | ||
} | ||
|
||
return response, err | ||
} | ||
|
||
type ContainerCreateRequest struct { | ||
// Name is the name the container will be given. | ||
// If no name is provided, one will be randomly generated. | ||
// Optional. | ||
Name string `json:"name,omitempty"` | ||
|
||
// Command is the container's command. | ||
// If not given and Image is specified, this will be populated by the | ||
// image's configuration. | ||
// Optional. | ||
Command []string `json:"command,omitempty"` | ||
|
||
// Entrypoint is the container's entrypoint. | ||
// If not given and Image is specified, this will be populated by the | ||
// image's configuration. | ||
// Optional. | ||
Entrypoint []string `json:"entrypoint,omitempty"` | ||
|
||
// WorkDir is the container's working directory. | ||
// If unset, the default, /, will be used. | ||
// Optional. | ||
WorkDir string `json:"work_dir,omitempty"` | ||
// Env is a set of environment variables that will be set in the | ||
// container. | ||
// Optional. | ||
Env map[string]string `json:"env,omitempty"` | ||
} | ||
|
||
type ContainerCreateResponse struct { | ||
Id string | ||
Warnings []string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// ContainerDelete deletes a container. | ||
// It takes the name or ID of a container. | ||
func (c *API) ContainerDelete(ctx context.Context, name string, force bool, deleteVolumes bool) error { | ||
|
||
res, err := c.Delete(ctx, fmt.Sprintf("/v1.0.0/libpod/containers/%s?force=%t&v=%t", name, force, deleteVolumes)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
if res.StatusCode == http.StatusNoContent { | ||
return nil | ||
} | ||
return fmt.Errorf("unknown error, status code: %d", res.StatusCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// ContainerInspect data takes a name or ID of a container returns the inspection data | ||
func (c *API) ContainerInspect(ctx context.Context, name string) (InspectContainerData, error) { | ||
|
||
var inspectData InspectContainerData | ||
|
||
res, err := c.Get(ctx, fmt.Sprintf("/v1.0.0/libpod/containers/%s/json", name)) | ||
if err != nil { | ||
return inspectData, err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return inspectData, fmt.Errorf("unknown error, status code: %d", res.StatusCode) | ||
} | ||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return inspectData, err | ||
} | ||
err = json.Unmarshal(body, &inspectData) | ||
if err != nil { | ||
return inspectData, err | ||
} | ||
|
||
return inspectData, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// ContainerKill sends a signal to a container | ||
func (c *API) ContainerKill(ctx context.Context, name string, signal string) error { | ||
|
||
res, err := c.Post(ctx, fmt.Sprintf("/v1.0.0/libpod/containers/%s/kill?signal=%s", name, signal), nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
if res.StatusCode == http.StatusNoContent { | ||
return nil | ||
} | ||
return fmt.Errorf("unknown error, status code: %d", res.StatusCode) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.