Skip to content

Commit

Permalink
opensearch integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
ribaraka committed May 26, 2023
1 parent dbc3bcc commit a90c26a
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 49 deletions.
37 changes: 37 additions & 0 deletions controllers/clusters/datatest/opensearch_v1alpha1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: clusters.instaclustr.com/v1alpha1
kind: OpenSearch
metadata:
name: opensearch
spec:
alertingPlugin: false
anomalyDetectionPlugin: false
asynchronousSearchPlugin: false
clusterManagerNodes:
- dedicatedManager: false
nodeSize: SRH-DEV-t4g.small-5
dataCentres:
- cloudProvider: AWS_VPC
name: AWS_VPC_US_EAST_1
network: 10.0.0.0/16
replicationFactor: 3
privateLink: false
region: US_EAST_1
# dataNodes:
# - nodeNumber: 3
# nodeSize: SRH-DEV-t4g.small-5
icuPlugin: false
indexManagementPlugin: true
knnPlugin: false
loadBalancer: false
name: operatorOpenSearch
notificationsPlugin: false
# opensearchDashboards:
# - nodeSize: SRH-DEV-t4g.small-5
# oidcProvider: ''
# version: opensearch-dashboards:2.5.0
version: 2.5.0
pciCompliance: false
privateNetworkCluster: false
reportingPlugin: false
slaTier: NON_PRODUCTION
sqlPlugin: false
1 change: 0 additions & 1 deletion controllers/clusters/datatest/postgresql_v1alpha1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ spec:
- isPrimaryDataCentre: true
slaTier: "NON_PRODUCTION"
privateNetworkCluster: true
pciCompliance: true
synchronousModeStrict: true
# twoFactorDelete:
# - email: "emailTEST"
Expand Down
108 changes: 108 additions & 0 deletions controllers/clusters/opensearch_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package clusters

import (
"context"
"os"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"

"github.com/instaclustr/operator/apis/clusters/v1alpha1"
openapi "github.com/instaclustr/operator/pkg/instaclustr/mock/server/go"
"github.com/instaclustr/operator/pkg/models"
)

const newOpenSearchNodeSize = "SRH-DEV-t4g.small-30"

var _ = Describe("OpenSearch Controller", func() {
var (
openSearchResource v1alpha1.OpenSearch
openSearchYAML v1alpha1.OpenSearch
o = "opensearch"
ns = "default"
openSearchNS = types.NamespacedName{Name: o, Namespace: ns}
timeout = time.Second * 30
interval = time.Second * 2
)

yfile, err := os.ReadFile("datatest/opensearch_v1alpha1.yaml")
Expect(err).NotTo(HaveOccurred())

err = yaml.Unmarshal(yfile, &openSearchYAML)
Expect(err).NotTo(HaveOccurred())

openSearchObjMeta := metav1.ObjectMeta{
Name: o,
Namespace: ns,
Annotations: map[string]string{
models.ResourceStateAnnotation: models.CreatingEvent,
},
}

openSearchYAML.ObjectMeta = openSearchObjMeta

ctx := context.Background()

When("apply a OpenSearch manifest", func() {
It("should create a OpenSearch resources", func() {
Expect(k8sClient.Create(ctx, &openSearchYAML)).Should(Succeed())
By("sending OpenSearch specification to the Instaclustr API and get ID of created cluster.")

Eventually(func() bool {
if err := k8sClient.Get(ctx, openSearchNS, &openSearchResource); err != nil {
return false
}

return openSearchResource.Status.ID == openapi.CreatedID
}).Should(BeTrue())
})
})

When("changing a node size", func() {
It("should update a OpenSearch resources", func() {
Expect(k8sClient.Get(ctx, openSearchNS, &openSearchResource)).Should(Succeed())
patch := openSearchResource.NewPatch()

openSearchResource.Spec.ClusterManagerNodes[0].NodeSize = newOpenSearchNodeSize

openSearchResource.Annotations = map[string]string{models.ResourceStateAnnotation: models.UpdatingEvent}
Expect(k8sClient.Patch(ctx, &openSearchResource, patch)).Should(Succeed())

By("sending a resize request to the Instaclustr API. And when the resize is completed, " +
"the status job get new data from the InstAPI and update it in k8s OpenSearch resource")

Eventually(func() bool {
if err := k8sClient.Get(ctx, openSearchNS, &openSearchResource); err != nil {
return false
}

return openSearchResource.Spec.ClusterManagerNodes[0].NodeSize == newOpenSearchNodeSize
}, timeout, interval).Should(BeTrue())
})
})

When("delete the OpenSearch resource", func() {
It("should send delete request to the Instaclustr API", func() {
Expect(k8sClient.Get(ctx, openSearchNS, &openSearchResource)).Should(Succeed())

openSearchResource.Annotations = map[string]string{models.ResourceStateAnnotation: models.DeletingEvent}

Expect(k8sClient.Delete(ctx, &openSearchResource)).Should(Succeed())

By("sending delete request to Instaclustr API")
Eventually(func() bool {
err := k8sClient.Get(ctx, openSearchNS, &openSearchResource)
if err != nil && !k8serrors.IsNotFound(err) {
return false
}

return true
}, timeout, interval).Should(BeTrue())
})
})
})
47 changes: 25 additions & 22 deletions controllers/clusters/postgresql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func (r *PostgreSQLReconciler) HandleCreateCluster(
pg *clustersv1alpha1.PostgreSQL,
logger logr.Logger,
) reconcile.Result {
logger = logger.WithName("PostgreSQL creation event")

var id string
var err error

Expand Down Expand Up @@ -238,6 +240,22 @@ func (r *PostgreSQLReconciler) HandleCreateCluster(
)
}

