Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/api/sigs.k8s.io/contro…
Browse files Browse the repository at this point in the history
…ller-runtime-0.20.0
  • Loading branch information
medmes authored Jan 24, 2025
2 parents c72e9ae + d4ab059 commit 3a4f41a
Show file tree
Hide file tree
Showing 45 changed files with 1,116 additions and 604 deletions.
50 changes: 24 additions & 26 deletions api/v1beta2/moduletemplate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,39 +223,27 @@ func (m *ModuleTemplate) IsInternal() bool {
return false
}

var ErrInvalidVersion = errors.New("can't find valid semantic version")

// getVersionLegacy() returns the version of the ModuleTemplate from the annotation on the object.
// Remove once shared.ModuleVersionAnnotation is removed.
func (m *ModuleTemplate) getVersionLegacy() (string, error) {
if m.Annotations != nil {
moduleVersion, found := m.Annotations[shared.ModuleVersionAnnotation]
if found {
return moduleVersion, nil
}
// https://github.com/kyma-project/lifecycle-manager/issues/2096
// Refactor this function to drop the label fallback after the migration to the new ModuleTemplate format is completed.
func (m *ModuleTemplate) GetVersion() string {
version := m.Spec.Version
if version == "" {
version = m.Annotations[shared.ModuleVersionAnnotation]
}
return "", ErrInvalidVersion
return version
}

// GetVersion returns the declared version of the ModuleTemplate from it's Spec.
func (m *ModuleTemplate) GetVersion() (*semver.Version, error) {
var versionValue string
var err error

if m.Spec.Version == "" {
versionValue, err = m.getVersionLegacy()
if err != nil {
return nil, err
}
} else {
versionValue = m.Spec.Version
}
var ErrInvalidVersion = errors.New("can't find valid semantic version")

version, err := semver.NewVersion(versionValue)
// GetSemanticVersion returns the declared version of the ModuleTemplate as semantic version.
func (m *ModuleTemplate) GetSemanticVersion() (*semver.Version, error) {
version := m.GetVersion()

semanticVersion, err := semver.NewVersion(version)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidVersion, err.Error())
}
return version, nil
return semanticVersion, nil
}

// https://github.com/kyma-project/lifecycle-manager/issues/2096
Expand All @@ -279,3 +267,13 @@ func (m *ModuleTemplate) HasSyncDisabled() bool {
}
return false
}

// https://github.com/kyma-project/lifecycle-manager/issues/2096
// Refactor this function to drop the label fallback after the migration to the new ModuleTemplate format is completed.
func (m *ModuleTemplate) GetModuleName() string {
moduleName := m.Spec.ModuleName
if moduleName == "" {
moduleName = m.Labels[shared.ModuleName]
}
return moduleName
}
131 changes: 120 additions & 11 deletions api/v1beta2/moduletemplate_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"strings"
"testing"

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

"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
)

