Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue-711, Avoiding cluster creation if cluster with provided name exists #732

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@
{
"path": "detect_secrets.filters.allowlist.is_line_allowlisted"
},
{
"path": "detect_secrets.filters.common.is_baseline_file",
"filename": ".secrets.baseline"
},
{
"path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
"min_level": 2
Expand Down Expand Up @@ -118,6 +114,15 @@
}
],
"results": {
".idea/workspace.xml": [
{
"type": "Base64 High Entropy String",
"filename": ".idea/workspace.xml",
"hashed_secret": "ceda554256d1b9fa42791b09d9798634f930ad04",
"is_verified": false,
"line_number": 42
}
],
"apis/clusterresources/v1beta1/cassandrauser_types.go": [
{
"type": "Secret Keyword",
Expand Down Expand Up @@ -528,14 +533,14 @@
"filename": "controllers/clusters/helpers.go",
"hashed_secret": "5ffe533b830f08a0326348a9160afafc8ada44db",
"is_verified": false,
"line_number": 122
"line_number": 119
},
{
"type": "Secret Keyword",
"filename": "controllers/clusters/helpers.go",
"hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
"is_verified": false,
"line_number": 127
"line_number": 124
}
],
"controllers/clusters/kafkaconnect_controller_test.go": [
Expand All @@ -553,7 +558,7 @@
"filename": "controllers/clusters/postgresql_controller.go",
"hashed_secret": "5ffe533b830f08a0326348a9160afafc8ada44db",
"is_verified": false,
"line_number": 1183
"line_number": 1192
}
],
"controllers/clusters/zookeeper_controller_test.go": [
Expand Down Expand Up @@ -1125,5 +1130,5 @@
}
]
},
"generated_at": "2024-03-04T13:33:17Z"
"generated_at": "2024-03-05T16:46:11Z"
}
15 changes: 13 additions & 2 deletions controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,20 @@ func (r *CassandraReconciler) createCluster(ctx context.Context, c *v1beta1.Cass
var instModel *models.CassandraCluster
var err error

if c.Spec.HasRestore() {
id, err := getClusterIDByName(r.API, models.CassandraAppType, c.Spec.Name)
if err != nil {
return err
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", c.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", c.Spec.Name)
}

switch {
case c.Spec.HasRestore():
instModel, err = r.createCassandraFromRestore(c, l)
} else {
default:
instModel, err = r.createCassandra(c, l)
}
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions controllers/clusters/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/instaclustr/operator/pkg/instaclustr"
"github.com/instaclustr/operator/pkg/models"
"github.com/instaclustr/operator/pkg/utils/dcomparison"
)
Expand Down Expand Up @@ -238,3 +239,20 @@ func incrementCIDR(cidr string) (string, error) {
incrementedIP := strings.Join(ipParts, ".")
return fmt.Sprintf("%s/%d", incrementedIP, prefixLength), nil
}

func getClusterIDByName(api instaclustr.API, appType string, name string) (string, error) {
clusters, err := api.ListClustersByName(name)
if err != nil {
return "", fmt.Errorf("failed to list clusters by name, err: %w", err)
}

if len(clusters) == 0 {
return "", nil
}

if clusters[0].Application != appType {
return "", fmt.Errorf("the cluster %s already exists, but it has other application type %s", name, clusters[0].Application)
}

return clusters[0].ID, nil
}
10 changes: 10 additions & 0 deletions controllers/clusters/kafka_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func (r *KafkaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
}

func (r *KafkaReconciler) createCluster(ctx context.Context, k *v1beta1.Kafka, l logr.Logger) error {
id, err := getClusterIDByName(r.API, models.KafkaAppType, k.Spec.Name)
if err != nil {
return err
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", k.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", k.Spec.Name)
}

l.Info("Creating cluster",
"cluster name", k.Spec.Name,
"data centres", k.Spec.DataCentres)
Expand Down
12 changes: 11 additions & 1 deletion controllers/clusters/kafkaconnect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,17 @@ func (r *KafkaConnectReconciler) mergeManagedClusterFromRef(ctx context.Context,
}

func (r *KafkaConnectReconciler) createCluster(ctx context.Context, kc *v1beta1.KafkaConnect, l logr.Logger) error {
err := r.mergeManagedClusterFromRef(ctx, kc)
id, err := getClusterIDByName(r.API, models.KafkaConnectAppType, kc.Spec.Name)
if err != nil {
return err
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", kc.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", kc.Spec.Name)
}

err = r.mergeManagedClusterFromRef(ctx, kc)
if err != nil {
return err
}
Expand Down
11 changes: 10 additions & 1 deletion controllers/clusters/opensearch_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,17 @@ func (r *OpenSearchReconciler) createOpenSearch(o *v1beta1.OpenSearch, logger lo
}

