Skip to content

Commit

Permalink
clusterreference for clusterresources were implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
tengu-alt authored and taaraora committed Jan 3, 2024
1 parent a57e86c commit 1105797
Show file tree
Hide file tree
Showing 83 changed files with 1,236 additions and 326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
// AWSEndpointServicePrincipalSpec defines the desired state of AWSEndpointServicePrincipal
type AWSEndpointServicePrincipalSpec struct {
// The ID of the cluster data center
ClusterDataCenterID string `json:"clusterDataCenterId"`
ClusterDataCenterID string `json:"clusterDataCenterId,omitempty"`
ClusterRef *ClusterRef `json:"clusterRef,omitempty"`

// The Instaclustr ID of the AWS endpoint service
EndPointServiceID string `json:"endPointServiceId,omitempty"`
Expand All @@ -36,7 +37,8 @@ type AWSEndpointServicePrincipalSpec struct {
// AWSEndpointServicePrincipalStatus defines the observed state of AWSEndpointServicePrincipal
type AWSEndpointServicePrincipalStatus struct {
// The Instaclustr ID of the IAM Principal ARN
ID string `json:"id,omitempty"`
ID string `json:"id,omitempty"`
CDCID string `json:"cdcId,omitempty"`

// The Instaclustr ID of the AWS endpoint service
EndPointServiceID string `json:"endPointServiceId,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ var principalArnPattern, _ = regexp.Compile(`^arn:aws:iam::[0-9]{12}:(root$|user
func (r *AWSEndpointServicePrincipal) ValidateCreate() error {
awsendpointserviceprincipallog.Info("validate create", "name", r.Name)

if r.Spec.ClusterDataCenterID == "" ||
r.Spec.PrincipalARN == "" {
return fmt.Errorf("spec.clusterDataCenterId and spec.principalArn should be filled")
if (r.Spec.ClusterDataCenterID == "" && r.Spec.ClusterRef == nil) ||
(r.Spec.ClusterDataCenterID != "" && r.Spec.ClusterRef != nil) {
return fmt.Errorf("only one of the following fields should be specified: dataCentreId, clusterRef")
}

if !principalArnPattern.MatchString(r.Spec.PrincipalARN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (r *AWSSecurityGroupFirewallRule) ValidateCreate() error {
r.Spec.Type, models.BundleTypes)
}

if (r.Spec.ClusterID == "" && r.Spec.ClusterRef == nil) ||
(r.Spec.ClusterID != "" && r.Spec.ClusterRef != nil) {
return fmt.Errorf("only one of dataCenter ID and cluster reference fields should be specified")
}

return nil
}

Expand Down
10 changes: 6 additions & 4 deletions apis/clusterresources/v1beta1/awsvpcpeering_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

// AWSVPCPeeringSpec defines the desired state of AWSVPCPeering
type AWSVPCPeeringSpec struct {
VPCPeeringSpec `json:",inline"`
PeeringSpec `json:",inline"`
PeerAWSAccountID string `json:"peerAwsAccountId"`
PeerVPCID string `json:"peerVpcId"`
PeerRegion string `json:"peerRegion,omitempty"`
Expand Down Expand Up @@ -128,9 +128,11 @@ 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 aws.DataCentreID != "" {
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) {
Expand Down
5 changes: 3 additions & 2 deletions apis/clusterresources/v1beta1/awsvpcpeering_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ 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.DataCentreID == "" && r.Spec.ClusterRef == nil) ||
(r.Spec.DataCentreID != "" && r.Spec.ClusterRef != nil) {
return fmt.Errorf("only one of the following fields should be specified: dataCentreId, clusterRef")
}

if r.Spec.PeerSubnets == nil {
Expand Down
2 changes: 1 addition & 1 deletion apis/clusterresources/v1beta1/azurevnetpeering_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

// AzureVNetPeeringSpec defines the desired state of AzureVNetPeering
type AzureVNetPeeringSpec struct {
VPCPeeringSpec `json:",inline"`
PeeringSpec `json:",inline"`
PeerResourceGroup string `json:"peerResourceGroup"`
PeerSubscriptionID string `json:"peerSubscriptionId"`
PeerADObjectID string `json:"peerAdObjectId,omitempty"`
Expand Down
5 changes: 3 additions & 2 deletions apis/clusterresources/v1beta1/azurevnetpeering_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ 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.DataCentreID == "" && r.Spec.ClusterRef == nil) ||
(r.Spec.DataCentreID != "" && r.Spec.ClusterRef != nil) {
return fmt.Errorf("only one of the following fields should be specified: dataCentreId, clusterRef")
}

if r.Spec.PeerSubnets == nil {
Expand Down
5 changes: 3 additions & 2 deletions apis/clusterresources/v1beta1/clusterbackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ import (

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

// ClusterBackupStatus defines the observed state of ClusterBackup
type ClusterBackupStatus struct {
ClusterID string `json:"clusterId,omitempty"`
OperationStatus string `json:"operationStatus,omitempty"`
Progress string `json:"progress,omitempty"`
Start int `json:"start,omitempty"`
Expand Down
10 changes: 9 additions & 1 deletion apis/clusterresources/v1beta1/clusterbackup_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,19 @@ var _ webhook.Validator = &ClusterBackup{}
func (r *ClusterBackup) ValidateCreate() error {
clusterbackuplog.Info("validate create", "name", r.Name)

_, ok := models.ClusterKindsMap[r.Spec.ClusterKind]
_, ok := models.ClusterKindsMap[r.Spec.ClusterRef.ClusterKind]
if !ok {
return models.ErrUnsupportedBackupClusterKind
}

if r.Spec.ClusterRef.Name != "" && r.Spec.ClusterRef.Namespace == "" {
return models.ErrEmptyNamespace
}

if r.Spec.ClusterRef.Namespace != "" && r.Spec.ClusterRef.Name == "" {
return models.ErrEmptyName
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (fr *ClusterNetworkFirewallRule) ValidateCreate() error {
fr.Spec.Type, models.BundleTypes)
}

if (fr.Spec.ClusterID == "" && fr.Spec.ClusterRef == nil) ||
(fr.Spec.ClusterID != "" && fr.Spec.ClusterRef != nil) {
return fmt.Errorf("only one of the following fields should be specified: clusterId, clusterRef")
}

return nil
}

Expand Down
22 changes: 19 additions & 3 deletions apis/clusterresources/v1beta1/exclusionwindow_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (

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

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

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -70,3 +72,17 @@ func (r *ExclusionWindow) NewPatch() client.Patch {
old := r.DeepCopy()
return client.MergeFrom(old)
}

func (e *ExclusionWindowSpec) validateUpdate(old ExclusionWindowSpec) bool {
if e.DayOfWeek != old.DayOfWeek ||
e.ClusterID != old.ClusterID ||
e.DurationInHours != old.DurationInHours ||
e.StartHour != old.StartHour ||
(e.ClusterRef != nil && old.ClusterRef == nil) ||
(e.ClusterRef == nil && old.ClusterRef != nil) ||
(e.ClusterRef != nil && *e.ClusterRef != *old.ClusterRef) {
return false
}

return true
}
7 changes: 6 additions & 1 deletion apis/clusterresources/v1beta1/exclusionwindow_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ var _ webhook.Validator = &ExclusionWindow{}
func (r *ExclusionWindow) ValidateCreate() error {
exclusionwindowlog.Info("validate create", "name", r.Name)

if (r.Spec.ClusterID == "" && r.Spec.ClusterRef == nil) ||
(r.Spec.ClusterID != "" && r.Spec.ClusterRef != nil) {
return fmt.Errorf("only one of the following fields should be specified: clusterId, clusterRef")
}

if !validation.Contains(r.Spec.DayOfWeek, models.DaysOfWeek) {
return fmt.Errorf("%v, available values: %v",
models.ErrIncorrectDayOfWeek, models.DaysOfWeek)
Expand All @@ -67,7 +72,7 @@ func (r *ExclusionWindow) ValidateUpdate(old runtime.Object) error {
exclusionwindowlog.Info("validate update", "name", r.Name)
oldWindow := old.(*ExclusionWindow)

if r.Spec != oldWindow.Spec {
if !r.Spec.validateUpdate(oldWindow.Spec) {
return models.ErrImmutableSpec
}

Expand Down
2 changes: 1 addition & 1 deletion apis/clusterresources/v1beta1/gcpvpcpeering_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

// GCPVPCPeeringSpec defines the desired state of GCPVPCPeering
type GCPVPCPeeringSpec struct {
VPCPeeringSpec `json:",inline"`
PeeringSpec `json:",inline"`
PeerVPCNetworkName string `json:"peerVpcNetworkName"`
PeerProjectID string `json:"peerProjectId"`
}
Expand Down
5 changes: 3 additions & 2 deletions apis/clusterresources/v1beta1/gcpvpcpeering_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func (r *GCPVPCPeering) ValidateCreate() error {
return fmt.Errorf("peer Project ID is empty")
}

if r.Spec.DataCentreID == "" {
return fmt.Errorf("dataCentre ID is empty")
if (r.Spec.DataCentreID == "" && r.Spec.ClusterRef == nil) ||
(r.Spec.DataCentreID != "" && r.Spec.ClusterRef != nil) {
return fmt.Errorf("only one of the following fields should be specified: dataCentreId, clusterRef")
}

if r.Spec.PeerSubnets == nil {
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: 7 additions & 5 deletions apis/clusterresources/v1beta1/opensearchegressrules_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ import (
)

type OpenSearchEgressRulesSpec struct {
ClusterID string `json:"clusterId"`
OpenSearchBindingID string `json:"openSearchBindingId"`
Source string `json:"source"`
Type string `json:"type,omitempty"`
ClusterID string `json:"clusterId,omitempty"`
ClusterRef *ClusterRef `json:"clusterRef,omitempty"`
OpenSearchBindingID string `json:"openSearchBindingId"`
Source string `json:"source"`
Type string `json:"type,omitempty"`
}

type OpenSearchEgressRulesStatus struct {
ID string `json:"id,omitempty"`
ID string `json:"id,omitempty"`
ClusterID string `json:"clusterId,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (r *OpenSearchEgressRules) ValidateCreate() error {
return fmt.Errorf("the type should be equal to one of the options: %q , got: %q", destinationTypes, r.Spec.Type)
}

if (r.Spec.ClusterID == "" && r.Spec.ClusterRef == nil) ||
(r.Spec.ClusterID != "" && r.Spec.ClusterRef != nil) {
return fmt.Errorf("only one of the following fields should be specified: clusterId, clusterRef")
}

return nil
}

Expand All @@ -78,7 +83,7 @@ func (r *OpenSearchEgressRules) ValidateUpdate(old runtime.Object) error {
return r.ValidateCreate()
}

if r.Spec != oldRules.Spec {
if r.Spec != oldRules.Spec && r.Generation != oldRules.Generation {
return models.ErrImmutableSpec
}

Expand Down
30 changes: 25 additions & 5 deletions apis/clusterresources/v1beta1/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ import (
"encoding/json"

"github.com/instaclustr/operator/pkg/apiextensions"

"k8s.io/apimachinery/pkg/types"
)

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

type PeeringStatus struct {
ID string `json:"id,omitempty"`
StatusCode string `json:"statusCode,omitempty"`
Name string `json:"name,omitempty"`
FailureReason string `json:"failureReason,omitempty"`
CDCID string `json:"cdcId,omitempty"`
}

type PatchRequest struct {
Expand All @@ -41,14 +45,16 @@ type PatchRequest struct {
}

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

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

type immutablePeeringFields struct {
Expand All @@ -57,3 +63,17 @@ type immutablePeeringFields struct {

// +kubebuilder:object:generate:=false
type SecretReference = apiextensions.ObjectReference

type ClusterRef struct {
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
ClusterKind string `json:"clusterKind,omitempty"`
CDCName string `json:"cdcName,omitempty"`
}

func (r *ClusterRef) AsNamespacedName() types.NamespacedName {
return types.NamespacedName{
Name: r.Name,
Namespace: r.Namespace,
}
}
Loading

0 comments on commit 1105797

Please sign in to comment.