Skip to content
Open
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
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ linters:
alias: skrkymarepo
- pkg: github.com/kyma-project/lifecycle-manager/internal/repository/skr/kyma/status
alias: skrkymastatusrepo
- pkg: github.com/kyma-project/lifecycle-manager/internal/repository/manifest
alias: manifestrepo
- pkg: github.com/kyma-project/lifecycle-manager/internal/repository/secret
alias: secretrepo
- pkg: github.com/kyma-project/lifecycle-manager/internal/result/event
Expand Down
9 changes: 8 additions & 1 deletion api/shared/cr_kind.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package shared

import "strings"
import (
"fmt"
"strings"
)

const (
KymaKind Kind = "Kyma"
Expand All @@ -15,3 +18,7 @@ type Kind string
func (k Kind) Plural() string {
return strings.ToLower(string(k)) + "s"
}

func (k Kind) List() string {
return fmt.Sprintf("%sList", k)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (

"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/internal/event"
"github.com/kyma-project/lifecycle-manager/internal/repository/manifest"
manifestrepo "github.com/kyma-project/lifecycle-manager/internal/repository/manifest"
"github.com/kyma-project/lifecycle-manager/internal/repository/modulereleasemeta"
"github.com/kyma-project/lifecycle-manager/internal/service/mandatorymodule/deletion"
"github.com/kyma-project/lifecycle-manager/internal/service/mandatorymodule/deletion/usecases"
)

func ComposeDeletionService(clnt client.Client, eventHandler event.Event) *deletion.Service {
mrmRepo := modulereleasemeta.NewRepository(clnt, shared.DefaultControlPlaneNamespace)
manifestRepo := manifest.NewRepository(clnt, shared.DefaultControlPlaneNamespace)
manifestRepo := manifestrepo.NewRepository(clnt, shared.DefaultControlPlaneNamespace)
ensureFinalizerUseCase := usecases.NewEnsureFinalizer(mrmRepo, eventHandler)
skipNonDeletingUseCase := usecases.NewSkipNonDeleting()
deleteManifestsUseCase := usecases.NewDeleteManifests(manifestRepo, eventHandler)
Expand Down
10 changes: 9 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"github.com/kyma-project/lifecycle-manager/internal/repository/istiogateway"
kymarepo "github.com/kyma-project/lifecycle-manager/internal/repository/kyma"
kymastatusrepo "github.com/kyma-project/lifecycle-manager/internal/repository/kyma/status"
manifestrepo "github.com/kyma-project/lifecycle-manager/internal/repository/manifest"
secretrepo "github.com/kyma-project/lifecycle-manager/internal/repository/secret"
skrkymarepo "github.com/kyma-project/lifecycle-manager/internal/repository/skr/kyma"
skrkymastatusrepo "github.com/kyma-project/lifecycle-manager/internal/repository/skr/kyma/status"
Expand Down Expand Up @@ -420,7 +421,7 @@
setupLog.V(log.DebugLevel).Info("scheduled job for cleaning up metrics")
}

func setupKymaReconciler(mgr ctrl.Manager, descriptorProvider *provider.CachedDescriptorProvider,

Check failure on line 424 in cmd/main.go

View workflow job for this annotation

GitHub Actions / lint

Function 'setupKymaReconciler' is too long (83 > 80) (funlen)
skrContextFactory remote.SkrContextProvider, event event.Event, flagVar *flags.FlagVar, options ctrlruntime.Options,
skrWebhookManager *watcher.SkrWebhookManifestManager, kymaMetrics *metrics.KymaMetrics,
setupLog logr.Logger, maintenanceWindow maintenancewindows.MaintenanceWindow, ociRegistryHost string,
Expand Down Expand Up @@ -460,9 +461,16 @@
accessSecretRepository)
deleteSkrKyma := usecases.NewDeleteSkrKyma(skrkymarepo.NewRepository(skrClientCache),
accessSecretRepository)
deleteManifests := usecases.NewDeleteManifests(
manifestrepo.NewRepository(
kcpClient,
shared.DefaultControlPlaneNamespace),
)
kymaDeletionService := kymadeletionsvc.NewService(setKcpKymaStateDeleting,
setSkrKymaStateDeleting,
deleteSkrKyma)
deleteSkrKyma,
deleteManifests,
)

if err := (&kyma.Reconciler{
Client: kcpClient,
Expand Down
45 changes: 39 additions & 6 deletions internal/repository/manifest/manifest_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,56 @@ func NewRepository(clnt client.Client, namespace string) *Repository {
}

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 {
if err := r.clnt.DeleteAllOf(ctx, // does not return 404 error if no objects found
&v1beta2.Manifest{},
client.InNamespace(r.namespace),
client.MatchingLabels{shared.ModuleName: moduleName},
); 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"))
manifestList.SetGroupVersionKind(v1beta2.GroupVersion.WithKind(shared.ManifestKind.List()))

if err := r.clnt.List(ctx, &manifestList, client.InNamespace(r.namespace),
client.MatchingLabels{shared.ModuleName: moduleName}); err != nil {
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
}

func (r *Repository) ExistForKyma(ctx context.Context, kymaName string) (bool, error) {
var manifestList apimetav1.PartialObjectMetadataList
manifestList.SetGroupVersionKind(v1beta2.GroupVersion.WithKind(shared.ManifestKind.List()))

if err := r.clnt.List(ctx,
&manifestList,
client.InNamespace(r.namespace),
client.MatchingLabels{shared.KymaName: kymaName},
client.Limit(1)); err != nil {
return false, fmt.Errorf("failed to list Manifests for kyma %s: %w", kymaName, err)
}

return len(manifestList.Items) > 0, nil
}

func (r *Repository) DeleteAllForKyma(ctx context.Context, kymaName string) error {
if err := r.clnt.DeleteAllOf(ctx, // does not return 404 error if no objects found
&v1beta2.Manifest{},
client.InNamespace(r.namespace),
client.MatchingLabels{shared.KymaName: kymaName},
); err != nil {
return fmt.Errorf("failed to delete all manifests for kyma %s: %w", kymaName, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package manifest_test

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/require"

"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
manifestrepo "github.com/kyma-project/lifecycle-manager/internal/repository/manifest"
"github.com/kyma-project/lifecycle-manager/pkg/testutils/random"
)

func TestRepository_DeleteAllForKyma(t *testing.T) {
ctx := context.Background()
testNamespace := random.Name()
testKymaName := random.Name()

t.Run("successfully deletes all manifests for Kyma", func(t *testing.T) {
stub := &clientStub{}
repo := manifestrepo.NewRepository(stub, testNamespace)

err := repo.DeleteAllForKyma(ctx, testKymaName)

require.NoError(t, err)
require.True(t, stub.deleteAllOfCalled)
require.Equal(t, testNamespace, stub.capturedNamespace)
require.Equal(t, testKymaName, stub.capturedLabels[shared.KymaName])
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 := manifestrepo.NewRepository(stub, testNamespace)

err := repo.DeleteAllForKyma(ctx, testKymaName)

require.Error(t, err)
require.Contains(t, err.Error(), "failed to delete all manifests for kyma")
require.Contains(t, err.Error(), testKymaName)
require.True(t, stub.deleteAllOfCalled)
require.Equal(t, testNamespace, stub.capturedNamespace)
require.Equal(t, testKymaName, stub.capturedLabels[shared.KymaName])
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package manifest_test

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/require"

"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
manifestrepo "github.com/kyma-project/lifecycle-manager/internal/repository/manifest"
"github.com/kyma-project/lifecycle-manager/pkg/testutils/random"
)

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 := manifestrepo.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 := manifestrepo.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])
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package manifest_test

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/require"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyma-project/lifecycle-manager/api/shared"
manifestrepo "github.com/kyma-project/lifecycle-manager/internal/repository/manifest"
"github.com/kyma-project/lifecycle-manager/pkg/testutils/random"
)

func TestRepository_ExistForKyma(t *testing.T) {
ctx := context.Background()
testNamespace := random.Name()
testKymaName := random.Name()

t.Run("returns true when manifests exist for Kyma", func(t *testing.T) {
expectedMetadata := []apimetav1.PartialObjectMetadata{
{
ObjectMeta: apimetav1.ObjectMeta{
Name: "manifest1",
Namespace: testNamespace,
Labels: map[string]string{shared.KymaName: testKymaName},
},
},
}

stub := &clientStub{partialObjectMetadata: expectedMetadata}
repo := manifestrepo.NewRepository(stub, testNamespace)

exists, err := repo.ExistForKyma(ctx, testKymaName)

require.NoError(t, err)
require.True(t, exists)
require.True(t, stub.listCalled)
require.Equal(t, testNamespace, stub.capturedNamespace)
require.Equal(t, testKymaName, stub.capturedLabels[shared.KymaName])
})

t.Run("returns false when no manifests exist for Kyma", func(t *testing.T) {
stub := &clientStub{partialObjectMetadata: []apimetav1.PartialObjectMetadata{}}
repo := manifestrepo.NewRepository(stub, testNamespace)

exists, err := repo.ExistForKyma(ctx, testKymaName)

require.NoError(t, err)
require.False(t, exists)
require.True(t, stub.listCalled)
require.Equal(t, testNamespace, stub.capturedNamespace)
require.Equal(t, testKymaName, stub.capturedLabels[shared.KymaName])
})

t.Run("returns error when list fails", func(t *testing.T) {
expectedErr := errors.New("list error")
stub := &clientStub{listErr: expectedErr}
repo := manifestrepo.NewRepository(stub, testNamespace)

exists, err := repo.ExistForKyma(ctx, testKymaName)

require.Error(t, err)
require.False(t, exists)
require.Contains(t, err.Error(), "failed to list Manifests for kyma")
require.Contains(t, err.Error(), testKymaName)
require.True(t, stub.listCalled)
require.Equal(t, testNamespace, stub.capturedNamespace)
require.Equal(t, testKymaName, stub.capturedLabels[shared.KymaName])
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package manifest_test

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/require"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyma-project/lifecycle-manager/api/shared"
manifestrepo "github.com/kyma-project/lifecycle-manager/internal/repository/manifest"
"github.com/kyma-project/lifecycle-manager/pkg/testutils/random"
)

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 := manifestrepo.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 := manifestrepo.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 := manifestrepo.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])
})
}
Loading
Loading