Skip to content

Commit

Permalink
clusterresources for postgres were migrated to reference pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
tengu-alt committed Oct 27, 2023
1 parent 4d5d744 commit 513188d
Show file tree
Hide file tree
Showing 63 changed files with 1,091 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func (fr *AWSSecurityGroupFirewallRule) NewPatch() client.Patch {
return client.MergeFrom(old)
}

func (fr *AWSSecurityGroupFirewallRule) AttachToCluster(id string) {
fr.Status.ClusterID = id
fr.Status.ResourceState = models.CreatingEvent
}

func (fr *AWSSecurityGroupFirewallRule) DetachFromCluster() {
fr.Status.ResourceState = models.DeletingEvent
}

func init() {
SchemeBuilder.Register(&AWSSecurityGroupFirewallRule{}, &AWSSecurityGroupFirewallRuleList{})
}
21 changes: 10 additions & 11 deletions apis/clusterresources/v1beta1/awsvpcpeering_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ func (aws *AWSVPCPeering) NewPatch() client.Patch {
return client.MergeFrom(old)
}

func (aws *AWSVPCPeering) AttachToCluster(id string) {
aws.Status.CDCID = id
aws.Status.ResourceState = models.CreatingEvent
}

func (aws *AWSVPCPeering) DetachFromCluster() {
aws.Status.ResourceState = models.DeletingEvent
}

func init() {
SchemeBuilder.Register(&AWSVPCPeering{}, &AWSVPCPeeringList{})
}

type immutableAWSVPCPeeringFields struct {
specificFields specificAWSVPCPeeringFields
peering immutablePeeringFields
}

type specificAWSVPCPeeringFields struct {
Expand All @@ -91,9 +99,6 @@ func (aws *AWSVPCPeeringSpec) newImmutableFields() *immutableAWSVPCPeeringFields
peerAWSAccountID: aws.PeerAWSAccountID,
peerRegion: aws.PeerRegion,
},
immutablePeeringFields{
DataCentreID: aws.DataCentreID,
},
}
}

Expand All @@ -106,8 +111,7 @@ func (aws *AWSVPCPeeringSpec) ValidateUpdate(oldSpec AWSVPCPeeringSpec) error {
return err
}

