Skip to content

Commit

Permalink
Maintenance Events resource was transfered to APIv2
Browse files Browse the repository at this point in the history
  • Loading branch information
testisnullus committed Sep 15, 2023
1 parent 8a6906b commit bcce9bb
Show file tree
Hide file tree
Showing 29 changed files with 1,246 additions and 668 deletions.
60 changes: 18 additions & 42 deletions apis/clusterresources/v1beta1/maintenanceevents_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,24 @@ limitations under the License.
package v1beta1

import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/instaclustr/operator/pkg/validation"
)

type MaintenanceEventRescheduleSpec struct {
ScheduledStartTime string `json:"scheduledStartTime"`
ScheduleID string `json:"scheduleId"`
}

// MaintenanceEventsSpec defines the desired state of MaintenanceEvents
type MaintenanceEventsSpec struct {
ClusterID string `json:"clusterId"`
MaintenanceEventsReschedules []*MaintenanceEventRescheduleSpec `json:"maintenanceEventsReschedule,omitempty"`
ClusterID string `json:"clusterId"`
MaintenanceEventsReschedules []*MaintenanceEventReschedule `json:"maintenanceEventsReschedule"`
}

// MaintenanceEventsStatus defines the observed state of MaintenanceEvents
type MaintenanceEventsStatus struct {
EventsStatuses []*MaintenanceEventStatus `json:"eventsStatuses,omitempty"`
CurrentRescheduledEvent MaintenanceEventReschedule `json:"currentRescheduledEvent"`
}

type MaintenanceEventReschedule struct {
ScheduledStartTime string `json:"scheduledStartTime"`
MaintenanceEventID string `json:"maintenanceEventId"`
}

type MaintenanceEventStatus struct {
Expand All @@ -47,7 +44,16 @@ type MaintenanceEventStatus struct {
ScheduledEndTime string `json:"scheduledEndTime,omitempty"`
ScheduledStartTimeMin string `json:"scheduledStartTimeMin,omitempty"`
ScheduledStartTimeMax string `json:"scheduledStartTimeMax,omitempty"`
IsFinalized bool `json:"isFinalized,omitempty"`
IsFinalized bool `json:"isFinalized"`
StartTime string `json:"startTime,omitempty"`
EndTime string `json:"endTime,omitempty"`
Outcome string `json:"outcome,omitempty"`
}

type ClusteredMaintenanceEventStatus struct {
InProgress []*MaintenanceEventStatus `json:"inProgress"`
Past []*MaintenanceEventStatus `json:"past"`
Upcoming []*MaintenanceEventStatus `json:"upcoming"`
}

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -80,36 +86,6 @@ func (me *MaintenanceEvents) NewPatch() client.Patch {
return client.MergeFrom(old)
}

func (me *MaintenanceEvents) AreMEventsStatusesEqual(instMEventsStatuses []*MaintenanceEventStatus) bool {
if len(instMEventsStatuses) != len(me.Status.EventsStatuses) {
return false
}

for _, instMEvent := range instMEventsStatuses {
for _, k8sMEvent := range me.Status.EventsStatuses {
if instMEvent.ID == k8sMEvent.ID {
if *instMEvent != *k8sMEvent {
return false
}

break
}
}
}

return true
}

func (mes *MaintenanceEventsSpec) ValidateMaintenanceEventsReschedules() error {
for _, event := range mes.MaintenanceEventsReschedules {
if dateValid, err := validation.ValidateISODate(event.ScheduledStartTime); err != nil || !dateValid {
return fmt.Errorf("scheduledStartTime must be provided in an ISO-8601 formatted UTC string: %v", err)
}
}

return nil
}

func init() {
SchemeBuilder.Register(&MaintenanceEvents{}, &MaintenanceEventsList{})
}
16 changes: 14 additions & 2 deletions apis/clusterresources/v1beta1/maintenanceevents_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/instaclustr/operator/pkg/validation"
)

