Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kuma-cp) delete related mesh resources with DeleteAll on ResourceManager #332

Merged
merged 3 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/core/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Kong/kuma/pkg/core/resources/apis/mesh"
core_manager "github.com/Kong/kuma/pkg/core/resources/manager"
core_model "github.com/Kong/kuma/pkg/core/resources/model"
"github.com/Kong/kuma/pkg/core/resources/registry"
core_runtime "github.com/Kong/kuma/pkg/core/runtime"
runtime_reports "github.com/Kong/kuma/pkg/core/runtime/reports"
secret_cipher "github.com/Kong/kuma/pkg/core/secrets/cipher"
Expand Down Expand Up @@ -219,11 +220,10 @@ func initializeBuiltinCaManager(builder *core_runtime.Builder) {

func initializeResourceManager(builder *core_runtime.Builder) {
defaultManager := core_manager.NewResourceManager(builder.ResourceStore())
meshManager := mesh_managers.NewMeshManager(builder.ResourceStore(), builder.BuiltinCaManager())
customManagers := map[core_model.ResourceType]core_manager.ResourceManager{
mesh.MeshType: meshManager,
}
customManagers := map[core_model.ResourceType]core_manager.ResourceManager{}
customizableManager := core_manager.NewCustomizableResourceManager(defaultManager, customManagers)
meshManager := mesh_managers.NewMeshManager(builder.ResourceStore(), builder.BuiltinCaManager(), customizableManager, builder.SecretManager(), registry.Global())
customManagers[mesh.MeshType] = meshManager
builder.WithResourceManager(customizableManager)
}

Expand Down
57 changes: 54 additions & 3 deletions pkg/core/managers/apis/mesh/mesh_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,33 @@ import (
core_mesh "github.com/Kong/kuma/pkg/core/resources/apis/mesh"
core_manager "github.com/Kong/kuma/pkg/core/resources/manager"
core_model "github.com/Kong/kuma/pkg/core/resources/model"
core_registry "github.com/Kong/kuma/pkg/core/resources/registry"
core_store "github.com/Kong/kuma/pkg/core/resources/store"
secrets_manager "github.com/Kong/kuma/pkg/core/secrets/manager"
)

func NewMeshManager(store core_store.ResourceStore, builtinCaManager builtin_ca.BuiltinCaManager) core_manager.ResourceManager {
func NewMeshManager(
store core_store.ResourceStore,
builtinCaManager builtin_ca.BuiltinCaManager,
otherManagers core_manager.ResourceManager,
secretManager secrets_manager.SecretManager,
registry core_registry.TypeRegistry,
) core_manager.ResourceManager {
return &meshManager{
store: store,
builtinCaManager: builtinCaManager,
otherManagers: otherManagers,
secretManager: secretManager,
registry: registry,
}
}

type meshManager struct {
store core_store.ResourceStore
builtinCaManager builtin_ca.BuiltinCaManager
otherManagers core_manager.ResourceManager
secretManager secrets_manager.SecretManager
registry core_registry.TypeRegistry
}

func (m *meshManager) Get(ctx context.Context, resource core_model.Resource, fs ...core_store.GetOptionsFunc) error {
Expand Down Expand Up @@ -81,14 +95,51 @@ 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 {
return err
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")
jakubdyszkiewicz marked this conversation as resolved.
Show resolved Hide resolved
}
// delete all other secrets
if err := m.secretManager.DeleteAll(ctx, core_store.DeleteAllByMesh(opts.Mesh)); err != nil {
return errors.Wrap(err, "could not delete associated secrets")
}
// delete other resources associated by mesh
for _, typ := range m.registry.ListTypes() {
list, err := m.registry.NewList(typ)
if err != nil {
return err
}
if err := m.otherManagers.DeleteAll(ctx, list, core_store.DeleteAllByMesh(opts.Name)); err != nil {
return errors.Wrap(err, "could not delete associated resources")
}
}
return notFoundErr
}

func (m *meshManager) DeleteAll(ctx context.Context, list core_model.ResourceList, fs ...core_store.DeleteAllOptionsFunc) error {
meshes, err := m.meshes(list)
if err != nil {
return err
}
opts := core_store.NewDeleteAllOptions(fs...)
if err := m.List(ctx, list, core_store.ListByMesh(opts.Mesh)); err != nil {
return err
}
for _, item := range meshes.Items {
if err := m.Delete(ctx, item, core_store.DeleteBy(core_model.MetaToResourceKey(item.Meta))); err != nil && !core_store.IsResourceNotFound(err) {
return err
jakubdyszkiewicz marked this conversation as resolved.
Show resolved Hide resolved
}
}
return nil
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/core/managers/apis/mesh/mesh_manager_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mesh

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestMeshManager(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Mesh Manager Suite")
}
118 changes: 118 additions & 0 deletions pkg/core/managers/apis/mesh/mesh_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package mesh

import (
"context"
"github.com/Kong/kuma/pkg/core/ca/builtin"
core_mesh "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/core/secrets/cipher"
secrets_manager "github.com/Kong/kuma/pkg/core/secrets/manager"
secrets_store "github.com/Kong/kuma/pkg/core/secrets/store"
"github.com/Kong/kuma/pkg/plugins/resources/memory"
test_resources "github.com/Kong/kuma/pkg/test/resources"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Mesh Manager", func() {

const namespace = "default"

var resManager manager.ResourceManager
var resStore store.ResourceStore
var caManager builtin.BuiltinCaManager

BeforeEach(func() {
resStore = memory.NewStore()
secretManager := secrets_manager.NewSecretManager(secrets_store.NewSecretStore(resStore), cipher.None())
caManager = builtin.NewBuiltinCaManager(secretManager)
manager := manager.NewResourceManager(resStore)

resManager = NewMeshManager(resStore, caManager, manager, secretManager, test_resources.Global())
})

Describe("Create()", func() {
It("should also create a built-in CA", func() {
// given
meshName := "mesh-1"
resKey := model.ResourceKey{
Mesh: meshName,
Namespace: namespace,
Name: meshName,
}

// when
mesh := core_mesh.MeshResource{}
err := resManager.Create(context.Background(), &mesh, store.CreateBy(resKey))

// then
Expect(err).ToNot(HaveOccurred())

// and built-in CA is created
certs, err := caManager.GetRootCerts(context.Background(), meshName)
Expect(err).ToNot(HaveOccurred())
Expect(certs).To(HaveLen(1))
})
})

Describe("Delete()", func() {
It("should delete all associated resources", func() {
// given mesh
meshName := "mesh-1"

mesh := core_mesh.MeshResource{}
resKey := model.ResourceKey{
Mesh: meshName,
Namespace: namespace,
Name: meshName,
}
err := resManager.Create(context.Background(), &mesh, store.CreateBy(resKey))
Expect(err).ToNot(HaveOccurred())

// and resource associated with it
dp := core_mesh.DataplaneResource{}
err = resStore.Create(context.Background(), &dp, store.CreateByKey(namespace, "dp-1", meshName))
Expect(err).ToNot(HaveOccurred())

// when mesh is deleted
err = resManager.Delete(context.Background(), &mesh, store.DeleteBy(resKey))

// then
Expect(err).ToNot(HaveOccurred())

// and resource is deleted
err = resStore.Get(context.Background(), &core_mesh.DataplaneResource{}, store.GetByKey(namespace, "dp-1", meshName))
Expect(store.IsResourceNotFound(err)).To(BeTrue())

// and built-in mesh CA is deleted
_, err = caManager.GetRootCerts(context.Background(), meshName)
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal("failed to load CA key pair for Mesh \"mesh-1\": Resource not found: type=\"Secret\" namespace=\"default\" name=\"builtinca.mesh-1\" mesh=\"mesh-1\""))
})

It("should delete all associated resources even if mesh is already removed", func() {
// given resource that was not deleted with mesh
dp := core_mesh.DataplaneResource{}
dpKey := model.ResourceKey{
Mesh: "already-deleted",
Namespace: namespace,
Name: "dp-1",
}
err := resStore.Create(context.Background(), &dp, store.CreateBy(dpKey))
Expect(err).ToNot(HaveOccurred())

// when
mesh := core_mesh.MeshResource{}
err = resManager.Delete(context.Background(), &mesh, store.DeleteByKey(namespace, "already-deleted", "already-deleted"))

// then
Expect(err).ToNot(HaveOccurred())

// and resource is deleted
err = resStore.Get(context.Background(), &dp, store.GetBy(dpKey))
Expect(store.IsResourceNotFound(err)).To(BeTrue())
})
})
})
7 changes: 0 additions & 7 deletions pkg/core/resources/apis/mesh/dataplane_overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package mesh

import (
"errors"
"github.com/Kong/kuma/pkg/core/resources/registry"

mesh_proto "github.com/Kong/kuma/api/mesh/v1alpha1"
"github.com/Kong/kuma/pkg/core/resources/model"
)
Expand Down Expand Up @@ -74,11 +72,6 @@ func (l *DataplaneOverviewResourceList) AddItem(r model.Resource) error {
}
}

