From ba750357eafce5ed60b9c9f58d425a2f9d1ce1f2 Mon Sep 17 00:00:00 2001 From: Bychkov Date: Tue, 3 Oct 2023 16:48:11 +0300 Subject: [PATCH] cluster settings for Opensearch --- apis/clusters/v1beta1/cassandra_types.go | 2 + apis/clusters/v1beta1/redis_types.go | 7 ++- apis/clusters/v1beta1/structs.go | 16 +++--- apis/clusters/v1beta1/validation.go | 4 ++ controllers/clusters/cassandra_controller.go | 4 ++ controllers/clusters/redis_controller.go | 58 +++++++++++++------- pkg/models/apiv2.go | 4 +- pkg/models/cassandra_apiv2.go | 1 + pkg/models/redis_apiv2.go | 1 + 9 files changed, 66 insertions(+), 31 deletions(-) diff --git a/apis/clusters/v1beta1/cassandra_types.go b/apis/clusters/v1beta1/cassandra_types.go index b325c9a1f..060a043fd 100644 --- a/apis/clusters/v1beta1/cassandra_types.go +++ b/apis/clusters/v1beta1/cassandra_types.go @@ -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, @@ -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), } } diff --git a/apis/clusters/v1beta1/redis_types.go b/apis/clusters/v1beta1/redis_types.go index 5a8da8f80..bd384d6b2 100644 --- a/apis/clusters/v1beta1/redis_types.go +++ b/apis/clusters/v1beta1/redis_types.go @@ -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"` } @@ -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(), } @@ -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, } } diff --git a/apis/clusters/v1beta1/structs.go b/apis/clusters/v1beta1/structs.go index 0725e137e..bffb39329 100644 --- a/apis/clusters/v1beta1/structs.go +++ b/apis/clusters/v1beta1/structs.go @@ -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 { diff --git a/apis/clusters/v1beta1/validation.go b/apis/clusters/v1beta1/validation.go index 8575a3f2b..6eb8e742f 100644 --- a/apis/clusters/v1beta1/validation.go +++ b/apis/clusters/v1beta1/validation.go @@ -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) diff --git a/controllers/clusters/cassandra_controller.go b/controllers/clusters/cassandra_controller.go index 3368b66a6..c5b17efd8 100644 --- a/controllers/clusters/cassandra_controller.go +++ b/controllers/clusters/cassandra_controller.go @@ -320,6 +320,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", diff --git a/controllers/clusters/redis_controller.go b/controllers/clusters/redis_controller.go index b8101bd3b..315c4273d 100644 --- a/controllers/clusters/redis_controller.go +++ b/controllers/clusters/redis_controller.go @@ -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() @@ -246,17 +248,6 @@ func (r *RedisReconciler) handleCreateCluster( "Cluster backups check job is started", ) - logger.Info( - "Redis resource has been created", - "cluster name", redis.Name, - "cluster ID", redis.Status.ID, - "kind", redis.Kind, - "api version", redis.APIVersion, - "namespace", redis.Namespace, - ) - - // Adding users is allowed when the cluster is running. - if redis.Spec.UserRefs != nil { err = r.startUsersCreationJob(redis) @@ -327,6 +318,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, diff --git a/pkg/models/apiv2.go b/pkg/models/apiv2.go index 548e9b1a6..74ee982a5 100644 --- a/pkg/models/apiv2.go +++ b/pkg/models/apiv2.go @@ -90,7 +90,7 @@ type Node struct { type TwoFactorDelete struct { ConfirmationPhoneNumber string `json:"confirmationPhoneNumber,omitempty"` - ConfirmationEmail string `json:"confirmationEmail"` + ConfirmationEmail string `json:"confirmationEmail,omitempty"` } type NodeReloadStatus struct { @@ -125,7 +125,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 diff --git a/pkg/models/cassandra_apiv2.go b/pkg/models/cassandra_apiv2.go index 475485910..7a7f09502 100644 --- a/pkg/models/cassandra_apiv2.go +++ b/pkg/models/cassandra_apiv2.go @@ -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 { diff --git a/pkg/models/redis_apiv2.go b/pkg/models/redis_apiv2.go index c3c5ed8dd..1664024ec 100644 --- a/pkg/models/redis_apiv2.go +++ b/pkg/models/redis_apiv2.go @@ -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 {