Skip to content

Commit

Permalink
inject resources into oc commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinsonneau committed Mar 5, 2024
1 parent 284e5e8 commit 0db99dc
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
output
tmp
2 changes: 1 addition & 1 deletion .mk/shortcuts.mk
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ endef
.PHONY: prepare
prepare:
@mkdir -p $(DIST_DIR)
mkdir -p tmp

.PHONY: vendors
vendors: ## Check go vendors
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions cmd/flow-capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var flowCmd = &cobra.Command{
}

var (
flowsToShow = 40
flowsToShow = 35
regexes = []string{}
lastFlows = []config.GenericMap{}

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions cmd/packet-capture.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cmd

import (
"fmt"
"net"
"os"
"os/exec"
"sync"
"time"

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion oc/oc-netobserv-flows
Original file line number Diff line number Diff line change
@@ -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
Expand Down
52 changes: 0 additions & 52 deletions res/functions.sh

This file was deleted.

1 change: 0 additions & 1 deletion res/service-account.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
apiVersion: v1
kind: ServiceAccount
metadata:
Expand Down
112 changes: 112 additions & 0 deletions scripts/functions.sh
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 additions & 0 deletions scripts/inject.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0db99dc

Please sign in to comment.