Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rewanthtammana/kubectl-fields

Kubernetes resources hierarchy parsing tool
https://github.com/rewanthtammana/kubectl-fields

cka-exam go golang kubectl kubernetes

Last synced: about 1 month ago
JSON representation

Kubernetes resources hierarchy parsing tool

Awesome Lists containing this project

README

        

kubectl-fields

Kubectl resources hierarchy parsing tool














Kubectl-fields is a cli tool to parse kubectl explain --recursive output to match given field and print its parental hierarchy in one-liner format.

## Installation

Installing with krew
```console
kubectl krew install fields
```

Installing with wget
```console
wget https://github.com/rewanthtammana/kubectl-fields/releases/download/v1.2.0-beta/kubectl-fields-Linux-x86_64.tar.gz
sudo tar xvf kubectl-fields-Linux-x86_64.tar.gz -C /usr/bin/
```

## Developer build

Build from Makefile
```console
make build
```

Build only for Linux
```console
make build-linux
```

Build with go
```console
go get ./...
go build -o kubectl-fields main.go
mv kubectl-fields /usr/bin
```

Cross platform builds with go
```console
go get ./...
GOOS=windows GOARCH=amd64 go build -o kubectl-fields.exe main.go
```

## Usage

```console
rewanth@ubuntu:~/go/src/kubectl-fields$ kubectl fields -h
kubectl-fields parses specified kubectl resources to match given pattern(s).
It prints matched fields parental hierarchy in one-liner format.

More info: https://github.com/rewanthtammana/kubectl-fields

Usage:
kubectl-fields [flags]

Examples:
Find resource field hierarchy:
$ kubectl fields svc affinity
spec.sessionAffinity
spec.sessionAffinityConfig

Find resource field hierarchy (case sensitive match):
$ kubectl fields po.spec.volumes -I Ver
downwardAPI.items.fieldRef.apiVersion
projected.sources.downwardAPI.items.fieldRef.apiVersion

Flags:
-I, --case-sensitive Case sensitive pattern match
-h, --help help for kubectl-fields
--no-color Do not print colored output
--stdin Expects input via pipes
```

## Examples

**Default match**

