public/Get-K8sEvent.ps1

<#
.SYNOPSIS
Get events for an object

.PARAMETER ObjectName
Name of the object to get events for

.PARAMETER NoNormal
If set, don't return normal events, only warnings

.PARAMETER Namespace
K8s namespace to use, defaults to default

.EXAMPLE
Get-K8sEvent -ObjectName test-minimal-7894b5dbf9-xkvgc

Get all events for a pod

.EXAMPLE
Get-K8sEvent -ObjectName test-minimal-7894b5dbf9-xkvgc -NoNormal -Namespace test

Get all non-normal events for a pod in namespace test

.EXAMPLE
Get-K8sEvent -ObjectName test-minimal-bf9c4b7f9 -NoNormal -Namespace test

Get all non-normal events for a replicaset in namespace test

.OUTPUTS
One or more event objects for the pod, $null if error
#>

function Get-K8sEvent {
    param (
        [CmdletBinding()]
        [Parameter(Mandatory = $true)]
        [Alias("PodName", "RsName")]
        [string] $ObjectName,
        [switch] $NoNormal,
        [string] $Namespace = "default"
    )
    Write-Verbose "kubectl get events --namespace $Namespace --field-selector `"involvedObject.name=$ObjectName`" -o json"
    $json = kubectl get events --namespace $Namespace --field-selector "involvedObject.name=$ObjectName" -o json

    Write-Verbose "kubectl exit code: $LASTEXITCODE"
    if ($LASTEXITCODE -ne 0) {
        Write-Status "kubectl get events --namespace $Namespace --field-selector `"involvedObject.name=$ObjectName`" -o json" -LogLevel warning
        Write-Status " had exit code of $LASTEXITCODE" -LogLevel warning
        Write-Status " JSON is $json" -LogLevel warning
        return $null
    }
    $events = $json | ConvertFrom-Json
    if ($null -eq $events) { # valid if no events, such as it didn't need to create a new pod
        Write-Verbose "Null events after conversion"
        Write-Output @() -NoEnumerate # Prevent @() from turning into $null
        return
    }
    if ($events.items) {
        Write-Verbose "Events count: $($events.items.count)"
    }
    if ($NoNormal) {
        $ret = $events.items | Where-Object { $_.type -ne "Normal" }
    } else {
        $ret = $events.items
    }
    if ($null -eq $ret) {
        Write-Verbose "Null items, returning empty array"
        $ret = @()
    }
    Write-Output $ret -NoEnumerate
}

Set-Alias -Name Get-PodEvent -Value Get-K8sEvent -Description "Get events for a pod"
Set-Alias -Name Get-RsEvent -Value Get-K8sEvent -Description "Get events for a replica set"
Set-Alias -Name Get-ReplicaSetEvent -Value Get-K8sEvent -Description "Get events for a replica set"