diff --git a/README.md b/README.md index a67bd94..80928cc 100644 --- a/README.md +++ b/README.md @@ -235,9 +235,10 @@ Gather logs using the following command: ./collect-logs.sh ``` -The following environment variables are supported for log collection: +The following environment parameters are supported for log collection: -| Environment variable | Description | Default | -| ------------------------- |:----------------------------------------|:-----------------------------------------------------------------------------------------------| -| RELEASE | Helm release name | `trendmicro` | -| NAMESPACE | The namespace that the helm chart is deployed in | Current namespace declared in `kubeconfig`. If no namespace setting exists in `kubeconfig`, then `trendmicro-system` will be used. | +| Parameters | Description | Default | +|------------|:-------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------| +| -release | Helm release name | `trendmicro` | +| -namespace | The namespace that the helm chart is deployed in | Current namespace declared in `kubeconfig`. If no namespace setting exists in `kubeconfig`, then `trendmicro-system` will be used. | +| -context | The Kluster context that the helm chart is deployed in | Current cluster declared in `kubeconfig`. | diff --git a/collect-logs.sh b/collect-logs.sh index c35d7b5..6c420ec 100755 --- a/collect-logs.sh +++ b/collect-logs.sh @@ -3,9 +3,28 @@ # a helper script to fetch Kubernetes settings and Trend Micro Cloud One container security logs. # -RELEASE=${RELEASE:-trendmicro} +RELEASE="trendmicro" KUBECTL=kubectl HELM=helm +CONTEXT="" + +help() +{ +cat << EOF +Helper script to fetch Kubernetes settings and Trend Micro Cloud One container security logs. +Options: +-release [Optional] Specifies the Trend Micro Cloud One container security release name. The default is trendmicro +-namespace [Optional] Specifies the the namespace of Trend Micro Cloud One container security deployment. + The default is the current namespace or default. +Usage examples: +# Display this help +./collect-logs.sh -h | H +# Collect logs for the default release, namespace and context +./collect-logs.sh +# Collect logs for the named release, namespace and context +./collect-logs.sh -release deepsecurity-smartcheck -namespace trendmicro -context kubernetes-cluster +EOF +} ##### # check prerequisites @@ -24,11 +43,48 @@ if ! command_exists $HELM; then exit 1 fi +while [[ $# -gt 0 ]] +do + key="$1" + case $key in + -h|-H) + help + exit 0 + ;; + -release) + RELEASE=$2 + shift + shift + ;; + -namespace) + NAMESPACE=$2 + shift + shift + ;; + -context) + CONTEXT=$2 + shift + shift + ;; + *) + echo "Unrecognized options are specified: $1" + echo "Use option -h for help." + exit 1 + ;; + esac +done + CURRENT_NS=$(kubectl config view --minify --output 'jsonpath={..namespace}') CURRENT_NS=${CURRENT_NS:-trendmicro-system} NAMESPACE=${NAMESPACE:-$CURRENT_NS} NAMESPACE_PARAM="--namespace=$NAMESPACE" +CURRENT_CONTEXT=$(kubectl config view --minify --output 'jsonpath={.current-context}') +CONTEXT=${CONTEXT:-$CURRENT_CONTEXT} +CONTEXT_PARAM="--context=$CONTEXT" + +KUBECTL="$KUBECTL $CONTEXT_PARAM" + PODS=$($KUBECTL get pods "$NAMESPACE_PARAM" -o=jsonpath='{range .items[*]}{.metadata.name}{";"}{end}' -l app.kubernetes.io/instance=$RELEASE) if [ -z "${PODS}" ]; then echo "No container security pods are found in release '$RELEASE' in namespace '$NAMESPACE'. You can use RELEASE and NAMESPACE environment variable to change its default settings."