1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
package compat
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"time"
"github.com/containers/podman/v4/libpod"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/api/handlers"
"github.com/containers/podman/v4/pkg/api/handlers/utils"
api "github.com/containers/podman/v4/pkg/api/types"
"github.com/containers/podman/v4/pkg/domain/filters"
"github.com/containers/podman/v4/pkg/domain/infra/abi/parse"
"github.com/containers/podman/v4/pkg/util"
docker_api_types "github.com/docker/docker/api/types"
docker_api_types_volume "github.com/docker/docker/api/types/volume"
"github.com/gorilla/schema"
)
func ListVolumes(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
filtersMap, err := util.PrepareFilters(r)
if err != nil {
utils.Error(w, http.StatusInternalServerError,
fmt.Errorf("failed to parse parameters for %s: %w", r.URL.String(), err))
return
}
// Reject any libpod specific filters since `GenerateVolumeFilters()` will
// happily parse them for us.
for filter := range *filtersMap {
if filter == "opts" {
utils.Error(w, http.StatusInternalServerError,
fmt.Errorf("unsupported libpod filters passed to docker endpoint"))
return
}
}
volumeFilters, err := filters.GenerateVolumeFilters(*filtersMap)
if err != nil {
utils.InternalServerError(w, err)
return
}
vols, err := runtime.Volumes(volumeFilters...)
if err != nil {
utils.InternalServerError(w, err)
return
}
volumeConfigs := make([]*docker_api_types.Volume, 0, len(vols))
for _, v := range vols {
mp, err := v.MountPoint()
if err != nil {
utils.InternalServerError(w, err)
return
}
config := docker_api_types.Volume{
Name: v.Name(),
Driver: v.Driver(),
Mountpoint: mp,
CreatedAt: v.CreatedTime().Format(time.RFC3339),
Labels: v.Labels(),
Scope: v.Scope(),
Options: v.Options(),
}
volumeConfigs = append(volumeConfigs, &config)
}
response := docker_api_types_volume.VolumeListOKBody{
Volumes: volumeConfigs,
Warnings: []string{},
}
utils.WriteResponse(w, http.StatusOK, response)
}
func CreateVolume(w http.ResponseWriter, r *http.Request) {
var (
volumeOptions []libpod.VolumeCreateOption
runtime = r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
decoder = r.Context().Value(api.DecoderKey).(*schema.Decoder)
)
/* No query string data*/
query := struct{}{}
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, http.StatusInternalServerError,
fmt.Errorf("failed to parse parameters for %s: %w", r.URL.String(), err))
return
}
// decode params from body
input := docker_api_types_volume.VolumeCreateBody{}
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("Decode(): %w", err))
return
}
var (
existingVolume *libpod.Volume
err error
)
if len(input.Name) != 0 {
// See if the volume exists already
existingVolume, err = runtime.GetVolume(input.Name)
if err != nil && !errors.Is(err, define.ErrNoSuchVolume) {
utils.InternalServerError(w, err)
return
}
}
// if using the compat layer and the volume already exists, we
// must return a 201 with the same information as create
if existingVolume != nil && !utils.IsLibpodRequest(r) {
mp, err := existingVolume.MountPoint()
if err != nil {
utils.InternalServerError(w, err)
return
}
response := docker_api_types.Volume{
CreatedAt: existingVolume.CreatedTime().Format(time.RFC3339),
Driver: existingVolume.Driver(),
Labels: existingVolume.Labels(),
Mountpoint: mp,
Name: existingVolume.Name(),
Options: existingVolume.Options(),
Scope: existingVolume.Scope(),
}
utils.WriteResponse(w, http.StatusCreated, response)
return
}
if len(input.Name) > 0 {
volumeOptions = append(volumeOptions, libpod.WithVolumeName(input.Name))
}
if len(input.Driver) > 0 {
volumeOptions = append(volumeOptions, libpod.WithVolumeDriver(input.Driver))
}
if len(input.Labels) > 0 {
volumeOptions = append(volumeOptions, libpod.WithVolumeLabels(input.Labels))
}
if len(input.DriverOpts) > 0 {
parsedOptions, err := parse.VolumeOptions(input.DriverOpts)
if err != nil {
utils.InternalServerError(w, err)
return
}
volumeOptions = append(volumeOptions, parsedOptions...)
}
vol, err := runtime.NewVolume(r.Context(), volumeOptions...)
if err != nil {
utils.InternalServerError(w, err)
return
}
config, err := vol.Config()
if err != nil {
utils.InternalServerError(w, err)
return
}
mp, err := vol.MountPoint()
if err != nil {
utils.InternalServerError(w, err)
return
}
volResponse := docker_api_types.Volume{
Name: config.Name,
Driver: config.Driver,
Mountpoint: mp,
CreatedAt: config.CreatedTime.Format(time.RFC3339),
Labels: config.Labels,
Options: config.Options,
Scope: "local",
// ^^ We don't have volume scoping so we'll just claim it's "local"
// like we do in the `libpod.Volume.Scope()` method
//
// TODO: We don't include the volume `Status` or `UsageData`, but both
// are nullable in the Docker engine API spec so that's fine for now
}
utils.WriteResponse(w, http.StatusCreated, volResponse)
}
func InspectVolume(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
name := utils.GetName(r)
vol, err := runtime.GetVolume(name)
if err != nil {
utils.VolumeNotFound(w, name, err)
return
}
mp, err := vol.MountPoint()
if err != nil {
utils.InternalServerError(w, err)
return
}
volResponse := docker_api_types.Volume{
Name: vol.Name(),
Driver: vol.Driver(),
Mountpoint: mp,
CreatedAt: vol.CreatedTime().Format(time.RFC3339),
Labels: vol.Labels(),
Options: vol.Options(),
Scope: vol.Scope(),
// TODO: As above, we don't return `Status` or `UsageData` yet
}
utils.WriteResponse(w, http.StatusOK, volResponse)
}
func RemoveVolume(w http.ResponseWriter, r *http.Request) {
var (
runtime = r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
decoder = r.Context().Value(api.DecoderKey).(*schema.Decoder)
)
query := struct {
Force bool `schema:"force"`
Timeout *uint `schema:"timeout"`
}{
// override any golang type defaults
}
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, http.StatusInternalServerError,
fmt.Errorf("failed to parse parameters for %s: %w", r.URL.String(), err))
return
}
/* The implications for `force` differ between Docker and us, so we can't
* simply pass the `force` parameter to `runtime.RemoveVolume()`.
* Specifically, Docker's behavior seems to be that `force` means "do not
* error on missing volume"; ours means "remove any not-running containers
* using the volume at the same time".
*
* With this in mind, we only consider the `force` query parameter when we
* hunt for specified volume by name, using it to selectively return a 204
* or blow up depending on `force` being truthy or falsey/unset
* respectively.
*/
name := utils.GetName(r)
vol, err := runtime.LookupVolume(name)
if err == nil {
// As above, we do not pass `force` from the query parameters here
if err := runtime.RemoveVolume(r.Context(), vol, false, query.Timeout); err != nil {
if errors.Is(err, define.ErrVolumeBeingUsed) {
utils.Error(w, http.StatusConflict, err)
} else {
utils.InternalServerError(w, err)
}
} else {
// Success
utils.WriteResponse(w, http.StatusNoContent, nil)
}
} else {
if !query.Force {
utils.VolumeNotFound(w, name, err)
} else {
// Volume does not exist and `force` is truthy - this emulates what
// Docker would do when told to `force` removal of a nonexistent
// volume
utils.WriteResponse(w, http.StatusNoContent, nil)
}
}
}
func PruneVolumes(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
filterMap, err := util.PrepareFilters(r)
if err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("Decode(): %w", err))
return
}
f := (url.Values)(*filterMap)
filterFuncs, err := filters.GeneratePruneVolumeFilters(f)
if err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("failed to parse filters for %s: %w", f.Encode(), err))
return
}
pruned, err := runtime.PruneVolumes(r.Context(), filterFuncs)
if err != nil {
utils.InternalServerError(w, err)
return
}
var errorMsg bytes.Buffer
var reclaimedSpace uint64
prunedIds := make([]string, 0, len(pruned))
for _, v := range pruned {
if v.Err != nil {
errorMsg.WriteString(v.Err.Error())
errorMsg.WriteString("; ")
continue
}
prunedIds = append(prunedIds, v.Id)
reclaimedSpace += v.Size
}
if errorMsg.Len() > 0 {
utils.InternalServerError(w, errors.New(errorMsg.String()))
return
}
payload := handlers.VolumesPruneReport{
VolumesPruneReport: docker_api_types.VolumesPruneReport{
VolumesDeleted: prunedIds,
SpaceReclaimed: reclaimedSpace,
},
}
utils.WriteResponse(w, http.StatusOK, payload)
}
|