Skip to content
Draft
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
71 changes: 69 additions & 2 deletions api/v1/perconaservermysql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type PerconaServerMySQLSpec struct {
Toolkit *ToolkitSpec `json:"toolkit,omitempty"`
UpgradeOptions UpgradeOptions `json:"upgradeOptions,omitempty"`
UpdateStrategy appsv1.StatefulSetUpdateStrategyType `json:"updateStrategy,omitempty"`
Hibernation *HibernationSpec `json:"hibernation,omitempty"`

// Deprecated: not supported since v0.12.0. Use initContainer instead
InitImage string `json:"initImage,omitempty"`
Expand Down Expand Up @@ -629,7 +630,8 @@ type PerconaServerMySQLStatus struct { // INSERT ADDITIONAL STATUS FIELD - defin
ToolkitVersion string `json:"toolkitVersion,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
// +optional
Host string `json:"host"`
Host string `json:"host"`
Hibernation *HibernationStatus `json:"hibernation,omitempty"`
}

func (s *PerconaServerMySQLStatus) CompareMySQLVersion(ver string) int {
Expand Down Expand Up @@ -960,6 +962,13 @@ func (cr *PerconaServerMySQL) CheckNSetDefaults(_ context.Context, serverVersion
cr.Spec.MySQL.VaultSecretName = cr.Name + "-vault"
}

// Validate hibernation configuration
if cr.Spec.Hibernation != nil {
if err := cr.Spec.Hibernation.Validate(); err != nil {
return errors.Wrap(err, "invalid hibernation configuration")
}
}

return nil
}

Expand Down Expand Up @@ -1200,6 +1209,36 @@ func (cr *PerconaServerMySQL) PVCResizeInProgress() bool {
return ok
}

// Validate validates the hibernation specification.
func (h *HibernationSpec) Validate() error {
if h == nil || !h.Enabled {
return nil
}

if h.Schedule.Pause != "" {
if _, err := cron.ParseStandard(h.Schedule.Pause); err != nil {
return errors.Wrap(err, "invalid pause schedule")
}
}

if h.Schedule.Unpause != "" {
if _, err := cron.ParseStandard(h.Schedule.Unpause); err != nil {
return errors.Wrap(err, "invalid unpause schedule")
}
}

if h.Schedule.Pause == "" && h.Schedule.Unpause == "" {
return errors.New("at least one schedule (pause or unpause) must be specified when hibernation is enabled")
}

return nil
}

// IsHibernationEnabled checks if hibernation is enabled.
func (cr *PerconaServerMySQL) IsHibernationEnabled() bool {
return cr.Spec.Hibernation != nil && cr.Spec.Hibernation.Enabled
}

// Registers PerconaServerMySQL types with the SchemeBuilder.
func init() {
SchemeBuilder.Register(&PerconaServerMySQL{}, &PerconaServerMySQLList{})
Expand All @@ -1213,9 +1252,37 @@ type UpgradeOptions struct {
Apply string `json:"apply,omitempty"`
}

type HibernationSpec struct {
Enabled bool `json:"enabled,omitempty"`
Schedule HibernationSchedule `json:"schedule,omitempty"`
}

type HibernationSchedule struct {
Pause string `json:"pause,omitempty"` // Cron expression for pause (minute hour day month weekday)
Unpause string `json:"unpause,omitempty"` // Cron expression for unpause (minute hour day month weekday)
}

type HibernationStatus struct {
// State indicates the current hibernation state of the cluster
// +kubebuilder:validation:Enum=Active;Paused;Scheduled;Blocked;Disabled
State string `json:"state,omitempty"` // Current hibernation state
LastPauseTime *metav1.Time `json:"lastPauseTime,omitempty"` // When cluster was last paused
LastUnpauseTime *metav1.Time `json:"lastUnpauseTime,omitempty"` // When cluster was last unpaused
NextPauseTime *metav1.Time `json:"nextPauseTime,omitempty"` // When cluster will be paused next
NextUnpauseTime *metav1.Time `json:"nextUnpauseTime,omitempty"` // When cluster will be unpaused next
Reason string `json:"reason,omitempty"` // Why pause was skipped or additional info
}

const (
UpgradeStrategyDisabled = "disabled"
UpgradeStrategyNever = "never"
UpgradeStrategyRecommended = "recommended"
UpgradeStrategyLatest = "latest"

// Hibernation states
HibernationStateActive = "Active" // Cluster is running normally
HibernationStatePaused = "Paused" // Cluster is paused by hibernation
HibernationStateScheduled = "Scheduled" // Hibernation is scheduled but not yet active
HibernationStateBlocked = "Blocked" // Hibernation is blocked by active operations
HibernationStateDisabled = "Disabled" // Hibernation is disabled
UpgradeStrategyLatest = "latest"
)
72 changes: 72 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/percona/percona-server-mysql-operator/pkg/clientcmd"
"github.com/percona/percona-server-mysql-operator/pkg/controller/ps"
"github.com/percona/percona-server-mysql-operator/pkg/controller/psbackup"
"github.com/percona/percona-server-mysql-operator/pkg/controller/pshibernation"
"github.com/percona/percona-server-mysql-operator/pkg/controller/psrestore"
"github.com/percona/percona-server-mysql-operator/pkg/k8s"
"github.com/percona/percona-server-mysql-operator/pkg/platform"
Expand Down Expand Up @@ -181,6 +182,14 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "PerconaServerMySQLRestore")
os.Exit(1)
}
if err = (&pshibernation.PerconaServerMySQLHibernationReconciler{
Client: nsClient,
Scheme: mgr.GetScheme(),
ServerVersion: serverVersion,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "pshibernation-controller")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

err = mgr.GetFieldIndexer().IndexField(
Expand Down
37 changes: 37 additions & 0 deletions config/crd/bases/ps.percona.com_perconaservermysqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,18 @@ spec:
type: string
enableVolumeExpansion:
type: boolean
hibernation:
properties:
enabled:
type: boolean
schedule:
properties:
pause:
type: string
unpause:
type: string
type: object
type: object
ignoreAnnotations:
items:
type: string
Expand Down Expand Up @@ -10815,6 +10827,31 @@ spec:
version:
type: string
type: object
hibernation:
properties:
lastPauseTime:
format: date-time
type: string
lastUnpauseTime:
format: date-time
type: string
nextPauseTime:
format: date-time
type: string
nextUnpauseTime:
format: date-time
type: string
reason:
type: string
state:
enum:
- Active
- Paused
- Scheduled
- Blocked
- Disabled
type: string
type: object
host:
type: string
mysql:
Expand Down
37 changes: 37 additions & 0 deletions deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5014,6 +5014,18 @@ spec:
type: string
enableVolumeExpansion:
type: boolean
hibernation:
properties:
enabled:
type: boolean
schedule:
properties:
pause:
type: string
unpause:
type: string
type: object
type: object
ignoreAnnotations:
items:
type: string
Expand Down Expand Up @@ -13148,6 +13160,31 @@ spec:
version:
type: string
type: object
hibernation:
properties:
lastPauseTime:
format: date-time
type: string
lastUnpauseTime:
format: date-time
type: string
nextPauseTime:
format: date-time
type: string
nextUnpauseTime:
format: date-time
type: string
reason:
type: string
state:
enum:
- Active
- Paused
- Scheduled
- Blocked
- Disabled
type: string
type: object
host:
type: string
mysql:
Expand Down
5 changes: 5 additions & 0 deletions deploy/cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ spec:
upgradeOptions:
versionServiceEndpoint: https://check.percona.com
apply: disabled
# hibernation:
# enabled: false
# schedule:
# pause: "55 12 * * 1-5" # Pause Mon-Fri at 8 PM
# unpause: "45 12 * * 1-5" # Unpause Mon-Fri at 8 AM
# initContainer:
# image: perconalab/percona-server-mysql-operator:main
# containerSecurityContext:
Expand Down
37 changes: 37 additions & 0 deletions deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5014,6 +5014,18 @@ spec:
type: string
enableVolumeExpansion:
type: boolean
hibernation:
properties:
enabled:
type: boolean
schedule:
properties:
pause:
type: string
unpause:
type: string
type: object
type: object
ignoreAnnotations:
items:
type: string
Expand Down Expand Up @@ -13148,6 +13160,31 @@ spec:
version:
type: string
type: object
hibernation:
properties:
lastPauseTime:
format: date-time
type: string
lastUnpauseTime:
format: date-time
type: string
nextPauseTime:
format: date-time
type: string
nextUnpauseTime:
format: date-time
type: string
reason:
type: string
state:
enum:
- Active
- Paused
- Scheduled
- Blocked
- Disabled
type: string
type: object
host:
type: string
mysql:
Expand Down
Loading
Loading