secret: add support for `--ignore` with rm

Signed-off-by: danishprakash <danish.prakash@suse.com>
This commit is contained in:
danishprakash 2023-06-23 17:34:49 +05:30
parent 608f484e9b
commit bfd2a8cad3
No known key found for this signature in database
GPG Key ID: 66820561C42B7C0C
6 changed files with 30 additions and 9 deletions

View File

@ -29,6 +29,7 @@ func init() {
})
flags := rmCmd.Flags()
flags.BoolVarP(&rmOptions.All, "all", "a", false, "Remove all secrets")
flags.BoolVarP(&rmOptions.Ignore, "ignore", "i", false, "Ignore errors when a specified secret is missing")
}
var (

View File

@ -26,6 +26,9 @@ Remove all existing secrets.
Print usage statement.
#### **--ignore**, **-i**
Ignore errors when specified secrets are not present.
## EXAMPLES
```

View File

@ -34,7 +34,8 @@ type SecretListReport struct {
}
type SecretRmOptions struct {
All bool
All bool
Ignore bool
}
type SecretRmReport struct {

View File

@ -167,10 +167,12 @@ func (ic *ContainerEngine) SecretRm(ctx context.Context, nameOrIDs []string, opt
for _, nameOrID := range toRemove {
deletedID, err := manager.Delete(nameOrID)
if err == nil || strings.Contains(err.Error(), "no such secret") {
reports = append(reports, &entities.SecretRmReport{
Err: err,
ID: deletedID,
})
if !options.Ignore {
reports = append(reports, &entities.SecretRmReport{
Err: err,
ID: deletedID,
})
}
continue
} else {
return nil, err

View File

@ -77,10 +77,12 @@ func (ic *ContainerEngine) SecretRm(ctx context.Context, nameOrIDs []string, opt
return nil, err
}
if errModel.ResponseCode == 404 {
allRm = append(allRm, &entities.SecretRmReport{
Err: fmt.Errorf("no secret with name or id %q: no such secret ", name),
ID: "",
})
if !options.Ignore {
allRm = append(allRm, &entities.SecretRmReport{
Err: fmt.Errorf("no secret with name or id %q: no such secret ", name),
ID: "",
})
}
continue
}
}

View File

@ -336,6 +336,18 @@ var _ = Describe("Podman secret", func() {
Expect(session.OutputToStringArray()).To(HaveLen(1))
})
It("podman secret rm --ignore", func() {
remove := podmanTest.Podman([]string{"secret", "rm", "non-existent-secret"})
remove.WaitWithDefaultTimeout()
Expect(remove).Should(Not(Exit(0)))
Expect(remove.ErrorToString()).To(Equal("Error: no secret with name or id \"non-existent-secret\": no such secret"))
ignoreRm := podmanTest.Podman([]string{"secret", "rm", "--ignore", "non-existent-secret"})
ignoreRm.WaitWithDefaultTimeout()
Expect(ignoreRm).Should(Exit(0))
Expect(ignoreRm.ErrorToString()).To(BeEmpty())
})
It("podman secret creates from environment variable", func() {
// no env variable set, should fail
session := podmanTest.Podman([]string{"secret", "create", "--env", "a", "MYENVVAR"})