Skip to content

Commit

Permalink
Make elasticsearch example compatible with 8.5.1 (#1747)
Browse files Browse the repository at this point in the history
Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com>

Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com>
  • Loading branch information
PrasadG193 committed Nov 18, 2022
1 parent d8e475c commit 089855a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
18 changes: 16 additions & 2 deletions examples/elasticsearch/elasticsearch-blueprint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ actions:
phases:
- func: KubeTask
name: backupToObjectStore
objects:
esMasterCredSecret:
kind: Secret
name: "{{ index .Object.metadata.labels.app }}-credentials"
namespace: "{{ .StatefulSet.Namespace }}"
args:
namespace: "{{ .StatefulSet.Namespace }}"
image: "ghcr.io/kanisterio/es-sidecar:0.84.0"
Expand All @@ -24,7 +29,9 @@ actions:
- |
host_name="{{ .Object.spec.serviceName }}.{{ .StatefulSet.Namespace }}.svc.cluster.local"
BACKUP_LOCATION=es_backups/{{ .StatefulSet.Namespace }}/{{ .StatefulSet.Name }}/{{ toDate "2006-01-02T15:04:05.999999999Z07:00" .Time | date "2006-01-02T15:04:05Z07:00" }}/backup.gz
elasticdump --bulk=true --input=http://${host_name}:9200 --output=/backup
master_username="{{ index .Phases.backupToObjectStore.Secrets.esMasterCredSecret.Data "username" | toString }}"
master_password="{{ index .Phases.backupToObjectStore.Secrets.esMasterCredSecret.Data "password" | toString }}"
NODE_TLS_REJECT_UNAUTHORIZED=0 elasticdump --bulk=true --input=https://${master_username}:${master_password}@${host_name}:9200 --output=/backup
gzip /backup
kando location push --profile '{{ toJson .Profile }}' /backup.gz --path $BACKUP_LOCATION
kando output backupLocation $BACKUP_LOCATION
Expand All @@ -34,6 +41,11 @@ actions:
phases:
- func: KubeTask
name: restoreFromObjectStore
objects:
esMasterCredSecret:
kind: Secret
name: "{{ index .Object.metadata.labels.app }}-credentials"
namespace: "{{ .StatefulSet.Namespace }}"
args:
namespace: "{{ .StatefulSet.Namespace }}"
image: "ghcr.io/kanisterio/es-sidecar:0.84.0"
Expand All @@ -46,7 +58,9 @@ actions:
- -c
- |
host_name="{{ .Object.spec.serviceName }}.{{ .StatefulSet.Namespace }}.svc.cluster.local"
kando location pull --profile '{{ toJson .Profile }}' --path '{{ .ArtifactsIn.cloudObject.KeyValue.backupLocation }}' - | gunzip | elasticdump --bulk=true --input=$ --output=http://${host_name}:9200
master_username="{{ index .Phases.restoreFromObjectStore.Secrets.esMasterCredSecret.Data "username" | toString }}"
master_password="{{ index .Phases.restoreFromObjectStore.Secrets.esMasterCredSecret.Data "password" | toString }}"
kando location pull --profile '{{ toJson .Profile }}' --path '{{ .ArtifactsIn.cloudObject.KeyValue.backupLocation }}' - | gunzip | NODE_TLS_REJECT_UNAUTHORIZED=0 elasticdump --bulk=true --input=$ --output=https://${master_username}:${master_password}@${host_name}:9200
delete:
inputArtifactNames:
- cloudObject
Expand Down
32 changes: 25 additions & 7 deletions pkg/app/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type ElasticsearchInstance struct {
elasticsearchURL string
}

// Last tested on 7.10.1
// Last tested on 8.5.1
func NewElasticsearchInstance(name string) App {
return &ElasticsearchInstance{
name: name,
Expand All @@ -71,7 +71,7 @@ func NewElasticsearchInstance(name string) App {
"replicas": "1",
},
},
elasticsearchURL: "localhost:9200",
elasticsearchURL: "https://localhost:9200",
}
}

Expand Down Expand Up @@ -144,14 +144,24 @@ func (esi *ElasticsearchInstance) Uninstall(ctx context.Context) error {
return nil
}

func (esi *ElasticsearchInstance) Secrets() map[string]crv1alpha1.ObjectReference {
return map[string]crv1alpha1.ObjectReference{
"elasticsearch": crv1alpha1.ObjectReference{
Kind: "Secret",
Name: esi.chart.Chart + "-master-credentials",
Namespace: esi.namespace,
},
}
}

