Skip to content

Commit

Permalink
feat(kuma-cp) ignore not found resource on delete all
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubdyszkiewicz committed Oct 21, 2019
1 parent 847f272 commit d5348ce
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
11 changes: 7 additions & 4 deletions pkg/core/managers/apis/mesh/mesh_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,18 @@ func (m *meshManager) Delete(ctx context.Context, resource core_model.Resource,
}
// delete Mesh first to avoid a state where a Mesh could exist without a Built-in CA.
// even if removal of Built-in CA fails later on, delete opration can be safely tried again.
var notFoundErr error
if err := m.store.Delete(ctx, mesh, fs...); err != nil {
if !core_store.IsResourceNotFound(err) { // ignore other errors so we can retry removing other resources
if core_store.IsResourceNotFound(err) {
notFoundErr = err
} else { // ignore other errors so we can retry removing other resources
return err
}
}
opts := core_store.NewDeleteOptions(fs...)
// delete CA
name := core_store.NewDeleteOptions(fs...).Mesh
if err := m.builtinCaManager.Delete(ctx, name); err != nil {
if err := m.builtinCaManager.Delete(ctx, name); err != nil && !core_store.IsResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete Builtin CA for a given mesh")
}
// delete all other secrets
Expand All @@ -120,7 +123,7 @@ func (m *meshManager) Delete(ctx context.Context, resource core_model.Resource,
return errors.Wrap(err, "could not delete associated resources")
}
}
return nil
return notFoundErr
}

func (m *meshManager) DeleteAll(ctx context.Context, list core_model.ResourceList, fs ...core_store.DeleteAllOptionsFunc) error {
Expand All @@ -133,7 +136,7 @@ func (m *meshManager) DeleteAll(ctx context.Context, list core_model.ResourceLis
return err
}
for _, item := range meshes.Items {
if err := m.Delete(ctx, item, core_store.DeleteBy(core_model.MetaToResourceKey(item.Meta))); err != nil {
if err := m.Delete(ctx, item, core_store.DeleteBy(core_model.MetaToResourceKey(item.Meta))); err != nil && !core_store.IsResourceNotFound(err) {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/resources/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (r *resourcesManager) DeleteAll(ctx context.Context, list model.ResourceLis
return err
}
for _, obj := range list.GetItems() {
if err := r.Delete(ctx, obj, store.DeleteBy(model.MetaToResourceKey(obj.GetMeta()))); err != nil {
if err := r.Delete(ctx, obj, store.DeleteBy(model.MetaToResourceKey(obj.GetMeta()))); err != nil && !store.IsResourceNotFound(err) {
return err
}
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/core/resources/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
mesh_proto "github.com/Kong/kuma/api/mesh/v1alpha1"
"github.com/Kong/kuma/pkg/core/resources/apis/mesh"
"github.com/Kong/kuma/pkg/core/resources/manager"
"github.com/Kong/kuma/pkg/core/resources/model"
"github.com/Kong/kuma/pkg/core/resources/store"
"github.com/Kong/kuma/pkg/plugins/resources/memory"
"github.com/Kong/kuma/pkg/test/apis/sample/v1alpha1"
Expand Down Expand Up @@ -74,6 +75,14 @@ var _ = Describe("Resource Manager", func() {
_, err = createSampleResource("mesh-2")
Expect(err).ToNot(HaveOccurred())

tlKey := model.ResourceKey{
Mesh: "mesh-1",
Namespace: "default",
Name: "tl-1",
}
err = resManager.Create(context.Background(), &mesh.TrafficLogResource{}, store.CreateBy(tlKey))
Expect(err).ToNot(HaveOccurred())

// when
err = resManager.DeleteAll(context.Background(), &sample.TrafficRouteResourceList{}, store.DeleteAllByMesh("mesh-1"))

Expand All @@ -85,10 +94,14 @@ var _ = Describe("Resource Manager", func() {
err = resManager.Get(context.Background(), &res1, store.GetByKey("default", "tr-1", "mesh-1"))
Expect(store.IsResourceNotFound(err)).To(BeTrue())

// and only TrafficRoutes are deleted
Expect(resManager.Get(context.Background(), &mesh.TrafficLogResource{}, store.GetBy(tlKey))).To(Succeed())

// and resource from mesh-2 is retained
res2 := sample.TrafficRouteResource{}
err = resManager.Get(context.Background(), &res2, store.GetByKey("default", "tr-1", "mesh-2"))
Expect(err).ToNot(HaveOccurred())

})
})
})
2 changes: 1 addition & 1 deletion pkg/core/secrets/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *secretManager) DeleteAll(ctx context.Context, fs ...core_store.DeleteAl
return err
}
for _, item := range list.Items {
if err := s.Delete(ctx, item, core_store.DeleteBy(model.MetaToResourceKey(item.Meta))); err != nil {
if err := s.Delete(ctx, item, core_store.DeleteBy(model.MetaToResourceKey(item.Meta))); err != nil && !core_store.IsResourceNotFound(err) {
return err
}
}
Expand Down

0 comments on commit d5348ce

Please sign in to comment.