Skip to content

Instantly share code, notes, and snippets.

@mikaelkrief
Last active October 21, 2024 15:59
Show Gist options
  • Save mikaelkrief/3e64049eb6a0bed92224b7ae6c244a26 to your computer and use it in GitHub Desktop.
Save mikaelkrief/3e64049eb6a0bed92224b7ae6c244a26 to your computer and use it in GitHub Desktop.
Get all pods image by namespaces
function Get-K8sPodImagesByNamespace {
param (
[string]$KubeConfigPath = "$HOME/.kube/config", # Chemin vers le fichier kubeconfig
[string[]]$ExcludedNamespaces = @("kube-system", "kube-public", "kube-node-lease", "default") # Namespaces à exclure
)
# Vérifie si kubectl est accessible
if (-not (Get-Command kubectl -ErrorAction SilentlyContinue)) {
Write-Error "kubectl n'est pas installé ou n'est pas accessible dans le PATH."
return
}
try {
# Récupère tous les namespaces du cluster
$namespaces = kubectl get namespaces -o jsonpath='{.items[*].metadata.name}'
foreach ($namespace in $namespaces.Split(" ")) {
if ($ExcludedNamespaces -contains $namespace) {
Write-Host "`nNamespace '$namespace' exclu." -ForegroundColor DarkGray
continue # Passe au namespace suivant
}
Write-Host "`nNamespace: $namespace" -ForegroundColor Cyan
# Récupère les images des pods dans le namespace courant
$pods = kubectl get pods -n $namespace -o jsonpath='{range .items[*]}{.metadata.name}{" "}{range .spec.containers[*]}{.image}{" "}{end}{"`n"}{end}'
if ($pods) {
$pods.Split("`n") | ForEach-Object {
if ($_ -match "^(.*) (.*)$") {
$podName = $matches[1]
$images = $matches[2] -split " "
Write-Host " Pod: $podName" -ForegroundColor Yellow
foreach ($image in $images) {
Write-Host " Image: $image"
}
}
}
} else {
Write-Host " Aucun pod trouvé dans ce namespace."
}
}
} catch {
Write-Error "Une erreur s'est produite : $_"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment