Skip to content

Commit

Permalink
commatrix cli plugin to add the commatrix to the command line of the oc
Browse files Browse the repository at this point in the history
  • Loading branch information
aabughosh committed Jan 7, 2025
1 parent 66b2b43 commit b31452c
Show file tree
Hide file tree
Showing 10 changed files with 989 additions and 1 deletion.
65 changes: 65 additions & 0 deletions .github/workflows/pre-main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Test Incoming Changes

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

env:
CM_BIN: /usr/local/bin/checkmake
CM_URL_LINUX: https://github.com/mrtazz/checkmake/releases/download/0.2.2/checkmake-0.2.2.linux.amd64 # yamllint disable-line

jobs:
lint:
name: Run Linters and Vet
runs-on: ubuntu-22.04
env:
SHELL: /bin/bash

steps:
- name: Set up Go 1.23
uses: actions/setup-go@v5 # Updated to use version tag instead of commit hash
with:
go-version: 1.23.1

- name: Disable default go problem matcher
run: echo "::remove-matcher owner=go::"

- name: Check out code
uses: actions/checkout@v4 # Updated to use version tag instead of commit hash
with:
ref: ${{ github.sha }}

- name: Extract dependent Pull Requests
uses: depends-on/depends-on-action@main # Using the latest stable release tag
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Install checkmake
run: |
curl --location --output $CM_BIN --silent $CM_URL_LINUX
chmod +x $CM_BIN
- name: Install shfmt (if shell scripts are used)
run: |
curl -sSfL https://github.com/mvdan/sh/releases/download/v3.5.0/shfmt_v3.5.0_linux_amd64 -o /usr/local/bin/shfmt
chmod +x /usr/local/bin/shfmt
- name: Golangci-lint
uses: golangci/[email protected] # Version tag used consistently
with:
version: v1.60
args: --timeout 10m0s

- name: Checkmake
run: checkmake --config=.checkmake Makefile

- name: Markdownlint
uses: nosborn/[email protected] # Version tag used
with:
files: .

- name: Run tests
run: make test # Corrected from 'Eun test' to 'Run test'
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kubectl-commatrix

bin/
vendor/
.vscode/
37 changes: 37 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
run:
issues-exit-code: 2
linters:
enable:
- godot
- goimports
- gofmt
- ginkgolinter
- dogsled
- exportloopref
- gocritic
- misspell
- nolintlint
- stylecheck
- unconvert
- unparam
- whitespace
- revive
- unused
- wastedassign
linters-settings:
godot:
scope: toplevel
capital: true
exclude:
- 'SPDX-License-Identifier.*'
- '\+groupName.*'
gocritic:
disabled-checks:
- captLocal
- exitAfterDefer
revive:
rules:
- name: receiver-naming
disabled: true
- name: dot-imports
disabled: true
59 changes: 59 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
GO_SRC := cmd/kubectl-commatrix.go
EXECUTABLE := kubectl-commatrix
.DEFAULT_GOAL := run
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
CURPATH=$(PWD)
BIN_DIR=$(CURPATH)/bin
BASH_SCRIPTS=$(shell find . -name "*.sh" -not -path "./.git/*")


.PHONY: all build deps-update check-deps fmt-code lint lint-go lint-shell lint-md lint-sh test clean

# Default target
all: lint test

# Build the executable
build:
go build -o $(EXECUTABLE) $(GO_SRC)

# Update dependencies
deps-update:
go mod tidy

# Check if go modules are up to date
check-deps: deps-update
@set +e; git diff --quiet HEAD go.sum go.mod; \
if [ $$? -eq 1 ]; \
then echo -e "\ngo modules are out of date. Please commit after running 'make deps-update' command\n"; \
exit 1; fi

# Run go fmt against code
fmt-code:
go fmt ./...

# Lint the project
lint: lint-go lint-shell lint-md lint-sh

# Run GolangCI-Lint
lint-go:
checkmake --config=.checkmake Makefile
golangci-lint run --timeout 10m0s

