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: Prevent unallowed internal/beta module installation #2111

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix linting
c-pius committed Dec 13, 2024
commit c1169cad8c2f98c1f4daacfd39460d8252d20d12
11 changes: 6 additions & 5 deletions internal/remote/remote_catalog_test.go
Original file line number Diff line number Diff line change
@@ -285,13 +285,13 @@ func Test_IsAllowedModuleReleaseMeta(t *testing.T) {
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mrm := newModuleReleaseMetaBuilder().withBeta(tc.moduleBeta).withInternal(tc.moduleInternal).build()
kyma := newKymaBuilder().withBeta(tc.kymaBeta).withInternal(tc.kymaInternal).build()
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
mrm := newModuleReleaseMetaBuilder().withBeta(testCase.moduleBeta).withInternal(testCase.moduleInternal).build()
kyma := newKymaBuilder().withBeta(testCase.kymaBeta).withInternal(testCase.kymaInternal).build()

result := remote.IsAllowedModuleReleaseMeta(*mrm, kyma)
assert.Equal(t, tc.expected, result)
assert.Equal(t, testCase.expected, result)
})
}
}
@@ -469,6 +469,7 @@ func (b *moduleReleaseMetaBuilder) withBetaEnabled() *moduleReleaseMetaBuilder {
b.moduleReleaseMeta.Spec.Beta = true
return b
}

