From 0db99dce223ceb6fa70174a68625579682726ae0 Mon Sep 17 00:00:00 2001 From: Julien Pinsonneau Date: Tue, 5 Mar 2024 14:19:22 +0100 Subject: [PATCH] inject resources into oc commands --- .gitignore | 1 + .mk/shortcuts.mk | 2 +- Makefile | 7 +-- cmd/flow-capture.go | 6 ++- cmd/packet-capture.go | 11 ++-- oc/oc-netobserv-flows | 2 +- res/functions.sh | 52 ------------------- res/service-account.yml | 1 - scripts/functions.sh | 112 ++++++++++++++++++++++++++++++++++++++++ scripts/inject.sh | 27 ++++++++++ 10 files changed, 154 insertions(+), 67 deletions(-) delete mode 100644 res/functions.sh create mode 100755 scripts/functions.sh create mode 100755 scripts/inject.sh diff --git a/.gitignore b/.gitignore index 76f46288..ce4b558b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build output +tmp \ No newline at end of file diff --git a/.mk/shortcuts.mk b/.mk/shortcuts.mk index dd9e5094..1c1d6754 100644 --- a/.mk/shortcuts.mk +++ b/.mk/shortcuts.mk @@ -1,7 +1,7 @@ ##@ shortcuts helpers .PHONY: build -build: prereqs fmt lint vendors build ## Test and Build cli +build: prereqs fmt lint vendors compile ## Test and Build cli .PHONY: build-image build-image: image-build ## Build MULTIARCH_TARGETS images diff --git a/Makefile b/Makefile index bb501aa5..5387df25 100644 --- a/Makefile +++ b/Makefile @@ -64,6 +64,7 @@ endef .PHONY: prepare prepare: @mkdir -p $(DIST_DIR) + mkdir -p tmp .PHONY: vendors vendors: ## Check go vendors @@ -97,9 +98,9 @@ clean: @rm -rf $(DIST_DIR) .PHONY: oc-commands -oc-commands: build - cp -a ./oc/. ./$(DIST_DIR) - cp -a ./res/. ./$(DIST_DIR)/network-observability-cli-resources +oc-commands: build ## Generate oc plugins and add them to /usr/bin/ + @echo "### Generating oc-commands" + ./scripts/inject.sh $(DIST_DIR) sudo cp -a ./build/. /usr/bin/ ##@ Images diff --git a/cmd/flow-capture.go b/cmd/flow-capture.go index 3823d155..283a8372 100644 --- a/cmd/flow-capture.go +++ b/cmd/flow-capture.go @@ -30,7 +30,7 @@ var flowCmd = &cobra.Command{ } var ( - flowsToShow = 40 + flowsToShow = 35 regexes = []string{} lastFlows = []config.GenericMap{} @@ -167,7 +167,9 @@ func updateTable() { lastRefresh = now // clear terminal to render table properly - fmt.Printf("\x1bc") + fmt.Print("\x1bc") + // no wrap + fmt.Print("\033[?7l") fmt.Print("Running network-observability-cli as Flow Capture\n") fmt.Printf("Log level: %s\n", logLevel) diff --git a/cmd/packet-capture.go b/cmd/packet-capture.go index e375ca7e..ed01f778 100644 --- a/cmd/packet-capture.go +++ b/cmd/packet-capture.go @@ -1,9 +1,9 @@ package cmd import ( + "fmt" "net" "os" - "os/exec" "sync" "time" @@ -109,12 +109,9 @@ func managePcapTable(result PcapResult) { lastRefresh = now // clear terminal to render table properly - c := exec.Command("clear") - c.Stdout = os.Stdout - err := c.Run() - if err != nil { - log.Fatal(err) - } + fmt.Print("\x1bc") + // no wrap + fmt.Print("\033[?7l") log.Infof("Running network-observability-cli as Packet Capture\nLog level: %s\nFilters: %s\n", logLevel, filter) diff --git a/oc/oc-netobserv-flows b/oc/oc-netobserv-flows index 2e0be8c0..9bd92d45 100755 --- a/oc/oc-netobserv-flows +++ b/oc/oc-netobserv-flows @@ -1,5 +1,5 @@ #!/bin/bash -source "${BASH_SOURCE%/*}/network-observability-cli-resources/functions.sh" +source "./res/functions.sh" # interface filter such as 'br-ex' filter=$1 diff --git a/res/functions.sh b/res/functions.sh deleted file mode 100644 index 5705d04f..00000000 --- a/res/functions.sh +++ /dev/null @@ -1,52 +0,0 @@ -function setup { - echo "Setting up... " - - stty -F /dev/tty cbreak min 1 - stty -F /dev/tty -echo - setterm -linewrap off - - if ! output=$(oc whoami 2>&1); then - printf 'You must be connected using oc login command first\n' >&2 - exit 1 - fi - - filename="" - mkdir -p ${BASH_SOURCE%/*}/current - - if [ $1 = "flows" ]; then - filename="flow-capture" - sed "s/{{FLOW_FILTER_VALUE}}/$2/gi" ${BASH_SOURCE%/*}/flow-capture.yml > ${BASH_SOURCE%/*}/current/agent.yml - elif [ $1 = "packets" ]; then - filename="packet-capture" - sed "s/{{PCA_FILTER_VALUE}}/$2/gi" ${BASH_SOURCE%/*}/packet-capture.yml > ${BASH_SOURCE%/*}/current/agent.yml - else - echo "invalid setup argument" - return - fi - - echo "creating netobserv-cli namespace" - oc apply -f ${BASH_SOURCE%/*}/namespace.yml - - echo "creating service account" - oc apply -f ${BASH_SOURCE%/*}/service-account.yml - - echo "creating $filename agents" - oc apply -f ${BASH_SOURCE%/*}/current/agent.yml - oc rollout status daemonset netobserv-cli -n netobserv-cli --timeout 60s - - echo "creating $filename collector service" - oc apply -f ${BASH_SOURCE%/*}/collector-service.yml -} - -function cleanup { - stty -F /dev/tty echo - setterm -linewrap on - - if output=$(oc whoami 2>&1); then - printf "\nCleaning up... " - oc delete namespace netobserv-cli - else - echo "Cleanup namespace skipped" - return - fi -} diff --git a/res/service-account.yml b/res/service-account.yml index 9dfc5b06..6120cd8d 100644 --- a/res/service-account.yml +++ b/res/service-account.yml @@ -1,4 +1,3 @@ ---- apiVersion: v1 kind: ServiceAccount metadata: diff --git a/scripts/functions.sh b/scripts/functions.sh new file mode 100755 index 00000000..3cbdfdeb --- /dev/null +++ b/scripts/functions.sh @@ -0,0 +1,112 @@ +function loadYAMLs() { + namespaceYAML=' + namespaceYAMLContent + ' + if [ -f ${BASH_SOURCE%/*}/namespace.yml ]; then + namespaceYAML="`cat ${BASH_SOURCE%/*}/namespace.yml`" + fi + + saYAML=' + saYAMLContent + ' + if [ -f ${BASH_SOURCE%/*}/service-account.yml ]; then + saYAML="`cat ${BASH_SOURCE%/*}/service-account.yml`" + fi + + flowAgentYAML=' + flowAgentYAMLContent + ' + if [ -f ${BASH_SOURCE%/*}/flow-capture.yml ]; then + flowAgentYAML="`cat ${BASH_SOURCE%/*}/flow-capture.yml`" + fi + + packetAgentYAML=' + packetAgentYAMLContent + ' + if [ -f ${BASH_SOURCE%/*}/packet-capture.yml ]; then + packetAgentYAML="`cat ${BASH_SOURCE%/*}/packet-capture.yml`" + fi + + collectorServiceYAML=' + collectorServiceYAMLContent + ' + if [ -f ${BASH_SOURCE%/*}/collector-service.yml ]; then + collectorServiceYAML="`cat ${BASH_SOURCE%/*}/collector-service.yml`" + fi +} + +function setup { + echo "Setting up... " + + # check for mandatory arguments + if ! [[ $1 =~ flows|packets ]]; then + echo "invalid setup argument" + return + fi + + # check if cluster is reachable + if ! output=$(oc whoami 2>&1); then + printf 'You must be connected using oc login command first\n' >&2 + exit 1 + fi + + # load yaml files + loadYAMLs + + # apply yamls + echo "creating netobserv-cli namespace" + echo "$namespaceYAML" | oc apply -f - + + echo "creating service account" + echo "$saYAML" | oc apply -f - + + if [ $1 = "flows" ]; then + echo "creating flow-capture agents" + echo "${flowAgentYAML/"{{FLOW_FILTER_VALUE}}"/$2}" | oc apply -f - + oc rollout status daemonset netobserv-cli -n netobserv-cli --timeout 60s + + echo "creating collector service" + echo "$collectorServiceYAML" | oc apply -f - + elif [ $1 = "packets" ]; then + echo "creating packet-capture agents" + echo "${packetAgentYAML/"{{PCA_FILTER_VALUE}}"/$2}" | oc apply -f - + oc rollout status daemonset netobserv-cli -n netobserv-cli --timeout 60s + + # TODO: remove that part once pcap moved to gRPC + echo "forwarding agents ports" + pods=$(oc get pods -n netobserv-cli -l app=netobserv-cli -o name) + port=9900 + nodes="" + ports="" + for pod in $pods + do + echo "forwarding $pod:9999 to local port $port" + pkill --oldest --full "$port:9999" + oc port-forward $pod $port:9999 -n netobserv-cli & # run in background + node=$(oc get $pod -n netobserv-cli -o jsonpath='{.spec.nodeName}') + if [ -z "$ports" ] + then + nodes="$node" + ports="$port" + else + nodes="$nodes,$node" + ports="$ports,$port" + fi + port=$((port+1)) + done + + # TODO: find a better way to ensure port forward are running + sleep 2 + fi +} + +function cleanup { + # TODO: copy output folder from collector before destroying + if output=$(oc whoami 2>&1); then + printf "\nCleaning up... " + oc delete namespace netobserv-cli + else + echo "Cleanup namespace skipped" + return + fi +} diff --git a/scripts/inject.sh b/scripts/inject.sh new file mode 100755 index 00000000..da3aecad --- /dev/null +++ b/scripts/inject.sh @@ -0,0 +1,27 @@ +#!/bin/bash +cp -a ./oc/. ./tmp +cp ./scripts/functions.sh ./tmp/functions.sh + +# inject YAML files to functions.sh +sed -i -e '/namespaceYAMLContent/{r ./res/namespace.yml' -e 'd}' ./tmp/functions.sh +sed -i -e '/saYAMLContent/{r ./res/service-account.yml' -e 'd}' ./tmp/functions.sh +sed -i -e '/flowAgentYAMLContent/{r ./res/flow-capture.yml' -e 'd}' ./tmp/functions.sh +sed -i -e '/packetAgentYAMLContent/{r ./res/packet-capture.yml' -e 'd}' ./tmp/functions.sh +sed -i -e '/collectorServiceYAMLContent/{r ./res/collector-service.yml' -e 'd}' ./tmp/functions.sh + +# inject updated functions to oc commands +sed -i -e '/source.*/{r ./tmp/functions.sh' -e 'd}' ./tmp/oc-netobserv-flows +sed -i -e '/source.*/{r ./tmp/functions.sh' -e 'd}' ./tmp/oc-netobserv-packets +sed -i -e '/source.*/{r ./tmp/functions.sh' -e 'd}' ./tmp/oc-netobserv-cleanup + +rm ./tmp/functions.sh + +if [ -z "$1" ] +then + echo "output generated in tmp folder" +else + echo "output generated in $1 folder" + cp -a ./tmp/. ./$1 + rm -rf ./tmp +fi +