| 
 | 1 | +#!/bin/bash  | 
 | 2 | +#set -x #echo on  | 
 | 3 | + | 
 | 4 | +#===========================================================  | 
 | 5 | +# This script was written by Kendall Adkins to generate  | 
 | 6 | +# manifest files using the sysdig-deploy helm chart.  | 
 | 7 | +#  | 
 | 8 | +# Please direct any questions to [email protected]  | 
 | 9 | +#  | 
 | 10 | +# Risk! In the event that the helm template output changes   | 
 | 11 | +# as a result of helm chart updates, this script could break.  | 
 | 12 | +#  | 
 | 13 | +# Date: November 2th, 2022  | 
 | 14 | +# Updated: February 14th, 2023  | 
 | 15 | +#  | 
 | 16 | +#===========================================================  | 
 | 17 | + | 
 | 18 | +function help {  | 
 | 19 | +  echo ""  | 
 | 20 | +  echo "Script: $(basename ${0})"  | 
 | 21 | +  echo ""  | 
 | 22 | +  echo "Description: This script will generate manifests using the sysdig-deploy helm chart."  | 
 | 23 | +  echo "             Helm must be installed and a helm values file is required."  | 
 | 24 | +  echo ""  | 
 | 25 | +  echo "Usage: $(basename ${0}) [ -f <helm values file>] [ -n <namespace>] [ -p <prefix>] [ -h]"  | 
 | 26 | +  echo ""  | 
 | 27 | +  echo "options:"  | 
 | 28 | +  echo "  -f, --helm-values-file: the helm values file (required)"  | 
 | 29 | +  echo "  -n, --namespace: the kubernetes namespace used in the manifests (default: sysdig-agent)"  | 
 | 30 | +  echo "  -p, --manifests-prefix: the prefix used in the manifest output file names (optional)"  | 
 | 31 | +  echo "  -h, --help: display this help message"  | 
 | 32 | +  echo ""  | 
 | 33 | +}  | 
 | 34 | + | 
 | 35 | +function is_valid_value {  | 
 | 36 | +  if [[ ${1} == -* ]] || [[ ${1} == --* ]] || [[ -z ${1} ]]; then  | 
 | 37 | +    return 1  | 
 | 38 | +  else  | 
 | 39 | +    return 0  | 
 | 40 | +  fi  | 
 | 41 | +}  | 
 | 42 | + | 
 | 43 | +function is_valid_file {  | 
 | 44 | +  if [ ! -f ${1} ]; then  | 
 | 45 | +    return 1  | 
 | 46 | +  else  | 
 | 47 | +    return 0  | 
 | 48 | +  fi  | 
 | 49 | +}  | 
 | 50 | + | 
 | 51 | +#  | 
 | 52 | +# validate and load the arguments  | 
 | 53 | +#  | 
 | 54 | +HELM_VALUES_FILE=""  | 
 | 55 | +NAMESPACE="sysdig-agent"  | 
 | 56 | +MANIFEST_PREFIX=""  | 
 | 57 | + | 
 | 58 | +if [[ ${#} -eq 0 ]]; then  | 
 | 59 | +  help  | 
 | 60 | +  exit 1  | 
 | 61 | +fi  | 
 | 62 | + | 
 | 63 | +while [[ ${#} > 0 ]]  | 
 | 64 | +do  | 
 | 65 | + | 
 | 66 | +  case ${1} in  | 
 | 67 | +      | 
 | 68 | +    -f|--helm-values-file)  | 
 | 69 | +      if is_valid_value "${2}"; then  | 
 | 70 | +        if is_valid_file "${2}"; then  | 
 | 71 | +          HELM_VALUES_FILE=${2}  | 
 | 72 | +        else  | 
 | 73 | +          echo "ERROR: Helm values file does not exist"  | 
 | 74 | +          echo "Use -h | --help for $(basename ${0}) usage."  | 
 | 75 | +          exit 1  | 
 | 76 | +        fi  | 
 | 77 | +      else  | 
 | 78 | +        echo "ERROR: Invalid argument for helm values file"  | 
 | 79 | +        echo "Use -h | --help for $(basename ${0}) usage."  | 
 | 80 | +        exit 1  | 
 | 81 | +      fi  | 
 | 82 | +      shift  | 
 | 83 | +      ;;  | 
 | 84 | + | 
 | 85 | +    -n|--namespace)  | 
 | 86 | +      if is_valid_value "${2}"; then  | 
 | 87 | +        NAMESPACE=${2}  | 
 | 88 | +      else  | 
 | 89 | +        echo "ERROR: Invalid argument for namespace"  | 
 | 90 | +        echo "Use -h | --help for $(basename ${0}) usage."  | 
 | 91 | +        exit 1  | 
 | 92 | +      fi  | 
 | 93 | +      shift  | 
 | 94 | +      ;;  | 
 | 95 | + | 
 | 96 | +    -p|--manifests-prefix)  | 
 | 97 | +      if is_valid_value "${2}"; then  | 
 | 98 | +        MANIFEST_PREFIX=${2}  | 
 | 99 | +      else  | 
 | 100 | +        echo "ERROR: Invalid argument for manifest prefix"  | 
 | 101 | +        echo "Use -h | --help for $(basename ${0}) usage."  | 
 | 102 | +        exit 1  | 
 | 103 | +      fi  | 
 | 104 | +      shift  | 
 | 105 | +      ;;  | 
 | 106 | + | 
 | 107 | +    -h|--help)  | 
 | 108 | +      help  | 
 | 109 | +      exit 1  | 
 | 110 | +      ;;  | 
 | 111 | + | 
 | 112 | +    *)   | 
 | 113 | +      echo "ERROR: Invalid option: ${1}, use -h | --help for $(basename ${0}) usage."  | 
 | 114 | +      exit 1  | 
 | 115 | +      ;;  | 
 | 116 | + | 
 | 117 | +  esac  | 
 | 118 | +  shift  | 
 | 119 | +done  | 
 | 120 | + | 
 | 121 | +#  | 
 | 122 | +# validate that helm is installed  | 
 | 123 | +#  | 
 | 124 | +echo "STATUS: Checking for Helm..."  | 
 | 125 | +if ! [ -x "$(command -v helm)" ]  | 
 | 126 | +then  | 
 | 127 | +  echo "Error: Helm not installed! See: https://helm.sh/docs/intro/install/"  | 
 | 128 | +  exit 1  | 
 | 129 | +fi  | 
 | 130 | + | 
 | 131 | +#  | 
 | 132 | +# Update the chart and use helm template to  | 
 | 133 | +# generate the manifests  | 
 | 134 | +#  | 
 | 135 | +TIMESTAMP=`date +"%Y-%m-%d_%H-%M-%S"`  | 
 | 136 | +TEMPLATE_FILE=helm-template-${TIMESAMP}.txt  | 
 | 137 | + | 
 | 138 | +echo "STATUS: Executing helm template to generate the Sysdig manifests"  | 
 | 139 | + | 
 | 140 | +helm repo add sysdig https://charts.sysdig.com --force-update > /dev/null  | 
 | 141 | +helm_exit_code=$?  | 
 | 142 | +if [ $helm_exit_code -ne 0 ]; then  | 
 | 143 | +  echo "ERROR: Unable to add Sysdig helm charts: https://charts.sysdig.com"  | 
 | 144 | +  exit 1  | 
 | 145 | +fi  | 
 | 146 | + | 
 | 147 | +helm template -f ${HELM_VALUES_FILE} sysdig-agent --namespace ${NAMESPACE} --skip-tests \  | 
 | 148 | +	sysdig/sysdig-deploy > ${TEMPLATE_FILE}  | 
 | 149 | +	#sysdig/sysdig-deploy --version 1.3.13 > ${TEMPLATE_FILE}  | 
 | 150 | +helm_exit_code=$?  | 
 | 151 | +if [ $helm_exit_code -ne 0 ]; then  | 
 | 152 | +  echo "ERROR: Helm template generation failed. This is likely due to an invalid values file."  | 
 | 153 | +  exit 1  | 
 | 154 | +fi  | 
 | 155 | + | 
 | 156 | +#  | 
 | 157 | +# Parse the helm output into individual files  | 
 | 158 | +#  | 
 | 159 | +echo "STATUS: Parsing helm template output file"  | 
 | 160 | +input=${TEMPLATE_FILE}  | 
 | 161 | +found_seperator="false"  | 
 | 162 | +found_manifest_name="false"  | 
 | 163 | +manifest_name_list=()  | 
 | 164 | + | 
 | 165 | +item_in_array() {  | 
 | 166 | +    local item_to_find=$1  | 
 | 167 | +    shift  | 
 | 168 | +    local array=("$@")  | 
 | 169 | + | 
 | 170 | +    local found=false  | 
 | 171 | + | 
 | 172 | +    for element in "${array[@]}"; do  | 
 | 173 | +        if [ "$element" = "$item_to_find" ]; then  | 
 | 174 | +            found=true  | 
 | 175 | +            break  | 
 | 176 | +        fi  | 
 | 177 | +    done  | 
 | 178 | + | 
 | 179 | +    # Return 0 if found, 1 if not found  | 
 | 180 | +    if [ "$found" = true ]; then  | 
 | 181 | +        return 0  # Found  | 
 | 182 | +    else  | 
 | 183 | +        return 1  # Not found  | 
 | 184 | +    fi  | 
 | 185 | +}  | 
 | 186 | + | 
 | 187 | +added_lines="false"  | 
 | 188 | + | 
 | 189 | +while IFS= read -r line  | 
 | 190 | +do  | 
 | 191 | +  if [ "$found_manifest_name" = "true" ] && [ "$line" != "---" ] && [ "$duplicate_manifest" = "false" ]; then   | 
 | 192 | +    echo "$line" >> $manifest_name  | 
 | 193 | +  fi  | 
 | 194 | +  if [ "$found_manifest_name" = "true" ] && [ "$line" != "---" ] && [ "$duplicate_manifest" = "true" ] && [ "$added_lines" = "false" ]; then   | 
 | 195 | +    echo "---" >> $manifest_name  | 
 | 196 | +    echo "$line" >> $manifest_name  | 
 | 197 | +    duplicate_manifest="false"  | 
 | 198 | +    added_lines="true"  | 
 | 199 | +  fi  | 
 | 200 | +  if [ "$found_seperator" = "true" ]; then  | 
 | 201 | +    if [ "$line" = "---" ]; then   | 
 | 202 | +      continue  | 
 | 203 | +    fi  | 
 | 204 | +    manifest_name="${line#*/}"  | 
 | 205 | +    manifest_name="${manifest_name#charts\/}"  | 
 | 206 | +    manifest_name="${manifest_name//templates\//}"  | 
 | 207 | +    manifest_name="${manifest_name//\//-}"  | 
 | 208 | +      | 
 | 209 | +    item_in_array "$manifest_name" "${manifest_name_list[@]}"  | 
 | 210 | + | 
 | 211 | +    # Check the return value of the function  | 
 | 212 | +    if [ $? -eq 0 ]; then  | 
 | 213 | +        echo "Item '$manifest_name' found in the array."  | 
 | 214 | +        duplicate_manifest="true"  | 
 | 215 | +        added_lines="false"  | 
 | 216 | +        found_seperator="false"  | 
 | 217 | +        found_manifest_name="true"  | 
 | 218 | +        continue  | 
 | 219 | +    fi  | 
 | 220 | + | 
 | 221 | +    manifest_name_list+=($manifest_name)  | 
 | 222 | + | 
 | 223 | +    duplicate_manifest="false"  | 
 | 224 | +    found_seperator="false"  | 
 | 225 | +    found_manifest_name="true"  | 
 | 226 | +  fi  | 
 | 227 | +  if [ "$line" = "---" ]; then   | 
 | 228 | +    found_seperator="true"  | 
 | 229 | +    found_manifest_name="false"  | 
 | 230 | +  fi  | 
 | 231 | +done < $input  | 
 | 232 | + | 
 | 233 | +rm ${TEMPLATE_FILE}  | 
 | 234 | + | 
 | 235 | +#  | 
 | 236 | +# Rename the raw helm template names using   | 
 | 237 | +# a standard manifest naming convention  | 
 | 238 | +#  | 
 | 239 | +echo "STATUS: Renaming parsed manifest files"  | 
 | 240 | +prefix=${MANIFEST_PREFIX}  | 
 | 241 | +mv agent-role.yaml ${prefix}sa-r.yaml 2> /dev/null  | 
 | 242 | +mv agent-rolebinding.yaml ${prefix}sa-rb.yaml 2> /dev/null  | 
 | 243 | +mv agent-clusterrole.yaml ${prefix}sa-cr.yaml 2> /dev/null  | 
 | 244 | +mv agent-clusterrolebinding.yaml ${prefix}sa-crb.yaml 2> /dev/null  | 
 | 245 | +mv agent-configmap.yaml ${prefix}sa-cm.yaml 2> /dev/null  | 
 | 246 | +mv agent-daemonset.yaml ${prefix}sa-ds.yaml 2> /dev/null  | 
 | 247 | +mv agent-psp.yaml ${prefix}sa-psp.yaml 2> /dev/null  | 
 | 248 | +mv agent-secrets.yaml ${prefix}sa-se.yaml 2> /dev/null  | 
 | 249 | +mv agent-serviceaccount.yaml ${prefix}sa-sa.yaml 2> /dev/null  | 
 | 250 | +mv nodeAnalyzer-clusterrole-node-analyzer.yaml ${prefix}sana-cr.yaml 2> /dev/null  | 
 | 251 | +mv nodeAnalyzer-clusterrolebinding-node-analyzer.yaml ${prefix}sana-crb.yaml 2> /dev/null  | 
 | 252 | +mv nodeAnalyzer-daemonset-node-analyzer.yaml ${prefix}sana-ds.yaml 2> /dev/null  | 
 | 253 | +mv nodeAnalyzer-psp.yaml ${prefix}sana-psp.yaml 2> /dev/null  | 
 | 254 | +mv nodeAnalyzer-runtimeScanner-runtime-scanner-configmap.yaml ${prefix}sana-rs-cm.yaml 2> /dev/null  | 
 | 255 | +mv nodeAnalyzer-secrets.yaml ${prefix}sana-se.yaml 2> /dev/null  | 
 | 256 | +mv nodeAnalyzer-serviceaccount-node-analyzer.yaml ${prefix}sana-sa.yaml 2> /dev/null  | 
 | 257 | +mv admissionController-webhook-admissioncontrollerconfigmap.yaml ${prefix}sa-ac-webhook-ac-cm.yaml 2> /dev/null  | 
 | 258 | +mv admissionController-webhook-admissionregistration.yaml ${prefix}sa-ac-webhook-tls-se.yaml 2> /dev/null  | 
 | 259 | +mv admissionController-webhook-clusterrole.yaml ${prefix}sa-ac-webhook-cr.yaml 2> /dev/null  | 
 | 260 | +mv admissionController-webhook-clusterrolebinding.yaml ${prefix}sa-ac-webhook-crb.yaml 2> /dev/null  | 
 | 261 | +mv admissionController-webhook-configmap.yaml ${prefix}sa-ac-webhook-cm.yaml 2> /dev/null  | 
 | 262 | +mv admissionController-webhook-deployment.yaml ${prefix}sa-ac-webhook-de.yaml 2> /dev/null  | 
 | 263 | +mv admissionController-webhook-secret.yaml ${prefix}sa-ac-webhook-se.yaml 2> /dev/null  | 
 | 264 | +mv admissionController-webhook-service.yaml ${prefix}sa-ac-webhook-svc.yaml 2> /dev/null  | 
 | 265 | +mv admissionController-webhook-serviceaccount.yaml ${prefix}sa-ac-webhook-sa.yaml 2> /dev/null  | 
 | 266 | +mv nodeAnalyzer-configmap-kspm-analyzer.yaml sana-kspm-cm.yaml 2> /dev/null  | 
 | 267 | +mv nodeAnalyzer-configmap-host-scanner.yaml sana-hs-cm.yaml 2> /dev/null  | 
 | 268 | +mv kspmCollector-clusterrole.yaml sa-kspm-cr.yaml 2> /dev/null  | 
 | 269 | +mv kspmCollector-clusterrolebinding.yaml sa-kspm-crb.yaml 2> /dev/null  | 
 | 270 | +mv kspmCollector-configmap.yaml sa-kspm-cm.yaml 2> /dev/null  | 
 | 271 | +mv kspmCollector-deployment.yaml sa-kspm-de.yaml 2> /dev/null  | 
 | 272 | +mv kspmCollector-secret.yaml sa-kspm-se.yaml 2> /dev/null  | 
 | 273 | +mv kspmCollector-serviceaccount.yaml sa-kspm-sa.yaml 2> /dev/null  | 
 | 274 | +#  | 
 | 275 | +# Done  | 
 | 276 | +#  | 
 | 277 | +echo "SUCCESS: Sysdig deployment manifests generation complete."  | 
0 commit comments