{{/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}

{{/*
  Add warnings to the warnings template
*/}}
{{ $warnings := (fromJson (include "warnings" .)).result }}
{{- if $warnings }}
---
{{ range $warning := $warnings }}
{{ $warning }}
{{- end }}

---
{{- end }}

{{-
/*
Any rpk command that's given to the user in in this file must be defined in _example-commands.tpl and tested in a test.
*/
-}}

{{- $anySASL := (include "sasl-enabled" . | fromJson).bool }}
{{- $rpk := deepCopy . }}

{{- $_ := set $rpk "rpk" "rpk" }}

Congratulations on installing {{ .Chart.Name }}!

The pods will rollout in a few seconds. To check the status:

  kubectl -n {{ .Release.Namespace }} rollout status statefulset {{ template "redpanda.fullname" . }} --watch

{{- if and (toJson (dict "bool" .Values.external.enabled)) (eq .Values.external.type "LoadBalancer") }}

If you are using the load balancer service with a cloud provider, the services will likely have automatically-generated addresses. In this scenario the advertised listeners must be updated in order for external access to work. Run the following command once Redpanda is deployed:

  {{ printf "helm upgrade %s redpanda/redpanda --reuse-values -n %s --set $(kubectl get svc -n %s -o jsonpath='{\"external.addresses={\"}{ range .items[*]}{.status.loadBalancer.ingress[0].ip }{.status.loadBalancer.ingress[0].hostname}{\",\"}{ end }{\"}\\n\"}')" (include "redpanda.name" .) .Release.Namespace .Release.Namespace }}
{{- end }}

Set up rpk for access to your external listeners:
{{- $profile := keys .Values.listeners.kafka.external | first -}}
{{ if (include "tls-enabled" . | fromJson).bool }}
  {{- $external := dig "tls" "cert" .Values.listeners.kafka.tls.cert (get .Values.listeners.kafka.external $profile )}}
  kubectl get secret -n {{ .Release.Namespace }} {{ include "redpanda.fullname" . }}-{{ $external }}-cert -o go-template='{{ "{{" }} index .data "ca.crt" | base64decode }}' > ca.crt
  {{- if or .Values.listeners.kafka.tls.requireClientAuth .Values.listeners.admin.tls.requireClientAuth }}
  kubectl get secret -n {{ .Release.Namespace }} {{ include "redpanda.fullname" . }}-client -o go-template='{{ "{{" }} index .data "tls.crt" | base64decode }}' > tls.crt
  kubectl get secret -n {{ .Release.Namespace }} {{ include "redpanda.fullname" . }}-client -o go-template='{{ "{{" }} index .data "tls.key" | base64decode }}' > tls.key
  {{- end }}
{{- end }}
  rpk profile create --from-profile <(kubectl get configmap -n {{ .Release.Namespace }} {{ include "redpanda.fullname" . }}-rpk -o go-template='{{ "{{" }} .data.profile }}') {{ $profile }}

Set up dns to look up the pods on their Kubernetes Nodes. You can use this query to get the list of short-names to IP addresses. Add your external domain to the hostnames and you could test by adding these to your /etc/hosts:

  kubectl get pod -n {{ .Release.Namespace }} -o custom-columns=node:.status.hostIP,name:.metadata.name --no-headers -l app.kubernetes.io/name=redpanda,app.kubernetes.io/component=redpanda-statefulset

{{- if and $anySASL }}

Set the credentials in the environment:

  kubectl -n {{ .Release.Namespace }} get secret {{ .Values.auth.sasl.secretRef }} -o go-template="{{ "{{" }} range .data }}{{ "{{" }} . | base64decode }}{{ "{{" }} end }}" | IFS=: read -r {{ include "rpk-sasl-environment-variables" . }}
  export {{ include "rpk-sasl-environment-variables" . }}

{{- end }}

Try some sample commands:

{{- if and $anySASL }}
Create a user:

  {{ include "rpk-acl-user-create" $rpk }}

Give the user permissions:

  {{ include "rpk-acl-create" $rpk }}

{{- end }}

Get the api status:

  {{ include "rpk-cluster-info" $rpk }}

Create a topic

  {{ include "rpk-topic-create" $rpk }}

Describe the topic:

  {{ include "rpk-topic-describe" $rpk }}

Delete the topic:

  {{ include "rpk-topic-delete" $rpk }}
