Merge pull request #19769 from vrothberg/fix-19711

kube down/play --replace: handle absent objects
This commit is contained in:
OpenShift Merge Robot
2023-08-28 15:15:41 +02:00
committed by GitHub
5 changed files with 35 additions and 42 deletions

View File

@ -30,6 +30,7 @@ type VolumeConfigResponse struct {
type VolumeRmOptions struct { type VolumeRmOptions struct {
All bool All bool
Force bool Force bool
Ignore bool
Timeout *uint Timeout *uint
} }

View File

@ -1450,6 +1450,9 @@ func (ic *ContainerEngine) PlayKubeDown(ctx context.Context, body io.Reader, opt
for _, name := range podNames { for _, name := range podNames {
pod, err := ic.Libpod.LookupPod(name) pod, err := ic.Libpod.LookupPod(name)
if err != nil { if err != nil {
if errors.Is(err, define.ErrNoSuchPod) {
continue
}
return nil, err return nil, err
} }
ctr, err := pod.ServiceContainer() ctr, err := pod.ServiceContainer()
@ -1463,23 +1466,23 @@ func (ic *ContainerEngine) PlayKubeDown(ctx context.Context, body io.Reader, opt
} }
// Add the reports // Add the reports
reports.StopReport, err = ic.PodStop(ctx, podNames, entities.PodStopOptions{}) reports.StopReport, err = ic.PodStop(ctx, podNames, entities.PodStopOptions{Ignore: true})
if err != nil { if err != nil {
return nil, err return nil, err
} }
reports.RmReport, err = ic.PodRm(ctx, podNames, entities.PodRmOptions{}) reports.RmReport, err = ic.PodRm(ctx, podNames, entities.PodRmOptions{Ignore: true})
if err != nil { if err != nil {
return nil, err return nil, err
} }
reports.SecretRmReport, err = ic.SecretRm(ctx, secretNames, entities.SecretRmOptions{}) reports.SecretRmReport, err = ic.SecretRm(ctx, secretNames, entities.SecretRmOptions{Ignore: true})
if err != nil { if err != nil {
return nil, err return nil, err
} }
if options.Force { if options.Force {
reports.VolumeRmReport, err = ic.VolumeRm(ctx, volumeNames, entities.VolumeRmOptions{}) reports.VolumeRmReport, err = ic.VolumeRm(ctx, volumeNames, entities.VolumeRmOptions{Ignore: true})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -166,17 +166,10 @@ 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 options.Ignore && errors.Is(err, secrets.ErrNoSuchSecret) {
if !options.Ignore {
reports = append(reports, &entities.SecretRmReport{
Err: err,
ID: deletedID,
})
}
continue continue
} else {
return nil, err
} }
reports = append(reports, &entities.SecretRmReport{Err: err, ID: deletedID})
} }
return reports, nil return reports, nil

View File

@ -61,6 +61,9 @@ func (ic *ContainerEngine) VolumeRm(ctx context.Context, namesOrIds []string, op
for _, id := range namesOrIds { for _, id := range namesOrIds {
vol, err := ic.Libpod.LookupVolume(id) vol, err := ic.Libpod.LookupVolume(id)
if err != nil { if err != nil {
if opts.Ignore && errors.Is(err, define.ErrNoSuchVolume) {
continue
}
reports = append(reports, &entities.VolumeRmReport{ reports = append(reports, &entities.VolumeRmReport{
Err: err, Err: err,
Id: id, Id: id,

View File

@ -4148,11 +4148,15 @@ invalid kube kind
Expect(ls).Should(Exit(0)) Expect(ls).Should(Exit(0))
Expect(ls.OutputToStringArray()).To(HaveLen(1)) Expect(ls.OutputToStringArray()).To(HaveLen(1))
// teardown
teardown := podmanTest.Podman([]string{"play", "kube", "--down", kubeYaml}) teardown := podmanTest.Podman([]string{"play", "kube", "--down", kubeYaml})
teardown.WaitWithDefaultTimeout() teardown.WaitWithDefaultTimeout()
Expect(teardown).Should(Exit(0)) Expect(teardown).Should(Exit(0))
// Removing a 2nd time to make sure no "no such error" is returned (see #19711)
teardown = podmanTest.Podman([]string{"play", "kube", "--down", kubeYaml})
teardown.WaitWithDefaultTimeout()
Expect(teardown).Should(Exit(0))
checkls := podmanTest.Podman([]string{"pod", "ps", "--format", "'{{.ID}}'"}) checkls := podmanTest.Podman([]string{"pod", "ps", "--format", "'{{.ID}}'"})
checkls.WaitWithDefaultTimeout() checkls.WaitWithDefaultTimeout()
Expect(checkls).Should(Exit(0)) Expect(checkls).Should(Exit(0))
@ -4172,12 +4176,16 @@ invalid kube kind
Expect(ls).Should(Exit(0)) Expect(ls).Should(Exit(0))
Expect(ls.OutputToStringArray()).To(HaveLen(1)) Expect(ls.OutputToStringArray()).To(HaveLen(1))
// teardown
teardown := podmanTest.Podman([]string{"kube", "down", kubeYaml}) teardown := podmanTest.Podman([]string{"kube", "down", kubeYaml})
teardown.WaitWithDefaultTimeout() teardown.WaitWithDefaultTimeout()
Expect(teardown).Should(Exit(0)) Expect(teardown).Should(Exit(0))
Expect(teardown.OutputToString()).Should(ContainSubstring(ls.OutputToString())) Expect(teardown.OutputToString()).Should(ContainSubstring(ls.OutputToString()))
// Removing a 2nd time to make sure no "no such error" is returned (see #19711)
teardown = podmanTest.Podman([]string{"kube", "down", kubeYaml})
teardown.WaitWithDefaultTimeout()
Expect(teardown).Should(Exit(0))
checkls := podmanTest.Podman([]string{"secret", "ls", "--format", "'{{.ID}}'"}) checkls := podmanTest.Podman([]string{"secret", "ls", "--format", "'{{.ID}}'"})
checkls.WaitWithDefaultTimeout() checkls.WaitWithDefaultTimeout()
Expect(checkls).Should(Exit(0)) Expect(checkls).Should(Exit(0))
@ -4185,13 +4193,15 @@ invalid kube kind
}) })
It("podman play kube teardown pod does not exist", func() { It("podman play kube teardown pod does not exist", func() {
// teardown err := writeYaml(simplePodYaml, kubeYaml)
Expect(err).ToNot(HaveOccurred())
teardown := podmanTest.Podman([]string{"play", "kube", "--down", kubeYaml}) teardown := podmanTest.Podman([]string{"play", "kube", "--down", kubeYaml})
teardown.WaitWithDefaultTimeout() teardown.WaitWithDefaultTimeout()
Expect(teardown).Should(Exit(125)) Expect(teardown).Should(Exit(0))
}) })
It("podman play kube teardown with volume without force delete", func() { It("podman play kube teardown volume --force", func() {
volName := RandomString(12) volName := RandomString(12)
volDevice := define.TypeTmpfs volDevice := define.TypeTmpfs
@ -4217,35 +4227,18 @@ invalid kube kind
teardown.WaitWithDefaultTimeout() teardown.WaitWithDefaultTimeout()
Expect(teardown).To(Exit(0)) Expect(teardown).To(Exit(0))
// volume should not be deleted on teardown // volume should not be deleted on teardown without --force
exists = podmanTest.Podman([]string{"volume", "exists", volName}) exists = podmanTest.Podman([]string{"volume", "exists", volName})
exists.WaitWithDefaultTimeout() exists.WaitWithDefaultTimeout()
Expect(exists).To(Exit(0)) Expect(exists).To(Exit(0))
})
It("podman play kube teardown with volume force delete", func() { // volume gets deleted with --force
teardown = podmanTest.Podman([]string{"play", "kube", "--down", "--force", kubeYaml})
teardown.WaitWithDefaultTimeout()
Expect(teardown).To(Exit(0))
volName := RandomString(12) // Removing a 2nd should succeed as well even if no volume matches
volDevice := define.TypeTmpfs teardown = podmanTest.Podman([]string{"play", "kube", "--down", "--force", kubeYaml})
volType := define.TypeTmpfs
volOpts := "nodev,noexec"
pvc := getPVC(withPVCName(volName),
withPVCAnnotations(util.VolumeDeviceAnnotation, volDevice),
withPVCAnnotations(util.VolumeTypeAnnotation, volType),
withPVCAnnotations(util.VolumeMountOptsAnnotation, volOpts))
err = generateKubeYaml("persistentVolumeClaim", pvc, kubeYaml)
Expect(err).ToNot(HaveOccurred())
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
kube.WaitWithDefaultTimeout()
Expect(kube).Should(Exit(0))
exists := podmanTest.Podman([]string{"volume", "exists", volName})
exists.WaitWithDefaultTimeout()
Expect(exists).To(Exit(0))
teardown := podmanTest.Podman([]string{"play", "kube", "--down", "--force", kubeYaml})
teardown.WaitWithDefaultTimeout() teardown.WaitWithDefaultTimeout()
Expect(teardown).To(Exit(0)) Expect(teardown).To(Exit(0))