-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement MaintenanceWindow determination logic (#2196)
* feat: Add metadata and status helpers to Kyma type * add go sum * cleanup go mod, add api to coverage * remove api from unit-test coverage * feat: MaintenanceWindow service * chore(dependabot): bump k8s.io/apimachinery from 0.32.0 to 0.32.1 in /api (#2185) chore(dependabot): bump k8s.io/apimachinery in /api Bumps [k8s.io/apimachinery](https://github.com/kubernetes/apimachinery) from 0.32.0 to 0.32.1. - [Commits](kubernetes/apimachinery@v0.32.0...v0.32.1) --- updated-dependencies: - dependency-name: k8s.io/apimachinery dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(dependabot): bump sigs.k8s.io/controller-runtime from 0.19.4 to 0.20.0 (#2192) chore(dependabot): bump sigs.k8s.io/controller-runtime Bumps [sigs.k8s.io/controller-runtime](https://github.com/kubernetes-sigs/controller-runtime) from 0.19.4 to 0.20.0. - [Release notes](https://github.com/kubernetes-sigs/controller-runtime/releases) - [Changelog](https://github.com/kubernetes-sigs/controller-runtime/blob/main/RELEASE.md) - [Commits](kubernetes-sigs/controller-runtime@v0.19.4...v0.20.0) --- updated-dependencies: - dependency-name: sigs.k8s.io/controller-runtime dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat: Add metadata and status helpers to Kyma type * cleanup go mod, add api to coverage * remove obsolete comment * fix fake arguments in suite_test * avoid handler name in suite_test * rename to maintenanceWindow consistently * underscore unused receiver arg * omit receiver arg --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
2dfa5de
commit 90b9197
Showing
13 changed files
with
533 additions
and
176 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
112 changes: 0 additions & 112 deletions
112
internal/maintenancewindows/maintenance_policy_handler_test.go
This file was deleted.
Oops, something went wrong.
This file contains 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,110 @@ | ||
package maintenancewindows | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
|
||
"github.com/kyma-project/lifecycle-manager/api/v1beta2" | ||
"github.com/kyma-project/lifecycle-manager/maintenancewindows/resolver" | ||
) | ||
|
||
var ErrNoMaintenanceWindowPolicyConfigured = errors.New("no maintenance window policy configured") | ||
|
||
type MaintenanceWindowPolicy interface { | ||
Resolve(runtime *resolver.Runtime, opts ...interface{}) (*resolver.ResolvedWindow, error) | ||
} | ||
|
||
type MaintenanceWindow struct { | ||
// make this private once we refactor the API | ||
// https://github.com/kyma-project/lifecycle-manager/issues/2190 | ||
MaintenanceWindowPolicy MaintenanceWindowPolicy | ||
} | ||
|
||
func InitializeMaintenanceWindow(log logr.Logger, | ||
policiesDirectory, policyName string, | ||
) (*MaintenanceWindow, error) { | ||
if err := os.Setenv(resolver.PolicyPathENV, policiesDirectory); err != nil { | ||
return nil, fmt.Errorf("failed to set the policy path env variable, %w", err) | ||
} | ||
|
||
policyFilePath := fmt.Sprintf("%s/%s.json", policiesDirectory, policyName) | ||
if !MaintenancePolicyFileExists(policyFilePath) { | ||
log.Info("maintenance windows policy file does not exist") | ||
return &MaintenanceWindow{ | ||
MaintenanceWindowPolicy: nil, | ||
}, nil | ||
} | ||
|
||
maintenancePolicyPool, err := resolver.GetMaintenancePolicyPool() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get maintenance policy pool, %w", err) | ||
} | ||
|
||
maintenancePolicy, err := resolver.GetMaintenancePolicy(maintenancePolicyPool, policyName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get maintenance window policy, %w", err) | ||
} | ||
|
||
return &MaintenanceWindow{ | ||
MaintenanceWindowPolicy: maintenancePolicy, | ||
}, nil | ||
} | ||
|
||
func MaintenancePolicyFileExists(policyFilePath string) bool { | ||
if _, err := os.Stat(policyFilePath); os.IsNotExist(err) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// IsRequired determines if a maintenance window is required to update the given module. | ||
func (MaintenanceWindow) IsRequired(moduleTemplate *v1beta2.ModuleTemplate, kyma *v1beta2.Kyma) bool { | ||
if !moduleTemplate.Spec.RequiresDowntime { | ||
return false | ||
} | ||
|
||
if kyma.Spec.SkipMaintenanceWindows { | ||
return false | ||
} | ||
|
||
// module not installed yet => no need for maintenance window | ||
moduleStatus := kyma.Status.GetModuleStatus(moduleTemplate.Spec.ModuleName) | ||
if moduleStatus == nil { | ||
return false | ||
} | ||
|
||
// module already installed in this version => no need for maintenance window | ||
installedVersion := moduleStatus.Version | ||
return installedVersion != moduleTemplate.Spec.Version | ||
} | ||
|
||
// IsActive determines if a maintenance window is currently active. | ||
func (mw MaintenanceWindow) IsActive(kyma *v1beta2.Kyma) (bool, error) { | ||
if mw.MaintenanceWindowPolicy == nil { | ||
return false, ErrNoMaintenanceWindowPolicyConfigured | ||
} | ||
|
||
runtime := &resolver.Runtime{ | ||
GlobalAccountID: kyma.GetGlobalAccount(), | ||
Region: kyma.GetRegion(), | ||
PlatformRegion: kyma.GetPlatformRegion(), | ||
Plan: kyma.GetPlan(), | ||
} | ||
|
||
resolvedWindow, err := mw.MaintenanceWindowPolicy.Resolve(runtime) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
now := time.Now() | ||
if now.After(resolvedWindow.Begin) && now.Before(resolvedWindow.End) { | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} |
Oops, something went wrong.