From 32aed71b84552fe7f309d5be14d980fa94022776 Mon Sep 17 00:00:00 2001 From: Martin Shwalbe Date: Thu, 20 Oct 2022 12:37:27 +0200 Subject: [PATCH 1/7] Add Cluster context to kubectl --- collect-logs.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/collect-logs.sh b/collect-logs.sh index c35d7b5..6ff90e9 100755 --- a/collect-logs.sh +++ b/collect-logs.sh @@ -3,9 +3,30 @@ # 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 setting and DTrend 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. +-corefilepattern [Optional] S[ecifies the core dump file name prefix pattern. The default value is 'core'. +-resultdir [Optional] Specifies the directory to save the logs. +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 +45,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." From 6818d28958a70c63084ec19c2a5edfba1ddc1e90 Mon Sep 17 00:00:00 2001 From: Martin Shwalbe Date: Thu, 20 Oct 2022 12:47:06 +0200 Subject: [PATCH 2/7] Update Readme for collection logs --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a67bd94..d8570bd 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 | +|------------|:-------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------| +| -reelease | 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`. | From 2a607d586ef701655af968da87efa9275ba4706a Mon Sep 17 00:00:00 2001 From: Martin Shwalbe Date: Fri, 21 Oct 2022 10:17:23 +0200 Subject: [PATCH 3/7] Update README.md Co-authored-by: Johan Cornelissen --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d8570bd..80928cc 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,6 @@ The following environment parameters are supported for log collection: | Parameters | Description | Default | |------------|:-------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------| -| -reelease | Helm release name | `trendmicro` | +| -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`. | From 0fef21ffec633aebeeaedaae09f6640f6c43b443 Mon Sep 17 00:00:00 2001 From: Martin Shwalbe Date: Fri, 21 Oct 2022 10:17:52 +0200 Subject: [PATCH 4/7] Update collect-logs.sh Co-authored-by: Johan Cornelissen --- collect-logs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collect-logs.sh b/collect-logs.sh index 6ff90e9..97b2c64 100755 --- a/collect-logs.sh +++ b/collect-logs.sh @@ -16,7 +16,7 @@ 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. --corefilepattern [Optional] S[ecifies the core dump file name prefix pattern. The default value is 'core'. +-corefilepattern [Optional] Specifies the core dump file name prefix pattern. The default value is 'core'. -resultdir [Optional] Specifies the directory to save the logs. Usage examples: # Display this help From becb79029b4f251d26600342ef7cde605c46d4df Mon Sep 17 00:00:00 2001 From: Martin Shwalbe Date: Fri, 21 Oct 2022 10:19:23 +0200 Subject: [PATCH 5/7] Update collect-logs.sh --- collect-logs.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/collect-logs.sh b/collect-logs.sh index 97b2c64..79a52fb 100755 --- a/collect-logs.sh +++ b/collect-logs.sh @@ -17,7 +17,6 @@ Options: -namespace [Optional] Specifies the the namespace of Trend Micro Cloud One container security deployment. The default is the current namespace or default. -corefilepattern [Optional] Specifies the core dump file name prefix pattern. The default value is 'core'. --resultdir [Optional] Specifies the directory to save the logs. Usage examples: # Display this help ./collect-logs.sh -h | H From 67bcec24b16b739ee349950a88fff6492a3d2f50 Mon Sep 17 00:00:00 2001 From: Martin Shwalbe Date: Fri, 21 Oct 2022 10:19:29 +0200 Subject: [PATCH 6/7] Update collect-logs.sh --- collect-logs.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/collect-logs.sh b/collect-logs.sh index 79a52fb..083f78a 100755 --- a/collect-logs.sh +++ b/collect-logs.sh @@ -16,7 +16,6 @@ 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. --corefilepattern [Optional] Specifies the core dump file name prefix pattern. The default value is 'core'. Usage examples: # Display this help ./collect-logs.sh -h | H From 9eb879eb0a17797a35cacbf07b87ab94094eb46d Mon Sep 17 00:00:00 2001 From: Martin Shwalbe Date: Tue, 8 Nov 2022 10:12:31 +0100 Subject: [PATCH 7/7] Fix parameters Help Text output --- collect-logs.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collect-logs.sh b/collect-logs.sh index 083f78a..6c420ec 100755 --- a/collect-logs.sh +++ b/collect-logs.sh @@ -11,7 +11,7 @@ CONTEXT="" help() { cat << EOF -Helper script to fetch Kubernetes setting and DTrend Micro Cloud One container security logs. +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.