func Test_GetVersion(t *testing.T) {
func Test_GetSemanticVersion(t *testing.T) {
const testVersion = "1.0.1"
const otherVersion = "0.0.1"
tests := []struct {
Expand All @@ -20,7 +21,7 @@ func Test_GetVersion(t *testing.T) {
expectedErr string
}{
{
name: "Test GetVersion() by annotation (legacy)",
name: "Test GetSemanticVersion() by annotation (legacy)",
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Annotations: map[string]string{
Expand All @@ -31,7 +32,7 @@ func Test_GetVersion(t *testing.T) {
expectedVersion: testVersion,
},
{
name: "Test GetVersion() by explicit version in Spec",
name: "Test GetSemanticVersion() by explicit version in Spec",
m: &v1beta2.ModuleTemplate{
Spec: v1beta2.ModuleTemplateSpec{
Version: testVersion,
Expand All @@ -40,7 +41,7 @@ func Test_GetVersion(t *testing.T) {
expectedVersion: testVersion,
},
{
name: "Test GetVersion() with both version in Spec and annotation",
name: "Test GetSemanticVersion() with both version in Spec and annotation",
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Annotations: map[string]string{
Expand Down Expand Up @@ -75,32 +76,140 @@ func Test_GetVersion(t *testing.T) {
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
actualVersion, err := testCase.m.GetVersion()
actualVersion, err := testCase.m.GetSemanticVersion()
if err != nil {
if actualVersion != nil {
t.Errorf("GetVersion(): Returned version should be nil when error is not nil")
t.Errorf("GetSemanticVersion(): Returned version should be nil when error is not nil")
}
if testCase.expectedErr == "" {
t.Errorf("GetVersion(): Unexpected error: %v", err)
t.Errorf("GetSemanticVersion(): Unexpected error: %v", err)
}
if !strings.Contains(err.Error(), testCase.expectedErr) {
t.Errorf("GetVersion(): Actual error = %v, expected error: %v", err, testCase.expectedErr)
t.Errorf("GetSemanticVersion(): Actual error = %v, expected error: %v", err, testCase.expectedErr)
}
return
}

if actualVersion == nil {
t.Errorf("GetVersion(): Returned version should not be nil when error is nil")
t.Errorf("GetSemanticVersion(): Returned version should not be nil when error is nil")
}

if testCase.expectedVersion == "" {
t.Errorf("GetVersion(): Expected version is empty but non-nil version is returned")
t.Errorf("GetSemanticVersion(): Expected version is empty but non-nil version is returned")
}

if actualVersion != nil && actualVersion.String() != testCase.expectedVersion {
t.Errorf("GetVersion(): actual version = %v, expected version: %v", actualVersion.String(),
t.Errorf("GetSemanticVersion(): actual version = %v, expected version: %v", actualVersion.String(),
testCase.expectedVersion)
}
})
}
}

//nolint:dupl // similar but not duplicate
func Test_GetVersion(t *testing.T) {
tests := []struct {
name string
m *v1beta2.ModuleTemplate
expectedVersion string
}{
{
name: "Test GetVersion() by annotation (legacy)",
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Annotations: map[string]string{
shared.ModuleVersionAnnotation: "1.0.0-annotated",
},
},
Spec: v1beta2.ModuleTemplateSpec{},
},
expectedVersion: "1.0.0-annotated",
},
{
name: "Test GetVersion() by spec.version",
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Annotations: map[string]string{},
},
Spec: v1beta2.ModuleTemplateSpec{
Version: "2.0.0-spec",
},
},
expectedVersion: "2.0.0-spec",
},
{
name: "Test GetVersion() spec.moduleName has priority over annotation",
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Annotations: map[string]string{
shared.ModuleVersionAnnotation: "1.0.0-annotated",
},
},
Spec: v1beta2.ModuleTemplateSpec{
Version: "2.0.0-spec",
},
},
expectedVersion: "2.0.0-spec",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
actualVersion := testCase.m.GetVersion()
assert.Equal(t, testCase.expectedVersion, actualVersion)
})
}
}

//nolint:dupl // similar but not duplicate
func Test_GetModuleName(t *testing.T) {
tests := []struct {
name string
m *v1beta2.ModuleTemplate
expectedName string
}{
{
name: "Test GetModuleName() by label",
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Labels: map[string]string{
shared.ModuleName: "labelled-module",
},
},
Spec: v1beta2.ModuleTemplateSpec{},
},
expectedName: "labelled-module",
},
{
name: "Test GetModuleName() by spec.moduleName",
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Labels: map[string]string{},
},
Spec: v1beta2.ModuleTemplateSpec{
ModuleName: "spec-module",
},
},
expectedName: "spec-module",
},
{
name: "Test GetModuleName() spec.moduleName has priority over label",
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Labels: map[string]string{
shared.ModuleName: "labelled-module",
},
},
Spec: v1beta2.ModuleTemplateSpec{
ModuleName: "spec-module",
},
},
expectedName: "spec-module",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
actualName := testCase.m.GetModuleName()
assert.Equal(t, testCase.expectedName, actualName)
})
}
}
13 changes: 10 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import (
"github.com/kyma-project/lifecycle-manager/pkg/matcher"
"github.com/kyma-project/lifecycle-manager/pkg/queue"
"github.com/kyma-project/lifecycle-manager/pkg/templatelookup"
"github.com/kyma-project/lifecycle-manager/pkg/templatelookup/moduletemplateinfolookup"
"github.com/kyma-project/lifecycle-manager/pkg/watcher"

_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand Down Expand Up @@ -116,7 +117,7 @@ func main() {
}

cacheOptions := internal.GetCacheOptions(flagVar.IsKymaManaged, flagVar.IstioNamespace,
flagVar.IstioGatewayNamespace, flagVar.RemoteSyncNamespace)
flagVar.IstioGatewayNamespace)
setupManager(flagVar, cacheOptions, scheme, setupLog)
}

