github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/api/handlers/libpod/play.go (about)

     1  package libpod
     2  
     3  import (
     4  	"net"
     5  	"net/http"
     6  
     7  	"github.com/containers/image/v5/types"
     8  	"github.com/hanks177/podman/v4/libpod"
     9  	"github.com/hanks177/podman/v4/pkg/api/handlers/utils"
    10  	api "github.com/hanks177/podman/v4/pkg/api/types"
    11  	"github.com/hanks177/podman/v4/pkg/auth"
    12  	"github.com/hanks177/podman/v4/pkg/domain/entities"
    13  	"github.com/hanks177/podman/v4/pkg/domain/infra/abi"
    14  	"github.com/gorilla/schema"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  func PlayKube(w http.ResponseWriter, r *http.Request) {
    19  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
    20  	decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder)
    21  	query := struct {
    22  		Annotations map[string]string `schema:"annotations"`
    23  		Network     []string          `schema:"network"`
    24  		TLSVerify   bool              `schema:"tlsVerify"`
    25  		LogDriver   string            `schema:"logDriver"`
    26  		LogOptions  []string          `schema:"logOptions"`
    27  		Start       bool              `schema:"start"`
    28  		StaticIPs   []string          `schema:"staticIPs"`
    29  		StaticMACs  []string          `schema:"staticMACs"`
    30  		NoHosts     bool              `schema:"noHosts"`
    31  	}{
    32  		TLSVerify: true,
    33  		Start:     true,
    34  	}
    35  
    36  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    37  		utils.Error(w, http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    38  		return
    39  	}
    40  
    41  	staticIPs := make([]net.IP, 0, len(query.StaticIPs))
    42  	for _, ipString := range query.StaticIPs {
    43  		ip := net.ParseIP(ipString)
    44  		if ip == nil {
    45  			utils.Error(w, http.StatusBadRequest, errors.Errorf("Invalid IP address %s", ipString))
    46  			return
    47  		}
    48  		staticIPs = append(staticIPs, ip)
    49  	}
    50  
    51  	staticMACs := make([]net.HardwareAddr, 0, len(query.StaticMACs))
    52  	for _, macString := range query.StaticMACs {
    53  		mac, err := net.ParseMAC(macString)
    54  		if err != nil {
    55  			utils.Error(w, http.StatusBadRequest, err)
    56  			return
    57  		}
    58  		staticMACs = append(staticMACs, mac)
    59  	}
    60  
    61  	authConf, authfile, err := auth.GetCredentials(r)
    62  	if err != nil {
    63  		utils.Error(w, http.StatusBadRequest, err)
    64  		return
    65  	}
    66  	defer auth.RemoveAuthfile(authfile)
    67  	var username, password string
    68  	if authConf != nil {
    69  		username = authConf.Username
    70  		password = authConf.Password
    71  	}
    72  
    73  	logDriver := query.LogDriver
    74  	if logDriver == "" {
    75  		config, err := runtime.GetConfig()
    76  		if err != nil {
    77  			utils.Error(w, http.StatusInternalServerError, err)
    78  			return
    79  		}
    80  		logDriver = config.Containers.LogDriver
    81  	}
    82  
    83  	containerEngine := abi.ContainerEngine{Libpod: runtime}
    84  	options := entities.PlayKubeOptions{
    85  		Annotations: query.Annotations,
    86  		Authfile:    authfile,
    87  		Username:    username,
    88  		Password:    password,
    89  		Networks:    query.Network,
    90  		NoHosts:     query.NoHosts,
    91  		Quiet:       true,
    92  		LogDriver:   logDriver,
    93  		LogOptions:  query.LogOptions,
    94  		StaticIPs:   staticIPs,
    95  		StaticMACs:  staticMACs,
    96  	}
    97  	if _, found := r.URL.Query()["tlsVerify"]; found {
    98  		options.SkipTLSVerify = types.NewOptionalBool(!query.TLSVerify)
    99  	}
   100  	if _, found := r.URL.Query()["start"]; found {
   101  		options.Start = types.NewOptionalBool(query.Start)
   102  	}
   103  	report, err := containerEngine.PlayKube(r.Context(), r.Body, options)
   104  	_ = r.Body.Close()
   105  	if err != nil {
   106  		utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "error playing YAML file"))
   107  		return
   108  	}
   109  	utils.WriteResponse(w, http.StatusOK, report)
   110  }
   111  
   112  func PlayKubeDown(w http.ResponseWriter, r *http.Request) {
   113  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
   114  	containerEngine := abi.ContainerEngine{Libpod: runtime}
   115  	options := new(entities.PlayKubeDownOptions)
   116  	report, err := containerEngine.PlayKubeDown(r.Context(), r.Body, *options)
   117  	_ = r.Body.Close()
   118  	if err != nil {
   119  		utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "error tearing down YAML file"))
   120  		return
   121  	}
   122  	utils.WriteResponse(w, http.StatusOK, report)
   123  }