Skip to content

Commit

Permalink
tests for opensearch webhook were implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
tengu-alt committed Feb 6, 2024
1 parent 513a9bd commit d7a6e53
Show file tree
Hide file tree
Showing 11 changed files with 466 additions and 71 deletions.
4 changes: 4 additions & 0 deletions apis/clusters/v1beta1/kafka_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ var _ = Describe("Kafka Controller", Ordered, func() {
testKafkaManifest.Spec.Kraft[0].ControllerNodeCount = 4
Expect(k8sClient.Create(ctx, &testKafkaManifest)).ShouldNot(Succeed())
testKafkaManifest.Spec.Kraft[0].ControllerNodeCount = 3

testKafkaManifest.Spec.Version += ".1"
Expect(k8sClient.Create(ctx, &testKafkaManifest)).ShouldNot(Succeed())
testKafkaManifest.Spec.Version = kafkaManifest.Spec.Version
})
})

Expand Down
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/opensearch_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type OpenSearchSpec struct {
DataCentres []*OpenSearchDataCentre `json:"dataCentres,omitempty"`
DataNodes []*OpenSearchDataNodes `json:"dataNodes,omitempty"`
Dashboards []*OpenSearchDashboards `json:"opensearchDashboards,omitempty"`
ClusterManagerNodes []*ClusterManagerNodes `json:"clusterManagerNodes,omitempty"`
ClusterManagerNodes []*ClusterManagerNodes `json:"clusterManagerNodes"`
ICUPlugin bool `json:"icuPlugin,omitempty"`
AsynchronousSearchPlugin bool `json:"asynchronousSearchPlugin,omitempty"`
KNNPlugin bool `json:"knnPlugin,omitempty"`
Expand Down
27 changes: 21 additions & 6 deletions apis/clusters/v1beta1/opensearch_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (osv *openSearchValidator) ValidateCreate(ctx context.Context, obj runtime.
return err
}

err = dc.ValidatePrivateLink()
err = dc.ValidatePrivateLink(os.Spec.PrivateNetwork)
if err != nil {
return err
}
Expand Down Expand Up @@ -172,6 +172,10 @@ func (osv *openSearchValidator) ValidateUpdate(ctx context.Context, old runtime.
return fmt.Errorf("cannot assert object %v to openSearch", new.GetObjectKind())
}

if os.Status.ID == "" {
return osv.ValidateCreate(ctx, os)
}

opensearchlog.Info("validate update", "name", os.Name)

oldCluster := old.(*OpenSearch)
Expand Down Expand Up @@ -271,12 +275,12 @@ type specificOpenSearchDC struct {
ReplicationFactor int
}

func (oss *OpenSearchDataCentre) newImmutableFields() *immutableOpenSearchDCFields {
func (osdc *OpenSearchDataCentre) newImmutableFields() *immutableOpenSearchDCFields {
return &immutableOpenSearchDCFields{
immutableDC: oss.GenericDataCentreSpec.immutableFields(),
immutableDC: osdc.GenericDataCentreSpec.immutableFields(),
specificOpenSearchDC: specificOpenSearchDC{
PrivateLink: oss.PrivateLink,
ReplicationFactor: oss.NumberOfRacks,
PrivateLink: osdc.PrivateLink,
ReplicationFactor: osdc.NumberOfRacks,
},
}
}
Expand Down Expand Up @@ -311,6 +315,14 @@ func (oss *OpenSearchSpec) validateUpdate(oldSpec OpenSearchSpec) error {
if err != nil {
return err
}
err = validateIngestNodes(oss.IngestNodes, oldSpec.IngestNodes)
if err != nil {
return err
}
err = validateClusterManagedNodes(oss.ClusterManagerNodes, oldSpec.ClusterManagerNodes)
if err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -392,10 +404,13 @@ func validateDataNode(newNodes, oldNodes []*OpenSearchDataNodes) error {
return nil
}

func (dc *OpenSearchDataCentre) ValidatePrivateLink() error {
func (dc *OpenSearchDataCentre) ValidatePrivateLink(privateNetworkCluster bool) error {
if dc.CloudProvider != models.AWSVPC && dc.PrivateLink {
return models.ErrPrivateLinkSupportedOnlyForAWS
}
if dc.PrivateLink && !privateNetworkCluster {
return models.ErrPrivateLinkAllowedOnlyWithPrivateNetworkCluster
}

return nil
}
315 changes: 315 additions & 0 deletions apis/clusters/v1beta1/opensearch_webhook_test.go

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions apis/clusters/v1beta1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,30 @@ func validateTwoFactorDelete(new, old []*TwoFactorDelete) error {
return nil
}

func validateIngestNodes(new, old []*OpenSearchIngestNodes) error {
if len(old) != len(new) {
return models.ErrImmutableIngestNodes
}

if *old[0] != *new[0] {
return models.ErrImmutableIngestNodes
}

return nil
}

func validateClusterManagedNodes(new, old []*ClusterManagerNodes) error {
if len(old) != len(new) {
return models.ErrImmutableClusterManagedNodes
}

if *old[0] != *new[0] {
return models.ErrImmutableClusterManagedNodes
}

return nil
}

func validateTagsUpdate(new, old map[string]string) error {
if len(old) != len(new) {
return models.ErrImmutableTags
Expand Down
10 changes: 9 additions & 1 deletion apis/clusters/v1beta1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
k8sappsv1 "k8s.io/api/apps/v1"
k8scorev1 "k8s.io/api/core/v1"

admissionv1beta1 "k8s.io/api/admission/v1beta1"
//+kubebuilder:scaffold:imports
Expand Down Expand Up @@ -79,6 +81,12 @@ var _ = BeforeSuite(func() {
err = AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = k8scorev1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = k8sappsv1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1beta1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -110,7 +118,7 @@ var _ = BeforeSuite(func() {
err = (&Redis{}).SetupWebhookWithManager(mgr, nil)
Expect(err).NotTo(HaveOccurred())

err = (&OpenSearch{}).SetupWebhookWithManager(mgr, nil)
err = (&OpenSearch{}).SetupWebhookWithManager(mgr, api)
Expect(err).NotTo(HaveOccurred())

err = (&Kafka{}).SetupWebhookWithManager(mgr, api)
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_opensearches.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ spec:
type: array
version:
type: string
required:
- clusterManagerNodes
type: object
status:
description: OpenSearchStatus defines the observed state of OpenSearch
Expand Down
2 changes: 1 addition & 1 deletion controllers/clusters/datatest/kafka_v1beta1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
defaulter: webhook
spec:
name: "kafka"
version: "2.8.2"
version: 1.0.0
pciCompliance: true
replicationFactor: 3
partitionsNumber: 3
Expand Down
27 changes: 21 additions & 6 deletions controllers/clusters/datatest/opensearch_v1beta1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ metadata:
defaulter: webhook
spec:
alertingPlugin: false
bundledUseOnly: false
anomalyDetectionPlugin: false
asynchronousSearchPlugin: false
# twoFactorDelete:
# - email: "emailTEST"
# phone: "phoneTEST"
# userRef:
# name: test-user
# namespace: default
clusterManagerNodes:
- dedicatedManager: false
- dedicatedManager: true
nodeSize: SRH-DEV-t4g.small-5
dataCentres:
- cloudProvider: AWS_VPC
Expand All @@ -22,9 +26,20 @@ spec:
numberOfRacks: 3
privateLink: false
region: US_EAST_1
# dataNodes:
# - nodeNumber: 3
# nodeSize: SRH-DEV-t4g.small-5
accountName: "Custom"
cloudProviderSettings:
- diskEncryptionKey: "123e4567-e89b-12d3-a456-426614174000"
tags:
tag: "oneTag"
tag2: "twoTags"
resizeSettings:
- concurrency: 1
ingestNodes:
- nodeSize: SRH-DI-PRD-m6g.large-10
nodeCount: 3
dataNodes:
- nodesNumber: 3
nodeSize: SRH-DEV-t4g.small-5
icuPlugin: false
indexManagementPlugin: true
knnPlugin: false
Expand All @@ -35,9 +50,9 @@ spec:
# - nodeSize: SRH-DEV-t4g.small-5
# oidcProvider: ''
# version: opensearch-dashboards:2.5.0
version: 2.5.0
version: 1.0.0
pciCompliance: false
privateNetworkCluster: false
privateNetwork: true
reportingPlugin: false
slaTier: NON_PRODUCTION
sqlPlugin: false
11 changes: 10 additions & 1 deletion pkg/instaclustr/mock/appversionsmock/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,14 @@ func NewInstAPI() *mockClient {
}

func (c *mockClient) ListAppVersions(app string) ([]*models.AppVersions, error) {
return []*models.AppVersions{{Application: app, Versions: []string{"1.0.0"}}}, nil
versions := []string{"1.0.0"}

switch app {
case models.OpenSearchAppKind:
return []*models.AppVersions{{Application: models.OpenSearchAppType, Versions: versions}}, nil
case models.KafkaAppKind:
return []*models.AppVersions{{Application: models.KafkaAppType, Versions: versions}}, nil
}

return []*models.AppVersions{{Application: app, Versions: versions}}, nil
}
113 changes: 58 additions & 55 deletions pkg/models/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,59 +21,62 @@ import (
)

var (
ErrZeroDataCentres = errors.New("cluster spec doesn't have data centres")
ErrMoreThanOneKraft = errors.New("cluster spec does not support more than one kraft")
ErrMoreThanThreeControllerNodeCount = errors.New("kraft does not support more than three controller nodes")
ErrNetworkOverlaps = errors.New("cluster network overlaps")
ErrImmutableTwoFactorDelete = errors.New("twoFactorDelete field is immutable")
ErrImmutableCloudProviderSettings = errors.New("cloudProviderSettings are immutable")
ErrImmutableIntraDataCentreReplication = errors.New("intraDataCentreReplication fields are immutable")
ErrImmutableInterDataCentreReplication = errors.New("interDataCentreReplication fields are immutable")
ErrImmutableDataCentresNumber = errors.New("data centres number is immutable")
ErrImmutableAWSSecurityGroupFirewallRule = errors.New("awsSecurityGroupFirewallRule is immutable")
ErrImmutableTags = errors.New("tags field is immutable")
ErrTypeAssertion = errors.New("unable to assert type")
ErrImmutableSchemaRegistry = errors.New("schema registry is immutable")
ErrImmutableRestProxy = errors.New("rest proxy is immutable")
ErrImmutableKraft = errors.New("kraft is immutable")
ErrImmutableKarapaceSchemaRegistry = errors.New("karapace schema registry is immutable")
ErrImmutableKarapaceRestProxy = errors.New("karapace rest proxy is immutable")
ErrImmutableDedicatedZookeeper = errors.New("dedicated zookeeper nodes cannot be changed")
ErrDecreasedDataCentresNumber = errors.New("data centres number cannot be decreased")
ErrImmutableTargetCluster = errors.New("TargetCluster field is immutable")
ErrImmutableExternalCluster = errors.New("ExternalCluster field is immutable")
ErrImmutableManagedCluster = errors.New("ManagedCluster field is immutable")
ErrIncorrectDayOfWeek = errors.New("dayOfWeek field is invalid")
ErrImmutableAWSArchival = errors.New("AWSArchival array is immutable")
ErrImmutableStandardProvisioning = errors.New("StandardProvisioning array is immutable")
ErrImmutableSharedProvisioning = errors.New("SharedProvisioning array is immutable")
ErrImmutablePackagedProvisioning = errors.New("PackagedProvisioning array is immutable")
ErrImmutableAdvancedVisibility = errors.New("AdvancedVisibility array is immutable")
ErrImmutablePrivateLink = errors.New("PrivateLink array is immutable")
ErrImmutableNodesNumber = errors.New("nodes number is immutable")
ErrImmutableSecretRef = errors.New("secret reference is immutable")
ErrEmptySecretRef = errors.New("secretRef.name and secretRef.namespace should not be empty")
ErrMissingSecretKeys = errors.New("the secret is missing the correct keys for the user")
ErrUserStillExist = errors.New("the user is still attached to the cluster. If you want to delete the user, remove the user from the cluster specification first")
ErrOnlyOneEntityTwoFactorDelete = errors.New("currently only one entity of two factor delete can be filled")
ErrPrivateLinkOnlyWithPrivateNetworkCluster = errors.New("private link is available only for private network clusters")
ErrPrivateLinkSupportedOnlyForSingleDC = errors.New("private link is only supported for a single data centre")
ErrPrivateLinkSupportedOnlyForAWS = errors.New("private link is supported only for an AWS cloud provider")
ErrImmutableSpec = errors.New("resource specification is immutable")
ErrUnsupportedBackupClusterKind = errors.New("backups for provided cluster kind are not supported")
ErrUnsupportedClusterKind = errors.New("provided cluster kind is not supported")
ErrExposeServiceNotCreatedYet = errors.New("expose service is not created yet")
ErrExposeServiceEndpointsNotCreatedYet = errors.New("expose service endpoints is not created yet")
ErrOnlySingleConcurrentResizeAvailable = errors.New("only single concurrent resize is allowed")
ErrBundledUseOnlyResourceUpdateIsNotSupported = errors.New("updating of bundled use resource is not supported")
ErrDebeziumImmutable = errors.New("debezium array is immutable")
ErrShotoverProxyImmutable = errors.New("shotoverProxy array is immutable")
ErrEmptyNamespace = errors.New("namespace field is empty")
ErrEmptyName = errors.New("name field is empty")
ErrCreateClusterWithMultiDC = errors.New("multiple data center is still not supported. Please create a cluster with one data centre and add a second one when the cluster is in the running state")
ErrOnPremicesWithMultiDC = errors.New("on-premises cluster can be provisioned with only one data centre")
ErrUnsupportedDeletingDC = errors.New("deleting data centre is not supported")
ErrClusterIsNotReadyToUpdate = errors.New("cluster is not ready to update")
ErrKubeVirtAddonNotFound = errors.New("cannot create KubeVirt based resources automatially without KubeVirt operator installed. Please install KubeVirt add-on")
ErrOpenSearchNumberOfRacksInvalid = errors.New("number of racks should be between 2 and 5")
ErrZeroDataCentres = errors.New("cluster spec doesn't have data centres")
ErrMoreThanOneKraft = errors.New("cluster spec does not support more than one kraft")
ErrMoreThanThreeControllerNodeCount = errors.New("kraft does not support more than three controller nodes")
ErrNetworkOverlaps = errors.New("cluster network overlaps")
ErrImmutableTwoFactorDelete = errors.New("twoFactorDelete field is immutable")
ErrImmutableIngestNodes = errors.New("IngestNodes field is immutable")
ErrImmutableClusterManagedNodes = errors.New("ClusterManagedNodes field is immutable")
ErrImmutableCloudProviderSettings = errors.New("cloudProviderSettings are immutable")
ErrImmutableIntraDataCentreReplication = errors.New("intraDataCentreReplication fields are immutable")
ErrImmutableInterDataCentreReplication = errors.New("interDataCentreReplication fields are immutable")
ErrImmutableDataCentresNumber = errors.New("data centres number is immutable")
ErrImmutableAWSSecurityGroupFirewallRule = errors.New("awsSecurityGroupFirewallRule is immutable")
ErrImmutableTags = errors.New("tags field is immutable")
ErrTypeAssertion = errors.New("unable to assert type")
ErrImmutableSchemaRegistry = errors.New("schema registry is immutable")
ErrImmutableRestProxy = errors.New("rest proxy is immutable")
ErrImmutableKraft = errors.New("kraft is immutable")
ErrImmutableKarapaceSchemaRegistry = errors.New("karapace schema registry is immutable")
ErrImmutableKarapaceRestProxy = errors.New("karapace rest proxy is immutable")
ErrImmutableDedicatedZookeeper = errors.New("dedicated zookeeper nodes cannot be changed")
ErrDecreasedDataCentresNumber = errors.New("data centres number cannot be decreased")
ErrImmutableTargetCluster = errors.New("TargetCluster field is immutable")
ErrImmutableExternalCluster = errors.New("ExternalCluster field is immutable")
ErrImmutableManagedCluster = errors.New("ManagedCluster field is immutable")
ErrIncorrectDayOfWeek = errors.New("dayOfWeek field is invalid")
ErrImmutableAWSArchival = errors.New("AWSArchival array is immutable")
ErrImmutableStandardProvisioning = errors.New("StandardProvisioning array is immutable")
ErrImmutableSharedProvisioning = errors.New("SharedProvisioning array is immutable")
ErrImmutablePackagedProvisioning = errors.New("PackagedProvisioning array is immutable")
ErrImmutableAdvancedVisibility = errors.New("AdvancedVisibility array is immutable")
ErrImmutablePrivateLink = errors.New("PrivateLink array is immutable")
ErrImmutableNodesNumber = errors.New("nodes number is immutable")
ErrImmutableSecretRef = errors.New("secret reference is immutable")
ErrEmptySecretRef = errors.New("secretRef.name and secretRef.namespace should not be empty")
ErrMissingSecretKeys = errors.New("the secret is missing the correct keys for the user")
ErrUserStillExist = errors.New("the user is still attached to the cluster. If you want to delete the user, remove the user from the cluster specification first")
ErrOnlyOneEntityTwoFactorDelete = errors.New("currently only one entity of two factor delete can be filled")
ErrPrivateLinkOnlyWithPrivateNetworkCluster = errors.New("private link is available only for private network clusters")
ErrPrivateLinkSupportedOnlyForSingleDC = errors.New("private link is only supported for a single data centre")
ErrPrivateLinkSupportedOnlyForAWS = errors.New("private link is supported only for an AWS cloud provider")
ErrPrivateLinkAllowedOnlyWithPrivateNetworkCluster = errors.New("private link is allowed only with privateNetworkCluster field enabled")
ErrImmutableSpec = errors.New("resource specification is immutable")
ErrUnsupportedBackupClusterKind = errors.New("backups for provided cluster kind are not supported")
ErrUnsupportedClusterKind = errors.New("provided cluster kind is not supported")
ErrExposeServiceNotCreatedYet = errors.New("expose service is not created yet")
ErrExposeServiceEndpointsNotCreatedYet = errors.New("expose service endpoints is not created yet")
ErrOnlySingleConcurrentResizeAvailable = errors.New("only single concurrent resize is allowed")
ErrBundledUseOnlyResourceUpdateIsNotSupported = errors.New("updating of bundled use resource is not supported")
ErrDebeziumImmutable = errors.New("debezium array is immutable")
ErrShotoverProxyImmutable = errors.New("shotoverProxy array is immutable")
ErrEmptyNamespace = errors.New("namespace field is empty")
ErrEmptyName = errors.New("name field is empty")
ErrCreateClusterWithMultiDC = errors.New("multiple data center is still not supported. Please create a cluster with one data centre and add a second one when the cluster is in the running state")
ErrOnPremicesWithMultiDC = errors.New("on-premises cluster can be provisioned with only one data centre")
ErrUnsupportedDeletingDC = errors.New("deleting data centre is not supported")
ErrClusterIsNotReadyToUpdate = errors.New("cluster is not ready to update")
ErrKubeVirtAddonNotFound = errors.New("cannot create KubeVirt based resources automatially without KubeVirt operator installed. Please install KubeVirt add-on")
ErrOpenSearchNumberOfRacksInvalid = errors.New("number of racks should be between 2 and 5")
)

0 comments on commit d7a6e53

Please sign in to comment.