From b65ebd23abeb427c9f35d45ae20376206ef9c0b7 Mon Sep 17 00:00:00 2001 From: Ian Zink Date: Fri, 14 Apr 2023 12:33:53 -0500 Subject: [PATCH 1/3] Add HIP for obtaining output from failed hook jobs and pods Signed-off-by: Ian Zink --- hips/hip-9999.md | 254 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100644 hips/hip-9999.md diff --git a/hips/hip-9999.md b/hips/hip-9999.md new file mode 100644 index 00000000..7fb6bdb5 --- /dev/null +++ b/hips/hip-9999.md @@ -0,0 +1,254 @@ +--- +hip: 9999 +title: "New annotations for displaying hook output" +authors: [ "Ian Zink " ] +created: "2023-01-26" +type: "feature" +status: "draft" +--- + +## Abstract + +This proposes a new annotation to indicate that the output from the hook should be displayed to the user. + +## Motivation + +The one motivation for this HIP is the ability to display preflight checks before the main chart installs. This provides vital feedback to the user as to why a Helm chart can not be successfully installed. + +Often it is important to verify that the Kubernetes cluster you are deploying a Helm chart into has certain properties. You might need to know that it has an Ingress controller, a certain amount of ephemeral storage, memory, or CPUs available. You might want to validate the service key they provided was correct or that that database they entered is reachable. Letting a Helm install and/or upgrade fail, and then make the user debug this to see why it failed is a poor user experience. These things can all be done with checks enabled by the hook proposed in this HIP. + +Another common motivation for this HIP is to indicate that database migration output running as a hook should be visible to the user in the case of failure. + +In general, allowing chart developers to run jobs and present that feedback directly to the users could also open up additional use cases beyond just the preflight use case that motivated this HIP. I could imagine scenarios where maybe CVE warnings are presented or specific upgrade feedback is presented instead of just a Helm install failure. + +## Rationale + +There are other ways that this could be implemented. For example, we could have a separate preflight hook type. However, this new hook type wouldn't be handled at all by previous versions of Helm. With this design, it requires minimal changes to Helm and allows for backwards compatibility. + +Another strategy could be for Helm to include Troubleshoot.sh as a dependent library, but this could result in too tight of a coupling between the projects and lower overall flexibility and adaptability. + +## Specification + +Templates may include the following annotations on Jobs or Pods: + +```yaml + "helm.sh/hook": pre-install, pre-upgrade + "helm.sh/hook-output-log-policy": hook-failed # or hook-succeeded or hook-failed,hook-succeeded +``` + + +`helm.sh/hook-output-log-policy` would indicate that Helm should display the output of the Job to the user. + +Additionally, a new user flag should be created `--no-log-output` that would skip the output of logs. + +Additionally, there will be a new item added to the action SDK configuration to allow SDK consumers to get the output. +By default this output will be written to stdout, but an SDK consumer can overwrite the HookOutputFunc to provide a custom writer. + + +```go +type Configuration struct { + ... + // Called with container name and returns and expects writer that will receive the log output + HookOutputFunc func(namespace, pod, container string) io.Writer +} +``` + +## Backwards compatibility + +The only backwards compatibility concern would be that scripts parsing `helm install` output would see some additional text in the case of logs being output. The fact that notes already make the output unstructured should mitigate any concern here. Since we already are trusting chart developers to provide output in the form of notes, this is a logical extension of that that allows the developer to provide more dynamic output. + +## Security implications + +Potentially the preflight checks could check for security misconfigurations that could enhance the security of the chart deployment. + +## How to teach this + +In the first instance, documentation plus the help text for `helm install` would explain the feature. + +An example template could be provided in documentation showing how to use this feature with a generic command used in a hook. + +A more advanced example showing how to use the new feature with Troubleshoot.sh to provide preflight checks could be linked in the documentation, provided directly in the documentation, or provided on the Troubleshoot.sh documentation site independently. + +## Reference implementation + +[Pull Request for Documentation ](https://github.com/helm/helm-www/pull/1242) + +[Pull Request for Helm](https://github.com/helm/helm/pull/10309) - most upvoted open PR + + +## Rejected ideas +N/A + +## Open issues +N/A + +## References + +[Troubleshoot.sh](https://troubleshoot.sh/) - the tool that is the motivation for this HIP. + +[safe-install plugin](https://github.com/z4ce/helm-safe-install) - Plugin that provides a similiar experience to what I hope this HIP will provide natively. + +## Reference - Examples Usage + +### Example using `false` + +Template: +```yaml +apiVersion: batch/v1 +kind: Job +metadata: + name: "{{ .Release.Name }}-false-job" + labels: + app.kubernetes.io/managed-by: {{ .Release.Service | quote }} + app.kubernetes.io/instance: {{ .Release.Name | quote }} + app.kubernetes.io/version: {{ .Chart.AppVersion }} + helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + annotations: + "helm.sh/hook": pre-install, pre-upgrade + "helm.sh/hook-output-log-policy": hook-failed, hook-suceeded + "helm.sh/hook-weight": "-5" + "helm.sh/hook-delete-policy": hook-succeeded, hook-failed + +spec: + backoffLimit: 0 + template: + metadata: + name: "{{ .Release.Name }}" + labels: + app.kubernetes.io/managed-by: {{ .Release.Service | quote }} + app.kubernetes.io/instance: {{ .Release.Name | quote }} + helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + spec: + restartPolicy: Never + containers: + - name: post-install-job + image: "alpine:3.18" + command: ["sh", "-c", "echo foo ; false"] +``` + +What it should loook when running: + +```text +$ helm install ./ my-release +Logs for pod: my-release-false-job-bgbz6, container: pre-install-job +foo +Error: INSTALLATION FAILED: failed pre-install: job failed: BackoffLimitExceeded +``` + +### Example using Troubleshoot Preflight Checks + +```yaml +apiVersion: batch/v1 +kind: Job +metadata: + name: "{{ .Release.Name }}-preflight-job" + labels: + app.kubernetes.io/managed-by: {{ .Release.Service | quote }} + app.kubernetes.io/instance: {{ .Release.Name | quote }} + app.kubernetes.io/version: {{ .Chart.AppVersion }} + helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + annotations: + "helm.sh/hook": pre-install, pre-upgrade + "helm.sh/hook-output-log-policy": hook-failed + "helm.sh/hook-weight": "-5" + "helm.sh/hook-delete-policy": hook-succeeded, hook-failed + +spec: + backoffLimit: 0 + template: + metadata: + name: "{{ .Release.Name }}" + labels: + app.kubernetes.io/managed-by: {{ .Release.Service | quote }} + app.kubernetes.io/instance: {{ .Release.Name | quote }} + helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + spec: + restartPolicy: Never + volumes: + - name: preflights + configMap: + name: "{{ .Release.Name }}-preflight-config" + containers: + - name: post-install-job + image: "replicated/preflight:latest" + command: ["preflight", "--interactive=false", "/preflights/preflights.yaml"] + volumeMounts: + - name: preflights + mountPath: /preflights + +--- +apiVersion: v1 +kind: ConfigMap +metadata: + annotations: + "helm.sh/hook": pre-install, pre-upgrade + "helm.sh/hook-weight": "-6" + "helm.sh/hook-delete-policy": hook-succeeded, hook-failed + labels: + app.kubernetes.io/managed-by: {{ .Release.Service | quote }} + app.kubernetes.io/instance: {{ .Release.Name | quote }} + app.kubernetes.io/version: {{ .Chart.AppVersion }} + helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + name: "{{ .Release.Name }}-preflight-config" +data: + preflights.yaml: | + apiVersion: troubleshoot.sh/v1beta2 + kind: Preflight + metadata: + name: preflight-tutorial + spec: + collectors: + {{ if eq .Values.mariadb.enabled false }} + - mysql: + collectorName: mysql + uri: '{{ .Values.externalDatabase.user }}:{{ .Values.externalDatabase.password }}@tcp({{ .Values.externalDatabase.host }}:{{ .Values.externalDatabase.port }})/{{ .Values.externalDatabase.database }}?tls=false' + {{ end }} + analyzers: + - clusterVersion: + outcomes: + - fail: + when: "< 1.16.0" + message: The application requires at least Kubernetes 1.16.0, and recommends 1.18.0. + uri: https://kubernetes.io + - warn: + when: "< 1.18.0" + message: Your cluster meets the minimum version of Kubernetes, but we recommend you update to 1.18.0 or later. + uri: https://kubernetes.io + - pass: + message: Your cluster meets the recommended and required versions of Kubernetes. + {{ if eq .Values.mariadb.enabled false }} + - mysql: + checkName: Must be MySQL 8.x or later + collectorName: mysql + outcomes: + - fail: + when: connected == false + message: Cannot connect to MySQL server + - fail: + when: version < 8.x + message: The MySQL server must be at least version 8 + - pass: + message: The MySQL server is ready + {{ end }} +``` + +Which should yield the following output to stdout: + +```text +$ helm install ./ my-release +Logs for pod: my-release-preflight-job-bgbz6, container: pre-install-job + --- FAIL: Required Kubernetes Version + --- The application requires at least Kubernetes 1.16.0, and recommends 1.18.0. + --- FAIL: Must be MySQL 8.x or later + --- Cannot connect to MySQL server +--- FAIL preflight-tutorial +FAILED +name: cluster-resources status: completed completed: 1 total: 3 +name: mysql/mysql status: running completed: 1 total: 3 +name: mysql/mysql status: completed completed: 2 total: 3 +name: cluster-info status: running completed: 2 total: 3 +name: cluster-info status: completed completed: 3 total: 3 + +Error: INSTALLATION FAILED: failed pre-install: job failed: BackoffLimitExceeded + +``` From bb750a19d88157b70e4d9dc92abaf703fa815f8f Mon Sep 17 00:00:00 2001 From: Ian Zink Date: Tue, 23 Jul 2024 12:35:04 -0500 Subject: [PATCH 2/3] Update to use stderr in HIP Co-authored-by: Evans Mungai Signed-off-by: Ian Zink --- hips/hip-9999.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hips/hip-9999.md b/hips/hip-9999.md index 7fb6bdb5..2a786fe8 100644 --- a/hips/hip-9999.md +++ b/hips/hip-9999.md @@ -55,7 +55,7 @@ type Configuration struct { ## Backwards compatibility -The only backwards compatibility concern would be that scripts parsing `helm install` output would see some additional text in the case of logs being output. The fact that notes already make the output unstructured should mitigate any concern here. Since we already are trusting chart developers to provide output in the form of notes, this is a logical extension of that that allows the developer to provide more dynamic output. +The only backwards compatibility concern would be that scripts parsing `helm install` output would see some additional text in the case of logs being output. The fact that notes already make the output unstructured should mitigate any concern here. Since we already are trusting chart developers to provide output in the form of notes, this is a logical extension of that that allows the developer to provide more dynamic output. The logs would be written to `stderr`, further mitigating any breaking changes to scripts using `helm` CLI. ## Security implications From 63410357f9b0ad0c21c9d9abc41607fec5b5022f Mon Sep 17 00:00:00 2001 From: Ian Zink Date: Tue, 23 Jul 2024 12:35:59 -0500 Subject: [PATCH 3/3] Update to use io.Discard for SDK by default Co-authored-by: Evans Mungai Signed-off-by: Ian Zink --- hips/hip-9999.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hips/hip-9999.md b/hips/hip-9999.md index 2a786fe8..855af9be 100644 --- a/hips/hip-9999.md +++ b/hips/hip-9999.md @@ -42,7 +42,7 @@ Templates may include the following annotations on Jobs or Pods: Additionally, a new user flag should be created `--no-log-output` that would skip the output of logs. Additionally, there will be a new item added to the action SDK configuration to allow SDK consumers to get the output. -By default this output will be written to stdout, but an SDK consumer can overwrite the HookOutputFunc to provide a custom writer. +By default this output will be discarded by writing to `io.Discard`, but an SDK consumer can overwrite the HookOutputFunc to provide a custom writer. ```go