err = r.Status().Patch(ctx, pg, patch)
if err != nil {
logger.Error(err, "Cannot patch PostgreSQL resource status",
"cluster name", pg.Spec.Name,
"status", pg.Status,
)

r.EventRecorder.Eventf(
pg, models.Warning, models.PatchFailed,
"Cluster resource status patch is failed. Reason: %v",
err,
)

return models.ReconcileRequeue
}

logger.Info(
"PostgreSQL resource has been created",
"cluster name", pg.Name,
Expand All @@ -248,36 +266,17 @@ func (r *PostgreSQLReconciler) HandleCreateCluster(
)
}

err = r.Status().Patch(ctx, pg, patch)
if err != nil {
logger.Error(err, "Cannot patch PostgreSQL resource status",
"cluster name", pg.Spec.Name,
"status", pg.Status,
)

r.EventRecorder.Eventf(
pg, models.Warning, models.PatchFailed,
"Cluster resource status patch is failed. Reason: %v",
err,
)

return models.ReconcileRequeue
}

controllerutil.AddFinalizer(pg, models.DeletionFinalizer)

err = r.Patch(ctx, pg, patch)
if err != nil {
logger.Error(err, "Cannot patch PostgreSQL resource status",
logger.Error(err, "Cannot patch PostgreSQL resource",
"cluster name", pg.Spec.Name,
"status", pg.Status,
)
"status", pg.Status)

r.EventRecorder.Eventf(
pg, models.Warning, models.PatchFailed,
"Cluster resource patch is failed. Reason: %v",
err,
)
"Cluster resource patch is failed. Reason: %v", err)

