From 9f28e416dbc6374dc9c7115304731a7bc0b4bfa9 Mon Sep 17 00:00:00 2001 From: Daniel Imberman Date: Tue, 24 Nov 2020 15:13:23 -0800 Subject: [PATCH] Allow webserver to read pod logs directly (#12598) * Allow webserver to read pod logs directly For users who are testing the KubernetesExecutor, allows users to read pod logs via the Kubernetes API. Worth noting that these logs will only be accessible while the worker is running. * fix tests --- chart/templates/rbac/pod-log-reader-role.yaml | 56 +++++++++++++++++++ .../rbac/pod-log-reader-rolebinding.yaml | 53 ++++++++++++++++++ chart/tests/test_basic_helm_chart.py | 4 +- chart/values.schema.json | 4 ++ chart/values.yaml | 1 + 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 chart/templates/rbac/pod-log-reader-role.yaml create mode 100644 chart/templates/rbac/pod-log-reader-rolebinding.yaml diff --git a/chart/templates/rbac/pod-log-reader-role.yaml b/chart/templates/rbac/pod-log-reader-role.yaml new file mode 100644 index 0000000000..72f5e35091 --- /dev/null +++ b/chart/templates/rbac/pod-log-reader-role.yaml @@ -0,0 +1,56 @@ +# 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. + +################################ +## Airflow Pod Reader Role +################################# +{{- if and .Values.rbacEnabled .Values.webserver.allowPodLogReading }} +{{- if .Values.multiNamespaceMode }} +kind: ClusterRole +{{- else }} +kind: Role +{{- end }} +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: {{ .Release.Name }}-pod-log-reader-role +{{- if not .Values.multiNamespaceMode }} + namespace: {{ .Release.Namespace }} +{{- end }} + labels: + tier: airflow + release: {{ .Release.Name }} + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + heritage: {{ .Release.Service }} +{{- with .Values.labels }} +{{ toYaml . | indent 4 }} +{{- end }} +rules: + - apiGroups: + - "" + resources: + - "pods" + verbs: + - "list" + - "get" + - "watch" + - apiGroups: + - "" + resources: + - "pods/log" + verbs: + - "get" +{{- end }} diff --git a/chart/templates/rbac/pod-log-reader-rolebinding.yaml b/chart/templates/rbac/pod-log-reader-rolebinding.yaml new file mode 100644 index 0000000000..25371eb5e0 --- /dev/null +++ b/chart/templates/rbac/pod-log-reader-rolebinding.yaml @@ -0,0 +1,53 @@ +# 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. + +################################ +## Airflow Pod Reader Role Binding +################################# +{{- if and .Values.rbacEnabled .Values.webserver.allowPodLogReading }} +{{- if .Values.multiNamespaceMode }} +kind: ClusterRoleBinding +{{- else }} +kind: RoleBinding +{{- end }} +apiVersion: rbac.authorization.k8s.io/v1 +metadata: +{{- if not .Values.multiNamespaceMode }} + namespace: {{ .Release.Namespace }} +{{- end }} + name: {{ .Release.Name }}-pod-log-reader-rolebinding + labels: + tier: airflow + release: {{ .Release.Name }} + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + heritage: {{ .Release.Service }} +{{- with .Values.labels }} +{{ toYaml . | indent 4 }} +{{- end }} +roleRef: + apiGroup: rbac.authorization.k8s.io +{{- if .Values.multiNamespaceMode }} + kind: ClusterRole +{{- else }} + kind: Role +{{- end }} + name: {{ .Release.Name }}-pod-log-reader-role +subjects: + - kind: ServiceAccount + name: {{ .Release.Name }}-webserver + namespace: {{ .Release.Namespace }} +{{- end }} diff --git a/chart/tests/test_basic_helm_chart.py b/chart/tests/test_basic_helm_chart.py index 89a6f5ad7c..ce1486be6b 100644 --- a/chart/tests/test_basic_helm_chart.py +++ b/chart/tests/test_basic_helm_chart.py @@ -22,7 +22,7 @@ import jmespath from tests.helm_template_generator import render_chart -OBJECT_COUNT_IN_BASIC_DEPLOYMENT = 22 +OBJECT_COUNT_IN_BASIC_DEPLOYMENT = 24 class TestBaseChartTest(unittest.TestCase): @@ -50,7 +50,9 @@ class TestBaseChartTest(unittest.TestCase): ('Secret', 'TEST-BASIC-airflow-result-backend'), ('ConfigMap', 'TEST-BASIC-airflow-config'), ('Role', 'TEST-BASIC-pod-launcher-role'), + ('Role', 'TEST-BASIC-pod-log-reader-role'), ('RoleBinding', 'TEST-BASIC-pod-launcher-rolebinding'), + ('RoleBinding', 'TEST-BASIC-pod-log-reader-rolebinding'), ('Service', 'TEST-BASIC-postgresql-headless'), ('Service', 'TEST-BASIC-postgresql'), ('Service', 'TEST-BASIC-statsd'), diff --git a/chart/values.schema.json b/chart/values.schema.json index f1d8271f03..3248b66641 100644 --- a/chart/values.schema.json +++ b/chart/values.schema.json @@ -688,6 +688,10 @@ "type": "object", "additionalProperties": false, "properties": { + "allowPodLogReading": { + "description": "Allow webserver to read k8s pod logs. Useful when you don't have an external log store.", + "type": "boolean" + }, "livenessProbe": { "description": "Liveness probe configuration.", "type": "object", diff --git a/chart/values.yaml b/chart/values.yaml index 091a0c9bd5..4e06b1f695 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -387,6 +387,7 @@ scheduler: # Airflow webserver settings webserver: + allowPodLogReading: true livenessProbe: initialDelaySeconds: 15 timeoutSeconds: 30