var maintenanceeventslog = logf.Log.WithName("maintenanceevents-resource")
Expand All @@ -42,7 +44,7 @@ var _ webhook.Validator = &MaintenanceEvents{}
func (r *MaintenanceEvents) ValidateCreate() error {
maintenanceeventslog.Info("validate create", "name", r.Name)

if err := r.Spec.ValidateMaintenanceEventsReschedules(); err != nil {
if err := r.ValidateMaintenanceEventsReschedules(); err != nil {
return fmt.Errorf("maintenance events reschedules validation failed: %v", err)
}

Expand All @@ -57,7 +59,7 @@ func (r *MaintenanceEvents) ValidateUpdate(old runtime.Object) error {
return nil
}

if err := r.Spec.ValidateMaintenanceEventsReschedules(); err != nil {
if err := r.ValidateMaintenanceEventsReschedules(); err != nil {
return fmt.Errorf("maintenance events reschedules validation failed: %v", err)
}

Expand All @@ -71,3 +73,13 @@ func (r *MaintenanceEvents) ValidateDelete() error {
// TODO(user): fill in your validation logic upon object deletion.
return nil
}

func (r *MaintenanceEvents) ValidateMaintenanceEventsReschedules() error {
for _, event := range r.Spec.MaintenanceEventsReschedules {
if dateValid, err := validation.ValidateISODate(event.ScheduledStartTime); err != nil || !dateValid {
return fmt.Errorf("scheduledStartTime must be provided in an ISO-8601 formatted UTC string: %v", err)
}
}

return nil
}
74 changes: 56 additions & 18 deletions apis/clusterresources/v1beta1/zz_generated.deepcopy.go

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

112 changes: 62 additions & 50 deletions apis/clusters/v1beta1/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"net"

clusterresource "github.com/instaclustr/operator/apis/clusterresources/v1beta1"
"github.com/instaclustr/operator/pkg/models"
)

Expand Down Expand Up @@ -90,24 +91,20 @@ type Cluster struct {
}

type ClusterStatus struct {
ID string `json:"id,omitempty"`
State string `json:"state,omitempty"`
DataCentres []*DataCentreStatus `json:"dataCentres,omitempty"`
CDCID string `json:"cdcid,omitempty"`
TwoFactorDeleteEnabled bool `json:"twoFactorDeleteEnabled,omitempty"`
Options *Options `json:"options,omitempty"`
CurrentClusterOperationStatus string `json:"currentClusterOperationStatus,omitempty"`
MaintenanceEvents []*MaintenanceEvent `json:"maintenanceEvents,omitempty"`
}

type MaintenanceEvent struct {
ID string `json:"id,omitempty"`
Description string `json:"description,omitempty"`
ScheduledStartTime string `json:"scheduledStartTime,omitempty"`
ScheduledEndTime string `json:"scheduledEndTime,omitempty"`
ScheduledStartTimeMin string `json:"scheduledStartTimeMin,omitempty"`
ScheduledStartTimeMax string `json:"scheduledStartTimeMax,omitempty"`
IsFinalized bool `json:"isFinalized,omitempty"`
ID string `json:"id,omitempty"`
State string `json:"state,omitempty"`
DataCentres []*DataCentreStatus `json:"dataCentres,omitempty"`
CDCID string `json:"cdcid,omitempty"`
TwoFactorDeleteEnabled bool `json:"twoFactorDeleteEnabled,omitempty"`
Options *Options `json:"options,omitempty"`
CurrentClusterOperationStatus string `json:"currentClusterOperationStatus,omitempty"`
MaintenanceEvents []*clusterresource.ClusteredMaintenanceEventStatus `json:"maintenanceEvents,omitempty"`
}

type ClusteredMaintenanceEvent struct {
InProgress []*clusterresource.MaintenanceEventStatus `json:"inProgress"`
Past []*clusterresource.MaintenanceEventStatus `json:"past"`
Upcoming []*clusterresource.MaintenanceEventStatus `json:"upcoming"`
}

type TwoFactorDelete struct {
Expand Down Expand Up @@ -419,26 +416,63 @@ func (c *Cluster) newImmutableFields() immutableCluster {
}
}

func (cs *ClusterStatus) AreMaintenanceEventsEqual(iEvents []*MaintenanceEvent) bool {
if len(cs.MaintenanceEvents) != len(iEvents) {
func (cs *ClusterStatus) AreMaintenanceEventStatusesEqual(
iEventStatuses []*clusterresource.ClusteredMaintenanceEventStatus,
) bool {
if len(cs.MaintenanceEvents) != len(iEventStatuses) {
return false
}

for _, iEvent := range iEvents {
for _, k8sEvent := range cs.MaintenanceEvents {
if iEvent.ID == k8sEvent.ID {
if *iEvent != *k8sEvent {
return false
}
for i := range iEventStatuses {
if !areEventStatusesEqual(iEventStatuses[i], cs.MaintenanceEvents[i]) {
return false
}
}

break
}
return true
}

func areEventStatusesEqual(a, b *clusterresource.ClusteredMaintenanceEventStatus) bool {
if len(a.Past) != len(b.Past) ||
len(a.InProgress) != len(b.InProgress) ||
len(a.Upcoming) != len(b.Upcoming) {
return false
}

for i := range a.Past {
if !areClusteredMaintenanceEventStatusEqual(a.InProgress[i], b.InProgress[i]) {
return false
}
}

for i := range a.InProgress {
if !areClusteredMaintenanceEventStatusEqual(a.InProgress[i], b.InProgress[i]) {
return false
}
}

for i := range a.Upcoming {
if !areClusteredMaintenanceEventStatusEqual(a.Upcoming[i], b.Upcoming[i]) {
return false
}
}
return true
}

func areClusteredMaintenanceEventStatusEqual(a, b *clusterresource.MaintenanceEventStatus) bool {
if a.ID == b.ID {
return a.Description == b.Description &&
a.ScheduledStartTime == b.ScheduledStartTime &&
a.ScheduledEndTime == b.ScheduledEndTime &&
a.ScheduledStartTimeMax == b.ScheduledStartTimeMax &&
a.ScheduledStartTimeMin == b.ScheduledStartTimeMin &&
a.IsFinalized == b.IsFinalized &&
a.StartTime == b.StartTime &&
a.EndTime == b.EndTime &&
a.Outcome == b.Outcome
}
}

Check failure on line 474 in apis/clusters/v1beta1/structs.go

View workflow job for this annotation

GitHub Actions / PRE-MAIN Build and push the Instaclustr Operator

missing return (typecheck)

func (cs *ClusterStatus) DCFromInstAPI(iDC models.DataCentre) *DataCentreStatus {
return &DataCentreStatus{
ID: iDC.ID,
Expand Down Expand Up @@ -536,28 +570,6 @@ func isCloudProviderSettingsEmpty(iDC models.DataCentre) bool {
return true
}

func (c *Cluster) CloudProviderSettingsFromInstAPIv1(iProviders []*models.ClusterProviderV1) (accountName string, settings []*CloudProviderSettings) {
for _, iProvider := range iProviders {
accountName = iProvider.AccountName
switch iProvider.Name {
case models.AWSVPC:
settings = append(settings, &CloudProviderSettings{
CustomVirtualNetworkID: iProvider.CustomVirtualNetworkID,
DiskEncryptionKey: iProvider.DiskEncryptionKey,
})
case models.GCP:
settings = append(settings, &CloudProviderSettings{
CustomVirtualNetworkID: iProvider.CustomVirtualNetworkID,
})
case models.AZUREAZ:
settings = append(settings, &CloudProviderSettings{
ResourceGroup: iProvider.ResourceGroup,
})
}
}
return
}

func (cs *ClusterStatus) NodesFromInstAPI(iNodes []*models.Node) (nodes []*Node) {
for _, iNode := range iNodes {
nodes = append(nodes, &Node{
Expand Down
Loading

0 comments on commit bcce9bb

Please sign in to comment.