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 13, 2023
1 parent 8a6906b commit 518b189
Show file tree
Hide file tree
Showing 28 changed files with 1,372 additions and 633 deletions.
46 changes: 18 additions & 28 deletions apis/clusterresources/v1beta1/maintenanceevents_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,27 @@ 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"`
RescheduledEvent *MaintenanceEventReschedule `json:"rescheduled"`
}

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

type MaintenanceEventStatus struct {
Expand All @@ -48,6 +49,15 @@ type MaintenanceEventStatus struct {
ScheduledStartTimeMin string `json:"scheduledStartTimeMin,omitempty"`
ScheduledStartTimeMax string `json:"scheduledStartTimeMax,omitempty"`
IsFinalized bool `json:"isFinalized,omitempty"`
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,26 +90,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 {
Expand Down
74 changes: 58 additions & 16 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.

160 changes: 115 additions & 45 deletions apis/clusters/v1beta1/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package v1beta1
import (
"encoding/json"
"net"
"time"

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

Expand Down Expand Up @@ -90,24 +92,33 @@ 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"`
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 []*MaintenanceEvent `json:"inProgress"`
Past []*MaintenanceEvent `json:"past"`
Upcoming []*MaintenanceEvent `json:"upcoming"`
}

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"`
ScheduledStartTime int64 `json:"scheduledStartTime,omitempty"`
ScheduledEndTime int64 `json:"scheduledEndTime,omitempty"`
ScheduledStartTimeMin int64 `json:"scheduledStartTimeMin,omitempty"`
ScheduledStartTimeMax int64 `json:"scheduledStartTimeMax,omitempty"`
IsFinalized bool `json:"isFinalized,omitempty"`
StartTime int64 `json:"startTime,omitempty"`
EndTime int64 `json:"endTime,omitempty"`
Outcome string `json:"outcome,omitempty"`
}

type TwoFactorDelete struct {
Expand Down Expand Up @@ -419,23 +430,82 @@ func (c *Cluster) newImmutableFields() immutableCluster {
}
}

func (cs *ClusterStatus) AreMaintenanceEventsEqual(iEvents []*MaintenanceEvent) bool {
if len(cs.MaintenanceEvents) != len(iEvents) {
return false
func (cs *ClusterStatus) MaintenanceEventStatusFromInstAPI(iEventStatuses []*MaintenanceEvent) []*clusterresource.MaintenanceEventStatus {
var meStatuses = make([]*clusterresource.MaintenanceEventStatus, 0)
for _, iEventStatus := range iEventStatuses {
meStatuses = append(meStatuses, &clusterresource.MaintenanceEventStatus{
ID: iEventStatus.ID,
Description: iEventStatus.Description,
ScheduledStartTime: time.Unix(iEventStatus.ScheduledStartTime, 0).Format(time.RFC3339),
ScheduledEndTime: time.Unix(iEventStatus.ScheduledEndTime, 0).Format(time.RFC3339),
ScheduledStartTimeMax: time.Unix(iEventStatus.ScheduledStartTimeMax, 0).Format(time.RFC3339),
ScheduledStartTimeMin: time.Unix(iEventStatus.ScheduledStartTimeMin, 0).Format(time.RFC3339),
IsFinalized: iEventStatus.IsFinalized,
StartTime: time.Unix(iEventStatus.StartTime, 0).Format(time.RFC3339),
EndTime: time.Unix(iEventStatus.EndTime, 0).Format(time.RFC3339),
Outcome: iEventStatus.Outcome,
})
}

for _, iEvent := range iEvents {
for _, k8sEvent := range cs.MaintenanceEvents {
if iEvent.ID == k8sEvent.ID {
if *iEvent != *k8sEvent {
return false
}
return meStatuses
}

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

break
for i, iES := range iEventStatuses {
if len(iES.Upcoming) != len(cs.MaintenanceEvents[i].Upcoming) ||
len(iES.Past) != len(cs.MaintenanceEvents[i].Past) ||
len(iES.InProgress) != len(cs.MaintenanceEvents[i].InProgress) {
return false
}
for j, ip := range iES.InProgress {
if ip.ID != cs.MaintenanceEvents[i].InProgress[j].ID ||
ip.Description != cs.MaintenanceEvents[i].InProgress[j].Description ||
ip.ScheduledStartTime != cs.MaintenanceEvents[i].InProgress[j].ScheduledStartTime ||
ip.ScheduledEndTime != cs.MaintenanceEvents[i].InProgress[j].ScheduledEndTime ||
ip.ScheduledStartTimeMax != cs.MaintenanceEvents[i].InProgress[j].ScheduledStartTimeMax ||
ip.ScheduledStartTimeMin != cs.MaintenanceEvents[i].InProgress[j].ScheduledStartTimeMin ||
ip.IsFinalized != cs.MaintenanceEvents[i].InProgress[j].IsFinalized ||
ip.StartTime != cs.MaintenanceEvents[i].InProgress[j].StartTime ||
ip.EndTime != cs.MaintenanceEvents[i].InProgress[j].EndTime ||
ip.Outcome != cs.MaintenanceEvents[i].InProgress[j].Outcome {
return false
}
}
for k, p := range iES.Past {
if p.ID != cs.MaintenanceEvents[i].Past[k].ID ||
p.Description != cs.MaintenanceEvents[i].Past[k].Description ||
p.ScheduledStartTime != cs.MaintenanceEvents[i].Past[k].ScheduledStartTime ||
p.ScheduledEndTime != cs.MaintenanceEvents[i].Past[k].ScheduledEndTime ||
p.ScheduledStartTimeMax != cs.MaintenanceEvents[i].Past[k].ScheduledStartTimeMax ||
p.ScheduledStartTimeMin != cs.MaintenanceEvents[i].Past[k].ScheduledStartTimeMin ||
p.IsFinalized != cs.MaintenanceEvents[i].Past[k].IsFinalized ||
p.StartTime != cs.MaintenanceEvents[i].Past[k].StartTime ||
p.EndTime != cs.MaintenanceEvents[i].Past[k].EndTime ||
p.Outcome != cs.MaintenanceEvents[i].Past[k].Outcome {
return false
}
}
for v, u := range iES.Upcoming {
if u.ID != cs.MaintenanceEvents[i].Upcoming[v].ID ||
u.Description != cs.MaintenanceEvents[i].Upcoming[v].Description ||
u.ScheduledStartTime != cs.MaintenanceEvents[i].Upcoming[v].ScheduledStartTime ||
u.ScheduledEndTime != cs.MaintenanceEvents[i].Upcoming[v].ScheduledEndTime ||
u.ScheduledStartTimeMax != cs.MaintenanceEvents[i].Upcoming[v].ScheduledStartTimeMax ||
u.ScheduledStartTimeMin != cs.MaintenanceEvents[i].Upcoming[v].ScheduledStartTimeMin ||
u.IsFinalized != cs.MaintenanceEvents[i].Upcoming[v].IsFinalized ||
u.StartTime != cs.MaintenanceEvents[i].Upcoming[v].StartTime ||
u.EndTime != cs.MaintenanceEvents[i].Upcoming[v].EndTime ||
u.Outcome != cs.MaintenanceEvents[i].Upcoming[v].Outcome {
return false
}
}
}

return true
}

Expand Down Expand Up @@ -509,6 +579,28 @@ func (c *Cluster) CloudProviderSettingsFromInstAPI(iDC models.DataCentre) (setti
return
}

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 isCloudProviderSettingsEmpty(iDC models.DataCentre) bool {
var empty bool

Expand Down Expand Up @@ -536,28 +628,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 518b189

Please sign in to comment.