#
# Copyright 2021 Kontain Inc
#
# Licensed 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.
#
# Daemon set to open access to core files
# Changes kernel setting to put cores in regular files, and maintains access to that directory
# Adapted from https://docs.microsoft.com/en-us/answers/questions/29354/how-can-i-access-the-core-dump-file-if-a-container.html
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: coredump-accessd
spec:
  selector:
    matchLabels:
      job: coredump-accessd
  template:
    metadata:
      labels:
        job: coredump-accessd
    spec:
      hostPID: true
      restartPolicy: Always
      containers:
      - image: alpine:3.12.0
        name: coredump-accessd
        securityContext:
          privileged: true
        command: ["/bin/sh"]
        args: [
           "-c",
           "nsenter -t 1 -m -- su -c \"mkdir -p /core && chmod a+rw /core && echo \\\"/core/core.%e.%P.%t\\\" > /proc/sys/kernel/core_pattern\" && while sleep 15m; do find /core -mtime +7 -delete; done"
        ]
        volumeMounts:
          - name: core
            mountPath: /core
      volumes:
         - name: core
           hostPath:
             path: /core
