From 62e0b8a8f06dbc290acabab2e1f9b6fdcfe8f6fa Mon Sep 17 00:00:00 2001 From: Julien Pinsonneau Date: Fri, 12 Apr 2024 16:35:09 +0200 Subject: [PATCH] add kubectl support --- .github/workflows/push_image_pr.yml | 4 +- Makefile | 35 ++++++++---- README.md | 18 ++++--- .../netobserv-cleanup | 0 .../netobserv-flows | 6 +-- .../netobserv-packets | 8 +-- res/namespace.yml | 7 +-- scripts/functions.sh | 31 +++++++---- scripts/inject.sh | 53 +++++++++++++------ scripts/kind-cluster.sh | 8 ++- 10 files changed, 113 insertions(+), 57 deletions(-) rename oc/oc-netobserv-cleanup => commands/netobserv-cleanup (100%) rename oc/oc-netobserv-flows => commands/netobserv-flows (89%) rename oc/oc-netobserv-packets => commands/netobserv-packets (79%) diff --git a/.github/workflows/push_image_pr.yml b/.github/workflows/push_image_pr.yml index 18b82305..56695552 100644 --- a/.github/workflows/push_image_pr.yml +++ b/.github/workflows/push_image_pr.yml @@ -51,9 +51,9 @@ jobs: It will expire after two weeks. - To use this build, update your oc commands using: + To use this build, update your commands using: \`\`\`bash - USER=netobserv VERSION=${{ env.short_sha }} make oc-commands + USER=netobserv VERSION=${{ env.short_sha }} make commands \`\`\` ` }) diff --git a/Makefile b/Makefile index 34d3fd3a..e82c2ca2 100644 --- a/Makefile +++ b/Makefile @@ -33,11 +33,16 @@ OUTPUT := $(DIST_DIR)/$(NAME) COMMANDS = flows packets cleanup COMMAND_ARGS ?= +# Get either oc (favorite) or kubectl paths +K8S_CLI_BIN_PATH = $(shell which oc 2>/dev/null || which kubectl) +K8S_CLI_BIN ?= $(shell basename ${K8S_CLI_BIN_PATH}) + # IMAGE_TAG_BASE defines the namespace and part of the image name for remote images. IMAGE_TAG_BASE ?= quay.io/$(IMAGE_ORG)/$(NAME) # Image URL to use all building/pushing image targets IMAGE ?= $(IMAGE_TAG_BASE):$(VERSION) +PULL_POLICY ?=Always OCI_BUILD_OPTS ?= # Image building tool (docker / podman) - docker is preferred in CI @@ -129,13 +134,25 @@ clean: ## Clean up build directory @rm -rf $(DIST_DIR) @rm -rf $(FILES_OUTPUT_DIR) +.PHONY: commands +commands: ## Generate either oc or kubectl plugins and add them to build folder + @echo "### Generating $(K8S_CLI_BIN) commands" + DIST_DIR=$(DIST_DIR) \ + K8S_CLI_BIN=$(K8S_CLI_BIN) \ + IMAGE=$(IMAGE) \ + PULL_POLICY=$(PULL_POLICY) \ + ./scripts/inject.sh + +.PHONY: kubectl-commands +kubectl-commands: K8S_CLI_BIN=kubectl +kubectl-commands: commands ## Generate kubectl plugins and add them to build folder + .PHONY: oc-commands -oc-commands: ## Generate oc plugins and add them to build folder - @echo "### Generating oc-commands" - ./scripts/inject.sh $(DIST_DIR) $(IMAGE) +oc-commands: K8S_CLI_BIN=oc +oc-commands: commands ## Generate oc plugins and add them to build folder -.PHONY: install-oc-commands -install-oc-commands: oc-commands ## Generate oc plugins and add them to /usr/bin/ +.PHONY: install-commands +install-commands: commands ## Generate plugins and add them to /usr/bin/ sudo cp -a ./build/. /usr/bin/ .PHONY: create-kind-cluster @@ -146,14 +163,14 @@ create-kind-cluster: prereqs ## Create a kind cluster destroy-kind-cluster: KUBECONFIG=./kubeconfig destroy-kind-cluster: ## Destroy the kind cluster. test -s ./kubeconfig || { echo "kubeconfig does not exist! Exiting..."; exit 1; } - oc delete -f ./res/namespace.yml --ignore-not-found + $(K8S_CLI_BIN) delete -f ./res/namespace.yml --ignore-not-found kind delete cluster --name netobserv-cli-cluster rm ./kubeconfig .PHONY: $(COMMANDS) -$(COMMANDS): oc-commands ## Run oc command using custom image - @echo "### Running oc-netobserv-$@ using $(IMAGE)" - ./$(DIST_DIR)/oc-netobserv-$@ $(COMMAND_ARGS) +$(COMMANDS): commands ## Run command using custom image + @echo "### Running ${K8S_CLI_BIN}-netobserv-$@ using $(IMAGE)" + ./$(DIST_DIR)/${K8S_CLI_BIN}-netobserv-$@ $(COMMAND_ARGS) ##@ Images diff --git a/README.md b/README.md index a2a85686..f5b37613 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This project is still a WIP. The following list gives an overview of the current - [X] Dockerfile - [ ] Advanced filter capabilities - [ ] Testing -- [ ] Allow switching between `kubectl` / `oc` commands +- [X] Allow switching between `kubectl` / `oc` commands Feel free to contribute ! @@ -24,7 +24,7 @@ Feel free to contribute ! To run this CLI, you will need: - A running kubernetes / OpenShift cluster -- `oc` command installed and connected +- either `oc` or `kubectl` command installed and connected to your cluster - Cluster admin rights ## Build @@ -43,7 +43,7 @@ sudo dnf install -y shellcheck make build ``` -This will also copy resources and oc commands to the `build` directory. +This will also copy resources and commands to the `build` directory. ### Images @@ -139,18 +139,18 @@ This will write pcap into a single file located in `./output/pcap//dev/null || which kubectl 2>/dev/null ) +K8S_CLI_BIN=$( basename "${K8S_CLI_BIN_PATH}" ) + function loadYAMLs() { namespaceYAML=' namespaceYAMLContent @@ -39,7 +45,12 @@ function loadYAMLs() { } function clusterIsReady() { - if oc whoami 2>&1 || oc cluster-info | grep -q "Kubernetes control plane"; then + # use oc whoami as connectivity check by default and fallback to kubectl get all if needed + K8S_CLI_CONNECTIVITY="${K8S_CLI_BIN} whoami" + if [ "${K8S_CLI_BIN}" = "kubectl" ]; then + K8S_CLI_CONNECTIVITY="${K8S_CLI_BIN} get all" + fi + if ${K8S_CLI_CONNECTIVITY} 2>&1 || ${K8S_CLI_BIN} cluster-info | grep -q "Kubernetes control plane"; then return 0 else return 1 @@ -65,22 +76,22 @@ function setup { # apply yamls echo "creating netobserv-cli namespace" - echo "$namespaceYAML" | oc apply -f - + echo "$namespaceYAML" | ${K8S_CLI_BIN} apply -f - echo "creating service account" - echo "$saYAML" | oc apply -f - + echo "$saYAML" | ${K8S_CLI_BIN} apply -f - echo "creating collector service" - echo "$collectorServiceYAML" | oc apply -f - + echo "$collectorServiceYAML" | ${K8S_CLI_BIN} 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 "${flowAgentYAML/"{{FLOW_FILTER_VALUE}}"/${2:-}}" | ${K8S_CLI_BIN} apply -f - + ${K8S_CLI_BIN} rollout status daemonset netobserv-cli -n netobserv-cli --timeout 60s 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 + echo "${packetAgentYAML/"{{PCA_FILTER_VALUE}}"/${2:-}}" | ${K8S_CLI_BIN} apply -f - + ${K8S_CLI_BIN} rollout status daemonset netobserv-cli -n netobserv-cli --timeout 60s fi } @@ -89,10 +100,10 @@ function cleanup { if clusterIsReady; then echo "Copying collector output files..." mkdir -p ./output - oc cp -n netobserv-cli collector:output ./output + ${K8S_CLI_BIN} cp -n netobserv-cli collector:output ./output printf "\nCleaning up... " - oc delete namespace netobserv-cli + ${K8S_CLI_BIN} delete namespace netobserv-cli else echo "Cleanup namespace skipped" return diff --git a/scripts/inject.sh b/scripts/inject.sh index 33410e2f..edac1e8d 100755 --- a/scripts/inject.sh +++ b/scripts/inject.sh @@ -1,7 +1,36 @@ #!/bin/bash -cp -a ./oc/. ./tmp +cp -a ./commands/. ./tmp cp ./scripts/functions.sh ./tmp/functions.sh +if [ -z "$IMAGE" ]; then + echo "image not provided, keeping current ones" +else + echo "updating CLI images to $IMAGE" + sed -i "/img=/c\img=\"$IMAGE\"" ./tmp/netobserv-flows + sed -i "/img=/c\img=\"$IMAGE\"" ./tmp/netobserv-packets +fi + +if [ -z "$PULL_POLICY" ]; then + echo "pull policy not provided, keeping current ones" +else + echo "updating CLI pull policy to $PULL_POLICY" + sed -i "/ --image-pull-policy/c\ --image-pull-policy='$PULL_POLICY' \\\\" ./tmp/netobserv-flows + sed -i "/ --image-pull-policy/c\ --image-pull-policy='$PULL_POLICY' \\\\" ./tmp/netobserv-packets +fi + +if [ -z "$K8S_CLI_BIN" ]; then + echo "ERROR: K8S CLI not provided" + exit 1 +else + echo "updating K8S CLI to $K8S_CLI_BIN" + sed -i "/K8S_CLI_BIN_PATH=/d" ./tmp/functions.sh + sed -i "/K8S_CLI_BIN=/c\K8S_CLI_BIN=$K8S_CLI_BIN" ./tmp/functions.sh + + mv ./tmp/netobserv-flows ./tmp/"$K8S_CLI_BIN"-netobserv-flows + mv ./tmp/netobserv-packets ./tmp/"$K8S_CLI_BIN"-netobserv-packets + mv ./tmp/netobserv-cleanup ./tmp/"$K8S_CLI_BIN"-netobserv-cleanup +fi + # 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 @@ -9,26 +38,18 @@ sed -i -e '/flowAgentYAMLContent/{r ./res/flow-capture.yml' -e 'd}' ./tmp/functi 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 - -if [ -z "$2" ]; then - echo "image not provided, keeping current ones" -else - echo "updating CLI images to $2" - sed -i "/img=/c\img=\"$2\"" ./tmp/oc-netobserv-flows - sed -i "/img=/c\img=\"$2\"" ./tmp/oc-netobserv-packets -fi +# inject updated functions to commands +sed -i -e '/source.*/{r ./tmp/functions.sh' -e 'd}' ./tmp/"$K8S_CLI_BIN"-netobserv-flows +sed -i -e '/source.*/{r ./tmp/functions.sh' -e 'd}' ./tmp/"$K8S_CLI_BIN"-netobserv-packets +sed -i -e '/source.*/{r ./tmp/functions.sh' -e 'd}' ./tmp/"$K8S_CLI_BIN"-netobserv-cleanup rm ./tmp/functions.sh -if [ -z "$1" ]; then +if [ -z "$DIST_DIR" ]; then echo "output generated in tmp folder" else - echo "output generated in $1 folder" - cp -a ./tmp/. ./"$1" + echo "output generated in $DIST_DIR folder" + cp -a ./tmp/. ./"$DIST_DIR" rm -rf ./tmp fi diff --git a/scripts/kind-cluster.sh b/scripts/kind-cluster.sh index 87bcaf29..b5582db1 100755 --- a/scripts/kind-cluster.sh +++ b/scripts/kind-cluster.sh @@ -1,6 +1,10 @@ #!/usr/bin/env bash set -eux +# get either oc (favorite) or kubectl paths +K8S_CLI_BIN_PATH=$( which oc 2>/dev/null || which kubectl 2>/dev/null ) +K8S_CLI_BIN=$( basename "${K8S_CLI_BIN_PATH}" ) + DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && cd ../ && pwd ) KIND_CLUSTER_NAME="netobserv-cli-cluster" @@ -52,8 +56,8 @@ SVC_CIDR_IPV6=${SVC_CIDR_IPV6:-fd00:10:96::/112} # At the minimum, deploy the kind cluster deploy_kind export KUBECONFIG=${DIR}/kubeconfig -oc label node ${KIND_CLUSTER_NAME}-worker node-role.kubernetes.io/worker= -oc label node ${KIND_CLUSTER_NAME}-worker2 node-role.kubernetes.io/worker= +${K8S_CLI_BIN} label node ${KIND_CLUSTER_NAME}-worker node-role.kubernetes.io/worker= +${K8S_CLI_BIN} label node ${KIND_CLUSTER_NAME}-worker2 node-role.kubernetes.io/worker= # Print success at the end of this script print_success