func (b *moduleReleaseMetaBuilder) withBeta(beta bool) *moduleReleaseMetaBuilder {
b.moduleReleaseMeta.Spec.Beta = beta
return b
3 changes: 2 additions & 1 deletion pkg/templatelookup/regular.go
Original file line number Diff line number Diff line change
@@ -163,7 +163,8 @@ func validateTemplateModeWithoutModuleReleaseMeta(template ModuleTemplateInfo, k
}

func validateTemplateModeWithModuleReleaseMeta(template ModuleTemplateInfo, kyma *v1beta2.Kyma,
moduleReleaseMeta *v1beta2.ModuleReleaseMeta) ModuleTemplateInfo {
moduleReleaseMeta *v1beta2.ModuleReleaseMeta,
) ModuleTemplateInfo {
if !remote.IsAllowedModuleReleaseMeta(*moduleReleaseMeta, kyma) {
template.Err = fmt.Errorf("%w: module is beta or internal", ErrTemplateNotAllowed)
}
22 changes: 11 additions & 11 deletions pkg/templatelookup/regular_test.go
Original file line number Diff line number Diff line change
@@ -233,13 +233,13 @@ func Test_ValidateTemplateMode_ForNewModuleTemplatesWithModuleReleaseMeta(t *tes
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
mrm := builder.NewModuleReleaseMetaBuilder().
WithName("test-module").
WithModuleName("test-module").
WithBeta(tc.moduleBeta).
WithInternal(tc.moduleInternal).
WithBeta(testCase.moduleBeta).
WithInternal(testCase.moduleInternal).
Build()
mti := templatelookup.ModuleTemplateInfo{
ModuleTemplate: builder.NewModuleTemplateBuilder().
@@ -248,13 +248,13 @@ func Test_ValidateTemplateMode_ForNewModuleTemplatesWithModuleReleaseMeta(t *tes
}
kyma := builder.NewKymaBuilder().
WithName("test-kyma").
WithBeta(tc.kymaBeta).
WithInternal(tc.kymaInternal).
WithBeta(testCase.kymaBeta).
WithInternal(testCase.kymaInternal).
Build()

tl := templatelookup.NewTemplateLookup(mrmFakeClient(*mrm), provider.NewCachedDescriptorProvider())
got := tl.ValidateTemplateMode(context.Background(), mti, kyma)
if tc.expectInstallation {
if testCase.expectInstallation {
require.NoError(t, got.Err)
} else {
require.ErrorIs(t, got.Err, templatelookup.ErrTemplateNotAllowed)
@@ -263,13 +263,13 @@ func Test_ValidateTemplateMode_ForNewModuleTemplatesWithModuleReleaseMeta(t *tes
}
}

func mrmFakeClient(mrms ...v1beta2.ModuleReleaseMeta) client.Reader {
func mrmFakeClient(mrms ...v1beta2.ModuleReleaseMeta) client.Client {
scheme := machineryruntime.NewScheme()
machineryutilruntime.Must(api.AddToScheme(scheme))

var objects []client.Object
for _, mrm := range mrms {
objects = append(objects, &mrm)
objects := make([]client.Object, len(mrms))
for i, mrm := range mrms {
objects[i] = &mrm
}

return fake.NewClientBuilder().WithScheme(scheme).WithObjects(objects...).Build()
Original file line number Diff line number Diff line change
@@ -14,14 +14,15 @@ import (
"github.com/kyma-project/lifecycle-manager/pkg/testutils/builder"
"github.com/kyma-project/lifecycle-manager/pkg/testutils/random"
"github.com/kyma-project/lifecycle-manager/pkg/watcher"
"github.com/mandelsoft/goutils/errors"

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

. "github.com/kyma-project/lifecycle-manager/pkg/testutils"
)

// version is the same as configured for the ModuleTemplate by using "WithOCM()"
// version is the same as configured for the ModuleTemplate by using "WithOCM()".
const moduleVersion = "1.1.1-e2e-test"

var _ = Describe("Module installation", func() {
@@ -47,7 +48,7 @@ var _ = Describe("Module installation", func() {
Eventually(expectInstallation, Timeout, Interval).WithArguments(kymaName, moduleName).Should(Succeed())
} else {
// we use Consistently here as the installation may require multiple reconciliation runs to be installed
// otherwise, we may get a false positive after the first reconcilation where no module is installed yet, but in a consecutive run it will be
// otherwise, we may get a false positive after the first reconciliation where no module is installed yet, but in a consecutive run it will be
Consistently(expectNoInstallation, Timeout, Interval).WithArguments(kymaName, moduleName).Should(Succeed())
}
},
@@ -104,7 +105,7 @@ func configureSKRKyma(kymaName, moduleName string, skrClient *remote.SkrContext)
}

func configureKCPModuleTemplates(moduleName string) error {
mt := builder.NewModuleTemplateBuilder().
moduleTemplate := builder.NewModuleTemplateBuilder().
WithNamespace(ControlPlaneNamespace).
WithName(fmt.Sprintf("%s-%s", moduleName, moduleVersion)).
WithModuleName(moduleName).
@@ -114,14 +115,14 @@ func configureKCPModuleTemplates(moduleName string) error {

Eventually(kcpClient.Create, Timeout, Interval).
WithContext(ctx).
WithArguments(mt).
WithArguments(moduleTemplate).
Should(Succeed())

return nil
}

func configureKCPModuleReleaseMeta(moduleName string, beta, internal bool) error {
mrm := builder.NewModuleReleaseMetaBuilder().
moduleReleaseMeta := builder.NewModuleReleaseMetaBuilder().
WithNamespace(ControlPlaneNamespace).
WithModuleName(moduleName).
WithBeta(beta).
@@ -130,7 +131,7 @@ func configureKCPModuleReleaseMeta(moduleName string, beta, internal bool) error
Build()

Eventually(kcpClient.Create, Timeout, Interval).WithContext(ctx).
WithArguments(mrm).
WithArguments(moduleReleaseMeta).
Should(Succeed())

return nil
@@ -141,7 +142,7 @@ func expectInstallation(kymaName, moduleName string) error {
if manifest != nil {
return nil
} else {
return fmt.Errorf("expected manifest to be installed, but it was not")
return errors.New("expected manifest to be installed, but it was not")
}
}

@@ -150,6 +151,6 @@ func expectNoInstallation(kymaName, moduleName string) error {
if manifest == nil {
return nil
} else {
return fmt.Errorf("expected manifest to not be installed, but it was")
return errors.New("expected manifest to not be installed, but it was")
}
}