Expand Down Expand Up @@ -280,13 +281,19 @@ func scheduleMetricsCleanup(kymaMetrics *metrics.KymaMetrics, cleanupIntervalInM
func setupKymaReconciler(mgr ctrl.Manager, descriptorProvider *provider.CachedDescriptorProvider,
skrContextFactory remote.SkrContextProvider, event event.Event, flagVar *flags.FlagVar, options ctrlruntime.Options,
skrWebhookManager *watcher.SKRWebhookManifestManager, kymaMetrics *metrics.KymaMetrics,
setupLog logr.Logger, maintenanceWindow templatelookup.MaintenanceWindow,
setupLog logr.Logger, _ *maintenancewindows.MaintenanceWindow,
) {
options.RateLimiter = internal.RateLimiter(flagVar.FailureBaseDelay,
flagVar.FailureMaxDelay, flagVar.RateLimiterFrequency, flagVar.RateLimiterBurst)
options.CacheSyncTimeout = flagVar.CacheSyncTimeout
options.MaxConcurrentReconciles = flagVar.MaxConcurrentKymaReconciles

moduleTemplateInfoLookupStrategies := moduletemplateinfolookup.NewModuleTemplateInfoLookupStrategies([]moduletemplateinfolookup.ModuleTemplateInfoLookupStrategy{
moduletemplateinfolookup.NewByVersionStrategy(mgr.GetClient()),
moduletemplateinfolookup.NewByChannelStrategy(mgr.GetClient()),
moduletemplateinfolookup.NewByModuleReleaseMetaStrategy(mgr.GetClient()),
})

if err := (&kyma.Reconciler{
Client: mgr.GetClient(),
SkrContextFactory: skrContextFactory,
Expand All @@ -306,7 +313,7 @@ func setupKymaReconciler(mgr ctrl.Manager, descriptorProvider *provider.CachedDe
Metrics: kymaMetrics,
RemoteCatalog: remote.NewRemoteCatalogFromKyma(mgr.GetClient(), skrContextFactory,
flagVar.RemoteSyncNamespace),
TemplateLookup: templatelookup.NewTemplateLookup(mgr.GetClient(), descriptorProvider, maintenanceWindow),
TemplateLookup: templatelookup.NewTemplateLookup(mgr.GetClient(), descriptorProvider, moduleTemplateInfoLookupStrategies),
}).SetupWithManager(
mgr, options, kyma.SetupOptions{
ListenerAddr: flagVar.KymaListenerAddr,
Expand Down
2 changes: 0 additions & 2 deletions config/rbac/namespace_bindings/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ resources:
- role_binding.yaml
# Comment the following to disable manifest integration
- watcher_certmanager_role.yaml
- skr_role.yaml
- watcher_certmanager_role_binding.yaml
- skr_role_binding.yaml
43 changes: 0 additions & 43 deletions config/rbac/namespace_bindings/skr_role.yaml

This file was deleted.

12 changes: 0 additions & 12 deletions config/rbac/namespace_bindings/skr_role_binding.yaml

This file was deleted.

Loading

0 comments on commit 3a4f41a

Please sign in to comment.