diff options
author | Niall Crowe <nicrowe@redhat.com> | 2022-06-24 15:43:05 +0100 |
---|---|---|
committer | Niall Crowe <nicrowe@redhat.com> | 2022-07-13 15:27:03 +0100 |
commit | e08a77ce64eec5cd0192ae1970fa859c00440174 (patch) | |
tree | 434b8a11abe0eb43a5a4e5e99ac4e04fdcc1730f /pkg | |
parent | 4df6122aaa3bc858e9488d366a2c9c91b6671170 (diff) | |
download | podman-e08a77ce64eec5cd0192ae1970fa859c00440174.tar.gz podman-e08a77ce64eec5cd0192ae1970fa859c00440174.tar.bz2 podman-e08a77ce64eec5cd0192ae1970fa859c00440174.zip |
Add "podman kube play" cmd
The "podman kube play" command is designed to be a replacement for the
"podman play kube" command.
It performs the same function as "play kube" while also still working with the same flags and options.
The "podman play kube" command is still functional as an alias of "kube play".
Closes #12475
Signed-off-by: Niall Crowe <nicrowe@redhat.com>
Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/api/handlers/libpod/kube.go | 123 | ||||
-rw-r--r-- | pkg/api/handlers/libpod/play.go | 114 | ||||
-rw-r--r-- | pkg/api/handlers/swagger/responses.go | 2 | ||||
-rw-r--r-- | pkg/api/server/register_kube.go (renamed from pkg/api/server/register_play.go) | 14 | ||||
-rw-r--r-- | pkg/api/server/server.go | 2 | ||||
-rw-r--r-- | pkg/bindings/kube/kube.go | 96 | ||||
-rw-r--r-- | pkg/bindings/kube/types.go (renamed from pkg/bindings/play/types.go) | 8 | ||||
-rw-r--r-- | pkg/bindings/kube/types_play_options.go (renamed from pkg/bindings/play/types_kube_options.go) | 78 | ||||
-rw-r--r-- | pkg/bindings/play/play.go | 88 | ||||
-rw-r--r-- | pkg/domain/entities/play.go | 2 | ||||
-rw-r--r-- | pkg/domain/infra/tunnel/play.go | 5 |
11 files changed, 288 insertions, 244 deletions
diff --git a/pkg/api/handlers/libpod/kube.go b/pkg/api/handlers/libpod/kube.go new file mode 100644 index 000000000..6cad58795 --- /dev/null +++ b/pkg/api/handlers/libpod/kube.go @@ -0,0 +1,123 @@ +package libpod + +import ( + "fmt" + "net" + "net/http" + + "github.com/containers/image/v5/types" + "github.com/containers/podman/v4/libpod" + "github.com/containers/podman/v4/pkg/api/handlers/utils" + api "github.com/containers/podman/v4/pkg/api/types" + "github.com/containers/podman/v4/pkg/auth" + "github.com/containers/podman/v4/pkg/domain/entities" + "github.com/containers/podman/v4/pkg/domain/infra/abi" + "github.com/gorilla/schema" +) + +func KubePlay(w http.ResponseWriter, r *http.Request) { + runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) + decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder) + query := struct { + Annotations map[string]string `schema:"annotations"` + Network []string `schema:"network"` + TLSVerify bool `schema:"tlsVerify"` + LogDriver string `schema:"logDriver"` + LogOptions []string `schema:"logOptions"` + Start bool `schema:"start"` + StaticIPs []string `schema:"staticIPs"` + StaticMACs []string `schema:"staticMACs"` + NoHosts bool `schema:"noHosts"` + }{ + TLSVerify: true, + Start: true, + } + + if err := decoder.Decode(&query, r.URL.Query()); err != nil { + utils.Error(w, http.StatusBadRequest, fmt.Errorf("failed to parse parameters for %s: %w", r.URL.String(), err)) + return + } + + staticIPs := make([]net.IP, 0, len(query.StaticIPs)) + for _, ipString := range query.StaticIPs { + ip := net.ParseIP(ipString) + if ip == nil { + utils.Error(w, http.StatusBadRequest, fmt.Errorf("invalid IP address %s", ipString)) + return + } + staticIPs = append(staticIPs, ip) + } + + staticMACs := make([]net.HardwareAddr, 0, len(query.StaticMACs)) + for _, macString := range query.StaticMACs { + mac, err := net.ParseMAC(macString) + if err != nil { + utils.Error(w, http.StatusBadRequest, err) + return + } + staticMACs = append(staticMACs, mac) + } + + authConf, authfile, err := auth.GetCredentials(r) + if err != nil { + utils.Error(w, http.StatusBadRequest, err) + return + } + defer auth.RemoveAuthfile(authfile) + var username, password string + if authConf != nil { + username = authConf.Username + password = authConf.Password + } + + logDriver := query.LogDriver + if logDriver == "" { + config, err := runtime.GetConfig() + if err != nil { + utils.Error(w, http.StatusInternalServerError, err) + return + } + logDriver = config.Containers.LogDriver + } + + containerEngine := abi.ContainerEngine{Libpod: runtime} + options := entities.PlayKubeOptions{ + Annotations: query.Annotations, + Authfile: authfile, + Username: username, + Password: password, + Networks: query.Network, + NoHosts: query.NoHosts, + Quiet: true, + LogDriver: logDriver, + LogOptions: query.LogOptions, + StaticIPs: staticIPs, + StaticMACs: staticMACs, + } + if _, found := r.URL.Query()["tlsVerify"]; found { + options.SkipTLSVerify = types.NewOptionalBool(!query.TLSVerify) + } + if _, found := r.URL.Query()["start"]; found { + options.Start = types.NewOptionalBool(query.Start) + } + report, err := containerEngine.PlayKube(r.Context(), r.Body, options) + _ = r.Body.Close() + if err != nil { + utils.Error(w, http.StatusInternalServerError, fmt.Errorf("error playing YAML file: %w", err)) + return + } + utils.WriteResponse(w, http.StatusOK, report) +} + +func KubePlayDown(w http.ResponseWriter, r *http.Request) { + runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) + containerEngine := abi.ContainerEngine{Libpod: runtime} + options := new(entities.PlayKubeDownOptions) + report, err := containerEngine.PlayKubeDown(r.Context(), r.Body, *options) + _ = r.Body.Close() + if err != nil { + utils.Error(w, http.StatusInternalServerError, fmt.Errorf("error tearing down YAML file: %w", err)) + return + } + utils.WriteResponse(w, http.StatusOK, report) +} diff --git a/pkg/api/handlers/libpod/play.go b/pkg/api/handlers/libpod/play.go index f8ce52a72..74830badb 100644 --- a/pkg/api/handlers/libpod/play.go +++ b/pkg/api/handlers/libpod/play.go @@ -1,123 +1,13 @@ package libpod import ( - "fmt" - "net" "net/http" - - "github.com/containers/image/v5/types" - "github.com/containers/podman/v4/libpod" - "github.com/containers/podman/v4/pkg/api/handlers/utils" - api "github.com/containers/podman/v4/pkg/api/types" - "github.com/containers/podman/v4/pkg/auth" - "github.com/containers/podman/v4/pkg/domain/entities" - "github.com/containers/podman/v4/pkg/domain/infra/abi" - "github.com/gorilla/schema" ) func PlayKube(w http.ResponseWriter, r *http.Request) { - runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) - decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder) - query := struct { - Annotations map[string]string `schema:"annotations"` - Network []string `schema:"network"` - TLSVerify bool `schema:"tlsVerify"` - LogDriver string `schema:"logDriver"` - LogOptions []string `schema:"logOptions"` - Start bool `schema:"start"` - StaticIPs []string `schema:"staticIPs"` - StaticMACs []string `schema:"staticMACs"` - NoHosts bool `schema:"noHosts"` - }{ - TLSVerify: true, - Start: true, - } - - if err := decoder.Decode(&query, r.URL.Query()); err != nil { - utils.Error(w, http.StatusBadRequest, fmt.Errorf("failed to parse parameters for %s: %w", r.URL.String(), err)) - return - } - - staticIPs := make([]net.IP, 0, len(query.StaticIPs)) - for _, ipString := range query.StaticIPs { - ip := net.ParseIP(ipString) - if ip == nil { - utils.Error(w, http.StatusBadRequest, fmt.Errorf("invalid IP address %s", ipString)) - return - } - staticIPs = append(staticIPs, ip) - } - - staticMACs := make([]net.HardwareAddr, 0, len(query.StaticMACs)) - for _, macString := range query.StaticMACs { - mac, err := net.ParseMAC(macString) - if err != nil { - utils.Error(w, http.StatusBadRequest, err) - return - } - staticMACs = append(staticMACs, mac) - } - - authConf, authfile, err := auth.GetCredentials(r) - if err != nil { - utils.Error(w, http.StatusBadRequest, err) - return - } - defer auth.RemoveAuthfile(authfile) - var username, password string - if authConf != nil { - username = authConf.Username - password = authConf.Password - } - - logDriver := query.LogDriver - if logDriver == "" { - config, err := runtime.GetConfig() - if err != nil { - utils.Error(w, http.StatusInternalServerError, err) - return - } - logDriver = config.Containers.LogDriver - } - - containerEngine := abi.ContainerEngine{Libpod: runtime} - options := entities.PlayKubeOptions{ - Annotations: query.Annotations, - Authfile: authfile, - Username: username, - Password: password, - Networks: query.Network, - NoHosts: query.NoHosts, - Quiet: true, - LogDriver: logDriver, - LogOptions: query.LogOptions, - StaticIPs: staticIPs, - StaticMACs: staticMACs, - } - if _, found := r.URL.Query()["tlsVerify"]; found { - options.SkipTLSVerify = types.NewOptionalBool(!query.TLSVerify) - } - if _, found := r.URL.Query()["start"]; found { - options.Start = types.NewOptionalBool(query.Start) - } - report, err := containerEngine.PlayKube(r.Context(), r.Body, options) - _ = r.Body.Close() - if err != nil { - utils.Error(w, http.StatusInternalServerError, fmt.Errorf("error playing YAML file: %w", err)) - return - } - utils.WriteResponse(w, http.StatusOK, report) + KubePlay(w, r) } func PlayKubeDown(w http.ResponseWriter, r *http.Request) { - runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) - containerEngine := abi.ContainerEngine{Libpod: runtime} - options := new(entities.PlayKubeDownOptions) - report, err := containerEngine.PlayKubeDown(r.Context(), r.Body, *options) - _ = r.Body.Close() - if err != nil { - utils.Error(w, http.StatusInternalServerError, fmt.Errorf("error tearing down YAML file: %w", err)) - return - } - utils.WriteResponse(w, http.StatusOK, report) + KubePlayDown(w, r) } diff --git a/pkg/api/handlers/swagger/responses.go b/pkg/api/handlers/swagger/responses.go index 93a508b39..5731f8edd 100644 --- a/pkg/api/handlers/swagger/responses.go +++ b/pkg/api/handlers/swagger/responses.go @@ -71,7 +71,7 @@ type imagesRemoveResponseLibpod struct { // PlayKube response // swagger:response -type playKubeResponseLibpod struct { +type kubePlayResponseLibpod struct { // in:body Body entities.PlayKubeReport } diff --git a/pkg/api/server/register_play.go b/pkg/api/server/register_kube.go index 35da80ccc..6ae9e8123 100644 --- a/pkg/api/server/register_play.go +++ b/pkg/api/server/register_kube.go @@ -7,8 +7,8 @@ import ( "github.com/gorilla/mux" ) -func (s *APIServer) registerPlayHandlers(r *mux.Router) error { - // swagger:operation POST /libpod/play/kube libpod PlayKubeLibpod +func (s *APIServer) registerKubeHandlers(r *mux.Router) error { + // swagger:operation POST /libpod/kube/play libpod KubePlayLibpod // --- // tags: // - containers @@ -57,24 +57,26 @@ func (s *APIServer) registerPlayHandlers(r *mux.Router) error { // - application/json // responses: // 200: - // $ref: "#/responses/playKubeResponseLibpod" + // $ref: "#/responses/kubePlayResponseLibpod" // 500: // $ref: "#/responses/internalError" + r.HandleFunc(VersionedPath("/libpod/kube/play"), s.APIHandler(libpod.KubePlay)).Methods(http.MethodPost) r.HandleFunc(VersionedPath("/libpod/play/kube"), s.APIHandler(libpod.PlayKube)).Methods(http.MethodPost) - // swagger:operation DELETE /libpod/play/kube libpod PlayKubeDownLibpod + // swagger:operation DELETE /libpod/kube/play libpod KubePlayDownLibpod // --- // tags: // - containers // - pods - // summary: Remove pods from play kube + // summary: Remove pods from kube play // description: Tears down pods defined in a YAML file // produces: // - application/json // responses: // 200: - // $ref: "#/responses/playKubeResponseLibpod" + // $ref: "#/responses/kubePlayResponseLibpod" // 500: // $ref: "#/responses/internalError" + r.HandleFunc(VersionedPath("/libpod/kube/play"), s.APIHandler(libpod.KubePlayDown)).Methods(http.MethodDelete) r.HandleFunc(VersionedPath("/libpod/play/kube"), s.APIHandler(libpod.PlayKubeDown)).Methods(http.MethodDelete) return nil } diff --git a/pkg/api/server/server.go b/pkg/api/server/server.go index 5482a8ec2..a6d8b5e4c 100644 --- a/pkg/api/server/server.go +++ b/pkg/api/server/server.go @@ -126,11 +126,11 @@ func newServer(runtime *libpod.Runtime, listener net.Listener, opts entities.Ser server.registerHealthCheckHandlers, server.registerImagesHandlers, server.registerInfoHandlers, + server.registerKubeHandlers, server.registerManifestHandlers, server.registerMonitorHandlers, server.registerNetworkHandlers, server.registerPingHandlers, - server.registerPlayHandlers, server.registerPluginsHandlers, server.registerPodsHandlers, server.registerSecretHandlers, diff --git a/pkg/bindings/kube/kube.go b/pkg/bindings/kube/kube.go new file mode 100644 index 000000000..b9cc0efa7 --- /dev/null +++ b/pkg/bindings/kube/kube.go @@ -0,0 +1,96 @@ +package kube + +import ( + "context" + "io" + "net/http" + "os" + "strconv" + + "github.com/containers/image/v5/types" + "github.com/containers/podman/v4/pkg/auth" + "github.com/containers/podman/v4/pkg/bindings" + "github.com/containers/podman/v4/pkg/domain/entities" + "github.com/sirupsen/logrus" +) + +func Play(ctx context.Context, path string, options *PlayOptions) (*entities.KubePlayReport, error) { + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + + return PlayWithBody(ctx, f, options) +} + +func PlayWithBody(ctx context.Context, body io.Reader, options *PlayOptions) (*entities.KubePlayReport, error) { + var report entities.KubePlayReport + if options == nil { + options = new(PlayOptions) + } + + conn, err := bindings.GetClient(ctx) + if err != nil { + return nil, err + } + + params, err := options.ToParams() + if err != nil { + return nil, err + } + if options.SkipTLSVerify != nil { + params.Set("tlsVerify", strconv.FormatBool(options.GetSkipTLSVerify())) + } + if options.Start != nil { + params.Set("start", strconv.FormatBool(options.GetStart())) + } + + header, err := auth.MakeXRegistryAuthHeader(&types.SystemContext{AuthFilePath: options.GetAuthfile()}, options.GetUsername(), options.GetPassword()) + if err != nil { + return nil, err + } + + response, err := conn.DoRequest(ctx, body, http.MethodPost, "/kube/play", params, header) + if err != nil { + return nil, err + } + defer response.Body.Close() + + if err := response.Process(&report); err != nil { + return nil, err + } + + return &report, nil +} + +func Down(ctx context.Context, path string) (*entities.KubePlayReport, error) { + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer func() { + if err := f.Close(); err != nil { + logrus.Warn(err) + } + }() + + return DownWithBody(ctx, f) +} + +func DownWithBody(ctx context.Context, body io.Reader) (*entities.KubePlayReport, error) { + var report entities.KubePlayReport + conn, err := bindings.GetClient(ctx) + if err != nil { + return nil, err + } + + response, err := conn.DoRequest(ctx, body, http.MethodDelete, "/kube/play", nil, nil) + if err != nil { + return nil, err + } + if err := response.Process(&report); err != nil { + return nil, err + } + return &report, nil +} diff --git a/pkg/bindings/play/types.go b/pkg/bindings/kube/types.go index 5aaa87b8c..783d1912a 100644 --- a/pkg/bindings/play/types.go +++ b/pkg/bindings/kube/types.go @@ -1,12 +1,12 @@ -package play +package kube import ( "net" ) -//go:generate go run ../generator/generator.go KubeOptions -// KubeOptions are optional options for replaying kube YAML files -type KubeOptions struct { +//go:generate go run ../generator/generator.go PlayOptions +// PlayOptions are optional options for replaying kube YAML files +type PlayOptions struct { // Annotations - Annotations to add to Pods Annotations map[string]string // Authfile - path to an authentication file. diff --git a/pkg/bindings/play/types_kube_options.go b/pkg/bindings/kube/types_play_options.go index 54c9a8e74..cdc2e9dd8 100644 --- a/pkg/bindings/play/types_kube_options.go +++ b/pkg/bindings/kube/types_play_options.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -package play +package kube import ( "net" @@ -9,23 +9,23 @@ import ( ) // Changed returns true if named field has been set -func (o *KubeOptions) Changed(fieldName string) bool { +func (o *PlayOptions) Changed(fieldName string) bool { return util.Changed(o, fieldName) } // ToParams formats struct fields to be passed to API service -func (o *KubeOptions) ToParams() (url.Values, error) { +func (o *PlayOptions) ToParams() (url.Values, error) { return util.ToParams(o) } // WithAnnotations set field Annotations to given value -func (o *KubeOptions) WithAnnotations(value map[string]string) *KubeOptions { +func (o *PlayOptions) WithAnnotations(value map[string]string) *PlayOptions { o.Annotations = value return o } // GetAnnotations returns value of field Annotations -func (o *KubeOptions) GetAnnotations() map[string]string { +func (o *PlayOptions) GetAnnotations() map[string]string { if o.Annotations == nil { var z map[string]string return z @@ -34,13 +34,13 @@ func (o *KubeOptions) GetAnnotations() map[string]string { } // WithAuthfile set field Authfile to given value -func (o *KubeOptions) WithAuthfile(value string) *KubeOptions { +func (o *PlayOptions) WithAuthfile(value string) *PlayOptions { o.Authfile = &value return o } // GetAuthfile returns value of field Authfile -func (o *KubeOptions) GetAuthfile() string { +func (o *PlayOptions) GetAuthfile() string { if o.Authfile == nil { var z string return z @@ -49,13 +49,13 @@ func (o *KubeOptions) GetAuthfile() string { } // WithCertDir set field CertDir to given value -func (o *KubeOptions) WithCertDir(value string) *KubeOptions { +func (o *PlayOptions) WithCertDir(value string) *PlayOptions { o.CertDir = &value return o } // GetCertDir returns value of field CertDir -func (o *KubeOptions) GetCertDir() string { +func (o *PlayOptions) GetCertDir() string { if o.CertDir == nil { var z string return z @@ -64,13 +64,13 @@ func (o *KubeOptions) GetCertDir() string { } // WithUsername set field Username to given value -func (o *KubeOptions) WithUsername(value string) *KubeOptions { +func (o *PlayOptions) WithUsername(value string) *PlayOptions { o.Username = &value return o } // GetUsername returns value of field Username -func (o *KubeOptions) GetUsername() string { +func (o *PlayOptions) GetUsername() string { if o.Username == nil { var z string return z @@ -79,13 +79,13 @@ func (o *KubeOptions) GetUsername() string { } // WithPassword set field Password to given value -func (o *KubeOptions) WithPassword(value string) *KubeOptions { +func (o *PlayOptions) WithPassword(value string) *PlayOptions { o.Password = &value return o } // GetPassword returns value of field Password -func (o *KubeOptions) GetPassword() string { +func (o *PlayOptions) GetPassword() string { if o.Password == nil { var z string return z @@ -94,13 +94,13 @@ func (o *KubeOptions) GetPassword() string { } // WithNetwork set field Network to given value -func (o *KubeOptions) WithNetwork(value []string) *KubeOptions { +func (o *PlayOptions) WithNetwork(value []string) *PlayOptions { o.Network = &value return o } // GetNetwork returns value of field Network -func (o *KubeOptions) GetNetwork() []string { +func (o *PlayOptions) GetNetwork() []string { if o.Network == nil { var z []string return z @@ -109,13 +109,13 @@ func (o *KubeOptions) GetNetwork() []string { } // WithNoHosts set field NoHosts to given value -func (o *KubeOptions) WithNoHosts(value bool) *KubeOptions { +func (o *PlayOptions) WithNoHosts(value bool) *PlayOptions { o.NoHosts = &value return o } // GetNoHosts returns value of field NoHosts -func (o *KubeOptions) GetNoHosts() bool { +func (o *PlayOptions) GetNoHosts() bool { if o.NoHosts == nil { var z bool return z @@ -124,13 +124,13 @@ func (o *KubeOptions) GetNoHosts() bool { } // WithQuiet set field Quiet to given value -func (o *KubeOptions) WithQuiet(value bool) *KubeOptions { +func (o *PlayOptions) WithQuiet(value bool) *PlayOptions { o.Quiet = &value return o } // GetQuiet returns value of field Quiet -func (o *KubeOptions) GetQuiet() bool { +func (o *PlayOptions) GetQuiet() bool { if o.Quiet == nil { var z bool return z @@ -139,13 +139,13 @@ func (o *KubeOptions) GetQuiet() bool { } // WithSignaturePolicy set field SignaturePolicy to given value -func (o *KubeOptions) WithSignaturePolicy(value string) *KubeOptions { +func (o *PlayOptions) WithSignaturePolicy(value string) *PlayOptions { o.SignaturePolicy = &value return o } // GetSignaturePolicy returns value of field SignaturePolicy -func (o *KubeOptions) GetSignaturePolicy() string { +func (o *PlayOptions) GetSignaturePolicy() string { if o.SignaturePolicy == nil { var z string return z @@ -154,13 +154,13 @@ func (o *KubeOptions) GetSignaturePolicy() string { } // WithSkipTLSVerify set field SkipTLSVerify to given value -func (o *KubeOptions) WithSkipTLSVerify(value bool) *KubeOptions { +func (o *PlayOptions) WithSkipTLSVerify(value bool) *PlayOptions { o.SkipTLSVerify = &value return o } // GetSkipTLSVerify returns value of field SkipTLSVerify -func (o *KubeOptions) GetSkipTLSVerify() bool { +func (o *PlayOptions) GetSkipTLSVerify() bool { if o.SkipTLSVerify == nil { var z bool return z @@ -169,13 +169,13 @@ func (o *KubeOptions) GetSkipTLSVerify() bool { } // WithSeccompProfileRoot set field SeccompProfileRoot to given value -func (o *KubeOptions) WithSeccompProfileRoot(value string) *KubeOptions { +func (o *PlayOptions) WithSeccompProfileRoot(value string) *PlayOptions { o.SeccompProfileRoot = &value return o } // GetSeccompProfileRoot returns value of field SeccompProfileRoot -func (o *KubeOptions) GetSeccompProfileRoot() string { +func (o *PlayOptions) GetSeccompProfileRoot() string { if o.SeccompProfileRoot == nil { var z string return z @@ -184,13 +184,13 @@ func (o *KubeOptions) GetSeccompProfileRoot() string { } // WithStaticIPs set field StaticIPs to given value -func (o *KubeOptions) WithStaticIPs(value []net.IP) *KubeOptions { +func (o *PlayOptions) WithStaticIPs(value []net.IP) *PlayOptions { o.StaticIPs = &value return o } // GetStaticIPs returns value of field StaticIPs -func (o *KubeOptions) GetStaticIPs() []net.IP { +func (o *PlayOptions) GetStaticIPs() []net.IP { if o.StaticIPs == nil { var z []net.IP return z @@ -199,13 +199,13 @@ func (o *KubeOptions) GetStaticIPs() []net.IP { } // WithStaticMACs set field StaticMACs to given value -func (o *KubeOptions) WithStaticMACs(value []net.HardwareAddr) *KubeOptions { +func (o *PlayOptions) WithStaticMACs(value []net.HardwareAddr) *PlayOptions { o.StaticMACs = &value return o } // GetStaticMACs returns value of field StaticMACs -func (o *KubeOptions) GetStaticMACs() []net.HardwareAddr { +func (o *PlayOptions) GetStaticMACs() []net.HardwareAddr { if o.StaticMACs == nil { var z []net.HardwareAddr return z @@ -214,13 +214,13 @@ func (o *KubeOptions) GetStaticMACs() []net.HardwareAddr { } // WithConfigMaps set field ConfigMaps to given value -func (o *KubeOptions) WithConfigMaps(value []string) *KubeOptions { +func (o *PlayOptions) WithConfigMaps(value []string) *PlayOptions { o.ConfigMaps = &value return o } // GetConfigMaps returns value of field ConfigMaps -func (o *KubeOptions) GetConfigMaps() []string { +func (o *PlayOptions) GetConfigMaps() []string { if o.ConfigMaps == nil { var z []string return z @@ -229,13 +229,13 @@ func (o *KubeOptions) GetConfigMaps() []string { } // WithLogDriver set field LogDriver to given value -func (o *KubeOptions) WithLogDriver(value string) *KubeOptions { +func (o *PlayOptions) WithLogDriver(value string) *PlayOptions { o.LogDriver = &value return o } // GetLogDriver returns value of field LogDriver -func (o *KubeOptions) GetLogDriver() string { +func (o *PlayOptions) GetLogDriver() string { if o.LogDriver == nil { var z string return z @@ -244,13 +244,13 @@ func (o *KubeOptions) GetLogDriver() string { } // WithLogOptions set field LogOptions to given value -func (o *KubeOptions) WithLogOptions(value []string) *KubeOptions { +func (o *PlayOptions) WithLogOptions(value []string) *PlayOptions { o.LogOptions = &value return o } // GetLogOptions returns value of field LogOptions -func (o *KubeOptions) GetLogOptions() []string { +func (o *PlayOptions) GetLogOptions() []string { if o.LogOptions == nil { var z []string return z @@ -259,13 +259,13 @@ func (o *KubeOptions) GetLogOptions() []string { } // WithStart set field Start to given value -func (o *KubeOptions) WithStart(value bool) *KubeOptions { +func (o *PlayOptions) WithStart(value bool) *PlayOptions { o.Start = &value return o } // GetStart returns value of field Start -func (o *KubeOptions) GetStart() bool { +func (o *PlayOptions) GetStart() bool { if o.Start == nil { var z bool return z @@ -274,13 +274,13 @@ func (o *KubeOptions) GetStart() bool { } // WithUserns set field Userns to given value -func (o *KubeOptions) WithUserns(value string) *KubeOptions { +func (o *PlayOptions) WithUserns(value string) *PlayOptions { o.Userns = &value return o } // GetUserns returns value of field Userns -func (o *KubeOptions) GetUserns() string { +func (o *PlayOptions) GetUserns() string { if o.Userns == nil { var z string return z diff --git a/pkg/bindings/play/play.go b/pkg/bindings/play/play.go index 0261b0250..d5d649135 100644 --- a/pkg/bindings/play/play.go +++ b/pkg/bindings/play/play.go @@ -3,95 +3,25 @@ package play import ( "context" "io" - "net/http" - "os" - "strconv" - "github.com/containers/image/v5/types" - "github.com/containers/podman/v4/pkg/auth" - "github.com/containers/podman/v4/pkg/bindings" + "github.com/containers/podman/v4/pkg/bindings/kube" "github.com/containers/podman/v4/pkg/domain/entities" - "github.com/sirupsen/logrus" ) -func Kube(ctx context.Context, path string, options *KubeOptions) (*entities.PlayKubeReport, error) { - f, err := os.Open(path) - if err != nil { - return nil, err - } - defer f.Close() +type KubeOptions = kube.PlayOptions - return KubeWithBody(ctx, f, options) +func Kube(ctx context.Context, path string, options *KubeOptions) (*entities.PlayKubeReport, error) { + return kube.Play(ctx, path, options) } func KubeWithBody(ctx context.Context, body io.Reader, options *KubeOptions) (*entities.PlayKubeReport, error) { - var report entities.PlayKubeReport - if options == nil { - options = new(KubeOptions) - } - - conn, err := bindings.GetClient(ctx) - if err != nil { - return nil, err - } - - params, err := options.ToParams() - if err != nil { - return nil, err - } - if options.SkipTLSVerify != nil { - params.Set("tlsVerify", strconv.FormatBool(options.GetSkipTLSVerify())) - } - if options.Start != nil { - params.Set("start", strconv.FormatBool(options.GetStart())) - } - - header, err := auth.MakeXRegistryAuthHeader(&types.SystemContext{AuthFilePath: options.GetAuthfile()}, options.GetUsername(), options.GetPassword()) - if err != nil { - return nil, err - } - - response, err := conn.DoRequest(ctx, body, http.MethodPost, "/play/kube", params, header) - if err != nil { - return nil, err - } - defer response.Body.Close() - - if err := response.Process(&report); err != nil { - return nil, err - } - - return &report, nil + return kube.PlayWithBody(ctx, body, options) } -func KubeDown(ctx context.Context, path string) (*entities.PlayKubeReport, error) { - f, err := os.Open(path) - if err != nil { - return nil, err - } - defer func() { - if err := f.Close(); err != nil { - logrus.Warn(err) - } - }() - - return KubeDownWithBody(ctx, f) +func Down(ctx context.Context, path string) (*entities.PlayKubeReport, error) { + return kube.Down(ctx, path) } -func KubeDownWithBody(ctx context.Context, body io.Reader) (*entities.PlayKubeReport, error) { - var report entities.PlayKubeReport - conn, err := bindings.GetClient(ctx) - if err != nil { - return nil, err - } - - response, err := conn.DoRequest(ctx, body, http.MethodDelete, "/play/kube", nil, nil) - if err != nil { - return nil, err - } - if err := response.Process(&report); err != nil { - return nil, err - } - - return &report, nil +func DownWithBody(ctx context.Context, body io.Reader) (*entities.PlayKubeReport, error) { + return kube.DownWithBody(ctx, body) } diff --git a/pkg/domain/entities/play.go b/pkg/domain/entities/play.go index f1ba21650..35a5d8a4a 100644 --- a/pkg/domain/entities/play.go +++ b/pkg/domain/entities/play.go @@ -90,6 +90,8 @@ type PlayKubeReport struct { PlayKubeTeardown } +type KubePlayReport = PlayKubeReport + // PlayKubeDownOptions are options for tearing down pods type PlayKubeDownOptions struct{} diff --git a/pkg/domain/infra/tunnel/play.go b/pkg/domain/infra/tunnel/play.go index d731a1d6c..ee9195681 100644 --- a/pkg/domain/infra/tunnel/play.go +++ b/pkg/domain/infra/tunnel/play.go @@ -5,12 +5,13 @@ import ( "io" "github.com/containers/image/v5/types" + "github.com/containers/podman/v4/pkg/bindings/kube" "github.com/containers/podman/v4/pkg/bindings/play" "github.com/containers/podman/v4/pkg/domain/entities" ) func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, opts entities.PlayKubeOptions) (*entities.PlayKubeReport, error) { - options := new(play.KubeOptions).WithAuthfile(opts.Authfile).WithUsername(opts.Username).WithPassword(opts.Password) + options := new(kube.PlayOptions).WithAuthfile(opts.Authfile).WithUsername(opts.Username).WithPassword(opts.Password) options.WithCertDir(opts.CertDir).WithQuiet(opts.Quiet).WithSignaturePolicy(opts.SignaturePolicy).WithConfigMaps(opts.ConfigMaps) options.WithLogDriver(opts.LogDriver).WithNetwork(opts.Networks).WithSeccompProfileRoot(opts.SeccompProfileRoot) options.WithStaticIPs(opts.StaticIPs).WithStaticMACs(opts.StaticMACs) @@ -31,5 +32,5 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, opts en } func (ic *ContainerEngine) PlayKubeDown(ctx context.Context, body io.Reader, _ entities.PlayKubeDownOptions) (*entities.PlayKubeReport, error) { - return play.KubeDownWithBody(ic.ClientCtx, body) + return play.DownWithBody(ic.ClientCtx, body) } |