forked from philippemerle/KubeDiagrams
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkubectl-diagrams
More file actions
executable file
·110 lines (99 loc) · 3.07 KB
/
Copy pathkubectl-diagrams
File metadata and controls
executable file
·110 lines (99 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
# kubectl diagrams: A kubectl plugin to generate a diagram of Kubernetes resources
# using kube-diagrams from the output of 'kubectl get <resources> -o yaml'
# Version
VERSION="0.1.0"
# Check if kube-diagrams is installed
if ! command -v kube-diagrams &>/dev/null; then
echo "Error: kube-diagrams is not installed. Please install it first."
exit 1
fi
# Check if kubectl is installed
if ! command -v kubectl &>/dev/null; then
echo "Error: kubectl is not installed. Please install it first."
exit 1
fi
# Display help message
show_help() {
echo "Usage: kubectl diagrams [OPTIONS] <resource1,resource2,...>"
echo ""
echo "A kubectl plugin to generate diagrams of Kubernetes resources using kube-diagrams."
echo ""
echo "Options:"
echo " -n, --namespace <namespace> Specify the namespace to query resources from"
echo " -o, --output <file> Specify the output file for the diagram"
echo " -f, --format <format> Specify the output format (e.g., png, svg)"
echo " --embed-all-icons Embed all icons into svg or dot_json output diagrams"
echo " -c, --config <file> Specify the custom kube-diagrams configuration file"
echo " --without-namespace Exclude namespace from the diagram"
echo " --version Display the version number"
echo " --help Display this help message"
echo ""
echo "Examples:"
echo " kubectl diagrams pod,service -n my-namespace -o diagram.png"
echo " kubectl diagrams deployment --without-namespace -f svg"
echo " kubectl diagrams --version"
exit 0
}
# Initialize variables
KUBE_DIAGRAMS_ARGS=("-") # Default to stdin indicator
KUBECTL_ARGS=()
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--version)
echo "kubectl diagrams version $VERSION"
exit
;;
--help)
show_help
;;
-n | --namespace)
if [[ -n "$2" ]]; then
KUBECTL_ARGS+=("-n" "$2")
shift 2
else
echo "Error: --namespace requires a value"
exit 64
fi
;;
-o | --output | -f | --format | -c | --config | --without-namespace)
KUBE_DIAGRAMS_ARGS+=("$1")
if [[ "$1" == "-o" || "$1" == "--output" || "$1" == "-f" || "$1" == "--format" || "$1" == "-c" || "$1" == "--config" ]]; then
if [[ -n "$2" ]]; then
KUBE_DIAGRAMS_ARGS+=("$2")
shift 2
else
echo "Error: $1 requires a value"
exit 64
fi
else
shift
fi
;;
--embed-all-icons)
KUBE_DIAGRAMS_ARGS+=("$1")
shift
;;
*)
KUBECTL_ARGS+=("$1")
shift
;;
esac
done
# Check if any resources were specified
if [[ ${#KUBECTL_ARGS[@]} -eq 0 ]]; then
echo "Error: At least one resource kind must be specified (e.g., pod, service, deployment)"
exit 64
fi
# Add default output format for kubectl
KUBECTL_ARGS+=("-o" "yaml")
# Execute kubectl and pipe to kube-diagrams with arguments
kubectl get "${KUBECTL_ARGS[@]}" | kube-diagrams "${KUBE_DIAGRAMS_ARGS[@]}"
EXIT_CODE=$?
if [[ $EXIT_CODE -eq 0 ]]; then
echo "Diagram generated successfully"
else
echo "Error: Failed to generate diagram"
exit 1
fi