#
# MIT License
#
# (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
apiVersion: v1
kind: ConfigMap
metadata:
  name: patch-service-monitors
data:
  fix-kubelet-target-down-alert.sh: |-
    #!/bin/sh

    echo "Reconfiguring cray-sysmgmt-health-promet-kubelet servicemonitor"
    mfile=/tmp/cray-sysmgmt-health-promet-kubelet.yaml
    kubectl get servicemonitors.monitoring.coreos.com -n sysmgmt-health cray-sysmgmt-health-promet-kubelet -o yaml > $mfile
    sed -i '/\/metrics\/resource/{N;s/https-metrics/"10255"/;}' $mfile
    sed -i '/\/metrics\/resource/{N;N;N;N;N;N;s/scheme: https/scheme: http/;}' $mfile
    kubectl -n sysmgmt-health apply -f $mfile

  fix-kube-proxy-target-down-alert.sh: |-
    #!/bin/sh

    echo "Setting the metricsBindAddress in kube-proxy configmap"
    kubectl get cm/kube-proxy -n kube-system -o yaml | sed 's/metricsBindAddress.*/metricsBindAddress: 0.0.0.0:10249/' > /tmp/kube-proxy.yaml
    kubectl apply -f /tmp/kube-proxy.yaml

    echo "Restarting kube-proxy pods in kube-system namespace"
    for pod in $(kubectl get po -n kube-system | grep kube-proxy | awk '{print $1}'); do
      echo "Restarting $pod"
      kubectl -n kube-system delete pod $pod
      sleep 5
    done

  disable-alerts.sh: |-
    #!/bin/sh

    echo "Disabling etcdHighNumberOfFailedGRPCRequests alert"
    mfile=/tmp/cray-sysmgmt-health-promet-etcd.yaml
    kubectl get prometheusrules.monitoring.coreos.com -n sysmgmt-health cray-sysmgmt-health-promet-etcd -o yaml > $mfile
    sed -i '/- alert: etcdHighNumberOfFailedGRPCRequests/,/severity: critical/d' $mfile
    kubectl -n sysmgmt-health apply -f $mfile
