Skip to content

Commit

Permalink
added primaryTargetCadence to Cadence spec and secondaryTargetCadence…
Browse files Browse the repository at this point in the history
… to its status
  • Loading branch information
worryg0d committed Aug 10, 2023
1 parent 4f54849 commit 8506bf7
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 17 deletions.
53 changes: 52 additions & 1 deletion apis/clusters/v1beta1/cadence_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type CadenceSpec struct {
StandardProvisioning []*StandardProvisioning `json:"standardProvisioning,omitempty"`
SharedProvisioning []*SharedProvisioning `json:"sharedProvisioning,omitempty"`
PackagedProvisioning []*PackagedProvisioning `json:"packagedProvisioning,omitempty"`
TargetPrimaryCadence []*TargetCadence `json:"targetPrimaryCadence,omitempty"`
}

type AWSArchival struct {
Expand Down Expand Up @@ -110,14 +111,20 @@ type TargetOpenSearch struct {
DependencyVPCType string `json:"dependencyVpcType"`
}

type TargetCadence struct {
DependencyCDCID string `json:"dependencyCdcId"`
DependencyVPCType string `json:"dependencyVpcType"`
}

type AdvancedVisibility struct {
TargetKafka *TargetKafka `json:"targetKafka"`
TargetOpenSearch *TargetOpenSearch `json:"targetOpenSearch"`
}

// CadenceStatus defines the observed state of Cadence
type CadenceStatus struct {
ClusterStatus `json:",inline"`
ClusterStatus `json:",inline"`
TargetSecondaryCadence []*TargetCadence `json:"targetSecondaryCadence,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -180,6 +187,7 @@ func (cs *CadenceSpec) ToInstAPI(ctx context.Context, k8sClient client.Client) (
AWSArchival: awsArchival,
SharedProvisioning: sharedProvisioning,
StandardProvisioning: standardProvisioning,
TargetPrimaryCadence: cs.TargetCadenceToInstAPI(),
}, nil
}

Expand Down Expand Up @@ -305,6 +313,18 @@ func (cd *CadenceDataCentre) privateLinkToInstAPI() (iPrivateLink []*models.Priv
return
}

func (cs *CadenceSpec) TargetCadenceToInstAPI() []*models.TargetCadence {
var targets []*models.TargetCadence
for _, target := range cs.TargetPrimaryCadence {
targets = append(targets, &models.TargetCadence{
DependencyCDCID: target.DependencyCDCID,
DependencyVPCType: target.DependencyVPCType,
})
}

return targets
}

func (cs *CadenceSpec) DCsToInstAPI() (iDCs []*models.CadenceDataCentre) {
for _, dc := range cs.DataCentres {
iDCs = append(iDCs, dc.ToInstAPI())
Expand Down Expand Up @@ -385,9 +405,22 @@ func (cs *CadenceStatus) FromInstAPI(iCad *models.CadenceCluster) CadenceStatus
CurrentClusterOperationStatus: iCad.CurrentClusterOperationStatus,
MaintenanceEvents: cs.MaintenanceEvents,
},
TargetSecondaryCadence: cs.SecondaryTargetsFromInstAPI(iCad),
}
}

func (cs *CadenceStatus) SecondaryTargetsFromInstAPI(iCad *models.CadenceCluster) []*TargetCadence {
var targets []*TargetCadence
for _, target := range iCad.TargetSecondaryCadence {
targets = append(targets, &TargetCadence{
DependencyCDCID: target.DependencyCDCID,
DependencyVPCType: target.DependencyVPCType,
})
}

return targets
}

func (cs *CadenceStatus) DCsFromInstAPI(iDCs []*models.CadenceDataCentre) (dcs []*DataCentreStatus) {
for _, iDC := range iDCs {
dcs = append(dcs, cs.ClusterStatus.DCFromInstAPI(iDC.DataCentre))
Expand Down Expand Up @@ -457,6 +490,11 @@ func (cs *CadenceSpec) validateUpdate(oldSpec CadenceSpec) error {
return err
}

err = cs.validateTargetsPrimaryCadence(&oldSpec)
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -554,6 +592,19 @@ func (cs *CadenceSpec) validatePackagedProvisioning(old []*PackagedProvisioning)
return nil
}

func (cs *CadenceSpec) validateTargetsPrimaryCadence(old *CadenceSpec) error {
if len(cs.TargetPrimaryCadence) != len(old.TargetPrimaryCadence) {
return fmt.Errorf("targetPrimaryCadence is immutable")
}

if len(old.TargetPrimaryCadence) > 0 &&
*cs.TargetPrimaryCadence[0] != *old.TargetPrimaryCadence[0] {
return fmt.Errorf("targetPrimaryCadence is immutable")
}

return nil
}

func (cdc *CadenceDataCentre) newImmutableFields() *immutableCadenceDCFields {
return &immutableCadenceDCFields{
immutableDC: immutableDC{
Expand Down
4 changes: 4 additions & 0 deletions apis/clusters/v1beta1/cadence_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ func (cv *cadenceValidator) ValidateCreate(ctx context.Context, obj runtime.Obje
}
}

if len(c.Spec.TargetPrimaryCadence) > 1 {
return fmt.Errorf("targetPrimaryCadence array must consist of <= 1 elements")
}

if len(c.Spec.DataCentres) == 0 {
return fmt.Errorf("data centres field is empty")
}
Expand Down
37 changes: 37 additions & 0 deletions apis/clusters/v1beta1/zz_generated.deepcopy.go

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

24 changes: 24 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_cadences.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ spec:
- targetCassandra
type: object
type: array
targetPrimaryCadence:
items:
properties:
dependencyCdcId:
type: string
dependencyVpcType:
type: string
required:
- dependencyCdcId
- dependencyVpcType
type: object
type: array
twoFactorDelete:
items:
properties:
Expand Down Expand Up @@ -335,6 +347,18 @@ spec:
type: object
state:
type: string
targetSecondaryCadence:
items:
properties:
dependencyCdcId:
type: string
dependencyVpcType:
type: string
required:
- dependencyCdcId
- dependencyVpcType
type: object
type: array
twoFactorDeleteEnabled:
type: boolean
type: object
Expand Down
6 changes: 4 additions & 2 deletions config/samples/clusters_v1beta1_cadence.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ spec:
nodesNumber: 2
clientEncryption: false
slaTier: "NON_PRODUCTION"
useCadenceWebAuth: true

useCadenceWebAuth: false
# targetPrimaryCadence:
# - dependencyCdcId: "cce79be3-7f41-4cad-837c-86d3d8b4be77"
# dependencyVpcType: "SEPARATE_VPC"
34 changes: 32 additions & 2 deletions controllers/clusters/cadence_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ func (r *CadenceReconciler) newWatchStatusJob(cadence *v1beta1.Cadence) schedule
if k8serrors.IsNotFound(err) {
l.Info("Resource is not found in the k8s cluster. Closing Instaclustr status sync.",
"namespaced name", namespacedName)
r.Scheduler.RemoveJob(cadence.GetJobID(scheduler.StatusChecker))
go r.Scheduler.RemoveJob(cadence.GetJobID(scheduler.StatusChecker))
return nil
}
if err != nil {
Expand Down Expand Up @@ -839,7 +839,9 @@ func (r *CadenceReconciler) newWatchStatusJob(cadence *v1beta1.Cadence) schedule
return err
}

if !areStatusesEqual(&iCadence.Status.ClusterStatus, &cadence.Status.ClusterStatus) {
cadenceTargetsEqual := areSecondaryCadenceTargetsEqual(cadence.Status.TargetSecondaryCadence, iCadence.Status.TargetSecondaryCadence)
if !areStatusesEqual(&iCadence.Status.ClusterStatus, &cadence.Status.ClusterStatus) ||
!cadenceTargetsEqual {
l.Info("Updating Cadence cluster status",
"new status", iCadence.Status.ClusterStatus,
"old status", cadence.Status.ClusterStatus,
Expand Down Expand Up @@ -874,6 +876,16 @@ func (r *CadenceReconciler) newWatchStatusJob(cadence *v1beta1.Cadence) schedule
return err
}
}

if !cadenceTargetsEqual {
patch := cadence.NewPatch()
cadence.Status.TargetSecondaryCadence = iCadence.Status.TargetSecondaryCadence
err = r.Status().Patch(context.Background(), cadence, patch)
if err != nil {
l.Error(err, "Cannot patch cadence status with new target secondary cadence")
return err
}
}
}

if iCadence.Status.CurrentClusterOperationStatus == models.NoOperation &&
Expand Down Expand Up @@ -1179,6 +1191,24 @@ func (r *CadenceReconciler) updateDescriptionAndTwoFactorDelete(cadence *v1beta1
return nil
}

func areSecondaryCadenceTargetsEqual(first, second []*v1beta1.TargetCadence) bool {
for _, secondTarget := range second {
var ok bool
for _, firstTarget := range first {
if *secondTarget == *firstTarget {
ok = true
break
}
}

if !ok {
return false
}
}

return true
}

// SetupWithManager sets up the controller with the Manager.
func (r *CadenceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Expand Down
31 changes: 19 additions & 12 deletions pkg/models/cadence_apiv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ const (
)

type CadenceCluster struct {
ClusterStatus `json:",inline"`
Name string `json:"name"`
CadenceVersion string `json:"cadenceVersion"`
DataCentres []*CadenceDataCentre `json:"dataCentres"`
SharedProvisioning []*CadenceSharedProvisioning `json:"sharedProvisioning,omitempty"`
StandardProvisioning []*CadenceStandardProvisioning `json:"standardProvisioning,omitempty"`
PCIComplianceMode bool `json:"pciComplianceMode"`
TwoFactorDelete []*TwoFactorDelete `json:"twoFactorDelete,omitempty"`
UseCadenceWebAuth bool `json:"useCadenceWebAuth"`
PrivateNetworkCluster bool `json:"privateNetworkCluster"`
SLATier string `json:"slaTier"`
AWSArchival []*AWSArchival `json:"awsArchival,omitempty"`
ClusterStatus `json:",inline"`
Name string `json:"name"`
CadenceVersion string `json:"cadenceVersion"`
DataCentres []*CadenceDataCentre `json:"dataCentres"`
SharedProvisioning []*CadenceSharedProvisioning `json:"sharedProvisioning,omitempty"`
StandardProvisioning []*CadenceStandardProvisioning `json:"standardProvisioning,omitempty"`
PCIComplianceMode bool `json:"pciComplianceMode"`
TwoFactorDelete []*TwoFactorDelete `json:"twoFactorDelete,omitempty"`
UseCadenceWebAuth bool `json:"useCadenceWebAuth"`
PrivateNetworkCluster bool `json:"privateNetworkCluster"`
SLATier string `json:"slaTier"`
AWSArchival []*AWSArchival `json:"awsArchival,omitempty"`
TargetPrimaryCadence []*TargetCadence `json:"targetPrimaryCadence,omitempty"`
TargetSecondaryCadence []*TargetCadence `json:"targetSecondaryCadence,omitempty"`
}

type CadenceDataCentre struct {
Expand Down Expand Up @@ -75,6 +77,11 @@ type TargetCassandra struct {
DependencyVPCType string `json:"dependencyVpcType"`
}

type TargetCadence struct {
DependencyCDCID string `json:"dependencyCdcId"`
DependencyVPCType string `json:"dependencyVpcType"`
}

type AWSArchival struct {
ArchivalS3Region string `json:"archivalS3Region,omitempty"`
AWSAccessKeyID string `json:"awsAccessKeyId,omitempty"`
Expand Down

0 comments on commit 8506bf7

Please sign in to comment.