Skip to content

Commit

Permalink
Merge pull request #239 from carlreid/exit-code
Browse files Browse the repository at this point in the history
Handle case when user already exists to avoid job failing
  • Loading branch information
joejulian authored Dec 10, 2022
2 parents 6fcfb27 + bc976c7 commit e974eac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion charts/redpanda/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type: application
# The chart version and the app version are not the same and will not track
# together. The chart version is a semver representation of changes to this
# chart.
version: 2.3.14
version: 2.3.15
# The app version is the default version of Redpanda to install.
appVersion: v22.3.5
# kubeVersion must be suffixed with "-0" to be able to match cloud providers
Expand Down
19 changes: 18 additions & 1 deletion charts/redpanda/templates/post-install-upgrade-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,24 @@ spec:
{{- if .Values.auth.sasl.enabled }}
{{- $values := .Values }}
{{- range $user := .Values.auth.sasl.users }}
rpk acl user create {{ $user.name }} -p {{ $user.password | quote }} --mechanism {{ include "sasl-user-mechanism" (dict "user" $user "Values" $values) }} {{ template "rpk-common-flags" $ }}
# To avoid `set -e` from exiting the command when a user exists; catch the stderr output and exit codes into `creation_result`
# and `creation_result_exit_code` for use later
creation_result=$(rpk acl user create {{ $user.name }} -p {{ $user.password | quote }} --mechanism {{ include "sasl-user-mechanism" (dict "user" $user "Values" $values) }} {{ template "rpk-common-flags" $ }} 2>&1) && creation_result_exit_code=$? || creation_result_exit_code=$?

# On a non-success exit code
if [[ $creation_result_exit_code -ne 0 ]]; then
# Check if the stderr contains "User already exists"
if [[ $creation_result == *"User already exists"* ]]; then
printf "The %s user already exists, skipping creation.\n" {{ $user.name }}
else
# Another error occurred, so output the original message and exit code
echo "$creation_result"
exit $creation_result_exit_code
fi
# On a success, the user was created so output that
else
printf "Created user %s.\n" {{ $user.name }}
fi
{{- end }}
{{- end }}
{{- if (include "redpanda-atleast-22-2-0" . | fromJson).bool }}
Expand Down

0 comments on commit e974eac

Please sign in to comment.