Skip to content

Commit

Permalink
refactor available module
Browse files Browse the repository at this point in the history
# Conflicts:
#	pkg/templatelookup/regular.go
  • Loading branch information
medmes authored and c-pius committed Dec 17, 2024
1 parent 0767ebb commit 7169967
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 106 deletions.
25 changes: 13 additions & 12 deletions internal/manifest/parser/template_to_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

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

"github.com/kyma-project/lifecycle-manager/internal/descriptor/provider"
"github.com/kyma-project/lifecycle-manager/internal/manifest/img"
"github.com/kyma-project/lifecycle-manager/pkg/log"
Expand Down Expand Up @@ -45,7 +46,7 @@ func (p *Parser) GenerateModulesFromTemplates(kyma *v1beta2.Kyma, templates temp
// (since we do not know which module we are dealing with)
modules := make(common.Modules, 0)

for _, module := range templatelookup.FindAvailableModules(kyma) {
for _, module := range templatelookup.FetchModuleStatusInfo(kyma) {
template := templates[module.Name]
modules = p.appendModuleWithInformation(module, kyma, template, modules)
}
Expand All @@ -67,27 +68,27 @@ func (p *Parser) GenerateMandatoryModulesFromTemplates(ctx context.Context,
moduleName = template.Name
}

modules = p.appendModuleWithInformation(templatelookup.AvailableModule{
modules = p.appendModuleWithInformation(templatelookup.ModuleStatusInfo{
Module: v1beta2.Module{
Name: moduleName,
CustomResourcePolicy: v1beta2.CustomResourcePolicyCreateAndDelete,
},
Enabled: true,
IsEnabled: true,
}, kyma, template, modules)
}

return modules
}

func (p *Parser) appendModuleWithInformation(module templatelookup.AvailableModule, kyma *v1beta2.Kyma,
func (p *Parser) appendModuleWithInformation(module templatelookup.ModuleStatusInfo, kyma *v1beta2.Kyma,
template *templatelookup.ModuleTemplateInfo, modules common.Modules,
) common.Modules {
if template.Err != nil && !errors.Is(template.Err, templatelookup.ErrTemplateNotAllowed) {
modules = append(modules, &common.Module{
ModuleName: module.Name,
Template: template,
Enabled: module.Enabled,
IsUnmanaged: module.Unmanaged,
Enabled: module.IsEnabled,
IsUnmanaged: module.IsUnmanaged,
})
return modules
}
Expand All @@ -97,8 +98,8 @@ func (p *Parser) appendModuleWithInformation(module templatelookup.AvailableModu
modules = append(modules, &common.Module{
ModuleName: module.Name,
Template: template,
Enabled: module.Enabled,
IsUnmanaged: module.Unmanaged,
Enabled: module.IsEnabled,
IsUnmanaged: module.IsUnmanaged,
})
return modules
}
Expand All @@ -112,8 +113,8 @@ func (p *Parser) appendModuleWithInformation(module templatelookup.AvailableModu
modules = append(modules, &common.Module{
ModuleName: module.Name,
Template: template,
Enabled: module.Enabled,
IsUnmanaged: module.Unmanaged,
Enabled: module.IsEnabled,
IsUnmanaged: module.IsUnmanaged,
})
return modules
}
Expand All @@ -126,8 +127,8 @@ func (p *Parser) appendModuleWithInformation(module templatelookup.AvailableModu
FQDN: fqdn,
Template: template,
Manifest: manifest,
Enabled: module.Enabled,
IsUnmanaged: module.Unmanaged,
Enabled: module.IsEnabled,
IsUnmanaged: module.IsUnmanaged,
})
return modules
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/status/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

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

"github.com/kyma-project/lifecycle-manager/pkg/status"
"github.com/kyma-project/lifecycle-manager/pkg/testutils/builder"
)
Expand All @@ -22,7 +23,7 @@ func TestInitConditions(t *testing.T) {
t.Parallel()
testcases := []testCase{
{
name: "Should Init Conditions properly with Watcher & Sync Enabled",
name: "Should Init Conditions properly with Watcher & Sync IsEnabled",
watcherEnabled: true,
hasSyncLabel: true,
syncLabelValueEnabled: true,
Expand All @@ -34,19 +35,19 @@ func TestInitConditions(t *testing.T) {
syncLabelValueEnabled: false,
},
{
name: "Should Init Conditions properly with Watcher Enabled & Sync Disabled",
name: "Should Init Conditions properly with Watcher IsEnabled & Sync Disabled",
watcherEnabled: true,
hasSyncLabel: true,
syncLabelValueEnabled: false,
},
{
name: "Should Init Conditions properly with Watcher Disabled & Sync Enabled",
name: "Should Init Conditions properly with Watcher Disabled & Sync IsEnabled",
watcherEnabled: false,
hasSyncLabel: true,
syncLabelValueEnabled: true,
},
{
name: "Should Init Conditions properly with Watcher Enabled & missing sync label",
name: "Should Init Conditions properly with Watcher IsEnabled & missing sync label",
watcherEnabled: true,
hasSyncLabel: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,52 @@ var (
ErrInvalidModuleInStatus = errors.New("invalid module entry in Kyma status")
)

type AvailableModule struct {
type ModuleStatusInfo struct {
v1beta2.Module
Enabled bool
IsEnabled bool
ValidationError error
Unmanaged bool
IsUnmanaged bool
}

func (a AvailableModule) IsInstalledByVersion() bool {
func (a ModuleStatusInfo) IsInstalledByVersion() bool {
return a.configuredWithVersionInSpec() || a.installedwithVersionInStatus()
}

// configuredWithVersionInSpec returns true if the Module is enabled in Spec using a specific version instead of a channel.
func (a AvailableModule) configuredWithVersionInSpec() bool {
return a.Enabled && a.Version != "" && a.Channel == ""
func (a ModuleStatusInfo) configuredWithVersionInSpec() bool {
return a.IsEnabled && a.Version != "" && a.Channel == ""
}

// installedwithVersionInStatus returns true if the Module installed using a specific version (instead of a channel) is reported in Status.
func (a AvailableModule) installedwithVersionInStatus() bool {
return !a.Enabled && shared.NoneChannel.Equals(a.Channel) && a.Version != ""
func (a ModuleStatusInfo) installedwithVersionInStatus() bool {
return !a.IsEnabled && shared.NoneChannel.Equals(a.Channel) && a.Version != ""
}

// FindAvailableModules returns a list of AvailableModule objects based on the Kyma CR Spec and Status.
func FindAvailableModules(kyma *v1beta2.Kyma) []AvailableModule {
// FetchModuleStatusInfo returns a list of ModuleStatusInfo objects based on the Kyma CR Spec and Status.
func FetchModuleStatusInfo(kyma *v1beta2.Kyma) []ModuleStatusInfo {
moduleMap := make(map[string]bool)
modules := make([]AvailableModule, 0)
modules := make([]ModuleStatusInfo, 0)
for _, module := range kyma.Spec.Modules {
moduleMap[module.Name] = true
if shared.NoneChannel.Equals(module.Channel) {
modules = append(modules, AvailableModule{
modules = append(modules, ModuleStatusInfo{
Module: module,
Enabled: true,
IsEnabled: true,
ValidationError: fmt.Errorf("%w for module %s: Channel \"none\" is not allowed", ErrInvalidModuleInSpec, module.Name),
Unmanaged: !module.Managed,
IsUnmanaged: !module.Managed,
})
continue
}
if module.Version != "" && module.Channel != "" {
modules = append(modules, AvailableModule{
modules = append(modules, ModuleStatusInfo{
Module: module,
Enabled: true,
IsEnabled: true,
ValidationError: fmt.Errorf("%w for module %s: Version and channel are mutually exclusive options", ErrInvalidModuleInSpec, module.Name),
Unmanaged: !module.Managed,
IsUnmanaged: !module.Managed,
})
continue
}
modules = append(modules, AvailableModule{Module: module, Enabled: true, Unmanaged: !module.Managed})
modules = append(modules, ModuleStatusInfo{Module: module, IsEnabled: true, IsUnmanaged: !module.Managed})
}

for _, moduleInStatus := range kyma.Status.Modules {
Expand All @@ -67,13 +67,13 @@ func FindAvailableModules(kyma *v1beta2.Kyma) []AvailableModule {
continue
}

modules = append(modules, AvailableModule{
modules = append(modules, ModuleStatusInfo{
Module: v1beta2.Module{
Name: moduleInStatus.Name,
Channel: moduleInStatus.Channel,
Version: moduleInStatus.Version,
},
Enabled: false,
IsEnabled: false,
ValidationError: determineModuleValidity(moduleInStatus),
})
}
Expand Down
Loading

0 comments on commit 7169967

Please sign in to comment.