Created
September 19, 2024 15:37
-
-
Save viatcheslavmogilevsky/20e7735910ba693de51bcbe4793dfba0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
target_namespaces=( default ) | |
target_cluster_context=$(kubectl config current-context | awk -F'/' '{print $NF}') | |
KNOWN_REGISTRY="123456789012.dkr.ecr.us-west-1.amazonaws.com" | |
inspect_container () { | |
echo -n '{{printf "%s,%s,%s,%d,%s,%s,%s," $element.metadata.namespace $podOwnerType $element.metadata.name $count $containerType $container.name $container.image }} | |
{{- range $envFromItem := $container.envFrom -}} | |
{{- if $envFromItem.configMapRef -}} | |
{{printf "%s;" $envFromItem.configMapRef.name }} | |
{{- end -}} | |
{{- end -}} | |
{{- range $envItem := $container.env -}} | |
{{- if $envItem.valueFrom -}} | |
{{- if $envItem.valueFrom.configMapKeyRef -}} | |
{{printf "%s;" $envItem.valueFrom.configMapKeyRef.name }} | |
{{- end -}} | |
{{- end -}} | |
{{- end -}} | |
{{- range $volumeItem := $container.volumes -}} | |
{{- if $volumeItem.configMap -}} | |
{{printf "%s;" $volumeItem.configMap.name }} | |
{{- end -}} | |
{{- end -}} | |
{{printf "," }} | |
{{- range $envFromItem := $container.envFrom -}} | |
{{- if $envFromItem.secretRef -}} | |
{{printf "%s;" $envFromItem.secretRef.name }} | |
{{- end -}} | |
{{- end -}} | |
{{- range $envItem := $container.env -}} | |
{{- if $envItem.valueFrom -}} | |
{{- if $envItem.valueFrom.secretKeyRef -}} | |
{{printf "%s;" $envItem.valueFrom.secretKeyRef.name }} | |
{{- end -}} | |
{{- end -}} | |
{{- end -}} | |
{{- range $volumeItem := $podTemplate.spec.volumes -}} | |
{{- if $volumeItem.secret -}} | |
{{printf "%s;" $volumeItem.secret.secretName }} | |
{{- end -}} | |
{{- end -}} | |
{{- printf "," -}} | |
{{- range $labelkey, $labelvalue := $podTemplate.metadata.labels -}} | |
{{ printf "%s=%s;" $labelkey $labelvalue }} | |
{{- end -}} | |
{{- printf "," -}} | |
{{- printf "%s" (or $podTemplate.spec.serviceAccountName "default") -}}' | |
} | |
( | |
echo "Namespace,Pod owner type,Pod owner name,Pod owner replicas,Container type,Container name,ECR image URL,Config map refs,Secret refs,Labels,ServiceAccount" | |
for namespace in "${target_namespaces[@]}"; do | |
kubectl get deployments -n $namespace -o go-template="{{range \$index, \$element := .items}} | |
{{- range \$ic, \$container := \$element.spec.template.spec.containers -}} | |
{{- \$podTemplate := \$element.spec.template -}} | |
{{- \$podOwnerType := \"deployment\" -}} | |
{{- \$containerType := \"regular\" -}} | |
{{- \$count := \$element.spec.replicas -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{- range \$ic, \$container := \$element.spec.template.spec.initContainers -}} | |
{{- \$podTemplate := \$element.spec.template -}} | |
{{- \$podOwnerType := \"deployment\" -}} | |
{{- \$containerType := \"init\" -}} | |
{{- \$count := \$element.spec.replicas -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{end}}" | |
kubectl get statefulsets -n $namespace -o go-template="{{range \$index, \$element := .items}} | |
{{- range \$ic, \$container := \$element.spec.template.spec.containers -}} | |
{{- \$podTemplate := \$element.spec.template -}} | |
{{- \$podOwnerType := \"statefulset\" -}} | |
{{- \$containerType := \"regular\" -}} | |
{{- \$count := \$element.spec.replicas -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{- range \$ic, \$container := \$element.spec.template.spec.initContainers -}} | |
{{- \$podTemplate := \$element.spec.template -}} | |
{{- \$podOwnerType := \"statefulset\" -}} | |
{{- \$containerType := \"init\" -}} | |
{{- \$count := \$element.spec.replicas -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{end}}" | |
kubectl get cronjobs -n $namespace -o go-template="{{range \$index, \$element := .items}} | |
{{- range \$ic, \$container := \$element.spec.jobTemplate.spec.template.spec.containers -}} | |
{{- \$podTemplate := \$element.spec.jobTemplate.spec.template -}} | |
{{- \$podOwnerType := \"cronjob\" -}} | |
{{- \$containerType := \"regular\" -}} | |
{{- \$count := (or \$element.spec.jobTemplate.spec.parallelism 1) -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{- range \$ic, \$container := \$element.spec.jobTemplate.spec.template.spec.initContainers -}} | |
{{- \$podTemplate := \$element.spec.jobTemplate.spec.template -}} | |
{{- \$podOwnerType := \"cronjob\" -}} | |
{{- \$containerType := \"init\" -}} | |
{{- \$count := (or \$element.spec.jobTemplate.spec.parallelism 1) -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{end}}" | |
kubectl get daemonsets -n $namespace -o go-template="{{range \$index, \$element := .items}} | |
{{- range \$ic, \$container := \$element.spec.template.spec.containers -}} | |
{{- \$podTemplate := \$element.spec.template -}} | |
{{- \$podOwnerType := \"daemonset\" -}} | |
{{- \$containerType := \"regular\" -}} | |
{{- \$count := 1 -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{- range \$ic, \$container := \$element.spec.template.spec.initContainers -}} | |
{{- \$podTemplate := \$element.spec.template -}} | |
{{- \$podOwnerType := \"daemonset\" -}} | |
{{- \$containerType := \"init\" -}} | |
{{- \$count := 1 -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{end}}" | |
kubectl get jobs -n $namespace -o go-template="{{range \$index, \$element := .items}} | |
{{- if not \$element.metadata.ownerReferences -}} | |
{{- range \$ic, \$container := \$element.spec.template.spec.containers -}} | |
{{- \$podTemplate := \$element.spec.template -}} | |
{{- \$podOwnerType := \"job\" -}} | |
{{- \$containerType := \"regular\" -}} | |
{{- \$count := (or \$element.spec.parallelism 1) -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{- range \$ic, \$container := \$element.spec.template.spec.initContainers -}} | |
{{- \$podTemplate := \$element.spec.template -}} | |
{{- \$podOwnerType := \"job\" -}} | |
{{- \$containerType := \"init\" -}} | |
{{- \$count := (or \$element.spec.parallelism 1) -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{- end -}} | |
{{end}}" | |
kubectl get pods -n $namespace -o go-template="{{range \$index, \$element := .items}} | |
{{- if not \$element.metadata.ownerReferences -}} | |
{{- range \$ic, \$container := \$element.spec.containers -}} | |
{{- \$podTemplate := \$element -}} | |
{{- \$podOwnerType := \"N/A\" -}} | |
{{- \$containerType := \"regular\" -}} | |
{{- \$count := 1 -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{- range \$ic, \$container := \$element.spec.initContainers -}} | |
{{- \$podTemplate := \$element -}} | |
{{- \$podOwnerType := \"N/A\" -}} | |
{{- \$containerType := \"init\" -}} | |
{{- \$count := 1 -}} | |
$(inspect_container) | |
{{printf \"\\n\" }} | |
{{- end -}} | |
{{- end -}} | |
{{end}}" | |
done | |
) | perl -pe 's/([^;]+)(;\1)+/$1/g' | sed "s/${KNOWN_REGISTRY}/\$ecr/g" > "${target_cluster_context}-pod-templates.csv" | |
( | |
echo "Namespace,Name" | |
for namespace in "${target_namespaces[@]}"; do | |
kubectl get configmaps -n $namespace -o go-template='{{- range $index, $element := .items -}} | |
{{printf "%s,%s\n" $element.metadata.namespace $element.metadata.name }} | |
{{- end -}}' | |
done | |
) > "${target_cluster_context}-configmaps.csv" | |
( | |
echo "Namespace,Name" | |
for namespace in "${target_namespaces[@]}"; do | |
kubectl get serviceaccounts -n $namespace -o go-template='{{- range $element := .items -}} | |
{{printf "%s,%s\n" $element.metadata.namespace $element.metadata.name }} | |
{{- end -}}' | |
done | |
) > "${target_cluster_context}-serviceaccounts.csv" | |
( | |
echo "Namespace,Kind,Type (Secret),Name,Owner/target" | |
for namespace in "${target_namespaces[@]}"; do | |
kubectl get secrets -n $namespace -o go-template='{{- range $element := .items -}} | |
{{printf "%s,%s,%s,%s," $element.metadata.namespace "Secret" $element.type $element.metadata.name }} | |
{{- range $ownerRef := $element.metadata.ownerReferences -}} | |
{{printf "%s/%s " $ownerRef.kind $ownerRef.name }} | |
{{- end -}} | |
{{ printf "\n" }} | |
{{- end -}}' | |
kubectl get externalsecrets -n $namespace -o go-template='{{- range $element := .items -}} | |
{{- if $element.metadata.namespace -}} | |
{{printf "%s,%s,%s,%s,Secret/%s\n" $element.metadata.namespace "ExternalSecret" "N/A" $element.metadata.name (or $element.target.name $element.metadata.name) }} | |
{{- end -}} | |
{{- end -}}' | |
done | |
) > "${target_cluster_context}-external-regular-secrets.csv" | |
( | |
echo "Namespace,Name,Backend services" | |
for namespace in "${target_namespaces[@]}"; do | |
kubectl get ingresses -n $namespace -o go-template='{{- range $element := .items -}} | |
{{printf "%s,%s," $element.metadata.namespace $element.metadata.name }} | |
{{- range $rule := $element.spec.rules -}} | |
{{- if $rule.http -}} | |
{{- range $path := $rule.http.paths -}} | |
{{- if $path.backend -}} | |
{{- if $path.backend.service -}} | |
{{printf "%s " $path.backend.service.name }} | |
{{- end -}} | |
{{- end -}} | |
{{- end -}} | |
{{- end -}} | |
{{- end -}} | |
{{ printf "\n" }} | |
{{- end -}}' | |
done | |
) | perl -pe 's/([^\s]+)(\s\1)+/$1/g' > "${target_cluster_context}-ingresses.csv" | |
( | |
echo "Namespace,Name,Selector" | |
for namespace in "${target_namespaces[@]}"; do | |
kubectl get services -n $namespace -o go-template='{{- range $element := .items -}} | |
{{printf "%s,%s," $element.metadata.namespace $element.metadata.name }} | |
{{- range $selectorKey, $selectorVal := $element.spec.selector -}} | |
{{printf "%s=%s " $selectorKey $selectorVal }} | |
{{- end -}} | |
{{ printf "\n" }} | |
{{- end -}}' | |
done | |
) > "${target_cluster_context}-services.csv" | |
( | |
echo "Namespace,Name,Selector Labels,Selector Expressions" | |
for namespace in "${target_namespaces[@]}"; do | |
kubectl get poddisruptionbudgets -n $namespace -o go-template='{{- range $element := .items -}} | |
{{printf "%s,%s," $element.metadata.namespace $element.metadata.name }} | |
{{- range $selectorKey, $selectorVal := $element.spec.selector.matchLabels -}} | |
{{printf "%s=%s;" $selectorKey $selectorVal }} | |
{{- end -}} | |
{{- printf "," -}} | |
{{- range $selectorExpr := $element.spec.selector.matchExpressions -}} | |
{{printf "%s_%s_(" $selectorExpr.key $selectorExpr.operator }} | |
{{- range $selectorExprVal := $selectorExpr.values -}} | |
{{ printf "%s " $selectorExprVal }} | |
{{- end -}} | |
{{- printf ")" -}} | |
{{- end -}} | |
{{ printf "\n" }} | |
{{- end -}}' | |
done | |
) > "${target_cluster_context}-poddisruptionbudgets.csv" | |
( | |
echo "Namespace,Name,Target" | |
for namespace in "${target_namespaces[@]}"; do | |
kubectl get hpa -n $namespace -o go-template='{{- range $element := .items -}} | |
{{printf "%s,%s,%s/%s\n" $element.metadata.namespace $element.metadata.name $element.spec.scaleTargetRef.kind $element.spec.scaleTargetRef.name }} | |
{{- end -}}' | |
done | |
) > "${target_cluster_context}-hpas.csv" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment