Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade opensearch to kb 1.0 api #1136

Merged
merged 19 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions addons-cluster/opensearch/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ spec:
{{- end }}
componentSpecs:
- name: opensearch
componentDef: opensearch
## TODO: use regex pattern when cluster validation supported
componentDef: opensearch-{{ .Chart.Version }}
replicas: {{ .Values.replicaCount | default 3 }}
{{- with .Values.resources }}
resources:
Expand All @@ -37,7 +38,7 @@ spec:
storage: {{ .Values.persistence.data.size }}
{{- end }}
- name: dashboard
componentDef: opensearch-dashboard
componentDef: opensearch-dashboard-{{ .Chart.Version }}
replicas: {{ .Values.dashboard.replicaCount | default 1 }}
{{- with .Values.dashboard.resources }}
resources:
Expand Down
42 changes: 42 additions & 0 deletions addons/opensearch/scripts-ut-spec/monitor_master_node_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# shellcheck shell=bash
# shellcheck disable=SC2034


# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run.
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then
echo "monitor_master_node_spec.sh skip cases because dependency bash version 4 or higher is not installed."
exit 0
fi


Describe "Opensearch Node Management Tests"
Include ../scripts/monitor-master-node.sh

init() {
# set ut_mode to true to hack control flow in the script
ut_mode="true"
}
BeforeAll "init"


Describe "cleanup()"
# Test scenario: When the node is not master
Context "when this node is not master"
setup() {
export CLUSTER_NAME="opensearch-cluster"
export OPENSEARCH_COMPONENT_SHORT_NAME="opensearch"
export NODE_NAME="test-node"
}
Before "setup"
It "exits the loop"
# Mock returning a master node that is different from current node
http() {
echo "opensearch-cluster-opensearch-master"
}
When call cleanup
The status should be success
The stdout should include "This node is not master."
End
End
End
End
58 changes: 58 additions & 0 deletions addons/opensearch/scripts/monitor-master-node.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

# This is magic for shellspec ut framework. "test" is a `test [expression]` well known as a shell command.
# Normally test without [expression] returns false. It means that __() { :; }
# function is defined if this script runs directly.
#
# shellspec overrides the test command and returns true *once*. It means that
# __() function defined internally by shellspec is called.
#
# In other words. If not in test mode, __ is just a comment. If test mode, __
# is a interception point.
#
# you should set ut_mode="true" when you want to run the script in shellspec file.
#
# shellcheck disable=SC2034
ut_mode="false"
test || __() {
# when running in non-unit test mode, set the options "set -eo pipefail".
set -eo pipefail;
}

http () {
local path="${1}"
if [ -n "${USERNAME}" ] && [ -n "${PASSWORD}" ]; then
BASIC_AUTH="-u ${USERNAME}:${PASSWORD}"
else
BASIC_AUTH=''
fi
curl -XGET -s -k --fail ${BASIC_AUTH} https://${CLUSTER_NAME}-${OPENSEARCH_COMPONENT_SHORT_NAME}-headless:9200:${path}
}

cleanup () {
while true ; do
local master="$(http "/_cat/master?h=node" || echo "")"
if [[ $master == "${CLUSTER_NAME}-${OPENSEARCH_COMPONENT_SHORT_NAME}"* && $master != "${NODE_NAME}" ]]; then
echo "This node is not master."
break
fi
echo "This node is still master, waiting gracefully for it to step down"
sleep 1
done

if [ "false" == "$ut_mode" ]; then
exit 0
fi
}

trap cleanup SIGTERM

# This is magic for shellspec ut framework.
# Sometime, functions are defined in a single shell script.
# You will want to test it. but you do not want to run the script.
# When included from shellspec, __SOURCED__ variable defined and script
# end here. The script path is assigned to the __SOURCED__ variable.
${__SOURCED__:+false} : || return 0

sleep infinity &
wait $!
87 changes: 87 additions & 0 deletions addons/opensearch/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,90 @@ Create the name of the service account to use
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

