Skip to content

Commit

Permalink
Add DeleteCollection method for Solr (#141)
Browse files Browse the repository at this point in the history
* Add delete collection method

Signed-off-by: pritamdas99 <[email protected]>
  • Loading branch information
pritamdas99 authored Oct 4, 2024
1 parent c1fd832 commit 7b28deb
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions solr/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
Action = "action"
ActionBackup = "BACKUP"
ActionRestore = "RESTORE"
ActionDelete = "DELETE"
ActionCreate = "CREATE"
ActionDeleteBackup = "DELETEBACKUP"
AddRole = "ADDROLE"
Expand Down Expand Up @@ -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)
}
23 changes: 23 additions & 0 deletions solr/solrv8.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
23 changes: 23 additions & 0 deletions solr/solrv9.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions solr/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7b28deb

Please sign in to comment.