func (esi *ElasticsearchInstance) GetClusterScopedResources(ctx context.Context) []crv1alpha1.ObjectReference {
return nil
}

func (esi *ElasticsearchInstance) Ping(ctx context.Context) error {
log.Print("Pinging the application to check if its accessible.", field.M{"app": esi.name})

pingCMD := []string{"sh", "-c", fmt.Sprintf("curl %s", esi.elasticsearchURL)}
pingCMD := []string{"sh", "-c", esi.curlCommand("GET", "")}
_, stderr, err := esi.execCommand(ctx, pingCMD)
if err != nil {
return errors.Wrapf(err, "Failed to ping the application. Error:%s", stderr)
Expand All @@ -161,7 +171,7 @@ func (esi *ElasticsearchInstance) Ping(ctx context.Context) error {
return nil
}
func (esi *ElasticsearchInstance) Insert(ctx context.Context) error {
addDocumentToIndexCMD := []string{"sh", "-c", fmt.Sprintf("curl -X POST %s/%s/_doc/?refresh=true -H 'Content-Type: application/json' -d'{\"appname\": \"kanister\" }'", esi.elasticsearchURL, esi.indexname)}
addDocumentToIndexCMD := []string{"sh", "-c", esi.curlCommandWithPayload("POST", esi.indexname+"/_doc/?refresh=true", "'{\"appname\": \"kanister\" }'")}
_, stderr, err := esi.execCommand(ctx, addDocumentToIndexCMD)
if err != nil {
// even one insert failed we will have to return becasue
Expand All @@ -174,7 +184,7 @@ func (esi *ElasticsearchInstance) Insert(ctx context.Context) error {
}

func (esi *ElasticsearchInstance) Count(ctx context.Context) (int, error) {
documentCountCMD := []string{"sh", "-c", fmt.Sprintf("curl %s/%s/_search?pretty", esi.elasticsearchURL, esi.indexname)}
documentCountCMD := []string{"sh", "-c", esi.curlCommand("GET", esi.indexname+"/_search?pretty")}
stdout, stderr, err := esi.execCommand(ctx, documentCountCMD)
if err != nil {
return 0, errors.Wrapf(err, "Error %s Counting the documents in an index.", stderr)
Expand All @@ -195,7 +205,7 @@ func (esi *ElasticsearchInstance) Reset(ctx context.Context) error {
log.Print("Resetting the application.", field.M{"app": esi.name})

// delete the index and then create it, in order to reset the es application
deleteIndexCMD := []string{"sh", "-c", fmt.Sprintf("curl -X DELETE %s/%s?pretty", esi.elasticsearchURL, esi.indexname)}
deleteIndexCMD := []string{"sh", "-c", esi.curlCommand("DELETE", esi.indexname+"/?pretty")}
_, stderr, err := esi.execCommand(ctx, deleteIndexCMD)
if err != nil {
return errors.Wrapf(err, "Error %s while deleting the index %s to reset the application.", stderr, esi.indexname)
Expand All @@ -207,7 +217,7 @@ func (esi *ElasticsearchInstance) Reset(ctx context.Context) error {
// Initialize is used to initialize the database or create schema
func (esi *ElasticsearchInstance) Initialize(ctx context.Context) error {
// create the index
createIndexCMD := []string{"sh", "-c", fmt.Sprintf("curl -X PUT %s/%s?pretty", esi.elasticsearchURL, esi.indexname)}
createIndexCMD := []string{"sh", "-c", esi.curlCommand("PUT", esi.indexname+"/?pretty")}
_, stderr, err := esi.execCommand(ctx, createIndexCMD)
if err != nil {
return errors.Wrapf(err, "Error %s: Resetting the application.", stderr)
Expand All @@ -222,3 +232,11 @@ func (esi *ElasticsearchInstance) execCommand(ctx context.Context, command []str
}
return kube.Exec(esi.cli, esi.namespace, podname, containername, command, nil)
}

func (esi *ElasticsearchInstance) curlCommand(method, path string) string {
return fmt.Sprintf("curl -k -X %s -H 'Content-Type: application/json' -u elastic:${ELASTIC_PASSWORD} %s/%s", method, esi.elasticsearchURL, path)
}

func (esi *ElasticsearchInstance) curlCommandWithPayload(method, path, data string) string {
return fmt.Sprintf("%s -d %s", esi.curlCommand(method, path), data)
}

0 comments on commit 089855a

Please sign in to comment.