opensearch-project / opensearch-k8s-operator

OpenSearch Kubernetes Operator
Apache License 2.0
403 stars 218 forks source link

Add Lifecycle Hooks to OpenSearch cluster CR #468

Open IshaGirdhar opened 1 year ago

IshaGirdhar commented 1 year ago

At present, if you need to configure or execute a command in an OpenSearch or OpenSearch dashboard container that's up, you can only do so by accessing the container and running the command manually. To simplify this process, we could introduce lifecycle hooks, which are a core Kubernetes feature(https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/) as part of the OpenSearch Cluster CR. These hooks will allow users to specify commands that should be executed after the container starts or before it shuts down gracefully. For example:

spec:
  general:
    version: 1.3.0
    httpPort: 9200
    vendor: opensearch
    serviceName: my-cluster
    lifecycle:
      preStop:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the preStart handler > /usr/share/message"]
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
    pluginsList: ["repository-s3","https://github.com/aiven/prometheus-exporter-plugin-for-opensearch/releases/download/1.3.0.0/prometheus-exporter-1.3.0.0.zip"]
  dashboards:
    version: 1.3.0
    enable: true
    replicas: 2
    lifecycle:
      preStop:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
      postStart:
        exec:
          command:
            - bash
            - -c
            - |
              #!/bin/bash
              # Add a template to adjust number of shards/replicas1
              TEMPLATE_NAME=my_template
              INDEX_PATTERN="logstash-*"
              SHARD_COUNT=8
              REPLICA_COUNT=1
              ES_URL=http://localhost:9200
              while [[ "$(curl -s -o /dev/null -w '%{http_code}\n' $ES_URL)" != "200" ]]; do sleep 1; done
              curl -XPUT "$ES_URL/_template/$TEMPLATE_NAME" -H 'Content-Type: application/json' -d'{"index_patterns":['\""$INDEX_PATTERN"\"'],"settings":{"number_of_shards":'$SHARD_COUNT',"number_of_replicas":'$REPLICA_COUNT'}}'

With these hooks, users can add the necessary commands to the container's configuration and Kubernetes will execute them automatically at the specified lifecycle events. One of the use cases for this is adding a new index template, so that when OS cluster is ready to ingest, it has the right set of index templates available.

idanl21 commented 1 year ago

hey @IshaGirdhar I didnt really understood the use case for those hooks, can you please add one more example ?

IshaGirdhar commented 1 year ago

@idanl21 A few of the use cases are

  1. Want to create default or out of the box index templates that are available by the time the OS cluster is up and running.
  2. Want to create default or out of the box ISM policies that are available by the time the OS cluster is up and running.
  3. Want to create default or out of the box data streams that are available by the time the OS cluster is up and running.
IshaGirdhar commented 1 year ago

OpenSearch official Helm Chart supports this as well https://github.com/opensearch-project/helm-charts/blob/main/charts/opensearch/values.yaml#L409

IshaGirdhar commented 1 year ago

I think similar request was also made here https://github.com/Opster/opensearch-k8s-operator/issues/435