From 7b28debefd1023ac6d0b13981221b63930440cb1 Mon Sep 17 00:00:00 2001 From: Pritam Das Date: Fri, 4 Oct 2024 17:53:46 +0600 Subject: [PATCH] Add DeleteCollection method for Solr (#141) * Add delete collection method Signed-off-by: pritamdas99 --- go.mod | 2 +- go.sum | 4 ++-- solr/api.go | 2 ++ solr/solrv8.go | 23 +++++++++++++++++++ solr/solrv9.go | 23 +++++++++++++++++++ solr/util.go | 17 ++++++++++++++ .../catalog/v1alpha1/openapi_generated.go | 14 +++++++---- .../catalog/v1alpha1/solr_version_types.go | 2 ++ .../catalog/v1alpha1/zz_generated.deepcopy.go | 1 + .../apis/kubedb/v1alpha2/solr_helpers.go | 6 ----- .../crds/catalog.kubedb.com_solrversions.yaml | 11 +++++++++ vendor/modules.txt | 2 +- 12 files changed, 93 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 619a3738..8f011b0c 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,7 @@ require ( k8s.io/klog/v2 v2.130.1 kmodules.xyz/client-go v0.30.17 kmodules.xyz/custom-resources v0.30.0 - kubedb.dev/apimachinery v0.48.0 + kubedb.dev/apimachinery v0.48.1-0.20241003061121-cbe53073e554 sigs.k8s.io/controller-runtime v0.18.4 xorm.io/xorm v1.3.6 ) diff --git a/go.sum b/go.sum index 4526e7be..d37f7e0e 100644 --- a/go.sum +++ b/go.sum @@ -804,8 +804,8 @@ kmodules.xyz/monitoring-agent-api v0.30.1 h1:OdYucfl7OblBqVhCyEcOC3HvUrOKtDh8lcM kmodules.xyz/monitoring-agent-api v0.30.1/go.mod h1:oR3tk5O4koYar4cD9N3AjbBFr9XTwBU3sw9qD2NdNQc= kmodules.xyz/offshoot-api v0.30.1 h1:TrulAYO+oBsXe9sZZGTmNWIuI8qD2izMpgcTSPvgAmI= kmodules.xyz/offshoot-api v0.30.1/go.mod h1:T3mpjR6fui0QzOcmQvIuANytW48fe9ytmy/1cgx6D4g= -kubedb.dev/apimachinery v0.48.0 h1:gMGqkBRs81wbmGPQIqMGY/MztKg4MYjZ39d9F656V2c= -kubedb.dev/apimachinery v0.48.0/go.mod h1:TeZW+vt9OLf0Jyb/AZktvOOzf3NV+rFhHDN/zTh1EjA= +kubedb.dev/apimachinery v0.48.1-0.20241003061121-cbe53073e554 h1:0lvCuDdkmUci+fP1XDtES2aZroO4QpIbiW+qM/YqrqQ= +kubedb.dev/apimachinery v0.48.1-0.20241003061121-cbe53073e554/go.mod h1:TeZW+vt9OLf0Jyb/AZktvOOzf3NV+rFhHDN/zTh1EjA= kubeops.dev/petset v0.0.7 h1:F77BTRfUqRVO7kNc8q2oFSSviDmYBqni/osXqu0kgJ4= kubeops.dev/petset v0.0.7/go.mod h1:lt0SZV4ohRy7RiwLNUnMoauG4lCbcRbSqhMg20rdUQg= lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= diff --git a/solr/api.go b/solr/api.go index dea0f8ae..fe8e281c 100644 --- a/solr/api.go +++ b/solr/api.go @@ -28,6 +28,7 @@ const ( Action = "action" ActionBackup = "BACKUP" ActionRestore = "RESTORE" + ActionDelete = "DELETE" ActionCreate = "CREATE" ActionDeleteBackup = "DELETEBACKUP" AddRole = "ADDROLE" @@ -71,4 +72,5 @@ type SLClient interface { BalanceReplica(async string) (*Response, error) AddRole(role, node string) (*Response, error) RemoveRole(role, node string) (*Response, error) + DeleteCollection(name string) (*Response, error) } diff --git a/solr/solrv8.go b/solr/solrv8.go index 7ab34862..5a5e8823 100644 --- a/solr/solrv8.go +++ b/solr/solrv8.go @@ -430,3 +430,26 @@ func (sc *SLClientV8) RemoveRole(role, node string) (*Response, error) { } return backupResponse, nil } + +func (sc *SLClientV8) DeleteCollection(name string) (*Response, error) { + sc.Config.log.V(5).Info(fmt.Sprintf("Delete COLLECTION: %s", name)) + req := sc.Client.R().SetDoNotParseResponse(true) + req.SetHeader("Content-Type", "application/json") + deleteParams := map[string]string{ + Action: ActionDelete, + Name: name, + } + req.SetQueryParams(deleteParams) + res, err := req.Delete("/solr/admin/collections") + if err != nil { + sc.Config.log.Error(err, "Failed to send http request to read a collection") + return nil, err + } + + deleteResponse := &Response{ + Code: res.StatusCode(), + header: res.Header(), + body: res.RawBody(), + } + return deleteResponse, nil +} diff --git a/solr/solrv9.go b/solr/solrv9.go index 008853b5..2bbdd471 100644 --- a/solr/solrv9.go +++ b/solr/solrv9.go @@ -145,6 +145,29 @@ func (sc *SLClientV9) ReadCollection() (*Response, error) { return writeResponse, nil } +func (sc *SLClientV9) DeleteCollection(name string) (*Response, error) { + sc.Config.log.V(5).Info(fmt.Sprintf("Delete COLLECTION: %s", name)) + req := sc.Client.R().SetDoNotParseResponse(true) + req.SetHeader("Content-Type", "application/json") + deleteParams := map[string]string{ + Action: ActionDelete, + Name: name, + } + req.SetQueryParams(deleteParams) + res, err := req.Delete("/solr/admin/collections") + if err != nil { + sc.Config.log.Error(err, "Failed to send http request to read a collection") + return nil, err + } + + deleteResponse := &Response{ + Code: res.StatusCode(), + header: res.Header(), + body: res.RawBody(), + } + return deleteResponse, nil +} + func (sc *SLClientV9) BackupCollection(ctx context.Context, collection string, backupName string, location string, repository string) (*Response, error) { sc.Config.log.V(5).Info(fmt.Sprintf("BACKUP COLLECTION: %s", collection)) req := sc.Client.R().SetDoNotParseResponse(true).SetContext(ctx) diff --git a/solr/util.go b/solr/util.go index 3f450774..cf3016f9 100644 --- a/solr/util.go +++ b/solr/util.go @@ -117,6 +117,23 @@ func (sc *Client) DecodeCollectionHealth(responseBody map[string]interface{}) er return errors.New("didn't find health") } if health != "GREEN" { + if name == writeCollectionName { + response, err := sc.DeleteCollection(name) + if err != nil { + return err + } + responseBody, err := sc.DecodeResponse(response) + if err != nil { + klog.Error(err) + return err + } + + _, err = sc.GetResponseStatus(responseBody) + if err != nil { + klog.Error(err) + return err + } + } config := sc.GetConfig() config.log.Error(errors.New(""), fmt.Sprintf("Health of collection %s IS NOT GREEN", name)) return errors.New(fmt.Sprintf("health for collection %s is not green", name)) diff --git a/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/openapi_generated.go b/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/openapi_generated.go index e0624fc7..e9fd58cf 100644 --- a/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/openapi_generated.go +++ b/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/openapi_generated.go @@ -31699,11 +31699,17 @@ func schema_apimachinery_apis_catalog_v1alpha1_SolrVersionSpec(ref common.Refere Format: "", }, }, - "securityContext": { + "updateConstraints": { SchemaProps: spec.SchemaProps{ - Description: "SecurityContext is for the additional security information for the Solr container", + Description: "SecurityContext is for the additional security information for the Solr container update constraints", Default: map[string]interface{}{}, - Ref: ref("kubedb.dev/apimachinery/apis/catalog/v1alpha1.SecurityContext"), + Ref: ref("kubedb.dev/apimachinery/apis/catalog/v1alpha1.UpdateConstraints"), + }, + }, + "securityContext": { + SchemaProps: spec.SchemaProps{ + Default: map[string]interface{}{}, + Ref: ref("kubedb.dev/apimachinery/apis/catalog/v1alpha1.SecurityContext"), }, }, }, @@ -31711,7 +31717,7 @@ func schema_apimachinery_apis_catalog_v1alpha1_SolrVersionSpec(ref common.Refere }, }, Dependencies: []string{ - "kubedb.dev/apimachinery/apis/catalog/v1alpha1.SecurityContext", "kubedb.dev/apimachinery/apis/catalog/v1alpha1.SolrInitContainer", "kubedb.dev/apimachinery/apis/catalog/v1alpha1.SolrVersionDatabase"}, + "kubedb.dev/apimachinery/apis/catalog/v1alpha1.SecurityContext", "kubedb.dev/apimachinery/apis/catalog/v1alpha1.SolrInitContainer", "kubedb.dev/apimachinery/apis/catalog/v1alpha1.SolrVersionDatabase", "kubedb.dev/apimachinery/apis/catalog/v1alpha1.UpdateConstraints"}, } } diff --git a/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/solr_version_types.go b/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/solr_version_types.go index 96848d0d..460e1f5e 100644 --- a/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/solr_version_types.go +++ b/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/solr_version_types.go @@ -59,6 +59,8 @@ type SolrVersionSpec struct { // +optional Deprecated bool `json:"deprecated,omitempty"` // SecurityContext is for the additional security information for the Solr container + // update constraints + UpdateConstraints UpdateConstraints `json:"updateConstraints,omitempty"` // +optional SecurityContext SecurityContext `json:"securityContext"` } diff --git a/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/zz_generated.deepcopy.go b/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/zz_generated.deepcopy.go index 8368d286..56c3f13b 100644 --- a/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/zz_generated.deepcopy.go +++ b/vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/zz_generated.deepcopy.go @@ -3713,6 +3713,7 @@ func (in *SolrVersionSpec) DeepCopyInto(out *SolrVersionSpec) { *out = *in out.DB = in.DB out.InitContainer = in.InitContainer + in.UpdateConstraints.DeepCopyInto(&out.UpdateConstraints) in.SecurityContext.DeepCopyInto(&out.SecurityContext) return } diff --git a/vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/solr_helpers.go b/vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/solr_helpers.go index b91305a7..1daf8ebd 100644 --- a/vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/solr_helpers.go +++ b/vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/solr_helpers.go @@ -268,12 +268,6 @@ func (s *Solr) SetDefaults() { } } - if s.Spec.KeystoreSecret == nil { - s.Spec.KeystoreSecret = &v1.LocalObjectReference{ - Name: s.SolrSecretName("keystore-cred"), - } - } - if s.Spec.ZookeeperDigestSecret == nil { s.Spec.ZookeeperDigestSecret = &v1.LocalObjectReference{ Name: s.SolrSecretName("zk-digest"), diff --git a/vendor/kubedb.dev/apimachinery/crds/catalog.kubedb.com_solrversions.yaml b/vendor/kubedb.dev/apimachinery/crds/catalog.kubedb.com_solrversions.yaml index 16bc33b5..0149f607 100644 --- a/vendor/kubedb.dev/apimachinery/crds/catalog.kubedb.com_solrversions.yaml +++ b/vendor/kubedb.dev/apimachinery/crds/catalog.kubedb.com_solrversions.yaml @@ -67,6 +67,17 @@ spec: format: int64 type: integer type: object + updateConstraints: + properties: + allowlist: + items: + type: string + type: array + denylist: + items: + type: string + type: array + type: object version: type: string required: diff --git a/vendor/modules.txt b/vendor/modules.txt index dc6a2180..0a39b720 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1564,7 +1564,7 @@ kmodules.xyz/offshoot-api/api/v1 kmodules.xyz/offshoot-api/api/v1/conversion kmodules.xyz/offshoot-api/api/v2 kmodules.xyz/offshoot-api/util -# kubedb.dev/apimachinery v0.48.0 +# kubedb.dev/apimachinery v0.48.1-0.20241003061121-cbe53073e554 ## explicit; go 1.22.1 kubedb.dev/apimachinery/apis kubedb.dev/apimachinery/apis/catalog