# Lint shell scripts
lint-shell:
shfmt -d scripts/*.sh
shellcheck --format=gcc ${BASH_SCRIPTS}

# Lint Markdown files
lint-md:
typos
markdownlint '**/*.md'

# Run tests
test:
go test ./...

# Clean target to remove generated files
clean:
rm -f $(EXECUTABLE) $(BIN_DIR)/*
76 changes: 75 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,75 @@
# commatrix-cli-plugin
# `kubectl commatrix` Plugin

The `kubectl commatrix` plugin enhances your Kubernetes CLI experience by
providing an easy-to-use command for generating a detailed and up-to-date
communication matrix. This tool leverages the [commatrix](https://github.com/openshift-kni/commatrix)
project to simplify the process of visualizing and documenting network
communication flows in OpenShift clusters.

---

## Overview

The `kubectl commatrix` plugin integrates the powerful capabilities of the
commatrix library directly into your Kubernetes command-line interface. It
enables users to automatically generate a communication flows matrix for OpenShift
deployments, including both **multi-node** and **single-node OpenShift (SNO)**
clusters. This communication matrix can be used for:

- Understanding and documenting ingress traffic flows.
- Assisting with troubleshooting network communication.
- Generating product documentation for customers.

---

## How It Works

The `kubectl commatrix` plugin uses the commatrix library to analyze the `EndpointSlice`
resource in your cluster. It inspects the following:

- **Host-networked Pods**: Identifies host-networked pods and their ingress flows.
- **NodePort Services**: Collects information about NodePort services.
- **LoadBalancer Services**: Tracks traffic entering the cluster through
LoadBalancer services.

By combining these data sources, the plugin generates a detailed communication matrix
for all ingress traffic in your cluster.

---

## Installation

### Prerequisites

- Kubernetes CLI (`kubectl`) installed and configured to access your cluster.
- Go installed for building the plugin, or download a pre-built binary (if available).

---

## Running

```sh
# assumes you have a working KUBECONFIG
$ go build cmd/kubectl-commatrix.go
# place the built binary somewhere in your PATH
$ cp ./kubectl-commatrix /usr/local/bin

# you can now begin using this plugin as a regular kubectl command:
# update your configuration to point to "new-namespace"
$ kubectl commatrix generate
```

---

## Example Output

Once you run the `kubectl commatrix generate` command, the plugin will
generate a communication matrix based on the ingress flows in your
OpenShift cluster. The output will be displayed in a tabular format,
similar to the following:

| Direction | Protocol | Port | Namespace | Service |
|-----------|----------|------|------------------------|----------------------|
| Pod | Container | Node Role | Optional |
| Ingress | TCP | 22 | Host system service | sshd |
| Ingress | TCP | 111 | Host system service | rpcbind |
20 changes: 20 additions & 0 deletions cmd/kubectl-commatrix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"commatrix-cli-plugin/pkg/cmd"
"os"

"github.com/spf13/pflag"

"k8s.io/cli-runtime/pkg/genericiooptions"
)

func main() {
flags := pflag.NewFlagSet("kubectl-commatrix", pflag.ExitOnError)
pflag.CommandLine = flags

root := cmd.NewCmdCommatrix(genericiooptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
if err := root.Execute(); err != nil {
os.Exit(1)
}
}
102 changes: 102 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
module commatrix-cli-plugin

go 1.22.4

require (
github.com/golang/mock v1.6.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/moby/spdystream v0.4.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/openshift/api v0.0.0-20241009131553-a1523024209f // indirect
github.com/openshift/client-go v0.0.0-20240906181530-b2f7c4ab0984 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.20.4 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.5.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.31.3 // indirect
k8s.io/apiextensions-apiserver v0.31.1 // indirect
k8s.io/apimachinery v0.31.3 // indirect
k8s.io/client-go v0.31.3 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/controller-runtime v0.19.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.17.2 // indirect
sigs.k8s.io/kustomize/kyaml v0.17.1 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/openshift-kni/commatrix v0.0.0-20241126103601-3ef458cbd1e8
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace
k8s.io/cli-runtime v0.31.3
k8s.io/kubectl v0.31.3
)
Loading

0 comments on commit b31452c

Please sign in to comment.