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 := rmCmd.Flags()
flags.BoolVarP(&rmOptions.All, "all", "a", false, "Remove all secrets") 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 ( var (

View File

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

View File

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

View File

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

View File

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

View File

@ -336,6 +336,18 @@ var _ = Describe("Podman secret", func() {
Expect(session.OutputToStringArray()).To(HaveLen(1)) 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() { It("podman secret creates from environment variable", func() {
// no env variable set, should fail // no env variable set, should fail
session := podmanTest.Podman([]string{"secret", "create", "--env", "a", "MYENVVAR"}) session := podmanTest.Podman([]string{"secret", "create", "--env", "a", "MYENVVAR"})