![default-match](https://user-images.githubusercontent.com/22347290/66461949-b4746900-ea97-11e9-83db-8ad643a31808.PNG)

**Case sensitive match**

![case-sensitive-match](https://user-images.githubusercontent.com/22347290/66461959-bccca400-ea97-11e9-989e-a43d9f52d09f.PNG)

**Multi pattern match**

![combo](https://user-images.githubusercontent.com/22347290/66461990-c81fcf80-ea97-11e9-93a6-7aa9077f5d5d.PNG)

**Output on windows cmd**

![windows-all](https://user-images.githubusercontent.com/22347290/66462021-d8d04580-ea97-11e9-8ac7-51fb5fe79439.PNG)

## Pipeline

This application uses [drone CI](https://github.com/drone/drone) for building and running all test cases.

All the test cases are associated with kubectl. But as of now we cannot run `kubectl` inside containers. As a way around, all the kubectl data required for running test cases is pre-saved into text files. All the test cases are executed from pre-saved data against fresh built binary executable to check its robustness.

## Why to use

CKA and CKAD are time-constrained exams. The default kubectl explain recursive command consumes lot of time to find the exact hierarchical order. This tool is built to ease the process of finding hierarchial structure for a given parameter.

It is difficult to lookup for exact hierarchical structure using kubectl explain recursive command. This tool solves this particular problem by printing the output in a easy readable one-liner format.

This tool has tremendous advantage over grep for this use case.

### Problem statement

For ex, in CKA/CKAD exam you are asked to add SYS_ADMIN capabilities to a given pod. Finding the exact hierarchial order using kubectl explain command is time consuming and tiresome.

### Current solution approach

Let's say you know that capabilities exist inside `po.spec`. You can apply recursive loop directly on `po.spec` instead of running it globally on `po`.

You can try to grep for string, "capabilities". The output is as follows:

```console
rewanth@ubuntu:~/go/src/kubectl-fields$ kubectl explain --recursive po.spec | grep capabilities
capabilities
capabilities
```

In case if you need to find "version" attribute in services, then the below command is executed.

```console
$ kubectl explain --recursive svc | grep -i version
VERSION: v1
apiVersion
apiVersion
resourceVersion
apiVersion
apiVersion
resourceVersion
```

But it doesn't show the hierarchial order of attribute. It just shows the matching lines. You can use advanced grep functions and try to find the parent hierarchy of `capabilities` attribute.

```console
rewanth@ubuntu:~/go/src/kubectl-fields$ kubectl explain --recursive po.spec | grep capabilities -C 5
resources
limits
requests
securityContext
allowPrivilegeEscalation
capabilities
add <[]string>
drop <[]string>
privileged
procMount
readOnlyRootFilesystem
--
resources
limits
requests
securityContext
allowPrivilegeEscalation
capabilities
add <[]string>
drop <[]string>
privileged
procMount
readOnlyRootFilesystem

```

But still it doesn't show the complete parent hierarchy order. The full list need to be printed, scrolled and analyzed to find the exact hierarchical structure.

Not only it will take so much time to scroll up and down to find parent of an element in terminal, the process is very tedious.

kubectl explain --recursive po.spec (click to expand 624 lines output)

```console
rewanth@ubuntu:~/go/src/kubectl-fields$ kubectl explain --recursive po.spec
KIND: Pod
VERSION: v1

RESOURCE: spec

DESCRIPTION:
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

PodSpec is a description of a pod.

FIELDS:
activeDeadlineSeconds
affinity
nodeAffinity
preferredDuringSchedulingIgnoredDuringExecution <[]Object>
preference
matchExpressions <[]Object>
key
operator
values <[]string>
matchFields <[]Object>
key
operator
values <[]string>
weight
requiredDuringSchedulingIgnoredDuringExecution
nodeSelectorTerms <[]Object>
matchExpressions <[]Object>
key
operator
values <[]string>
matchFields <[]Object>
key
operator
values <[]string>
podAffinity
preferredDuringSchedulingIgnoredDuringExecution <[]Object>
podAffinityTerm
labelSelector
matchExpressions <[]Object>
key
operator
values <[]string>
matchLabels
namespaces <[]string>
topologyKey
weight
requiredDuringSchedulingIgnoredDuringExecution <[]Object>
labelSelector
matchExpressions <[]Object>
key
operator
values <[]string>
matchLabels
namespaces <[]string>
topologyKey
podAntiAffinity
preferredDuringSchedulingIgnoredDuringExecution <[]Object>
podAffinityTerm
labelSelector
matchExpressions <[]Object>
key
operator
values <[]string>
matchLabels
namespaces <[]string>
topologyKey
weight
requiredDuringSchedulingIgnoredDuringExecution <[]Object>
labelSelector
matchExpressions <[]Object>
key
operator
values <[]string>
matchLabels
namespaces <[]string>
topologyKey
automountServiceAccountToken
containers <[]Object>
args <[]string>
command <[]string>
env <[]Object>
name
value
valueFrom
configMapKeyRef
key
name
optional
fieldRef
apiVersion
fieldPath
resourceFieldRef
containerName
divisor
resource
secretKeyRef
key
name
optional
envFrom <[]Object>
configMapRef
name
optional
prefix
secretRef
name
optional
image
imagePullPolicy
lifecycle
postStart
exec
command <[]string>
httpGet
host
httpHeaders <[]Object>
name
value
path
port
scheme
tcpSocket
host
port
preStop
exec
command <[]string>
httpGet
host
httpHeaders <[]Object>
name
value
path
port
scheme
tcpSocket
host
port
livenessProbe
exec
command <[]string>
failureThreshold
httpGet
host
httpHeaders <[]Object>
name
value
path
port
scheme
initialDelaySeconds
periodSeconds
successThreshold
tcpSocket
host
port
timeoutSeconds
name
ports <[]Object>
containerPort
hostIP
hostPort
name
protocol
readinessProbe
exec
command <[]string>
failureThreshold
httpGet
host
httpHeaders <[]Object>
name
value
path
port
scheme
initialDelaySeconds
periodSeconds
successThreshold
tcpSocket
host
port
timeoutSeconds
resources
limits
requests
securityContext
allowPrivilegeEscalation
capabilities
add <[]string>
drop <[]string>
privileged
procMount
readOnlyRootFilesystem
runAsGroup
runAsNonRoot
runAsUser
seLinuxOptions
level
role
type
user
windowsOptions
gmsaCredentialSpec
gmsaCredentialSpecName
stdin
stdinOnce
terminationMessagePath
terminationMessagePolicy
tty
volumeDevices <[]Object>
devicePath
name
volumeMounts <[]Object>
mountPath
mountPropagation
name
readOnly
subPath
subPathExpr
workingDir
dnsConfig
nameservers <[]string>
options <[]Object>
name
value
searches <[]string>
dnsPolicy
enableServiceLinks
hostAliases <[]Object>
hostnames <[]string>
ip
hostIPC
hostNetwork
hostPID
hostname
imagePullSecrets <[]Object>
name
initContainers <[]Object>
args <[]string>
command <[]string>
env <[]Object>
name
value
valueFrom
configMapKeyRef
key
name
optional
fieldRef
apiVersion
fieldPath
resourceFieldRef
containerName
divisor
resource
secretKeyRef
key
name
optional
envFrom <[]Object>
configMapRef
name
optional
prefix
secretRef
name
optional
image
imagePullPolicy
lifecycle
postStart
exec
command <[]string>
httpGet
host
httpHeaders <[]Object>
name
value
path
port
scheme
tcpSocket
host
port
preStop
exec
command <[]string>
httpGet
host
httpHeaders <[]Object>
name
value
path
port
scheme
tcpSocket