Skip to content

Commit

Permalink
issue-676, added immutability for replicationFactor and replicaNodes …
Browse files Browse the repository at this point in the history
…for redis resource
  • Loading branch information
Bohdan Siryk authored and Bohdan Siryk committed Jan 23, 2024
1 parent 8ff29e9 commit 9192f11
Show file tree
Hide file tree
Showing 21 changed files with 152 additions and 45 deletions.
7 changes: 5 additions & 2 deletions apis/clusters/v1beta1/redis_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ import (
)

type RedisDataCentre struct {
DataCentre `json:",inline"`
DataCentre `json:",inline"`

MasterNodes int `json:"masterNodes"`

//+kubebuilder:validation:Minimum:=0
//+kubebuilder:validation:Maximum:=5
// ReplicationFactor defines how many replica nodes (aka nodesNumber) should be created for each master node
// (e.a. if there are 3 masterNodes and replicationFactor 1 then it creates 1 replicaNode for each accordingly).
ReplicationFactor int `json:"replicationFactor,omitempty"`

//+kubebuilder:validation:MaxItems:=1
Expand Down Expand Up @@ -303,7 +306,7 @@ func (rs *RedisStatus) DCsFromInstAPI(iDCs []*models.RedisDataCentre) (dcs []*Da
for _, iDC := range iDCs {
dc := rs.ClusterStatus.DCFromInstAPI(iDC.DataCentre)
dc.PrivateLink = privateLinkStatusesFromInstAPI(iDC.PrivateLink)

dc.NodesNumber += iDC.MasterNodes
dcs = append(dcs, dc)
}
return
Expand Down
10 changes: 10 additions & 0 deletions apis/clusters/v1beta1/redis_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func (r *Redis) Default() {

for _, dataCentre := range r.Spec.DataCentres {
dataCentre.SetDefaultValues()

if dataCentre.MasterNodes != 0 {
if dataCentre.ReplicationFactor > 0 {
dataCentre.NodesNumber = dataCentre.MasterNodes * dataCentre.ReplicationFactor
} else {
dataCentre.ReplicationFactor = dataCentre.NodesNumber / dataCentre.MasterNodes
}
}
}
}

Expand Down Expand Up @@ -234,6 +242,7 @@ type specificRedisFields struct {

type immutableRedisDCFields struct {
immutableDC
ReplicationFactor int
}

func (rs *RedisSpec) ValidateUpdate(oldSpec RedisSpec) error {
Expand Down Expand Up @@ -332,6 +341,7 @@ func (rdc *RedisDataCentre) newImmutableFields() *immutableRedisDCFields {
ProviderAccountName: rdc.ProviderAccountName,
Network: rdc.Network,
},
ReplicationFactor: rdc.ReplicationFactor,
}
}

Expand Down
12 changes: 6 additions & 6 deletions apis/clusters/v1beta1/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type DataCentreStatus struct {
ID string `json:"id,omitempty"`
Status string `json:"status,omitempty"`
Nodes []*Node `json:"nodes,omitempty"`
NodeNumber int `json:"nodeNumber,omitempty"`
NodesNumber int `json:"nodesNumber,omitempty"`
EncryptionKeyID string `json:"encryptionKeyId,omitempty"`
PrivateLink PrivateLinkStatuses `json:"privateLink,omitempty"`
ResizeOperations []*ResizeOperation `json:"resizeOperations,omitempty"`
Expand Down Expand Up @@ -569,11 +569,11 @@ func areClusteredMaintenanceEventStatusEqual(a, b *clusterresource.MaintenanceEv

func (cs *ClusterStatus) DCFromInstAPI(iDC models.DataCentre) *DataCentreStatus {
return &DataCentreStatus{
Name: iDC.Name,
ID: iDC.ID,
Status: iDC.Status,
Nodes: cs.NodesFromInstAPI(iDC.Nodes),
NodeNumber: iDC.NumberOfNodes,
Name: iDC.Name,
ID: iDC.ID,
Status: iDC.Status,
Nodes: cs.NodesFromInstAPI(iDC.Nodes),
NodesNumber: iDC.NumberOfNodes,
}
}

Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/clusters.instaclustr.com_cadences.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,6 @@ spec:
type: string
name:
type: string
nodeNumber:
type: integer
nodes:
items:
properties:
Expand All @@ -380,6 +378,8 @@ spec:
type: string
type: object
type: array
nodesNumber:
type: integer
privateLink:
items:
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/clusters.instaclustr.com_cassandras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,6 @@ spec:
type: string
name:
type: string
nodeNumber:
type: integer
nodes:
items:
properties:
Expand All @@ -337,6 +335,8 @@ spec:
type: string
type: object
type: array
nodesNumber:
type: integer
privateLink:
items:
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/clusters.instaclustr.com_kafkaconnects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,6 @@ spec:
type: string
name:
type: string
nodeNumber:
type: integer
nodes:
items:
properties:
Expand All @@ -346,6 +344,8 @@ spec:
type: string
type: object
type: array
nodesNumber:
type: integer
privateLink:
items:
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/clusters.instaclustr.com_kafkas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,6 @@ spec:
type: string
name:
type: string
nodeNumber:
type: integer
nodes:
items:
properties:
Expand All @@ -354,6 +352,8 @@ spec:
type: string
type: object
type: array
nodesNumber:
type: integer
privateLink:
items:
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/clusters.instaclustr.com_opensearches.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,6 @@ spec:
type: string
name:
type: string
nodeNumber:
type: integer
nodes:
items:
properties:
Expand All @@ -316,6 +314,8 @@ spec:
type: string
type: object
type: array
nodesNumber:
type: integer
privateLink:
items:
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/clusters.instaclustr.com_postgresqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,6 @@ spec:
type: string
name:
type: string
nodeNumber:
type: integer
nodes:
items:
properties:
Expand All @@ -327,6 +325,8 @@ spec:
type: string
type: object
type: array
nodesNumber:
type: integer
privateLink:
items:
properties:
Expand Down
8 changes: 6 additions & 2 deletions config/crd/bases/clusters.instaclustr.com_redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ spec:
region:
type: string
replicationFactor:
description: ReplicationFactor defines how many replica nodes
(aka nodesNumber) should be created for each master node (e.a.
if there are 3 masterNodes and replicationFactor 1 then it
creates 1 replicaNode for each accordingly).
maximum: 5
minimum: 0
type: integer
Expand Down Expand Up @@ -285,8 +289,6 @@ spec:
type: string
name:
type: string
nodeNumber:
type: integer
nodes:
items:
properties:
Expand All @@ -308,6 +310,8 @@ spec:
type: string
type: object
type: array
nodesNumber:
type: integer
privateLink:
items:
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/clusters.instaclustr.com_zookeepers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ spec:
type: string
name:
type: string
nodeNumber:
type: integer
nodes:
items:
properties:
Expand All @@ -173,6 +171,8 @@ spec:
type: string
type: object
type: array
nodesNumber:
type: integer
privateLink:
items:
properties:
Expand Down
18 changes: 15 additions & 3 deletions controllers/clusters/cadence_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,10 +987,22 @@ func (r *CadenceReconciler) newWatchStatusJob(c *v1beta1.Cadence) scheduler.Job
}
}

if iCadence.Status.CurrentClusterOperationStatus == models.NoOperation &&
c.Annotations[models.ResourceStateAnnotation] != models.UpdatingEvent &&
equals := c.Spec.IsEqual(iCadence.Spec)

if equals && c.Annotations[models.ExternalChangesAnnotation] == models.True {
patch := c.NewPatch()
delete(c.Annotations, models.ExternalChangesAnnotation)
err := r.Patch(context.Background(), c, patch)
if err != nil {
return err
}

r.EventRecorder.Event(c, models.Normal, models.ExternalChanges,
"External changes were automatically reconciled",
)
} else if c.Status.CurrentClusterOperationStatus == models.NoOperation &&
c.Annotations[models.UpdateQueuedAnnotation] != models.True &&
!c.Spec.AreDCsEqual(iCadence.Spec.DataCentres) {
!equals {
l.Info(msgExternalChanges,
"instaclustr data", iCadence.Spec.DataCentres,
"k8s resource spec", c.Spec.DataCentres)
Expand Down
18 changes: 15 additions & 3 deletions controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,10 +849,22 @@ func (r *CassandraReconciler) newWatchStatusJob(c *v1beta1.Cassandra) scheduler.
}
}

if iCassandra.Status.CurrentClusterOperationStatus == models.NoOperation &&
c.Annotations[models.ResourceStateAnnotation] != models.UpdatingEvent &&
equals := c.Spec.IsEqual(iCassandra.Spec)

if equals && c.Annotations[models.ExternalChangesAnnotation] == models.True {
patch := c.NewPatch()
delete(c.Annotations, models.ExternalChangesAnnotation)
err := r.Patch(context.Background(), c, patch)
if err != nil {
return err
}

r.EventRecorder.Event(c, models.Normal, models.ExternalChanges,
"External changes were automatically reconciled",
)
} else if c.Status.CurrentClusterOperationStatus == models.NoOperation &&
c.Annotations[models.UpdateQueuedAnnotation] != models.True &&
!c.Spec.IsEqual(iCassandra.Spec) {
!equals {
k8sData, err := removeRedundantFieldsFromSpec(c.Spec, "userRefs")
if err != nil {
l.Error(err, "Cannot remove redundant fields from k8s Spec")
Expand Down
2 changes: 1 addition & 1 deletion controllers/clusters/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func areDataCentresEqual(a, b []*v1beta1.DataCentreStatus) bool {
}

if a[i].Status != b[i].Status ||
a[i].NodeNumber != b[i].NodeNumber ||
a[i].NodesNumber != b[i].NodesNumber ||
a[i].EncryptionKeyID != b[i].EncryptionKeyID {
return false
}
Expand Down
17 changes: 15 additions & 2 deletions controllers/clusters/kafkaconnect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,22 @@ func (r *KafkaConnectReconciler) newWatchStatusJob(kc *v1beta1.KafkaConnect) sch
}
}

if iKC.Status.CurrentClusterOperationStatus == models.NoOperation &&
equals := kc.Spec.IsEqual(iKC.Spec)

if equals && kc.Annotations[models.ExternalChangesAnnotation] == models.True {
patch := kc.NewPatch()
delete(kc.Annotations, models.ExternalChangesAnnotation)
err := r.Patch(context.Background(), kc, patch)
if err != nil {
return err
}

r.EventRecorder.Event(kc, models.Normal, models.ExternalChanges,
"External changes were automatically reconciled",
)
} else if kc.Status.CurrentClusterOperationStatus == models.NoOperation &&
kc.Annotations[models.UpdateQueuedAnnotation] != models.True &&
!kc.Spec.IsEqual(iKC.Spec) {
!equals {
k8sData, err := removeRedundantFieldsFromSpec(kc.Spec, "userRefs")
if err != nil {
l.Error(err, "Cannot remove redundant fields from k8s Spec")
Expand Down
5 changes: 3 additions & 2 deletions controllers/clusters/kafkaconnect_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ package clusters

import (
"context"
openapi "github.com/instaclustr/operator/pkg/instaclustr/mock/server/go"
"os"

openapi "github.com/instaclustr/operator/pkg/instaclustr/mock/server/go"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -103,7 +104,7 @@ var _ = Describe("Kafka Connect Controller", func() {
return false
}

return kafkaConnect.Status.DataCentres[0].NodeNumber == newKafkaConnectNodeNumbers
return kafkaConnect.Status.DataCentres[0].NodesNumber == newKafkaConnectNodeNumbers
}, timeout, interval).Should(BeTrue())
})
})
Expand Down
17 changes: 15 additions & 2 deletions controllers/clusters/opensearch_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,22 @@ func (r *OpenSearchReconciler) newWatchStatusJob(o *v1beta1.OpenSearch) schedule
}
}

