-
Notifications
You must be signed in to change notification settings - Fork 339
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(kumacp) Delete related mesh resources #312
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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" | ||
. "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() | ||
caManager = builtin.NewBuiltinCaManager(secrets_manager.NewSecretManager(secrets_store.NewSecretStore(resStore), cipher.None())) | ||
resManager = NewMeshManager(resStore, caManager) | ||
}) | ||
|
||
It("Create() 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)) | ||
}) | ||
|
||
It("Delete() 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\"")) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ type ResourceStore interface { | |
Create(context.Context, model.Resource, ...CreateOptionsFunc) error | ||
Update(context.Context, model.Resource, ...UpdateOptionsFunc) error | ||
Delete(context.Context, model.Resource, ...DeleteOptionsFunc) error | ||
DeleteMany(context.Context, ...DeleteManyOptionsFunc) error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, For example, consider an audit feature where we would be required to maintain a log of all operations that modify state of resources. Another example would be complex delete logic per resource type (like in the case of |
||
Get(context.Context, model.Resource, ...GetOptionsFunc) error | ||
List(context.Context, model.ResourceList, ...ListOptionsFunc) error | ||
} | ||
|
@@ -88,6 +89,9 @@ func (s *strictResourceStore) Delete(ctx context.Context, r model.Resource, fs . | |
} | ||
return s.delegate.Delete(ctx, r, fs...) | ||
} | ||
func (s *strictResourceStore) DeleteMany(ctx context.Context, fs ...DeleteManyOptionsFunc) error { | ||
return s.delegate.DeleteMany(ctx, fs...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I would suggest to forbid it inside |
||
} | ||
func (s *strictResourceStore) Get(ctx context.Context, r model.Resource, fs ...GetOptionsFunc) error { | ||
if r == nil { | ||
return fmt.Errorf("ResourceStore.Get() requires a non-nil resource") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,8 @@ import ( | |
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
|
||
// +kubebuilder:scaffold:imports | ||
sample_v1alpha1 "github.com/Kong/kuma/pkg/plugins/resources/k8s/native/test/api/sample/v1alpha1" | ||
mesh_proto "github.com/Kong/kuma/pkg/plugins/resources/k8s/native/api/v1alpha1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usage of |
||
sample_proto "github.com/Kong/kuma/pkg/plugins/resources/k8s/native/test/api/sample/v1alpha1" | ||
) | ||
|
||
var k8sClient client.Client | ||
|
@@ -50,14 +51,20 @@ var _ = BeforeSuite(func(done Done) { | |
|
||
By("bootstrapping test environment") | ||
testEnv = &envtest.Environment{ | ||
CRDDirectoryPaths: []string{filepath.Join("native", "test", "config", "crd", "bases")}, | ||
CRDDirectoryPaths: []string{ | ||
filepath.Join("native", "test", "config", "crd", "bases"), | ||
filepath.Join("native", "config", "crd", "bases"), | ||
}, | ||
} | ||
|
||
cfg, err := testEnv.Start() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(cfg).ToNot(BeNil()) | ||
|
||
err = sample_v1alpha1.AddToScheme(k8sClientScheme) | ||
err = sample_proto.AddToScheme(k8sClientScheme) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
err = mesh_proto.AddToScheme(k8sClientScheme) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// +kubebuilder:scaffold:scheme | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package k8s | |
import ( | ||
"context" | ||
"fmt" | ||
|
||
core_model "github.com/Kong/kuma/pkg/core/resources/model" | ||
"github.com/Kong/kuma/pkg/core/resources/store" | ||
k8s_model "github.com/Kong/kuma/pkg/plugins/resources/k8s/native/pkg/model" | ||
|
@@ -18,14 +17,16 @@ import ( | |
var _ store.ResourceStore = &KubernetesStore{} | ||
|
||
type KubernetesStore struct { | ||
Client kube_client.Client | ||
Converter Converter | ||
Client kube_client.Client | ||
Converter Converter | ||
TypeRegistry k8s_registry.TypeRegistry | ||
} | ||
|
||
func NewStore(client kube_client.Client) (store.ResourceStore, error) { | ||
return &KubernetesStore{ | ||
Client: client, | ||
Converter: DefaultConverter(), | ||
Client: client, | ||
Converter: DefaultConverter(), | ||
TypeRegistry: k8s_registry.Global(), | ||
}, nil | ||
} | ||
|
||
|
@@ -93,6 +94,23 @@ func (s *KubernetesStore) Delete(ctx context.Context, r core_model.Resource, fs | |
} | ||
return nil | ||
} | ||
func (s *KubernetesStore) DeleteMany(ctx context.Context, fs ...store.DeleteManyOptionsFunc) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's a good idea to remove resources without taking into account their type |
||
opts := store.NewDeleteManyOptions(fs...) | ||
|
||
for _, list := range s.TypeRegistry.RegisteredLists() { | ||
if err := s.Client.List(ctx, list); err != nil { | ||
return errors.Wrapf(err, "failed to list resources of type %s", list.GetObjectKind().GroupVersionKind().String()) | ||
} | ||
for _, obj := range list.GetItems() { | ||
if opts.Mesh == "" || opts.Mesh == obj.GetMesh() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Support for |
||
if err := s.Client.Delete(ctx, obj); err != nil { | ||
return errors.Wrap(err, "failed to delete k8s resource") | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
func (s *KubernetesStore) Get(ctx context.Context, r core_model.Resource, fs ...store.GetOptionsFunc) error { | ||
opts := store.NewGetOptions(fs...) | ||
obj, err := s.Converter.ToKubernetesObject(r) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect the logic that iterates over all supported resource types in here (rather than in
k8s
store)