Skip to content

Commit

Permalink
cluster settings for Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
ribaraka committed Oct 6, 2023
1 parent 71e1255 commit e2401e3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 20 deletions.
2 changes: 2 additions & 0 deletions apis/clusters/v1beta1/cassandra_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func (cs *CassandraSpec) FromInstAPI(iCass *models.CassandraCluster) CassandraSp
PrivateNetworkCluster: iCass.PrivateNetworkCluster,
SLATier: iCass.SLATier,
TwoFactorDelete: cs.Cluster.TwoFactorDeleteFromInstAPI(iCass.TwoFactorDelete),
Description: iCass.Description,
},
DataCentres: cs.DCsFromInstAPI(iCass.DataCentres),
LuceneEnabled: iCass.LuceneEnabled,
Expand Down Expand Up @@ -355,6 +356,7 @@ func (cs *CassandraSpec) ToInstAPI() *models.CassandraCluster {
PCIComplianceMode: cs.PCICompliance,
TwoFactorDelete: cs.TwoFactorDeletesToInstAPI(),
BundledUseOnly: cs.BundledUseOnly,
Description: cs.Description,
ResizeSettings: resizeSettingsToInstAPI(cs.ResizeSettings),
}
}
Expand Down
7 changes: 4 additions & 3 deletions apis/clusters/v1beta1/redis_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ type RedisSpec struct {
PasswordAndUserAuth bool `json:"passwordAndUserAuth,omitempty"`
//+kubebuilder:validation:MaxItems:=2
DataCentres []*RedisDataCentre `json:"dataCentres,omitempty"`
Description string `json:"description,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`

UserRefs []*UserReference `json:"userRefs,omitempty"`
//+kubebuilder:validation:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
}
Expand Down Expand Up @@ -157,6 +157,7 @@ func (rs *RedisSpec) ToInstAPI() *models.RedisCluster {
PrivateNetworkCluster: rs.PrivateNetworkCluster,
PasswordAndUserAuth: rs.PasswordAndUserAuth,
SLATier: rs.SLATier,
Description: rs.Description,
DataCentres: rs.DCsToInstAPI(),
TwoFactorDelete: rs.TwoFactorDeletesToInstAPI(),
}
Expand Down Expand Up @@ -273,11 +274,11 @@ func (rs *RedisSpec) FromInstAPI(iRedis *models.RedisCluster) RedisSpec {
PrivateNetworkCluster: iRedis.PrivateNetworkCluster,
SLATier: iRedis.SLATier,
TwoFactorDelete: rs.Cluster.TwoFactorDeleteFromInstAPI(iRedis.TwoFactorDelete),
Description: iRedis.Description,
},
ClientEncryption: iRedis.ClientToNodeEncryption,
PasswordAndUserAuth: iRedis.PasswordAndUserAuth,
DataCentres: rs.DCsFromInstAPI(iRedis.DataCentres),
Description: rs.Description,
}
}

Expand Down
16 changes: 9 additions & 7 deletions apis/clusters/v1beta1/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,17 @@ func (c *Cluster) ClusterSettingsUpdateToInstAPI() (*models.ClusterSettings, err
return nil, models.ErrOnlyOneEntityTwoFactorDelete
}

iTFD := &models.TwoFactorDelete{}
for _, tfd := range c.TwoFactorDelete {
iTFD = tfd.ToInstAPI()
settingsToAPI := &models.ClusterSettings{}
if c.TwoFactorDelete != nil {
iTFD := &models.TwoFactorDelete{}
for _, tfd := range c.TwoFactorDelete {
iTFD = tfd.ToInstAPI()
}
settingsToAPI.TwoFactorDelete = iTFD
}
settingsToAPI.Description = c.Description

return &models.ClusterSettings{
Description: c.Description,
TwoFactorDelete: iTFD,
}, nil
return settingsToAPI, nil
}