if newImmutableFields.peering != oldImmutableFields.peering ||
newImmutableFields.specificFields != oldImmutableFields.specificFields {
if newImmutableFields.specificFields != oldImmutableFields.specificFields {
return fmt.Errorf("cannot update immutable fields: old spec: %+v: new spec: %+v", oldSpec, aws)
}

Expand All @@ -125,11 +129,6 @@ func (aws *AWSVPCPeeringSpec) Validate(availableRegions []string) error {
return fmt.Errorf("VPC ID must begin with 'vpc-' and fit pattern: %s. %v", models.PeerVPCIDRegExp, err)
}

dataCentreIDMatched, err := regexp.Match(models.UUIDStringRegExp, []byte(aws.DataCentreID))
if !dataCentreIDMatched || err != nil {
return fmt.Errorf("data centre ID is a UUID formated string. It must fit the pattern: %s. %v", models.UUIDStringRegExp, err)
}

if !validation.Contains(aws.PeerRegion, availableRegions) {
return fmt.Errorf("AWS Region to peer: %s is unavailable, available regions: %v",
aws.PeerRegion, availableRegions)
Expand Down
4 changes: 0 additions & 4 deletions apis/clusterresources/v1beta1/awsvpcpeering_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ func (r *AWSVPCPeering) ValidateCreate() error {
return fmt.Errorf("peer AWS Account Region is empty")
}

if r.Spec.DataCentreID == "" {
return fmt.Errorf("dataCentre ID is empty")
}

if r.Spec.PeerSubnets == nil {
return fmt.Errorf("peer Subnets list is empty")
}
Expand Down
17 changes: 9 additions & 8 deletions apis/clusterresources/v1beta1/azurevnetpeering_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,20 @@ func (azure *AzureVNetPeering) NewPatch() client.Patch {
return client.MergeFrom(old)
}

func (azure *AzureVNetPeering) AttachToCluster(id string) {
azure.Status.CDCID = id
azure.Status.ResourceState = models.CreatingEvent
}

func (azure *AzureVNetPeering) DetachFromCluster() {
azure.Status.ResourceState = models.DeletingEvent
}

func init() {
SchemeBuilder.Register(&AzureVNetPeering{}, &AzureVNetPeeringList{})
}

func (azure *AzureVNetPeeringSpec) Validate() error {
dataCentreIDMatched, err := regexp.Match(models.UUIDStringRegExp, []byte(azure.DataCentreID))
if err != nil {
return err
}
if !dataCentreIDMatched {
return fmt.Errorf("data centre ID is a UUID formated string. It must fit the pattern: %s", models.UUIDStringRegExp)
}

for _, subnet := range azure.PeerSubnets {
peerSubnetMatched, err := regexp.Match(models.PeerSubnetsRegExp, []byte(subnet))
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions apis/clusterresources/v1beta1/azurevnetpeering_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ func (r *AzureVNetPeering) ValidateCreate() error {
return fmt.Errorf("peer Subscription ID is empty")
}

if r.Spec.DataCentreID == "" {
return fmt.Errorf("dataCentre ID is empty")
}

if r.Spec.PeerSubnets == nil {
return fmt.Errorf("peer Subnets list is empty")
}
Expand Down
11 changes: 10 additions & 1 deletion apis/clusterresources/v1beta1/clusterbackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

// ClusterBackupSpec defines the desired state of ClusterBackup
type ClusterBackupSpec struct {
ClusterID string `json:"clusterId"`
ClusterID string `json:"clusterId,omitempty"`
ClusterKind string `json:"clusterKind"`
}

Expand All @@ -37,6 +37,7 @@ type ClusterBackupStatus struct {
Progress string `json:"progress,omitempty"`
Start int `json:"start,omitempty"`
End int `json:"end,omitempty"`
ClusterID string `json:"clusterID,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -71,6 +72,14 @@ func (cbs *ClusterBackupStatus) UpdateStatus(instBackup *models.BackupEvent) {
cbs.Progress = fmt.Sprintf("%f", instBackup.Progress)
}

func (cb *ClusterBackup) AttachToCluster(id string) {
cb.Status.ClusterID = id
}

func (cb *ClusterBackup) DetachFromCluster() {

}

func init() {
SchemeBuilder.Register(&ClusterBackup{}, &ClusterBackupList{})
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func (fr *ClusterNetworkFirewallRule) NewPatch() client.Patch {
return client.MergeFrom(old)
}

func (fr *ClusterNetworkFirewallRule) AttachToCluster(id string) {
fr.Status.ClusterID = id
fr.Status.ResourceState = models.CreatingEvent
}

func (fr *ClusterNetworkFirewallRule) DetachFromCluster() {
fr.Status.ResourceState = models.DeletingEvent
}

func init() {
SchemeBuilder.Register(&ClusterNetworkFirewallRule{}, &ClusterNetworkFirewallRuleList{})
}
22 changes: 17 additions & 5 deletions apis/clusterresources/v1beta1/exclusionwindow_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Expand All @@ -19,11 +18,13 @@ package v1beta1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

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

// ExclusionWindowSpec defines the desired state of ExclusionWindow
type ExclusionWindowSpec struct {
ClusterID string `json:"clusterId"`
ClusterID string `json:"clusterId,omitempty"`
DayOfWeek string `json:"dayOfWeek"`
//+kubebuilder:validation:Minimum:=0
//+kubebuilder:validation:Maximum:=23
Expand All @@ -36,7 +37,9 @@ type ExclusionWindowSpec struct {

// ExclusionWindowStatus defines the observed state of ExclusionWindow
type ExclusionWindowStatus struct {
ID string `json:"id"`
ID string `json:"id,omitempty"`
ClusterID string `json:"clusterID,omitempty"`
ResourceState string `json:"resourceState,omitempty"`
}

//+kubebuilder:object:root=true
Expand All @@ -60,11 +63,20 @@ type ExclusionWindowList struct {
Items []ExclusionWindow `json:"items"`
}

func (ew *ExclusionWindow) AttachToCluster(id string) {
ew.Status.ClusterID = id
ew.Status.ResourceState = models.CreatingEvent
}

func (ew *ExclusionWindow) DetachFromCluster() {
ew.Status.ResourceState = models.DeletingEvent
}

func init() {
SchemeBuilder.Register(&ExclusionWindow{}, &ExclusionWindowList{})
}

func (r *ExclusionWindow) NewPatch() client.Patch {
old := r.DeepCopy()
func (ew *ExclusionWindow) NewPatch() client.Patch {
old := ew.DeepCopy()
return client.MergeFrom(old)
}
9 changes: 9 additions & 0 deletions apis/clusterresources/v1beta1/gcpvpcpeering_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ func (gcp *GCPVPCPeering) NewPatch() client.Patch {
return client.MergeFrom(old)
}

func (gcp *GCPVPCPeering) AttachToCluster(id string) {
gcp.Status.CDCID = id
gcp.Status.ResourceState = models.CreatingEvent
}

func (gcp *GCPVPCPeering) DetachFromCluster() {
gcp.Status.ResourceState = models.DeletingEvent
}

func init() {
SchemeBuilder.Register(&GCPVPCPeering{}, &GCPVPCPeeringList{})
}
Expand Down
1 change: 0 additions & 1 deletion apis/clusterresources/v1beta1/maintenanceevents_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

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

Expand Down
12 changes: 6 additions & 6 deletions apis/clusterresources/v1beta1/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

type VPCPeeringSpec struct {
DataCentreID string `json:"cdcId"`
DataCentreID string `json:"cdcId,omitempty"`
PeerSubnets []string `json:"peerSubnets"`
}

Expand All @@ -30,6 +30,8 @@ type PeeringStatus struct {
StatusCode string `json:"statusCode,omitempty"`
Name string `json:"name,omitempty"`
FailureReason string `json:"failureReason,omitempty"`
CDCID string `json:"cdcid,omitempty"`
ResourceState string `json:"resourceState,omitempty"`
}

type PatchRequest struct {
Expand All @@ -39,18 +41,16 @@ type PatchRequest struct {
}

type FirewallRuleSpec struct {
ClusterID string `json:"clusterId"`
ClusterID string `json:"clusterId,omitempty"`
Type string `json:"type"`
}

type FirewallRuleStatus struct {
ID string `json:"id,omitempty"`
DeferredReason string `json:"deferredReason,omitempty"`
Status string `json:"status,omitempty"`
}

type immutablePeeringFields struct {
DataCentreID string
ClusterID string `json:"clusterID,omitempty"`
ResourceState string `json:"resourceState,omitempty"`
}

type SecretReference struct {
Expand Down
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/cassandra_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type CassandraSpec struct {
PasswordAndUserAuth bool `json:"passwordAndUserAuth,omitempty"`
Spark []*Spark `json:"spark,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
UserRefs []*NamespacedName `json:"userRefs,omitempty"`
//+kubebuilder:validate:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/kafka_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type KafkaSpec struct {
KarapaceRestProxy []*KarapaceRestProxy `json:"karapaceRestProxy,omitempty"`
KarapaceSchemaRegistry []*KarapaceSchemaRegistry `json:"karapaceSchemaRegistry,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
UserRefs []*NamespacedName `json:"userRefs,omitempty"`
Kraft []*Kraft `json:"kraft,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/opensearch_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type OpenSearchSpec struct {
IndexManagementPlugin bool `json:"indexManagementPlugin,omitempty"`
AlertingPlugin bool `json:"alertingPlugin,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
UserRefs []*NamespacedName `json:"userRefs,omitempty"`
//+kubuilder:validation:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
}
Expand Down
20 changes: 15 additions & 5 deletions apis/clusters/v1beta1/postgresql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,25 @@ type PgRestoreFrom struct {
type PgSpec struct {
PgRestoreFrom *PgRestoreFrom `json:"pgRestoreFrom,omitempty"`
Cluster `json:",inline"`
DataCentres []*PgDataCentre `json:"dataCentres,omitempty"`
ClusterConfigurations map[string]string `json:"clusterConfigurations,omitempty"`
SynchronousModeStrict bool `json:"synchronousModeStrict,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
DataCentres []*PgDataCentre `json:"dataCentres,omitempty"`
ClusterConfigurations map[string]string `json:"clusterConfigurations,omitempty"`
SynchronousModeStrict bool `json:"synchronousModeStrict,omitempty"`
UserRefs []*NamespacedName `json:"userRefs,omitempty"`
ClusterResources ClusterResourceRefs `json:"clusterResources,omitempty"`
//+kubebuilder:validate:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
}

type ClusterResourceRefs struct {
ClusterBackups []*NamespacedName `json:"clusterBackups,omitempty"`
ClusterNetworkFirewallRules []*NamespacedName `json:"clusterNetworkFirewallRules,omitempty"`
AWSVPCPeerings []*NamespacedName `json:"awsVPCPeerings,omitempty"`
AWSSecurityGroupFirewallRules []*NamespacedName `json:"awsSecurityGroupFirewallRules,omitempty"`
ExclusionWindows []*NamespacedName `json:"exclusionWindows,omitempty"`
GCPVPCPeerings []*NamespacedName `json:"gcpVPCPeerings,omitempty"`
AzureVNetPeerings []*NamespacedName `json:"azureVNetPeerings,omitempty"`
}

// PgStatus defines the observed state of PostgreSQL
type PgStatus struct {
ClusterStatus `json:",inline"`
Expand Down Expand Up @@ -159,7 +170,6 @@ func (pg *PostgreSQL) NewBackupSpec(startTimestamp int) *clusterresourcesv1beta1
Finalizers: []string{models.DeletionFinalizer},
},
Spec: clusterresourcesv1beta1.ClusterBackupSpec{
ClusterID: pg.Status.ID,
ClusterKind: models.PgClusterKind,
},
}
Expand Down
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/redis_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type RedisSpec struct {
//+kubebuilder:validation:MaxItems:=2
DataCentres []*RedisDataCentre `json:"dataCentres,omitempty"`

UserRefs []*UserReference `json:"userRefs,omitempty"`
UserRefs []*NamespacedName `json:"userRefs,omitempty"`
//+kubebuilder:validation:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ func (cs *ClusterStatus) PrivateLinkStatusesEqual(iStatus *ClusterStatus) bool {
return true
}

type UserReference struct {
type NamespacedName struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
}
Loading

0 comments on commit 513188d

Please sign in to comment.