{{/*
Define image
*/}}
{{- define "opensearch.repository" -}}
{{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}
{{- end }}

{{- define "opensearch.image" -}}
{{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}
{{- end }}

{{- define "dashboard.repository" -}}
{{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.dashboard.repository }}
{{- end }}

{{- define "dashboard.image" -}}
{{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.dashboard.repository }}:{{ default .Chart.AppVersion .Values.image.dashboard.tag }}
{{- end }}

{{- define "os-master-graceful-handler.repository" -}}
{{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}
{{- end }}

{{- define "os-master-graceful-handler.image" -}}
{{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}
{{- end }}

{{- define "fsgroup-volume.image" -}}
{{ .Values.image.registry | default "docker.io" }}/apecloud/alpine:3.16
{{- end }}

{{- define "sysctl.image" -}}
{{ .Values.image.registry | default "docker.io" }}/apecloud/alpine:3.16
{{- end }}

{{/*
Common annotations
*/}}
{{- define "opensearch.annotations" -}}
helm.sh/resource-policy: keep
{{- end }}

{{/*
Define opensearch-dashboard component definition name
*/}}
{{- define "opensearch-dashboard.cmpdName" -}}
opensearch-dashboard-{{ .Chart.Version }}
{{- end -}}

{{/*
Define opensearch component definition name
*/}}
{{- define "opensearch.cmpdName" -}}
opensearch-{{ .Chart.Version }}
{{- end -}}

{{/*
Define opensearch component definition regular expression name prefix
*/}}
{{- define "opensearch.cmpdRegexpPattern" -}}
^opensearch-
{{- end -}}

{{/*
Define opensearch-dashboard component definition regular expression name prefix
*/}}
{{- define "opensearch-dashboard.cmpdRegexpPattern" -}}
^opensearch-dashboard-
{{- end -}}

{{/*
Generate scripts configmap
*/}}
{{- define "opensearch.extend.scripts" -}}
{{- range $path, $_ := $.Files.Glob "scripts/**" }}
{{ $path | base }}: |-
{{- $.Files.Get $path | nindent 2 }}
{{- end }}
{{- end }}

{{/*
Define opensearch component script template name
*/}}
{{- define "opensearch.scriptsTemplate" -}}
opensearch-scripts-template-{{ .Chart.Version }}
{{- end -}}
12 changes: 7 additions & 5 deletions addons/opensearch/templates/cmpd-dashboard.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
apiVersion: apps.kubeblocks.io/v1
kind: ComponentDefinition
metadata:
name: opensearch-dashboard-{{ .Chart.Version }}
name: {{ include "opensearch-dashboard.cmpdName" . }}
labels:
{{- include "opensearch.labels" . | nindent 4 }}
annotations:
{{- include "opensearch.annotations" . | nindent 4 }}
spec:
provider: Community
description: {{ .Chart.Description }}
serviceKind: {{ .Chart.Name }}
serviceVersion: {{ .Chart.AppVersion }}
serviceVersion: {{ .Values.defaultServiceVersion.opensearch }}
runtime:
containers:
- name: dashboard
image: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.dashboard.repository }}:{{ default .Chart.AppVersion .Values.image.dashboard.tag }}
image: {{ include "dashboard.image" . }}
imagePullPolicy: {{ default "IfNotPresent" .Values.image.pullPolicy }}
command:
- sh
Expand Down Expand Up @@ -58,9 +60,9 @@ spec:
- name: OPENSEARCH_HOST
valueFrom:
serviceVarRef:
compDef: opensearch
compDef: {{ include "opensearch.cmpdRegexpPattern" . }}
optional: false
Host: Required
host: Required
services:
- name: default
spec:
Expand Down
76 changes: 34 additions & 42 deletions addons/opensearch/templates/cmpd.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
apiVersion: apps.kubeblocks.io/v1
kind: ComponentDefinition
metadata:
name: opensearch-{{ .Chart.Version }}
name: {{ include "opensearch.cmpdName" . }}
labels:
{{- include "opensearch.labels" . | nindent 4 }}
annotations:
{{- include "opensearch.annotations" . | nindent 4 }}
spec:
provider: Community
description: {{ .Chart.Description }}
serviceKind: {{ .Chart.Name }}
serviceVersion: {{ .Chart.AppVersion }}
serviceVersion: {{ .Values.defaultServiceVersion.opensearch }}
vars:
## the name of current cluster instance
- name: CLUSTER_NAME
valueFrom:
clusterVarRef:
clusterName: Required
## the component name of opensearch, it's the shortname of opensearch component
- name: OPENSEARCH_COMPONENT_SHORT_NAME
valueFrom:
componentVarRef:
optional: false
shortName: Required
runtime:
initContainers:
- name: fsgroup-volume
image: {{ .Values.image.registry | default "docker.io" }}/apecloud/alpine:3.16
image: {{ include "fsgroup-volume.image" . }}
imagePullPolicy: {{ default "IfNotPresent" .Values.image.pullPolicy }}
command: [ 'sh', '-c' ]
args:
Expand All @@ -23,7 +37,7 @@ spec:
- name: data
mountPath: /usr/share/opensearch/data
- name: sysctl
image: {{ .Values.image.registry | default "docker.io" }}/apecloud/alpine:3.16
image: {{ include "sysctl.image" . }}
imagePullPolicy: {{ default "IfNotPresent" .Values.image.pullPolicy }}
command:
- sh
Expand All @@ -40,7 +54,7 @@ spec:
privileged: true
containers:
- name: opensearch
image: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}
image: {{ include "opensearch.image" . }}
imagePullPolicy: {{ default "IfNotPresent" .Values.image.pullPolicy }}
readinessProbe:
tcpSocket:
Expand Down Expand Up @@ -68,11 +82,11 @@ spec:
fieldRef:
fieldPath: metadata.name
- name: cluster.initial_master_nodes
value: "$(KB_CLUSTER_NAME)-$(KB_COMP_NAME)-0"
value: "$(CLUSTER_NAME)-$(OPENSEARCH_COMPONENT_SHORT_NAME)-0"
- name: discovery.seed_hosts
value: "$(KB_CLUSTER_NAME)-$(KB_COMP_NAME)-headless"
value: "$(CLUSTER_NAME)-$(OPENSEARCH_COMPONENT_SHORT_NAME)-headless"
- name: cluster.name
value: "$(KB_CLUSTER_NAME)"
value: "$(CLUSTER_NAME)"
- name: network.host
value: "0.0.0.0"
- name: OPENSEARCH_JAVA_OPTS
Expand All @@ -85,44 +99,16 @@ spec:
- mountPath: /usr/share/opensearch/config/opensearch.yaml
subPath: opensearch.yaml
name: opensearch-config
- name: opensearch-master-graceful-termination-handler
image: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}
- name: os-master-graceful-handler
image: {{ include "os-master-graceful-handler.image" . }}
imagePullPolicy: {{ default "IfNotPresent" .Values.image.pullPolicy }}
command:
- "sh"
- -c
- |
#!/usr/bin/env bash
set -eo pipefail

