Skip to content

Commit

Permalink
Merge pull request #195 from vshn/refactor-timeofday
Browse files Browse the repository at this point in the history
Refactor time of day in maintenance
  • Loading branch information
zugao authored Jul 17, 2024
2 parents 6ef4e93 + f0046ae commit 5336a3a
Show file tree
Hide file tree
Showing 17 changed files with 286 additions and 95 deletions.
48 changes: 45 additions & 3 deletions apis/vshn/v1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package v1
import (
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
alertmanagerv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
"time"
)

type TimeOfDay string

// K8upBackupSpec specifies when a backup for redis should be triggered.
// It also contains the retention policy for the backup.
type K8upBackupSpec struct {
Expand Down Expand Up @@ -67,10 +70,11 @@ type VSHNDBaaSMaintenanceScheduleSpec struct {
DayOfWeek string `json:"dayOfWeek,omitempty"`

// +kubebuilder:validation:Pattern="^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"
// +kubebuilder:validation:Type=string

// TimeOfDay for installing updates in UTC.
// Format: "hh:mm:ss".
TimeOfDay string `json:"timeOfDay,omitempty"`
TimeOfDay TimeOfDay `json:"timeOfDay,omitempty"`
}

// GetMaintenanceDayOfWeek returns the currently set day of week
Expand All @@ -79,7 +83,7 @@ func (n *VSHNDBaaSMaintenanceScheduleSpec) GetMaintenanceDayOfWeek() string {
}

// GetMaintenanceTimeOfDay returns the currently set time of day
func (n *VSHNDBaaSMaintenanceScheduleSpec) GetMaintenanceTimeOfDay() string {
func (n *VSHNDBaaSMaintenanceScheduleSpec) GetMaintenanceTimeOfDay() TimeOfDay {
return n.TimeOfDay
}

Expand All @@ -89,7 +93,7 @@ func (n *VSHNDBaaSMaintenanceScheduleSpec) SetMaintenanceDayOfWeek(dow string) {
}

// SetMaintenanceTimeOfDay sets the time of day to the given value
func (n *VSHNDBaaSMaintenanceScheduleSpec) SetMaintenanceTimeOfDay(tod string) {
func (n *VSHNDBaaSMaintenanceScheduleSpec) SetMaintenanceTimeOfDay(tod TimeOfDay) {
n.TimeOfDay = tod
}

Expand Down Expand Up @@ -208,3 +212,41 @@ type VSHNAccess struct {
// created in the namespace where the claim is located.
WriteConnectionSecretToReference *xpv1.SecretReference `json:"writeConnectionSecretToRef,omitempty"`
}

// GetTime will parse TimeOfDay into time.Time
// If TimeOfDay is nil or non-parsable then a zero time.Time will be returned
func (a *TimeOfDay) GetTime() time.Time {
if a == nil {
return time.Time{}
}
v, err := time.Parse(time.TimeOnly, string(*a))
if err != nil {
return time.Time{}
}
return v
}

func (a *TimeOfDay) IsSet() bool {
if a == nil {
return false
}
if *a == "" {
return false
}
return true
}

func (a *TimeOfDay) IsNotSet() bool {
return !a.IsSet()
}

// SetTime will set TimeOfDay from time.Time with time only format
func (a *TimeOfDay) SetTime(t time.Time) {
*a = TimeOfDay(t.Format(time.TimeOnly))
}

// AddTime adds duration to current time
func (a *TimeOfDay) AddTime(d time.Duration) TimeOfDay {
a.SetTime(a.GetTime().Add(d))
return *a
}
109 changes: 109 additions & 0 deletions apis/vshn/v1/common_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package v1

import (
"github.com/stretchr/testify/assert"
"k8s.io/utils/ptr"
"testing"
"time"
)

func Test_GetTime(t *testing.T) {
tests := []struct {
name string
scheduleSpec VSHNDBaaSMaintenanceScheduleSpec
want string
}{
{
name: "GivenNormalSchedule_ThenExpectCorrectTimeOfDay",
scheduleSpec: VSHNDBaaSMaintenanceScheduleSpec{
TimeOfDay: TimeOfDay("00:30:00"),
},
want: "00:30:00",
},
{
name: "GivenEmptyTimeOfDay_ThenExpectZeroTimeOfDay",
scheduleSpec: VSHNDBaaSMaintenanceScheduleSpec{
TimeOfDay: TimeOfDay(""),
},
want: "00:00:00",
},
{
name: "GivenNoTimeOfDay_ThenExpectZeroTimeOfDay",
scheduleSpec: VSHNDBaaSMaintenanceScheduleSpec{},
want: "00:00:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualTime := tt.scheduleSpec.TimeOfDay.GetTime()
assert.Equal(t, tt.want, actualTime.Format(time.TimeOnly))
})
}
}

func Test_AddTime(t *testing.T) {
tests := []struct {
name string
scheduleSpec VSHNDBaaSMaintenanceScheduleSpec
d time.Duration
want string
}{
{
name: "GivenNormalSchedule_ThenExpectAddedTimeOfDay",
scheduleSpec: VSHNDBaaSMaintenanceScheduleSpec{
TimeOfDay: TimeOfDay("00:30:00"),
},
d: 60 * time.Minute,
want: "01:30:00",
},
{
name: "GivenEmptyTimeOfDay_ThenExpectAddedTimeOfDay",
scheduleSpec: VSHNDBaaSMaintenanceScheduleSpec{
TimeOfDay: TimeOfDay(""),
},
d: 10 * time.Minute,
want: "00:10:00",
},
{
name: "GivenNoTimeOfDay_ThenExpectAddedTimeOfDay",
scheduleSpec: VSHNDBaaSMaintenanceScheduleSpec{},
d: 5 * time.Minute,
want: "00:05:00",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualTime := tt.scheduleSpec.TimeOfDay.AddTime(tt.d)
assert.Equal(t, tt.want, string(actualTime))
})
}
}

func Test_IsSet(t *testing.T) {
tests := []struct {
name string
timeOfDay *TimeOfDay
want bool
}{
{
name: "GivenNormalSchedule_ThenExpectIsTrue",
timeOfDay: ptr.To[TimeOfDay]("00:00:00"),
want: true,
},
{
name: "GivenEmptyTimeOfDay_ThenExpectIsFalse",
timeOfDay: ptr.To[TimeOfDay](""),
want: false,
},
{
name: "GivenEmptyTimeOfDay_ThenExpectIsFalse",
timeOfDay: nil,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, tt.timeOfDay.IsSet())
})
}
}
10 changes: 5 additions & 5 deletions apis/vshn/v1/dbaas_vshn_keycloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ func (n *VSHNKeycloak) GetMaintenanceDayOfWeek() string {
}