func init() {
registry.RegisterType(&DataplaneOverviewResource{})
registry.RegistryListType(&DataplaneOverviewResourceList{})
}

func NewDataplaneOverviews(dataplanes DataplaneResourceList, insights DataplaneInsightResourceList) DataplaneOverviewResourceList {
insightsByKey := map[model.ResourceKey]*DataplaneInsightResource{}
for _, insight := range insights.Items {
Expand Down
7 changes: 0 additions & 7 deletions pkg/core/resources/apis/system/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package system
import (
"errors"

"github.com/Kong/kuma/pkg/core/resources/registry"

"github.com/Kong/kuma/pkg/core/resources/model"
"github.com/gogo/protobuf/types"
)
Expand Down Expand Up @@ -69,8 +67,3 @@ func (l *SecretResourceList) AddItem(r model.Resource) error {
return model.ErrorInvalidItemType((*SecretResource)(nil), r)
}
}

func init() {
registry.RegisterType(&SecretResource{})
registry.RegistryListType(&SecretResourceList{})
}
4 changes: 4 additions & 0 deletions pkg/core/resources/manager/customizable_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (m *customizableResourceManager) Delete(ctx context.Context, resource model
return m.resourceManager(resource.GetType()).Delete(ctx, resource, fs...)
}

func (m *customizableResourceManager) DeleteAll(ctx context.Context, list model.ResourceList, fs ...store.DeleteAllOptionsFunc) error {
return m.resourceManager(list.GetItemType()).DeleteAll(ctx, list, fs...)
}

func (m *customizableResourceManager) Update(ctx context.Context, resource model.Resource, fs ...store.UpdateOptionsFunc) error {
return m.resourceManager(resource.GetType()).Update(ctx, resource, fs...)
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/core/resources/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ResourceManager interface {
Create(context.Context, model.Resource, ...store.CreateOptionsFunc) error
Update(context.Context, model.Resource, ...store.UpdateOptionsFunc) error
Delete(context.Context, model.Resource, ...store.DeleteOptionsFunc) error
DeleteAll(context.Context, model.ResourceList, ...store.DeleteAllOptionsFunc) error
Get(context.Context, model.Resource, ...store.GetOptionsFunc) error
List(context.Context, model.ResourceList, ...store.ListOptionsFunc) error
}
Expand Down Expand Up @@ -62,6 +63,20 @@ func (r *resourcesManager) Delete(ctx context.Context, resource model.Resource,
return r.Store.Delete(ctx, resource, fs...)
}

func (r *resourcesManager) DeleteAll(ctx context.Context, list model.ResourceList, fs ...store.DeleteAllOptionsFunc) error {
opts := store.NewDeleteAllOptions(fs...)

if err := r.List(ctx, list, store.ListByMesh(opts.Mesh)); err != nil {
return err
}
for _, obj := range list.GetItems() {
if err := r.Delete(ctx, obj, store.DeleteBy(model.MetaToResourceKey(obj.GetMeta()))); err != nil && !store.IsResourceNotFound(err) {
return err
jakubdyszkiewicz marked this conversation as resolved.
Show resolved Hide resolved
}
}
return nil
}

func (r *resourcesManager) Update(ctx context.Context, resource model.Resource, fs ...store.UpdateOptionsFunc) error {
return r.Store.Update(ctx, resource, fs...)
}
Expand Down
Loading