return models.ReconcileRequeue
}
Expand Down Expand Up @@ -332,6 +331,8 @@ func (r *PostgreSQLReconciler) HandleUpdateCluster(
pg *clustersv1alpha1.PostgreSQL,
logger logr.Logger,
) reconcile.Result {
logger = logger.WithName("PostgreSQL update event")

iData, err := r.API.GetPostgreSQL(pg.Status.ID)
if err != nil {
logger.Error(
Expand Down Expand Up @@ -542,6 +543,8 @@ func (r *PostgreSQLReconciler) HandleDeleteCluster(
pg *clustersv1alpha1.PostgreSQL,
logger logr.Logger,
) reconcile.Result {
logger = logger.WithName("PostgreSQL deletion event")

_, err := r.API.GetPostgreSQL(pg.Status.ID)
if err != nil && !errors.Is(err, instaclustr.NotFound) {
logger.Error(err, "Cannot get PostgreSQL cluster status",
Expand Down
5 changes: 4 additions & 1 deletion controllers/clusters/postgresql_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ = Describe("PostgreSQL Controller", func() {
p = "postgresql"
ns = "default"
postgresqlNS = types.NamespacedName{Name: p, Namespace: ns}
timeout = time.Second * 60
timeout = time.Second * 20
interval = time.Second * 2
)

Expand Down Expand Up @@ -110,3 +110,6 @@ var _ = Describe("PostgreSQL Controller", func() {
})
})
})

// "instaclustr data": {"name":"testPostgre","version":"14.5.0","privateNetworkCluster":true,"slaTier":"NON_PRODUCTION","dataCentres":[{"name":"testDC1","region":"US_EAST_1","cloudProvider":"AWS_VPC","accountName":"accountNameTEST","network":"10.1.0.0/16","nodeSize":"PGS-DEV-t4g.small-5","nodesNumber":2,"clientEncryption":true,"interDataCentreReplication":[{"isPrimaryDataCentre":true}],"intraDataCentreReplication":[{"replicationMode":"SYNCHRONOUS"}],"pgBouncer":[{"pgBouncerVersion":"pgBouncerVersionTest","poolMode":"poolModeTest"}]}],"synchronousModeStrict":true},
// "k8s resource spec": {"name":"testPostgre","version":"14.5.0","pciCompliance":true,"privateNetworkCluster":true,"slaTier":"NON_PRODUCTION","dataCentres":[{"name":"testDC1","region":"US_EAST_1","cloudProvider":"AWS_VPC","accountName":"accountNameTEST","network":"10.1.0.0/16","nodeSize":"PGS-DEV-t4g.medium-30","nodesNumber":2,"clientEncryption":true,"interDataCentreReplication":[{"isPrimaryDataCentre":true}],"intraDataCentreReplication":[{"replicationMode":"SYNCHRONOUS"}],"pgBouncer":[{"pgBouncerVersion":"pgBouncerVersionTest","poolMode":"poolModeTest"}]}],"clusterConfigurations":{"idle_in_transaction_session_timeout":"1"},"synchronousModeStrict":true}}
9 changes: 9 additions & 0 deletions controllers/clusters/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ var _ = BeforeSuite(func() {
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&OpenSearchReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
API: instaClient,
Scheduler: scheduler.NewScheduler(logf.Log),
EventRecorder: eRecorder,
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&RedisReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *OpenSearchClusterV2ApiController) Routes() Routes {
{
"ClusterManagementV2ResourcesApplicationsOpensearchClustersV2Post",
strings.ToUpper("Post"),
"/cluster-management/v2/resources/applications/opensearch/clusters/v2/",
"/cluster-management/v2/resources/applications/opensearch/clusters/v2",
c.ClusterManagementV2ResourcesApplicationsOpensearchClustersV2Post,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ package openapi

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

// OpenSearchClusterV2ApiService is a service that implements the logic for the OpenSearchClusterV2ApiServicer
// This service should implement the business logic for every endpoint for the OpenSearchClusterV2Api API.
// Include any external packages or services that will be required by this service.
type OpenSearchClusterV2ApiService struct {
MockOpenSearchCluster *OpenSearchClusterV2
}

// NewOpenSearchClusterV2ApiService creates a default api service
Expand All @@ -31,44 +30,45 @@ func (s *OpenSearchClusterV2ApiService) ClusterManagementV2ResourcesApplications
// TODO - update ClusterManagementV2ResourcesApplicationsOpensearchClustersV2ClusterIdDelete with the required logic for this service method.
// Add api_open_search_cluster_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(204, {}) or use other options such as http.Ok ...
//return Response(204, nil),nil

return Response(http.StatusNotImplemented, nil), errors.New("ClusterManagementV2ResourcesApplicationsOpensearchClustersV2ClusterIdDelete method not implemented")
s.MockOpenSearchCluster = nil
return Response(204, nil), nil
}

// ClusterManagementV2ResourcesApplicationsOpensearchClustersV2ClusterIdGet - Get OpenSearch cluster details
func (s *OpenSearchClusterV2ApiService) ClusterManagementV2ResourcesApplicationsOpensearchClustersV2ClusterIdGet(ctx context.Context, clusterId string) (ImplResponse, error) {
// TODO - update ClusterManagementV2ResourcesApplicationsOpensearchClustersV2ClusterIdGet with the required logic for this service method.
// Add api_open_search_cluster_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, OpenSearchClusterV2{}) or use other options such as http.Ok ...
//return Response(200, OpenSearchClusterV2{}), nil
if s.MockOpenSearchCluster != nil {
s.MockOpenSearchCluster.Status = RUNNING
} else {
return Response(404, nil), nil
}

return Response(http.StatusNotImplemented, nil), errors.New("ClusterManagementV2ResourcesApplicationsOpensearchClustersV2ClusterIdGet method not implemented")
return Response(200, s.MockOpenSearchCluster), nil
}

// ClusterManagementV2ResourcesApplicationsOpensearchClustersV2ClusterIdPut - Update a OpenSearch cluster
func (s *OpenSearchClusterV2ApiService) ClusterManagementV2ResourcesApplicationsOpensearchClustersV2ClusterIdPut(ctx context.Context, clusterId string, body OpenSearchClusterUpdateV2) (ImplResponse, error) {
// TODO - update ClusterManagementV2ResourcesApplicationsOpensearchClustersV2ClusterIdPut with the required logic for this service method.
// Add api_open_search_cluster_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(202, OpenSearchClusterV2{}) or use other options such as http.Ok ...
//return Response(202, OpenSearchClusterV2{}), nil
s.MockOpenSearchCluster.DataNodes = body.DataNodes
s.MockOpenSearchCluster.OpensearchDashboards = body.OpensearchDashboards
s.MockOpenSearchCluster.ClusterManagerNodes = body.ClusterManagerNodes

return Response(202, nil), nil

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

return Response(http.StatusNotImplemented, nil), errors.New("ClusterManagementV2ResourcesApplicationsOpensearchClustersV2ClusterIdPut method not implemented")
}

// ClusterManagementV2ResourcesApplicationsOpensearchClustersV2Post - Create an OpenSearch cluster
func (s *OpenSearchClusterV2ApiService) ClusterManagementV2ResourcesApplicationsOpensearchClustersV2Post(ctx context.Context, body OpenSearchClusterV2) (ImplResponse, error) {
// TODO - update ClusterManagementV2ResourcesApplicationsOpensearchClustersV2Post with the required logic for this service method.
// Add api_open_search_cluster_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(202, OpenSearchClusterV2{}) or use other options such as http.Ok ...
//return Response(202, OpenSearchClusterV2{}), nil

return Response(http.StatusNotImplemented, nil), errors.New("ClusterManagementV2ResourcesApplicationsOpensearchClustersV2Post method not implemented")
s.MockOpenSearchCluster = &body
s.MockOpenSearchCluster.Id = CreatedID
return Response(202, s.MockOpenSearchCluster), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ type OpenSearchClusterV2 struct {
// AssertOpenSearchClusterV2Required checks if the required fields are not zero-ed
func AssertOpenSearchClusterV2Required(obj OpenSearchClusterV2) error {
elements := map[string]interface{}{
"dataNodes": obj.DataNodes,
"pciComplianceMode": obj.PciComplianceMode,
"opensearchVersion": obj.OpensearchVersion,
"dataCentres": obj.DataCentres,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type OpenSearchDataCentreV2 struct {
Region string `json:"region"`

// Private link enable or not
PrivateLink []OpenSearchPrivateLinkV2 `json:"privateLink,omitempty"`
PrivateLink bool `json:"privateLink,omitempty"`

// For customers running in their own account. Your provider account can be found on the Create Cluster page on the Instaclustr Console, or the \"Provider Account\" property on any existing cluster. For customers provisioning on Instaclustr's cloud provider accounts, this property may be omitted.
ProviderAccountName string `json:"providerAccountName,omitempty"`
Expand Down Expand Up @@ -93,11 +93,6 @@ func AssertOpenSearchDataCentreV2Required(obj OpenSearchDataCentreV2) error {
return err
}
}
for _, el := range obj.PrivateLink {
if err := AssertOpenSearchPrivateLinkV2Required(el); err != nil {
return err
}
}
return nil
}

Expand Down

0 comments on commit a90c26a

Please sign in to comment.