// GetMaintenanceTimeOfDay returns the currently set time of day
func (v *VSHNKeycloak) GetMaintenanceTimeOfDay() string {
func (v *VSHNKeycloak) GetMaintenanceTimeOfDay() *TimeOfDay {
if v.Spec.Parameters.Maintenance.TimeOfDay != "" {
return v.Spec.Parameters.Maintenance.TimeOfDay
return &v.Spec.Parameters.Maintenance.TimeOfDay
}
return v.Status.Schedules.Maintenance.TimeOfDay
return &v.Status.Schedules.Maintenance.TimeOfDay
}

// SetMaintenanceDayOfWeek sets the day of week to the given value
Expand All @@ -250,7 +250,7 @@ func (v *VSHNKeycloak) SetMaintenanceDayOfWeek(dow string) {
}

// SetMaintenanceTimeOfDay sets the time of day to the given value
func (v *VSHNKeycloak) SetMaintenanceTimeOfDay(tod string) {
func (v *VSHNKeycloak) SetMaintenanceTimeOfDay(tod TimeOfDay) {
v.Status.Schedules.Maintenance.TimeOfDay = tod
}

Expand Down Expand Up @@ -281,7 +281,7 @@ func (v *VSHNKeycloak) GetServiceName() string {
func (v *VSHNKeycloak) GetFullMaintenanceSchedule() VSHNDBaaSMaintenanceScheduleSpec {
schedule := v.Spec.Parameters.Maintenance
schedule.DayOfWeek = v.GetMaintenanceDayOfWeek()
schedule.TimeOfDay = v.GetMaintenanceTimeOfDay()
schedule.TimeOfDay = *v.GetMaintenanceTimeOfDay()
return schedule
}

Expand Down
10 changes: 5 additions & 5 deletions apis/vshn/v1/dbaas_vshn_mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ func (v *VSHNMariaDB) GetMaintenanceDayOfWeek() string {
}

// GetMaintenanceTimeOfDay returns the currently set time of day
func (v *VSHNMariaDB) GetMaintenanceTimeOfDay() string {
func (v *VSHNMariaDB) GetMaintenanceTimeOfDay() *TimeOfDay {
if v.Spec.Parameters.Maintenance.TimeOfDay != "" {
return v.Spec.Parameters.Maintenance.TimeOfDay
return &v.Spec.Parameters.Maintenance.TimeOfDay
}
return v.Status.Schedules.Maintenance.TimeOfDay
return &v.Status.Schedules.Maintenance.TimeOfDay
}

// SetMaintenanceDayOfWeek sets the day of week to the given value
Expand All @@ -200,7 +200,7 @@ func (v *VSHNMariaDB) SetMaintenanceDayOfWeek(dow string) {
}

// SetMaintenanceTimeOfDay sets the time of day to the given value
func (v *VSHNMariaDB) SetMaintenanceTimeOfDay(tod string) {
func (v *VSHNMariaDB) SetMaintenanceTimeOfDay(tod TimeOfDay) {
v.Status.Schedules.Maintenance.TimeOfDay = tod
}

Expand Down Expand Up @@ -231,7 +231,7 @@ func (v *VSHNMariaDB) GetServiceName() string {
func (v *VSHNMariaDB) GetFullMaintenanceSchedule() VSHNDBaaSMaintenanceScheduleSpec {
schedule := v.Spec.Parameters.Maintenance
schedule.DayOfWeek = v.GetMaintenanceDayOfWeek()
schedule.TimeOfDay = v.GetMaintenanceTimeOfDay()
schedule.TimeOfDay = *v.GetMaintenanceTimeOfDay()
return schedule
}

Expand Down
10 changes: 5 additions & 5 deletions apis/vshn/v1/dbaas_vshn_postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ func (v *VSHNPostgreSQL) GetMaintenanceDayOfWeek() string {
}

// GetMaintenanceTimeOfDay returns the currently set time of day
func (v *VSHNPostgreSQL) GetMaintenanceTimeOfDay() string {
func (v *VSHNPostgreSQL) GetMaintenanceTimeOfDay() *TimeOfDay {
if v.Spec.Parameters.Maintenance.TimeOfDay != "" {
return v.Spec.Parameters.Maintenance.TimeOfDay
return &v.Spec.Parameters.Maintenance.TimeOfDay
}
return v.Status.Schedules.Maintenance.TimeOfDay
return &v.Status.Schedules.Maintenance.TimeOfDay
}

// SetMaintenanceDayOfWeek sets the day of week to the given value
Expand All @@ -281,7 +281,7 @@ func (v *VSHNPostgreSQL) SetMaintenanceDayOfWeek(dow string) {
}

// SetMaintenanceTimeOfDay sets the time of day to the given value
func (v *VSHNPostgreSQL) SetMaintenanceTimeOfDay(tod string) {
func (v *VSHNPostgreSQL) SetMaintenanceTimeOfDay(tod TimeOfDay) {
v.Status.Schedules.Maintenance.TimeOfDay = tod
}

Expand All @@ -302,7 +302,7 @@ func (v *VSHNPostgreSQL) SetBackupSchedule(schedule string) {
func (v *VSHNPostgreSQL) GetFullMaintenanceSchedule() VSHNDBaaSMaintenanceScheduleSpec {
schedule := v.Spec.Parameters.Maintenance
schedule.DayOfWeek = v.GetMaintenanceDayOfWeek()
schedule.TimeOfDay = v.GetMaintenanceTimeOfDay()
schedule.TimeOfDay = *v.GetMaintenanceTimeOfDay()
return schedule
}

Expand Down
10 changes: 5 additions & 5 deletions apis/vshn/v1/dbaas_vshn_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ func (v *VSHNRedis) GetMaintenanceDayOfWeek() string {
}

// GetMaintenanceTimeOfDay returns the currently set time of day
func (v *VSHNRedis) GetMaintenanceTimeOfDay() string {
func (v *VSHNRedis) GetMaintenanceTimeOfDay() *TimeOfDay {
if v.Spec.Parameters.Maintenance.TimeOfDay != "" {
return v.Spec.Parameters.Maintenance.TimeOfDay
return &v.Spec.Parameters.Maintenance.TimeOfDay
}
return v.Status.Schedules.Maintenance.TimeOfDay
return &v.Status.Schedules.Maintenance.TimeOfDay
}

// SetMaintenanceDayOfWeek sets the day of week to the given value
Expand All @@ -221,7 +221,7 @@ func (v *VSHNRedis) SetMaintenanceDayOfWeek(dow string) {
}

// SetMaintenanceTimeOfDay sets the time of day to the given value
func (v *VSHNRedis) SetMaintenanceTimeOfDay(tod string) {
func (v *VSHNRedis) SetMaintenanceTimeOfDay(tod TimeOfDay) {
v.Status.Schedules.Maintenance.TimeOfDay = tod
}

Expand Down Expand Up @@ -252,7 +252,7 @@ func (v *VSHNRedis) GetServiceName() string {
func (v *VSHNRedis) GetFullMaintenanceSchedule() VSHNDBaaSMaintenanceScheduleSpec {
schedule := v.Spec.Parameters.Maintenance
schedule.DayOfWeek = v.GetMaintenanceDayOfWeek()
schedule.TimeOfDay = v.GetMaintenanceTimeOfDay()
schedule.TimeOfDay = *v.GetMaintenanceTimeOfDay()
return schedule
}

Expand Down
10 changes: 5 additions & 5 deletions apis/vshn/v1/vshn_minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ func (v *VSHNMinio) GetMaintenanceDayOfWeek() string {
}

// GetMaintenanceTimeOfDay returns the currently set time of day
func (v *VSHNMinio) GetMaintenanceTimeOfDay() string {
func (v *VSHNMinio) GetMaintenanceTimeOfDay() *TimeOfDay {
if v.Spec.Parameters.Maintenance.TimeOfDay != "" {
return v.Spec.Parameters.Maintenance.TimeOfDay
return &v.Spec.Parameters.Maintenance.TimeOfDay
}
return v.Status.Schedules.Maintenance.TimeOfDay
return &v.Status.Schedules.Maintenance.TimeOfDay
}

// SetMaintenanceDayOfWeek sets the day of week to the given value
Expand All @@ -173,7 +173,7 @@ func (v *VSHNMinio) SetMaintenanceDayOfWeek(dow string) {
}

// SetMaintenanceTimeOfDay sets the time of day to the given value
func (v *VSHNMinio) SetMaintenanceTimeOfDay(tod string) {
func (v *VSHNMinio) SetMaintenanceTimeOfDay(tod TimeOfDay) {
v.Status.Schedules.Maintenance.TimeOfDay = tod
}

Expand All @@ -194,7 +194,7 @@ func (v *VSHNMinio) SetBackupSchedule(schedule string) {
func (v *VSHNMinio) GetFullMaintenanceSchedule() VSHNDBaaSMaintenanceScheduleSpec {
schedule := v.Spec.Parameters.Maintenance
schedule.DayOfWeek = v.GetMaintenanceDayOfWeek()
schedule.TimeOfDay = v.GetMaintenanceTimeOfDay()
schedule.TimeOfDay = *v.GetMaintenanceTimeOfDay()
return schedule
}

Expand Down
10 changes: 5 additions & 5 deletions apis/vshn/v1/vshn_nextcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ func (n *VSHNNextcloud) GetMaintenanceDayOfWeek() string {
}

// GetMaintenanceTimeOfDay returns the currently set time of day
func (v *VSHNNextcloud) GetMaintenanceTimeOfDay() string {
func (v *VSHNNextcloud) GetMaintenanceTimeOfDay() *TimeOfDay {
if v.Spec.Parameters.Maintenance.TimeOfDay != "" {
return v.Spec.Parameters.Maintenance.TimeOfDay
return &v.Spec.Parameters.Maintenance.TimeOfDay
}
return v.Status.Schedules.Maintenance.TimeOfDay
return &v.Status.Schedules.Maintenance.TimeOfDay
}

// SetMaintenanceDayOfWeek sets the day of week to the given value
Expand All @@ -226,7 +226,7 @@ func (v *VSHNNextcloud) SetMaintenanceDayOfWeek(dow string) {
}

// SetMaintenanceTimeOfDay sets the time of day to the given value
func (v *VSHNNextcloud) SetMaintenanceTimeOfDay(tod string) {
func (v *VSHNNextcloud) SetMaintenanceTimeOfDay(tod TimeOfDay) {
v.Status.Schedules.Maintenance.TimeOfDay = tod
}

Expand Down Expand Up @@ -257,7 +257,7 @@ func (v *VSHNNextcloud) GetServiceName() string {
func (v *VSHNNextcloud) GetFullMaintenanceSchedule() VSHNDBaaSMaintenanceScheduleSpec {
schedule := v.Spec.Parameters.Maintenance
schedule.DayOfWeek = v.GetMaintenanceDayOfWeek()
schedule.TimeOfDay = v.GetMaintenanceTimeOfDay()
schedule.TimeOfDay = *v.GetMaintenanceTimeOfDay()
return schedule
}

Expand Down
Loading

0 comments on commit 5336a3a

Please sign in to comment.