http () {
local path="${1}"
if [ -n "${USERNAME}" ] && [ -n "${PASSWORD}" ]; then
BASIC_AUTH="-u ${USERNAME}:${PASSWORD}"
else
BASIC_AUTH=''
fi
curl -XGET -s -k --fail ${BASIC_AUTH} https://$(KB_CLUSTER_NAME)-$(KB_COMP_NAME)-headless:9200:${path}
}

cleanup () {
while true ; do
local master="$(http "/_cat/master?h=node" || echo "")"
if [[ $master == "$(KB_CLUSTER_NAME)-$(KB_COMP_NAME)"* && $master != "${NODE_NAME}" ]]; then
echo "This node is not master."
break
fi
echo "This node is still master, waiting gracefully for it to step down"
sleep 1
done

exit 0
}

trap cleanup SIGTERM

sleep infinity &
wait $!
- /scripts/monitor-master-node.sh
volumeMounts:
- name: scripts
mountPath: /scripts
volumes:
- name: data
services:
Expand All @@ -140,3 +126,9 @@ spec:
templateRef: opensearch-config-template
volumeName: opensearch-config
namespace: {{.Release.Namespace}}
scripts:
- name: opensearch-scripts
templateRef: {{ include "opensearch.scriptsTemplate" . }}
namespace: {{ .Release.Namespace }}
volumeName: scripts
defaultMode: 0555
18 changes: 18 additions & 0 deletions addons/opensearch/templates/cmpv-dashboard.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: apps.kubeblocks.io/v1
kind: ComponentVersion
metadata:
name: opensearch-dashboard
labels:
{{- include "opensearch.labels" . | nindent 4 }}
spec:
compatibilityRules:
- compDefs:
- {{ include "opensearch-dashboard.cmpdRegexpPattern" . }}
releases:
- 2.7.0
releases:
- name: 2.7.0
changes:
serviceVersion: 2.7.0
images:
dashboard: {{ include "dashboard.repository" . }}:2.7.0
Loading
Loading