Skip to content

Commit 0a58e05

Browse files
Merge pull request #27142 from nothiaki/feat-artifact-rm-ignore
Feat artifact rm ignore
2 parents d9ca93e + b415b0a commit 0a58e05

File tree

11 files changed

+76
-0
lines changed

11 files changed

+76
-0
lines changed

cmd/podman/artifact/rm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
podman artifact rm quay.io/myimage/myartifact:latest
2424
podman artifact rm -a
2525
podman artifact rm c4dfb1609ee2 93fd78260bd1 c0ed59d05ff7
26+
podman artifact rm -i c4dfb1609ee2
2627
`,
2728
}
2829

@@ -32,6 +33,7 @@ var (
3233
func rmFlags(cmd *cobra.Command) {
3334
flags := cmd.Flags()
3435
flags.BoolVarP(&rmOptions.All, "all", "a", false, "Remove all artifacts")
36+
flags.BoolVarP(&rmOptions.Ignore, "ignore", "i", false, "Ignore error if artifact does not exist")
3537
}
3638
func init() {
3739
registry.Commands = append(registry.Commands, registry.CliCommand{

docs/source/markdown/podman-artifact-rm.1.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ providing a name or digest of the artifact.
2222

2323
Print usage statement.
2424

25+
#### **--ignore**, **-i**
26+
27+
Remove artifacts in the local store, ignoring errors when trying to remove artifacts that do not exist.
2528

2629
## EXAMPLES
2730

@@ -49,6 +52,11 @@ Deleted: cee15f7c5ce3e86ae6ce60d84bebdc37ad34acfa9a2611cf47501469ac83a1ab
4952
Deleted: 72875f8f6f78d5b8ba98b2dd2c0a6f395fde8f05ff63a1df580d7a88f5afa97b
5053
```
5154

55+
Remove artifacts ignoring the errors if the artifact does not exist.
56+
```
57+
$ podman artifact rm -i 3f78d5b8ba98b2
58+
```
59+
5260
## SEE ALSO
5361
**[podman(1)](podman.1.md)**, **[podman-artifact(1)](podman-artifact.1.md)**
5462

pkg/api/handlers/libpod/artifacts.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func BatchRemoveArtifact(w http.ResponseWriter, r *http.Request) {
169169
query := struct {
170170
All bool `schema:"all"`
171171
Artifacts []string `schema:"artifacts"`
172+
Ignore bool `schema:"ignore"`
172173
}{}
173174

174175
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
@@ -191,11 +192,16 @@ func BatchRemoveArtifact(w http.ResponseWriter, r *http.Request) {
191192
removeOptions := entities.ArtifactRemoveOptions{
192193
Artifacts: query.Artifacts,
193194
All: query.All,
195+
Ignore: query.Ignore,
194196
}
195197

196198
artifacts, err := imageEngine.ArtifactRm(r.Context(), removeOptions)
197199
if err != nil {
198200
if errors.Is(err, libartifact_types.ErrArtifactNotExist) {
201+
if removeOptions.Ignore {
202+
utils.WriteResponse(w, http.StatusOK, artifacts)
203+
return
204+
}
199205
utils.ArtifactNotFound(w, "", err)
200206
return
201207
}

pkg/api/server/register_artifacts.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ func (s *APIServer) registerArtifactHandlers(r *mux.Router) error {
116116
// in: query
117117
// description: Remove all artifacts
118118
// type: boolean
119+
// - name: ignore
120+
// in: query
121+
// description: Ignore errors if artifact does not exist
122+
// type: boolean
119123
// responses:
120124
// 200:
121125
// $ref: "#/responses/artifactRemoveResponse"

pkg/bindings/artifacts/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type RemoveOptions struct {
5252
All *bool
5353
// Artifacts is a list of Artifact IDs or names to remove
5454
Artifacts []string
55+
// Ignore errors if IDs or names are not defined
56+
Ignore *bool
5557
}
5658

5759
// AddOptions are optional options for removing images

pkg/bindings/artifacts/types_remove_options.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/domain/entities/artifact.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ type ArtifactRemoveOptions struct {
9696
All bool
9797
// Artifacts is a list of Artifact IDs or names to remove
9898
Artifacts []string
99+
// Ignore if a specified artifact does not exist and do not throw any error.
100+
Ignore bool
99101
}
100102

101103
type ArtifactRemoveReport = entitiesTypes.ArtifactRemoveReport

pkg/domain/infra/abi/artifact.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package abi
44

55
import (
66
"context"
7+
"errors"
78
"fmt"
89
"io"
910
"os"
@@ -12,6 +13,7 @@ import (
1213
"github.com/containers/podman/v5/pkg/domain/entities"
1314
"github.com/containers/podman/v5/pkg/libartifact/types"
1415
"github.com/opencontainers/go-digest"
16+
"github.com/sirupsen/logrus"
1517
"go.podman.io/common/libimage"
1618
)
1719

@@ -124,6 +126,10 @@ func (ir *ImageEngine) ArtifactRm(ctx context.Context, opts entities.ArtifactRem
124126
for _, namesOrDigest := range namesOrDigests {
125127
artifactDigest, err := artStore.Remove(ctx, namesOrDigest)
126128
if err != nil {
129+
if opts.Ignore && errors.Is(err, types.ErrArtifactNotExist) {
130+
logrus.Debugf("Artifact with name or digest %q does not exist, ignoring error as request", namesOrDigest)
131+
continue
132+
}
127133
return nil, err
128134
}
129135
artifactDigests = append(artifactDigests, artifactDigest)

pkg/domain/infra/tunnel/artifact.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func (ir *ImageEngine) ArtifactRm(_ context.Context, opts entities.ArtifactRemov
5757
removeOptions := artifacts.RemoveOptions{
5858
All: &opts.All,
5959
Artifacts: opts.Artifacts,
60+
Ignore: &opts.Ignore,
6061
}
6162

6263
return artifacts.Remove(ir.ClientCtx, "", &removeOptions)

test/apiv2/python/rest_api/test_v2_0_0_artifact.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,20 @@ def test_remove_all(self):
527527
rjson = r.json()
528528
self.assertEqual(len(rjson), 0)
529529

530+
def test_remove_with_ignore(self):
531+
# Test remove non existent artifacts with ignore
532+
removeparameters: dict[str, str | list[str]] = {
533+
"Artifacts": "fake_artifact",
534+
"ignore": "true",
535+
}
536+
537+
url = self.uri("/artifacts/remove")
538+
r = requests.delete(url, params=removeparameters)
539+
rjson = r.json()
540+
541+
# Assert correct response code
542+
self.assertEqual(r.status_code, 200, r.text)
543+
530544
def test_remove_absent_artifact_fails(self):
531545
ARTIFACT_NAME = "localhost/fake/artifact:latest"
532546
url = self.uri("/artifacts/" + ARTIFACT_NAME)

0 commit comments

Comments
 (0)