-
Notifications
You must be signed in to change notification settings - Fork 32
feat: Introduce Repositories for Manifest, MRMs, and MTs #2810
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
Merged
LeelaChacha
merged 12 commits into
kyma-project:main
from
LeelaChacha:feature/2631-prepare-repositories-for-mandatory-mrms
Oct 14, 2025
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d58a9fd
feat: Introduce New Repositories
LeelaChacha 66cdd9c
refactor: Use controllerutil for Finalizer Changes
LeelaChacha c29444a
test: Unit test for repositories
LeelaChacha d74b775
test: Unit test for repositories
LeelaChacha dff2b8a
chore: Update Test Coverage
LeelaChacha 36aea14
chore: Update Test Coverage
LeelaChacha d3f5769
Merge branch 'main' into feature/2631-prepare-repositories-for-mandat…
LeelaChacha e93a8a1
refactor: Fetch PartialMetadataList instead of ManifestList
LeelaChacha 6f29aaf
chore: gofumpt
LeelaChacha 922df9c
chore: remove redundant test
LeelaChacha 470525d
Merge branch 'main' into feature/2631-prepare-repositories-for-mandat…
LeelaChacha 660ccf6
chore: gci lint
LeelaChacha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package manifest | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "github.com/kyma-project/lifecycle-manager/api/shared" | ||
| "github.com/kyma-project/lifecycle-manager/api/v1beta2" | ||
| ) | ||
|
|
||
| type Repository struct { | ||
| clnt client.Client | ||
| namespace string | ||
| } | ||
|
|
||
| func NewRepository(clnt client.Client, namespace string) *Repository { | ||
| return &Repository{ | ||
| clnt: clnt, | ||
| namespace: namespace, | ||
| } | ||
| } | ||
|
|
||
| func (r *Repository) DeleteAllForModule(ctx context.Context, moduleName string) error { | ||
| err := r.clnt.DeleteAllOf(ctx, &v1beta2.Manifest{}, client.InNamespace(r.namespace), | ||
| client.MatchingLabels{shared.ModuleName: moduleName}) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to delete all manifests for module %s: %w", moduleName, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *Repository) ListAllForModule(ctx context.Context, moduleName string) ( | ||
| []apimetav1.PartialObjectMetadata, error, | ||
| ) { | ||
| var manifestList apimetav1.PartialObjectMetadataList | ||
| manifestList.SetGroupVersionKind(v1beta2.GroupVersion.WithKind("ManifestList")) | ||
|
|
||
| if err := r.clnt.List(ctx, &manifestList, client.InNamespace(r.namespace), | ||
| client.MatchingLabels{shared.ModuleName: moduleName}); err != nil { | ||
| return nil, fmt.Errorf("failed to list Manifests for module %s: %w", moduleName, err) | ||
| } | ||
| return manifestList.Items, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package manifest_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/kyma-project/lifecycle-manager/pkg/testutils/random" | ||
| "github.com/stretchr/testify/require" | ||
| apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "github.com/kyma-project/lifecycle-manager/api/shared" | ||
| "github.com/kyma-project/lifecycle-manager/api/v1beta2" | ||
| "github.com/kyma-project/lifecycle-manager/internal/repository/manifest" | ||
| ) | ||
|
|
||
| type clientStub struct { | ||
| client.Client | ||
|
|
||
| deleteAllOfCalled bool | ||
| listCalled bool | ||
| deleteAllOfErr error | ||
| listErr error | ||
|
|
||
| capturedNamespace string | ||
| capturedLabels map[string]string | ||
| capturedObjectType client.Object | ||
|
|
||
| partialObjectMetadata []apimetav1.PartialObjectMetadata | ||
| } | ||
|
|
||
| func (c *clientStub) DeleteAllOf(_ context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error { | ||
| c.deleteAllOfCalled = true | ||
| c.capturedObjectType = obj | ||
|
|
||
| for _, opt := range opts { | ||
| if nsOpt, ok := opt.(client.InNamespace); ok { | ||
| c.capturedNamespace = string(nsOpt) | ||
| } | ||
| if labelOpt, ok := opt.(client.MatchingLabels); ok { | ||
| c.capturedLabels = labelOpt | ||
| } | ||
| } | ||
|
|
||
| return c.deleteAllOfErr | ||
| } | ||
|
|
||
| func (c *clientStub) List(_ context.Context, list client.ObjectList, opts ...client.ListOption) error { | ||
| c.listCalled = true | ||
|
|
||
| for _, opt := range opts { | ||
| if nsOpt, ok := opt.(client.InNamespace); ok { | ||
| c.capturedNamespace = string(nsOpt) | ||
| } | ||
| if labelOpt, ok := opt.(client.MatchingLabels); ok { | ||
| c.capturedLabels = labelOpt | ||
| } | ||
| } | ||
|
|
||
| if c.listErr != nil { | ||
| return c.listErr | ||
| } | ||
|
|
||
| if partialList, ok := list.(*apimetav1.PartialObjectMetadataList); ok { | ||
| partialList.Items = c.partialObjectMetadata | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func TestRepository_DeleteAllForModule(t *testing.T) { | ||
| ctx := context.Background() | ||
| testNamespace := random.Name() | ||
| testModuleName := random.Name() | ||
|
|
||
| t.Run("successfully deletes all manifests for module", func(t *testing.T) { | ||
| stub := &clientStub{} | ||
| repo := manifest.NewRepository(stub, testNamespace) | ||
|
|
||
| err := repo.DeleteAllForModule(ctx, testModuleName) | ||
|
|
||
| require.NoError(t, err) | ||
| require.True(t, stub.deleteAllOfCalled) | ||
| require.Equal(t, testNamespace, stub.capturedNamespace) | ||
| require.Equal(t, testModuleName, stub.capturedLabels[shared.ModuleName]) | ||
| require.IsType(t, &v1beta2.Manifest{}, stub.capturedObjectType) | ||
| }) | ||
|
|
||
| t.Run("returns error when deleteAllOf fails", func(t *testing.T) { | ||
| expectedErr := errors.New("delete error") | ||
| stub := &clientStub{deleteAllOfErr: expectedErr} | ||
| repo := manifest.NewRepository(stub, testNamespace) | ||
|
|
||
| err := repo.DeleteAllForModule(ctx, testModuleName) | ||
|
|
||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "failed to delete all manifests for module") | ||
| require.Contains(t, err.Error(), testModuleName) | ||
| require.True(t, stub.deleteAllOfCalled) | ||
| require.Equal(t, testNamespace, stub.capturedNamespace) | ||
| require.Equal(t, testModuleName, stub.capturedLabels[shared.ModuleName]) | ||
| }) | ||
| } | ||
|
|
||
| func TestRepository_ListAllForModule(t *testing.T) { | ||
| ctx := context.Background() | ||
| testNamespace := random.Name() | ||
| testModuleName := random.Name() | ||
|
|
||
| t.Run("successfully lists all manifests for module", func(t *testing.T) { | ||
| expectedMetadata := []apimetav1.PartialObjectMetadata{ | ||
| { | ||
| ObjectMeta: apimetav1.ObjectMeta{ | ||
| Name: "manifest1", | ||
| Namespace: testNamespace, | ||
| Labels: map[string]string{shared.ModuleName: testModuleName}, | ||
| }, | ||
| }, | ||
| { | ||
| ObjectMeta: apimetav1.ObjectMeta{ | ||
| Name: "manifest2", | ||
| Namespace: testNamespace, | ||
| Labels: map[string]string{shared.ModuleName: testModuleName}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| stub := &clientStub{partialObjectMetadata: expectedMetadata} | ||
| repo := manifest.NewRepository(stub, testNamespace) | ||
|
|
||
| result, err := repo.ListAllForModule(ctx, testModuleName) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Len(t, result, 2) | ||
| require.Equal(t, expectedMetadata, result) | ||
| require.True(t, stub.listCalled) | ||
| require.Equal(t, testNamespace, stub.capturedNamespace) | ||
| require.Equal(t, testModuleName, stub.capturedLabels[shared.ModuleName]) | ||
| }) | ||
|
|
||
| t.Run("returns empty list when no manifests found", func(t *testing.T) { | ||
| stub := &clientStub{partialObjectMetadata: []apimetav1.PartialObjectMetadata{}} | ||
| repo := manifest.NewRepository(stub, testNamespace) | ||
|
|
||
| result, err := repo.ListAllForModule(ctx, testModuleName) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Empty(t, result) | ||
| require.True(t, stub.listCalled) | ||
| require.Equal(t, testNamespace, stub.capturedNamespace) | ||
| require.Equal(t, testModuleName, stub.capturedLabels[shared.ModuleName]) | ||
| }) | ||
|
|
||
| t.Run("returns error when list fails", func(t *testing.T) { | ||
| expectedErr := errors.New("list error") | ||
| stub := &clientStub{listErr: expectedErr} | ||
| repo := manifest.NewRepository(stub, testNamespace) | ||
|
|
||
| result, err := repo.ListAllForModule(ctx, testModuleName) | ||
|
|
||
| require.Error(t, err) | ||
| require.Nil(t, result) | ||
| require.Contains(t, err.Error(), "failed to list Manifests for module") | ||
| require.Contains(t, err.Error(), testModuleName) | ||
| require.True(t, stub.listCalled) | ||
| require.Equal(t, testNamespace, stub.capturedNamespace) | ||
| require.Equal(t, testModuleName, stub.capturedLabels[shared.ModuleName]) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package modulereleasemeta | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
|
||
| "github.com/kyma-project/lifecycle-manager/api/v1beta2" | ||
| ) | ||
|
|
||
| type Repository struct { | ||
| clnt client.Client | ||
| namespace string | ||
| } | ||
|
|
||
| func NewRepository(clnt client.Client, namespace string) *Repository { | ||
| return &Repository{ | ||
| clnt: clnt, | ||
| namespace: namespace, | ||
| } | ||
| } | ||
|
|
||
| func (r *Repository) EnsureFinalizer(ctx context.Context, mrmName string, finalizer string) error { | ||
| mrm, err := r.Get(ctx, mrmName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if updated := controllerutil.AddFinalizer(mrm, finalizer); updated { | ||
| if err := r.clnt.Update(ctx, mrm); err != nil { | ||
| return fmt.Errorf("failed to add finalizer to ModuleReleaseMeta %s: %w", mrmName, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *Repository) RemoveFinalizer(ctx context.Context, mrmName string, finalizer string) error { | ||
| mrm, err := r.Get(ctx, mrmName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if updated := controllerutil.RemoveFinalizer(mrm, finalizer); updated { | ||
| if err := r.clnt.Update(ctx, mrm); err != nil { | ||
| return fmt.Errorf("failed to remove finalizer from ModuleReleaseMeta %s: %w", mrmName, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *Repository) Get(ctx context.Context, mrmName string) (*v1beta2.ModuleReleaseMeta, error) { | ||
| mrm := &v1beta2.ModuleReleaseMeta{} | ||
| err := r.clnt.Get(ctx, client.ObjectKey{Name: mrmName, Namespace: r.namespace}, mrm) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get ModuleReleaseMeta %s in namespace %s: %w", mrmName, r.namespace, err) | ||
| } | ||
| return mrm, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.