mirror of
https://github.com/containers/podman.git
synced 2025-06-21 17:38:12 +08:00
Refactored networkPrune function
Refactored the networkPrune function to improve readability. This commit changes the `networkPrune` function to use the `PrintNetworkPruneResults` function. [NO NEW TESTS NEEDED] Signed-off-by: Toshiki Sonoda <sonoda.toshiki@fujitsu.com>
This commit is contained in:
@ -52,10 +52,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func networkPrune(cmd *cobra.Command, _ []string) error {
|
func networkPrune(cmd *cobra.Command, _ []string) error {
|
||||||
var (
|
var err error
|
||||||
errs utils.OutputErrors
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
if !force {
|
if !force {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
fmt.Println("WARNING! This will remove all networks not used by at least one container.")
|
fmt.Println("WARNING! This will remove all networks not used by at least one container.")
|
||||||
@ -77,13 +74,5 @@ func networkPrune(cmd *cobra.Command, _ []string) error {
|
|||||||
setExitCode(err)
|
setExitCode(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, r := range responses {
|
return utils.PrintNetworkPruneResults(responses, false)
|
||||||
if r.Error == nil {
|
|
||||||
fmt.Println(r.Name)
|
|
||||||
} else {
|
|
||||||
setExitCode(r.Error)
|
|
||||||
errs = append(errs, r.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errs.PrintErrors()
|
|
||||||
}
|
}
|
||||||
|
@ -85,16 +85,16 @@ func PrintImagePruneResults(imagePruneReports []*reports.PruneReport, heading bo
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrintNetworkPruneResults(networkPruneReport []*reports.PruneReport, heading bool) error {
|
func PrintNetworkPruneResults(networkPruneReport []*entities.NetworkPruneReport, heading bool) error {
|
||||||
var errs OutputErrors
|
var errs OutputErrors
|
||||||
if heading && len(networkPruneReport) > 0 {
|
if heading && len(networkPruneReport) > 0 {
|
||||||
fmt.Println("Deleted Networks")
|
fmt.Println("Deleted Networks")
|
||||||
}
|
}
|
||||||
for _, r := range networkPruneReport {
|
for _, r := range networkPruneReport {
|
||||||
if r.Err == nil {
|
if r.Error == nil {
|
||||||
fmt.Println(r.Id)
|
fmt.Println(r.Name)
|
||||||
} else {
|
} else {
|
||||||
errs = append(errs, r.Err)
|
errs = append(errs, r.Error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return errs.PrintErrors()
|
return errs.PrintErrors()
|
||||||
|
@ -81,8 +81,7 @@ type NetworkPruneReport struct {
|
|||||||
Error error
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
// NetworkPruneOptions describes options for pruning
|
// NetworkPruneOptions describes options for pruning unused networks
|
||||||
// unused cni networks
|
|
||||||
type NetworkPruneOptions struct {
|
type NetworkPruneOptions struct {
|
||||||
Filters map[string][]string
|
Filters map[string][]string
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ type SystemPruneReport struct {
|
|||||||
PodPruneReport []*PodPruneReport
|
PodPruneReport []*PodPruneReport
|
||||||
ContainerPruneReports []*reports.PruneReport
|
ContainerPruneReports []*reports.PruneReport
|
||||||
ImagePruneReports []*reports.PruneReport
|
ImagePruneReports []*reports.PruneReport
|
||||||
NetworkPruneReports []*reports.PruneReport
|
NetworkPruneReports []*NetworkPruneReport
|
||||||
VolumePruneReports []*reports.PruneReport
|
VolumePruneReports []*reports.PruneReport
|
||||||
ReclaimedSpace uint64
|
ReclaimedSpace uint64
|
||||||
}
|
}
|
||||||
|
@ -157,15 +157,15 @@ func (ic *ContainerEngine) SystemPrune(ctx context.Context, options entities.Sys
|
|||||||
|
|
||||||
// TODO: Figure out cleaner way to handle all of the different PruneOptions
|
// TODO: Figure out cleaner way to handle all of the different PruneOptions
|
||||||
// Remove all unused pods.
|
// Remove all unused pods.
|
||||||
podPruneReport, err := ic.prunePodHelper(ctx)
|
podPruneReports, err := ic.prunePodHelper(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(podPruneReport) > 0 {
|
if len(podPruneReports) > 0 {
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
|
|
||||||
systemPruneReport.PodPruneReport = append(systemPruneReport.PodPruneReport, podPruneReport...)
|
systemPruneReport.PodPruneReport = append(systemPruneReport.PodPruneReport, podPruneReports...)
|
||||||
|
|
||||||
// Remove all unused containers.
|
// Remove all unused containers.
|
||||||
containerPruneOptions := entities.ContainerPruneOptions{}
|
containerPruneOptions := entities.ContainerPruneOptions{}
|
||||||
@ -201,38 +201,35 @@ func (ic *ContainerEngine) SystemPrune(ctx context.Context, options entities.Sys
|
|||||||
networkPruneOptions := entities.NetworkPruneOptions{}
|
networkPruneOptions := entities.NetworkPruneOptions{}
|
||||||
networkPruneOptions.Filters = options.Filters
|
networkPruneOptions.Filters = options.Filters
|
||||||
|
|
||||||
networkPruneReport, err := ic.NetworkPrune(ctx, networkPruneOptions)
|
networkPruneReports, err := ic.NetworkPrune(ctx, networkPruneOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(networkPruneReport) > 0 {
|
if len(networkPruneReports) > 0 {
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
for _, net := range networkPruneReport {
|
|
||||||
systemPruneReport.NetworkPruneReports = append(systemPruneReport.NetworkPruneReports, &reports.PruneReport{
|
// Networks reclaimedSpace are always '0'.
|
||||||
Id: net.Name,
|
systemPruneReport.NetworkPruneReports = append(systemPruneReport.NetworkPruneReports, networkPruneReports...)
|
||||||
Err: net.Error,
|
|
||||||
Size: 0,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove unused volume data.
|
// Remove unused volume data.
|
||||||
if options.Volume {
|
if options.Volume {
|
||||||
volumePruneOptions := entities.VolumePruneOptions{}
|
volumePruneOptions := entities.VolumePruneOptions{}
|
||||||
volumePruneOptions.Filters = (url.Values)(options.Filters)
|
volumePruneOptions.Filters = (url.Values)(options.Filters)
|
||||||
|
|
||||||
volumePruneReport, err := ic.VolumePrune(ctx, volumePruneOptions)
|
volumePruneReports, err := ic.VolumePrune(ctx, volumePruneOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(volumePruneReport) > 0 {
|
if len(volumePruneReports) > 0 {
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
|
|
||||||
reclaimedSpace += reports.PruneReportsSize(volumePruneReport)
|
reclaimedSpace += reports.PruneReportsSize(volumePruneReports)
|
||||||
systemPruneReport.VolumePruneReports = append(systemPruneReport.VolumePruneReports, volumePruneReport...)
|
systemPruneReport.VolumePruneReports = append(systemPruneReport.VolumePruneReports, volumePruneReports...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
systemPruneReport.ReclaimedSpace = reclaimedSpace
|
systemPruneReport.ReclaimedSpace = reclaimedSpace
|
||||||
return systemPruneReport, nil
|
return systemPruneReport, nil
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,7 @@ func (ic *ContainerEngine) NetworkExists(ctx context.Context, networkname string
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Network prune removes unused cni networks
|
// Network prune removes unused networks
|
||||||
func (ic *ContainerEngine) NetworkPrune(ctx context.Context, options entities.NetworkPruneOptions) ([]*entities.NetworkPruneReport, error) {
|
func (ic *ContainerEngine) NetworkPrune(ctx context.Context, options entities.NetworkPruneOptions) ([]*entities.NetworkPruneReport, error) {
|
||||||
opts := new(network.PruneOptions).WithFilters(options.Filters)
|
opts := new(network.PruneOptions).WithFilters(options.Filters)
|
||||||
return network.Prune(ic.ClientCtx, opts)
|
return network.Prune(ic.ClientCtx, opts)
|
||||||
|
@ -259,11 +259,12 @@ var _ = Describe("Podman prune", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("podman system prune networks", func() {
|
It("podman system prune networks", func() {
|
||||||
// About netavark network backend test.
|
// Create new network.
|
||||||
session := podmanTest.Podman([]string{"network", "create", "test"})
|
session := podmanTest.Podman([]string{"network", "create", "test"})
|
||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
Expect(session).Should(Exit(0))
|
Expect(session).Should(Exit(0))
|
||||||
|
|
||||||
|
// Remove all unused networks.
|
||||||
session = podmanTest.Podman([]string{"system", "prune", "-f"})
|
session = podmanTest.Podman([]string{"system", "prune", "-f"})
|
||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
Expect(session).Should(Exit(0))
|
Expect(session).Should(Exit(0))
|
||||||
@ -274,7 +275,7 @@ var _ = Describe("Podman prune", func() {
|
|||||||
Expect(session).Should(Exit(0))
|
Expect(session).Should(Exit(0))
|
||||||
Expect(session.OutputToStringArray()).To(HaveLen(1))
|
Expect(session.OutputToStringArray()).To(HaveLen(1))
|
||||||
|
|
||||||
// Remove all unused networks.
|
// Unused networks removed.
|
||||||
session = podmanTest.Podman([]string{"network", "ls", "-q", "--filter", "name=^test$"})
|
session = podmanTest.Podman([]string{"network", "ls", "-q", "--filter", "name=^test$"})
|
||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
Expect(session).Should(Exit(0))
|
Expect(session).Should(Exit(0))
|
||||||
|
Reference in New Issue
Block a user