podman

Форк
0
/
runtime_volume.go 
150 строк · 3.6 Кб
1
//go:build !remote
2

3
package libpod
4

5
import (
6
	"context"
7
	"errors"
8

9
	"github.com/containers/podman/v5/libpod/define"
10
	"github.com/containers/podman/v5/libpod/events"
11
	"github.com/containers/podman/v5/pkg/domain/entities/reports"
12
)
13

14
// Contains the public Runtime API for volumes
15

16
// A VolumeCreateOption is a functional option which alters the Volume created by
17
// NewVolume
18
type VolumeCreateOption func(*Volume) error
19

20
// VolumeFilter is a function to determine whether a volume is included in command
21
// output. Volumes to be outputted are tested using the function. a true return will
22
// include the volume, a false return will exclude it.
23
type VolumeFilter func(*Volume) bool
24

25
// RemoveVolume removes a volumes
26
func (r *Runtime) RemoveVolume(ctx context.Context, v *Volume, force bool, timeout *uint) error {
27
	if !r.valid {
28
		return define.ErrRuntimeStopped
29
	}
30

31
	if !v.valid {
32
		if ok, _ := r.state.HasVolume(v.Name()); !ok {
33
			// Volume probably already removed
34
			// Or was never in the runtime to begin with
35
			return nil
36
		}
37
	}
38
	return r.removeVolume(ctx, v, force, timeout, false)
39
}
40

41
// GetVolume retrieves a volume given its full name.
42
func (r *Runtime) GetVolume(name string) (*Volume, error) {
43
	if !r.valid {
44
		return nil, define.ErrRuntimeStopped
45
	}
46

47
	vol, err := r.state.Volume(name)
48
	if err != nil {
49
		return nil, err
50
	}
51

52
	return vol, nil
53
}
54

55
// LookupVolume retrieves a volume by unambiguous partial name.
56
func (r *Runtime) LookupVolume(name string) (*Volume, error) {
57
	if !r.valid {
58
		return nil, define.ErrRuntimeStopped
59
	}
60

61
	vol, err := r.state.LookupVolume(name)
62
	if err != nil {
63
		return nil, err
64
	}
65

66
	return vol, nil
67
}
68

69
// HasVolume checks to see if a volume with the given name exists
70
func (r *Runtime) HasVolume(name string) (bool, error) {
71
	if !r.valid {
72
		return false, define.ErrRuntimeStopped
73
	}
74

75
	return r.state.HasVolume(name)
76
}
77

78
// Volumes retrieves all volumes
79
// Filters can be provided which will determine which volumes are included in the
80
// output. If multiple filters are used, a volume will be returned if
81
// any of the filters are matched
82
func (r *Runtime) Volumes(filters ...VolumeFilter) ([]*Volume, error) {
83
	if !r.valid {
84
		return nil, define.ErrRuntimeStopped
85
	}
86

87
	vols, err := r.state.AllVolumes()
88
	if err != nil {
89
		return nil, err
90
	}
91

92
	if len(filters) == 0 {
93
		return vols, nil
94
	}
95

96
	volsFiltered := make([]*Volume, 0, len(vols))
97
	for _, vol := range vols {
98
		include := false
99
		for _, filter := range filters {
100
			include = include || filter(vol)
101
		}
102

103
		if include {
104
			volsFiltered = append(volsFiltered, vol)
105
		}
106
	}
107

108
	return volsFiltered, nil
109
}
110

111
// GetAllVolumes retrieves all the volumes
112
func (r *Runtime) GetAllVolumes() ([]*Volume, error) {
113
	if !r.valid {
114
		return nil, define.ErrRuntimeStopped
115
	}
116

117
	return r.state.AllVolumes()
118
}
119

120
// PruneVolumes removes unused volumes from the system
121
func (r *Runtime) PruneVolumes(ctx context.Context, filterFuncs []VolumeFilter) ([]*reports.PruneReport, error) {
122
	preports := make([]*reports.PruneReport, 0)
123
	vols, err := r.Volumes(filterFuncs...)
124
	if err != nil {
125
		return nil, err
126
	}
127

128
	for _, vol := range vols {
129
		report := new(reports.PruneReport)
130
		volSize, err := vol.Size()
131
		if err != nil {
132
			volSize = 0
133
		}
134
		report.Size = volSize
135
		report.Id = vol.Name()
136
		var timeout *uint
137
		if err := r.RemoveVolume(ctx, vol, false, timeout); err != nil {
138
			if !errors.Is(err, define.ErrVolumeBeingUsed) && !errors.Is(err, define.ErrVolumeRemoved) {
139
				report.Err = err
140
			} else {
141
				// We didn't remove the volume for some reason
142
				continue
143
			}
144
		} else {
145
			vol.newVolumeEvent(events.Prune)
146
		}
147
		preports = append(preports, report)
148
	}
149
	return preports, nil
150
}
151

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.