func (c *Cluster) TwoFactorDeleteToInstAPIv1() *models.TwoFactorDeleteV1 {
Expand Down
4 changes: 4 additions & 0 deletions apis/clusters/v1beta1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (c *Cluster) ValidateCreation() error {
return fmt.Errorf("two factor delete should not have more than 1 item")
}

if c.Description != "" {
return fmt.Errorf("description is not supported yet, when create a cluster. You can add this field when the cluster is in the running state")
}

if !validation.Contains(c.SLATier, models.SLATiers) {
return fmt.Errorf("cluster SLATier %s is unavailable, available values: %v",
c.SLATier, models.SLATiers)
Expand Down
4 changes: 4 additions & 0 deletions controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ func (r *CassandraReconciler) handleUpdateCluster(

if len(cassandra.Spec.TwoFactorDelete) != 0 && len(iCassandra.Spec.TwoFactorDelete) == 0 ||
cassandra.Spec.Description != iCassandra.Spec.Description {
l.Info("Updating cluster settings",
"instaclustr description", iCassandra.Spec.Description,
"instaclustr two factor delete", iCassandra.Spec.TwoFactorDelete)

settingsToInstAPI, err := cassandra.Spec.ClusterSettingsUpdateToInstAPI()
if err != nil {
l.Error(err, "Cannot convert cluster settings to Instaclustr API",
Expand Down
47 changes: 39 additions & 8 deletions controllers/clusters/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,18 @@ func (r *RedisReconciler) handleCreateCluster(
redis.Status.ID = id
err = r.Status().Patch(ctx, redis, patch)
if err != nil {
logger.Error(err, "Cannot update Redis cluster status",
"cluster name", redis.Spec.Name,
)
r.EventRecorder.Eventf(
redis, models.Warning, models.PatchFailed,
"Cluster resource status patch is failed. Reason: %v",
err,
)
logger.Error(err, "Cannot patch Redis cluster status",
"cluster name", redis.Spec.Name)
r.EventRecorder.Eventf(redis, models.Warning, models.PatchFailed,
"Cluster resource status patch is failed. Reason: %v", err)

return models.ReconcileRequeue
}

logger.Info("Redis resource has been created",
"cluster name", redis.Name,
"cluster ID", redis.Status.ID,
"api version", redis.APIVersion)
}

patch := redis.NewPatch()
Expand Down Expand Up @@ -327,6 +329,35 @@ func (r *RedisReconciler) handleUpdateCluster(
return r.handleExternalChanges(redis, iRedis, logger)
}

if len(redis.Spec.TwoFactorDelete) != 0 && len(iRedis.Spec.TwoFactorDelete) == 0 ||
redis.Spec.Description != iRedis.Spec.Description {
logger.Info("Updating cluster settings",
"instaclustr description", iRedis.Spec.Description,
"instaclustr two factor delete", iRedis.Spec.TwoFactorDelete)

settingsToInstAPI, err := redis.Spec.ClusterSettingsUpdateToInstAPI()
if err != nil {
logger.Error(err, "Cannot convert cluster settings to Instaclustr API",
"cluster ID", redis.Status.ID,
"cluster spec", redis.Spec)
r.EventRecorder.Eventf(redis, models.Warning, models.UpdateFailed,
"Cannot update cluster settings. Reason: %v", err)

return models.ReconcileRequeue
}

err = r.API.UpdateClusterSettings(redis.Status.ID, settingsToInstAPI)
if err != nil {
logger.Error(err, "Cannot update cluster settings",
"cluster ID", redis.Status.ID, "cluster spec", redis.Spec)

r.EventRecorder.Eventf(redis, models.Warning, models.UpdateFailed,
"Cannot update cluster settings. Reason: %v", err)

return models.ReconcileRequeue
}
}

if !redis.Spec.IsEqual(iRedis.Spec) {
logger.Info("Update request to Instaclustr API has been sent",
"spec data centres", redis.Spec.DataCentres,
Expand Down
4 changes: 2 additions & 2 deletions pkg/models/apiv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type Node struct {

type TwoFactorDelete struct {
ConfirmationPhoneNumber string `json:"confirmationPhoneNumber,omitempty"`
ConfirmationEmail string `json:"confirmationEmail"`
ConfirmationEmail string `json:"confirmationEmail,omitempty"`
}

type NodeReloadStatus struct {
Expand Down Expand Up @@ -127,7 +127,7 @@ type PrivateLink struct {

type ClusterSettings struct {
Description string `json:"description"`
TwoFactorDelete *TwoFactorDelete `json:"twoFactorDelete"`
TwoFactorDelete *TwoFactorDelete `json:"twoFactorDelete,omitempty"`
}

// ResizeSettings determines how resize requests will be performed for the cluster
Expand Down
1 change: 1 addition & 0 deletions pkg/models/cassandra_apiv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type CassandraCluster struct {
TwoFactorDelete []*TwoFactorDelete `json:"twoFactorDelete,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
ResizeSettings []*ResizeSettings `json:"resizeSettings"`
Description string `json:"description"`
}

type CassandraDataCentre struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/models/redis_apiv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type RedisCluster struct {
PasswordAndUserAuth bool `json:"passwordAndUserAuth"`
TwoFactorDelete []*TwoFactorDelete `json:"twoFactorDelete,omitempty"`
SLATier string `json:"slaTier"`
Description string `json:"description"`
}

type RedisDataCentre struct {
Expand Down

0 comments on commit e2401e3

Please sign in to comment.