-
Notifications
You must be signed in to change notification settings - Fork 25
Add hotreload.sh #2110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Molter73
wants to merge
4
commits into
master
Choose a base branch
from
mauro/hotreload-collector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add hotreload.sh #2110
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
NAMESPACE="${NAMESPACE:-stackrox}" | ||
REVERT=0 | ||
|
||
# The following line allows to symlink the script somewhere in PATH for | ||
# easier use, has no effect if run directly. | ||
REAL_SCRIPT="$(realpath "${BASH_SOURCE[0]}")" | ||
SCRIPT_DIR="$(cd -- "$( dirname -- "${REAL_SCRIPT}")" &> /dev/null && pwd)" | ||
COLLECTOR_PATH="$(realpath "${SCRIPT_DIR:-}/..")" | ||
|
||
function usage() { | ||
cat << EOF | ||
$(basename "$0") [OPTIONS] | ||
|
||
Configure a running collector pod to use a local binary. | ||
|
||
OPTIONS: | ||
-h, --help | ||
Show this help. | ||
-r, --revert | ||
Remove any configuration added by this script. | ||
-p, --path | ||
Override the path to the collector repo. | ||
-n, --namespace | ||
Set the namespace the collector daemonset is deployed to. | ||
Default: stackrox | ||
EOF | ||
} | ||
|
||
function die() { | ||
echo >&2 "$1" | ||
exit 1 | ||
} | ||
|
||
function check_command() { | ||
if ! command -v "$1" &> /dev/null; then | ||
die "$1 not found. Make sure it is in your path" | ||
fi | ||
} | ||
|
||
function krox() { | ||
kubectl -n "${NAMESPACE}" "$@" | ||
} | ||
|
||
function patch_hotreload() { | ||
cat << EOF | jq -Mc \ | ||
--arg hostPath "$COLLECTOR_PATH" \ | ||
'.spec.template.spec.volumes.[0].hostPath.path |= $hostPath' | ||
{ | ||
"spec": { | ||
"template": { | ||
"spec": { | ||
"containers": [ | ||
{ | ||
"name": "collector", | ||
"command": ["/host/src/cmake-build/collector/collector"], | ||
"volumeMounts": [ | ||
{ | ||
"mountPath": "/host/src", | ||
"name": "collector-src", | ||
"readOnly": true | ||
} | ||
] | ||
} | ||
], | ||
"volumes": [ | ||
{ | ||
"hostPath": { | ||
"path": "\$hostPath", | ||
"type": "" | ||
}, | ||
"name": "collector-src" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
EOF | ||
} | ||
|
||
function revert_command() { | ||
cat << EOF | jq -Mc | ||
{ | ||
"spec": { | ||
"template": { | ||
"spec": { | ||
"containers": [ | ||
{ | ||
"name": "collector", | ||
"command": ["collector"] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
EOF | ||
} | ||
|
||
function remove_mount() { | ||
jq -Mcn \ | ||
--arg container "$1" \ | ||
--arg volume "$2" \ | ||
'[{"op":"remove","path":"/spec/template/spec/containers/\($container)/volumeMounts/\($volume)"}]' | ||
} | ||
|
||
function remove_volume() { | ||
jq -Mcn \ | ||
--arg volume "$1" \ | ||
'[{"op":"remove","path":"/spec/template/spec/volumes/\($volume)"}]' | ||
} | ||
|
||
function find_index() { | ||
local element="$1" | ||
local filter="$2" | ||
ret="$(krox get -o json ds/collector | jq "$element | map($filter) | index(true)")" | ||
if [[ "$ret" == "null" ]]; then | ||
die "Filter failed for '$filter'" | ||
fi | ||
echo "$ret" | ||
} | ||
|
||
function add_hotreload() { | ||
krox patch ds collector -p "$(patch_hotreload)" | ||
} | ||
|
||
function revert_config() { | ||
local collector_idx | ||
collector_idx="$(find_index ".spec.template.spec.containers" '.name == "collector"')" | ||
local mount_idx | ||
mount_idx="$(find_index ".spec.template.spec.containers.[$collector_idx].volumeMounts" '.name == "collector-src"')" | ||
local volume_idx | ||
volume_idx="$(find_index ".spec.template.spec.volumes" '.name == "collector-src"')" | ||
|
||
krox patch ds collector -p "$(revert_command)" | ||
krox patch ds collector --type=json -p "$(remove_mount "$collector_idx" "$mount_idx")" | ||
krox patch ds collector --type=json -p "$(remove_volume "$volume_idx")" | ||
} | ||
|
||
check_command jq | ||
check_command kubectl | ||
|
||
if ! krox get ds/collector &> /dev/null; then | ||
die "collector daemonset not found (did you forget to deploy stackrox?)" | ||
fi | ||
|
||
TEMP=$(getopt -o 'hrp:n:' -l 'help,revert,path:,namespace:' -n "$0" -- "$@") | ||
|
||
# shellcheck disable=SC2181 | ||
if [ $? -ne 0 ]; then | ||
exit 1 | ||
fi | ||
|
||
eval set -- "$TEMP" | ||
unset TEMP | ||
|
||
while true; do | ||
case "${1:-}" in | ||
'-h' | '--help') | ||
usage | ||
exit 0 | ||
;; | ||
|
||
'-r' | '--revert') | ||
REVERT=1 | ||
shift | ||
;; | ||
|
||
'-p' | '--path') | ||
COLLECTOR_PATH="$2" | ||
shift 2 | ||
;; | ||
|
||
'-n' | '--namespace') | ||
NAMESPACE="$2" | ||
shift 2 | ||
;; | ||
|
||
'--') | ||
shift | ||
break | ||
;; | ||
esac | ||
done | ||
|
||
if ((REVERT)); then | ||
revert_config | ||
else | ||
add_hotreload | ||
fi |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to add this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I did, it has been wrong since we first added it.