├── .github └── CODEOWNERS ├── epsagon-role.yaml └── epsagon_kubernetes_subscription.sh /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Each line is a file pattern followed by one or more owners. 2 | * @epsagon/the-fabulous-team 3 | -------------------------------------------------------------------------------- /epsagon-role.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Namespace 3 | metadata: 4 | name: epsagon-monitoring 5 | labels: 6 | name: epsagon-monitoring 7 | --- 8 | apiVersion: v1 9 | kind: ServiceAccount 10 | metadata: 11 | name: epsagon-monitoring 12 | namespace: epsagon-monitoring 13 | --- 14 | apiVersion: rbac.authorization.k8s.io/v1beta1 15 | kind: ClusterRole 16 | metadata: 17 | name: epsagon-prometheus 18 | rules: 19 | - apiGroups: [""] 20 | resources: 21 | - nodes 22 | - nodes/proxy 23 | - services 24 | - services/proxy 25 | - endpoints 26 | - pods 27 | - pods/proxy 28 | - pods/log 29 | - namespaces 30 | - configmaps 31 | verbs: ["get", "list", "watch"] 32 | - apiGroups: 33 | - extensions 34 | resources: 35 | - ingresses 36 | verbs: ["get", "list", "watch"] 37 | - apiGroups: ["extensions", "apps"] 38 | resources: ["deployments"] 39 | verbs: ["get", "list", "watch"] 40 | - nonResourceURLs: ["/metrics"] 41 | verbs: ["get"] 42 | --- 43 | apiVersion: rbac.authorization.k8s.io/v1beta1 44 | kind: ClusterRoleBinding 45 | metadata: 46 | name: epsagon-prometheus 47 | roleRef: 48 | apiGroup: rbac.authorization.k8s.io 49 | kind: ClusterRole 50 | name: epsagon-prometheus 51 | subjects: 52 | - kind: ServiceAccount 53 | name: epsagon-monitoring 54 | namespace: epsagon-monitoring 55 | -------------------------------------------------------------------------------- /epsagon_kubernetes_subscription.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | ## Script to attach Epsagon Role to kubernetes 4 | 5 | function usage { 6 | echo "Usage: epsagon_kubernetes_subscription.sh EPSAGON_TOKEN" 7 | } 8 | 9 | ROLE_FILE=epsagon-role.yaml 10 | ROLE_URL=https://raw.githubusercontent.com/epsagon/epsagon-k8s-role/master/epsagon-role.yaml 11 | RANCHER_TOKEN="" 12 | 13 | function track { 14 | EPSAGON_TOKEN=$1 15 | EVENT_NAME=$2 16 | EVENT_DATA=$3 17 | curl -s -X POST https://track.epsagon.com/production/record -d "{\"event_name\": \"$EVENT_NAME\", \"token\": \"$EPSAGON_TOKEN\", \"event_data\": $EVENT_DATA}" -H 'Content-Type: application/json' > /dev/null 18 | } 19 | 20 | function fetch_epsagon_role { 21 | echo "Fetching ${ROLE_FILE}" 22 | if [ -f $ROLE_FILE ] ; then 23 | echo "${ROLE_FILE} already exists - replacing it" 24 | rm -f $ROLE_FILE 25 | fi 26 | if [ `which wget` ] ; then 27 | wget $ROLE_URL 28 | else 29 | if [ `which curl` ] ; then 30 | curl $ROLE_URL -o ${ROLE_FILE} 31 | else 32 | if [ -s ${ROLE_FILE} ] ; then 33 | echo "Could not get ${ROLE_FILE}" 34 | echo "Please download the role from:" 35 | echo $ROLE_URL 36 | exit 1 37 | fi 38 | fi 39 | fi 40 | } 41 | 42 | function test_connection { 43 | SERVER=$1 44 | EPSAGON_TOKEN=$2 45 | ROLE_TOKEN=$3 46 | echo "Testing Epsagon connection to server ${SERVER}..." 47 | RESULT=`curl -X POST https://api.epsagon.com/containers/k8s/check_cluster_connection -d "{\"k8s_cluster_url\": \"$SERVER\", \"epsagon_token\": \"$EPSAGON_TOKEN\", \"cluster_token\": \"$ROLE_TOKEN\"}" -H 'Content-Type: application/json'` 48 | #Expected Response format: 49 | # { 50 | # "connection_status": "successful" / "failed", 51 | # "connection_failure_reason": "" # Optional, failure reason string, only relevant if "status"=="failed" 52 | # } 53 | CONNECTION_STATUS=`echo $RESULT | grep -o -E "\"connection_status\": \"[^\"]+\"" | awk -F\: '{print $2}'` 54 | CONNECTION_STATUS=`echo $CONNECTION_STATUS | xargs` 55 | if [ ! -z $CONNECTION_STATUS ]; then 56 | if [ "$CONNECTION_STATUS" == "successful" ]; then 57 | echo "Succesfully connected to server ${SERVER}" 58 | return 0 59 | else 60 | ERROR=`echo $RESULT | grep -o -E "\"connection_failure_reason\": \".+\"" | awk '{print $2}'` 61 | echo "Integration failed, see https://docs.epsagon.com/docs/environments-kubernetes. Error message: ${ERROR}" 62 | return 1 63 | fi 64 | else 65 | echo "Connection to Epsagon failed, please see: https://docs.epsagon.com/docs/environments-kubernetes" 66 | track $EPSAGON_TOKEN "K8s Integration Failed" "{\"K8s Cluster URL\": \"$SERVER\", \"Failure Reason\": \"Connection to Epsagon for connection check failed\"}" 67 | return 1 68 | fi 69 | } 70 | 71 | function send_to_epsagon { 72 | EPSAGON_TOKEN=$1 73 | ROLE_TOKEN=$2 74 | CONTEXT=$3 75 | if [ $# == 4 ]; then 76 | CONFIG=$4 77 | KUBECTL="kubectl config view --kubeconfig ${CONFIG}" 78 | else 79 | KUBECTL="kubectl config view" 80 | fi 81 | SERVER=`${KUBECTL} --context ${CONTEXT} | grep -B 3 -E "[[:space:]]$CONTEXT\>" | grep -E "\