func (r *OpenSearchReconciler) createCluster(ctx context.Context, o *v1beta1.OpenSearch, logger logr.Logger) error {
id, err := getClusterIDByName(r.API, models.OpenSearchAppType, o.Spec.Name)
if err != nil {
return err
}

if id != "" {
logger.Info("Cluster with provided name already exists", "name", o.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", o.Spec.Name)
}

var instaModel *models.OpenSearchCluster
var err error

if o.Spec.HasRestore() {
instaModel, err = r.createOpenSearchFromRestore(o, logger)
Expand Down
11 changes: 10 additions & 1 deletion controllers/clusters/postgresql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,17 @@ func (r *PostgreSQLReconciler) createPostgreSQL(pg *v1beta1.PostgreSQL, l logr.L
}

func (r *PostgreSQLReconciler) createCluster(ctx context.Context, pg *v1beta1.PostgreSQL, l logr.Logger) error {
id, err := getClusterIDByName(r.API, models.PgAppType, pg.Spec.Name)
if err != nil {
return err
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", pg.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", pg.Spec.Name)
}

var instaModel *models.PGCluster
var err error

if pg.Spec.HasRestore() {
instaModel, err = r.createFromRestore(pg, l)
Expand Down
11 changes: 10 additions & 1 deletion controllers/clusters/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,17 @@ func (r *RedisReconciler) createRedis(redis *v1beta1.Redis, l logr.Logger) (*mod
}

func (r *RedisReconciler) createCluster(ctx context.Context, redis *v1beta1.Redis, l logr.Logger) error {
id, err := getClusterIDByName(r.API, models.RedisAppType, redis.Spec.Name)
if err != nil {
return err
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", redis.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", redis.Spec.Name)
}

var instaModel *models.RedisCluster
var err error

if redis.Spec.HasRestore() {
instaModel, err = r.createFromRestore(redis, l)
Expand Down
10 changes: 10 additions & 0 deletions controllers/clusters/zookeeper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ func (r *ZookeeperReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}

func (r *ZookeeperReconciler) createCluster(ctx context.Context, zook *v1beta1.Zookeeper, l logr.Logger) error {
id, err := getClusterIDByName(r.API, models.ZookeeperAppType, zook.Spec.Name)
if err != nil {
return err
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", zook.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", zook.Spec.Name)
}

l.Info("Creating zookeeper cluster",
"cluster name", zook.Spec.Name,
"data centres", zook.Spec.DataCentres)
Expand Down
7 changes: 4 additions & 3 deletions pkg/instaclustr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2112,8 +2112,9 @@ func (c *Client) UpdatePostgreSQLDefaultUserPassword(id, password string) error
return nil
}

func (c *Client) ListClusters() ([]*models.ActiveClusters, error) {
url := c.serverHostname + ClustersEndpoint
func (c *Client) ListClustersByName(name string) ([]*models.ActiveCluster, error) {
url := c.serverHostname + ClustersEndpoint + fmt.Sprintf("?search=name:%s", name)

resp, err := c.DoRequest(url, http.MethodGet, nil)
if err != nil {
return nil, err
Expand All @@ -2135,7 +2136,7 @@ func (c *Client) ListClusters() ([]*models.ActiveClusters, error) {
return nil, err
}

return response, nil
return response[0].Clusters, nil
}

func (c *Client) CreateEncryptionKey(
Expand Down
2 changes: 1 addition & 1 deletion pkg/instaclustr/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type API interface {
ResetPostgreSQLConfiguration(id, name string) error
GetCadence(id string) (*models.CadenceCluster, error)
UpdatePostgreSQLDefaultUserPassword(id, password string) error
ListClusters() ([]*models.ActiveClusters, error)
ListClustersByName(name string) ([]*models.ActiveCluster, error)
CreateEncryptionKey(encryptionKeySpec any) (*clusterresourcesv1beta1.AWSEncryptionKeyStatus, error)
GetEncryptionKeyStatus(encryptionKeyID string, encryptionKeyEndpoint string) (*clusterresourcesv1beta1.AWSEncryptionKeyStatus, error)
DeleteEncryptionKey(encryptionKeyID string) error
Expand Down
4 changes: 2 additions & 2 deletions pkg/instaclustr/mock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ func (c *mockClient) UpdateKafkaConnect(id string, kc models.KafkaConnectAPIUpda
panic("UpdateKafkaConnect: is not implemented")
}

func (c *mockClient) ListClusters() ([]*models.ActiveClusters, error) {
panic("ListClusters: is not implemented")
func (c *mockClient) ListClustersByName(name string) ([]*models.ActiveCluster, error) {
panic("ListClustersByName: is not implemented")
}

func (c *mockClient) CreateRedisUser(user *models.RedisUser) (string, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ package openapi

import (
"context"
"errors"
"net/http"
)

// AccountClusterListV2APIService is a service that implements the logic for the AccountClusterListV2APIServicer
Expand All @@ -31,8 +29,8 @@ func (s *AccountClusterListV2APIService) ClusterManagementV2DataSourcesClustersV
// TODO - update ClusterManagementV2DataSourcesClustersV2Get with the required logic for this service method.
// Add api_account_cluster_list_v2_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

// TODO: Uncomment the next line to return response Response(200, []AccountClustersV2{}) or use other options such as http.Ok ...
// return Response(200, []AccountClustersV2{}), nil

return Response(http.StatusNotImplemented, nil), errors.New("ClusterManagementV2DataSourcesClustersV2Get method not implemented")
return Response(200, []AccountClustersV2{{
AccountId: "test-account-id",
Clusters: []ClusterSummaryV2{},
}}), nil
}
Loading