if iO.Status.CurrentClusterOperationStatus == models.NoOperation &&
equals := o.Spec.IsEqual(iO.Spec)

if equals && o.Annotations[models.ExternalChangesAnnotation] == models.True {
patch := o.NewPatch()
delete(o.Annotations, models.ExternalChangesAnnotation)
err := r.Patch(context.Background(), o, patch)
if err != nil {
return err
}

r.EventRecorder.Event(o, models.Normal, models.ExternalChanges,
"External changes were automatically reconciled",
)
} else if o.Status.CurrentClusterOperationStatus == models.NoOperation &&
o.Annotations[models.UpdateQueuedAnnotation] != models.True &&
!o.Spec.IsEqual(iO.Spec) {
!equals {
k8sData, err := removeRedundantFieldsFromSpec(o.Spec, "userRefs")
if err != nil {
l.Error(err, "Cannot remove redundant fields from k8s Spec")
Expand Down
17 changes: 15 additions & 2 deletions controllers/clusters/postgresql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1284,9 +1284,22 @@ func (r *PostgreSQLReconciler) newWatchStatusJob(pg *v1beta1.PostgreSQL) schedul
}
}

if iPg.Status.CurrentClusterOperationStatus == models.NoOperation &&
equals := pg.Spec.IsEqual(iPg.Spec)

if equals && pg.Annotations[models.ExternalChangesAnnotation] == models.True {
patch := pg.NewPatch()
delete(pg.Annotations, models.ExternalChangesAnnotation)
err := r.Patch(context.Background(), pg, patch)
if err != nil {
return err
}

r.EventRecorder.Event(pg, models.Normal, models.ExternalChanges,
"External changes were automatically reconciled",
)
} else if pg.Status.CurrentClusterOperationStatus == models.NoOperation &&
pg.Annotations[models.UpdateQueuedAnnotation] != models.True &&
!pg.Spec.IsEqual(iPg.Spec) {
!equals {
k8sData, err := removeRedundantFieldsFromSpec(pg.Spec, "userRefs")
if err != nil {
l.Error(err, "Cannot remove redundant fields from k8s Spec")
Expand Down
Loading

0 comments on commit 9192f11

Please sign in to comment.