Skip to content

Commit

Permalink
Merge pull request #8767 from baude/bindings3pods
Browse files Browse the repository at this point in the history
podman v3 pod bindings
  • Loading branch information
openshift-merge-robot authored Dec 18, 2020
2 parents f568658 + 9ec7603 commit a73c76d
Show file tree
Hide file tree
Showing 19 changed files with 1,468 additions and 107 deletions.
113 changes: 75 additions & 38 deletions pkg/bindings/pods/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package pods

import (
"context"
"errors"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/containers/podman/v2/pkg/api/handlers"
Expand All @@ -15,10 +13,14 @@ import (
jsoniter "github.com/json-iterator/go"
)

func CreatePodFromSpec(ctx context.Context, s *specgen.PodSpecGenerator) (*entities.PodCreateReport, error) {
func CreatePodFromSpec(ctx context.Context, s *specgen.PodSpecGenerator, options *CreateOptions) (*entities.PodCreateReport, error) {
var (
pcr entities.PodCreateReport
)
if options == nil {
options = new(CreateOptions)
}
_ = options
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -49,10 +51,14 @@ func Exists(ctx context.Context, nameOrID string) (bool, error) {
}

// Inspect returns low-level information about the given pod.
func Inspect(ctx context.Context, nameOrID string) (*entities.PodInspectReport, error) {
func Inspect(ctx context.Context, nameOrID string, options *InspectOptions) (*entities.PodInspectReport, error) {
var (
report entities.PodInspectReport
)
if options == nil {
options = new(InspectOptions)
}
_ = options
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
Expand All @@ -66,17 +72,20 @@ func Inspect(ctx context.Context, nameOrID string) (*entities.PodInspectReport,

// Kill sends a SIGTERM to all the containers in a pod. The optional signal parameter
// can be used to override SIGTERM.
func Kill(ctx context.Context, nameOrID string, signal *string) (*entities.PodKillReport, error) {
func Kill(ctx context.Context, nameOrID string, options *KillOptions) (*entities.PodKillReport, error) {
var (
report entities.PodKillReport
)
if options == nil {
options = new(KillOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params := url.Values{}
if signal != nil {
params.Set("signal", *signal)
params, err := options.ToParams()
if err != nil {
return nil, err
}
response, err := conn.DoRequest(nil, http.MethodPost, "/pods/%s/kill", params, nil, nameOrID)
if err != nil {
Expand All @@ -86,8 +95,12 @@ func Kill(ctx context.Context, nameOrID string, signal *string) (*entities.PodKi
}

// Pause pauses all running containers in a given pod.
func Pause(ctx context.Context, nameOrID string) (*entities.PodPauseReport, error) {
func Pause(ctx context.Context, nameOrID string, options *PauseOptions) (*entities.PodPauseReport, error) {
var report entities.PodPauseReport
if options == nil {
options = new(PauseOptions)
}
_ = options
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
Expand All @@ -101,8 +114,12 @@ func Pause(ctx context.Context, nameOrID string) (*entities.PodPauseReport, erro

// Prune by default removes all non-running pods in local storage.
// And with force set true removes all pods.
func Prune(ctx context.Context) ([]*entities.PodPruneReport, error) {
func Prune(ctx context.Context, options *PruneOptions) ([]*entities.PodPruneReport, error) {
var reports []*entities.PodPruneReport
if options == nil {
options = new(PruneOptions)
}
_ = options
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
Expand All @@ -116,21 +133,20 @@ func Prune(ctx context.Context) ([]*entities.PodPruneReport, error) {

// List returns all pods in local storage. The optional filters parameter can
// be used to refine which pods should be listed.
func List(ctx context.Context, filters map[string][]string) ([]*entities.ListPodsReport, error) {
func List(ctx context.Context, options *ListOptions) ([]*entities.ListPodsReport, error) {
var (
podsReports []*entities.ListPodsReport
)
if options == nil {
options = new(ListOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params := url.Values{}
if filters != nil {
stringFilter, err := bindings.FiltersToString(filters)
if err != nil {
return nil, err
}
params.Set("filters", stringFilter)
params, err := options.ToParams()
if err != nil {
return nil, err
}
response, err := conn.DoRequest(nil, http.MethodGet, "/pods/json", params, nil)
if err != nil {
Expand All @@ -140,8 +156,12 @@ func List(ctx context.Context, filters map[string][]string) ([]*entities.ListPod
}

// Restart restarts all containers in a pod.
func Restart(ctx context.Context, nameOrID string) (*entities.PodRestartReport, error) {
func Restart(ctx context.Context, nameOrID string, options *RestartOptions) (*entities.PodRestartReport, error) {
var report entities.PodRestartReport
if options == nil {
options = new(RestartOptions)
}
_ = options
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
Expand All @@ -155,15 +175,18 @@ func Restart(ctx context.Context, nameOrID string) (*entities.PodRestartReport,

// Remove deletes a Pod from from local storage. The optional force parameter denotes
// that the Pod can be removed even if in a running state.
func Remove(ctx context.Context, nameOrID string, force *bool) (*entities.PodRmReport, error) {
func Remove(ctx context.Context, nameOrID string, options *RemoveOptions) (*entities.PodRmReport, error) {
var report entities.PodRmReport
if options == nil {
options = new(RemoveOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params := url.Values{}
if force != nil {
params.Set("force", strconv.FormatBool(*force))
params, err := options.ToParams()
if err != nil {
return nil, err
}
response, err := conn.DoRequest(nil, http.MethodDelete, "/pods/%s", params, nil, nameOrID)
if err != nil {
Expand All @@ -173,8 +196,12 @@ func Remove(ctx context.Context, nameOrID string, force *bool) (*entities.PodRmR
}

// Start starts all containers in a pod.
func Start(ctx context.Context, nameOrID string) (*entities.PodStartReport, error) {
func Start(ctx context.Context, nameOrID string, options *StartOptions) (*entities.PodStartReport, error) {
var report entities.PodStartReport
if options == nil {
options = new(StartOptions)
}
_ = options
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
Expand All @@ -192,15 +219,18 @@ func Start(ctx context.Context, nameOrID string) (*entities.PodStartReport, erro

// Stop stops all containers in a Pod. The optional timeout parameter can be
// used to override the timeout before the container is killed.
func Stop(ctx context.Context, nameOrID string, timeout *int) (*entities.PodStopReport, error) {
func Stop(ctx context.Context, nameOrID string, options *StopOptions) (*entities.PodStopReport, error) {
var report entities.PodStopReport
if options == nil {
options = new(StopOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params := url.Values{}
if timeout != nil {
params.Set("t", strconv.Itoa(*timeout))
params, err := options.ToParams()
if err != nil {
return nil, err
}
response, err := conn.DoRequest(nil, http.MethodPost, "/pods/%s/stop", params, nil, nameOrID)
if err != nil {
Expand All @@ -215,15 +245,16 @@ func Stop(ctx context.Context, nameOrID string, timeout *int) (*entities.PodStop

// Top gathers statistics about the running processes in a pod. The nameOrID can be a pod name
// or a partial/full ID. The descriptors allow for specifying which data to collect from each process.
func Top(ctx context.Context, nameOrID string, descriptors []string) ([]string, error) {
func Top(ctx context.Context, nameOrID string, options *TopOptions) ([]string, error) {
if options == nil {
options = new(TopOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params := url.Values{}

if len(descriptors) > 0 {
// flatten the slice into one string
if descriptors := options.GetDescriptors(); len(descriptors) > 0 {
params.Set("ps_args", strings.Join(descriptors, ","))
}
response, err := conn.DoRequest(nil, http.MethodGet, "/pods/%s/top", params, nil, nameOrID)
Expand All @@ -248,7 +279,11 @@ func Top(ctx context.Context, nameOrID string, descriptors []string) ([]string,
}

// Unpause unpauses all paused containers in a Pod.
func Unpause(ctx context.Context, nameOrID string) (*entities.PodUnpauseReport, error) {
func Unpause(ctx context.Context, nameOrID string, options *UnpauseOptions) (*entities.PodUnpauseReport, error) {
if options == nil {
options = new(UnpauseOptions)
}
_ = options
var report entities.PodUnpauseReport
conn, err := bindings.GetClient(ctx)
if err != nil {
Expand All @@ -262,19 +297,21 @@ func Unpause(ctx context.Context, nameOrID string) (*entities.PodUnpauseReport,
}

// Stats display resource-usage statistics of one or more pods.
func Stats(ctx context.Context, namesOrIDs []string, options entities.PodStatsOptions) ([]*entities.PodStatsReport, error) {
if options.Latest {
return nil, errors.New("latest is not supported")
func Stats(ctx context.Context, namesOrIDs []string, options *StatsOptions) ([]*entities.PodStatsReport, error) {
if options == nil {
options = new(StatsOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params := url.Values{}
params, err := options.ToParams()
if err != nil {
return nil, err
}
for _, i := range namesOrIDs {
params.Add("namesOrIDs", i)
}
params.Set("all", strconv.FormatBool(options.All))

var reports []*entities.PodStatsReport
response, err := conn.DoRequest(nil, http.MethodGet, "/pods/stats", params, nil)
Expand Down
72 changes: 72 additions & 0 deletions pkg/bindings/pods/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package pods

//go:generate go run ../generator/generator.go CreateOptions
// CreateOptions are optional options for creating pods
type CreateOptions struct {
}

//go:generate go run ../generator/generator.go InspectOptions
// InspectOptions are optional options for inspecting pods
type InspectOptions struct {
}

//go:generate go run ../generator/generator.go KillOptions
// KillOptions are optional options for killing pods
type KillOptions struct {
Signal *string
}

//go:generate go run ../generator/generator.go PauseOptions
// PauseOptions are optional options for pausing pods
type PauseOptions struct {
}

//go:generate go run ../generator/generator.go PruneOptions
// PruneOptions are optional options for pruning pods
type PruneOptions struct {
}

//go:generate go run ../generator/generator.go ListOptions
// ListOptions are optional options for listing pods
type ListOptions struct {
Filters map[string][]string
}

//go:generate go run ../generator/generator.go RestartOptions
// RestartOptions are optional options for restarting pods
type RestartOptions struct {
}

//go:generate go run ../generator/generator.go StartOptions
// StartOptions are optional options for starting pods
type StartOptions struct {
}

//go:generate go run ../generator/generator.go StopOptions
// StopOptions are optional options for stopping pods
type StopOptions struct {
Timeout *int
}

//go:generate go run ../generator/generator.go TopOptions
// TopOptions are optional options for getting top on pods
type TopOptions struct {
Descriptors []string
}

//go:generate go run ../generator/generator.go UnpauseOptions
// UnpauseOptions are optional options for unpausinging pods
type UnpauseOptions struct {
}

//go:generate go run ../generator/generator.go StatsOptions
// StatsOptions are optional options for getting stats of pods
type StatsOptions struct {
All *bool
}

//go:generate go run ../generator/generator.go RemoveOptions
// RemoveOptions are optional options for removing pods
type RemoveOptions struct {
Force *bool
}
Loading

0 comments on commit a73c76d

Please sign in to comment.