bergerx/kubectl-status

View on GitHub
pkg/plugin/templates/common.tmpl

Summary

Maintainability
Test Coverage
{{- define "DefaultResource" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- template "status_summary_line" . }}
    {{- template "kstatus_summary" . }}
    {{- template "finalizer_details_on_termination" . }}
    {{- template "observed_generation_summary" . }}
    {{- template "application_details" . }}
    {{- template "replicas_status" . }}
    {{- template "suspended" . }}
    {{- template "conditions_summary" . }}
    {{- template "recent_updates" . }}
    {{- template "events" . }}
    {{- template "owners" . }}
{{- end }}

{{- define "suspended" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- if hasKey .Spec "replicas" }}{{ if not .Spec.replicas }}
        {{- "Suspended" | red | bold | nindent 2 }}: Scaled down to 0.
    {{- end }}{{ end }}
{{- end }}

{{- define "recent_updates" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- if .Config.GetBool "include-managed-fields" }}
        {{- with .Metadata.managedFields | default list | sortMapListByKeysValue "time" }}
            {{- "Known/recorded manage events:" | nindent 2 }}
            {{- range . }}
                {{- ""  | nindent 4 }}
                {{- with .time }}{{ . | colorAgo }} ago {{ end }}
                {{- .operation | bold }}d by {{ .manager | bold }} ({{ .fieldsV1 | default dict | keys | sortAlpha | join ", " | replace "f:" "" }})
            {{- end }}
        {{- end }}
    {{- end }}
{{- end }}

{{- define "replicas_status" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- if hasKey .Status "replicas" }}
        {{- $spec_replicas := .Spec.replicas }}
        {{- printf "desired:%d" $spec_replicas | bold | nindent 2 }}
        {{- with .Status.replicas }}, {{ printf "existing:%d" . | redBoldIf (not (eq $spec_replicas .)) }}{{ end }}
        {{- if hasKey .Status "readyReplicas" }}, {{ printf "ready:%d" .Status.readyReplicas | redBoldIf (not (eq $spec_replicas .Status.readyReplicas)) }}{{ end }}
        {{- if hasKey .Status "currentReplicas" }}, {{ printf "current:%d" .Status.currentReplicas | redBoldIf (not (eq $spec_replicas .Status.currentReplicas)) }}{{ end }}
        {{- if hasKey .Status "updatedReplicas" }}, {{ printf "updated:%d" .Status.updatedReplicas | redBoldIf (not (eq $spec_replicas .Status.updatedReplicas)) }}{{ end }}
        {{- if hasKey .Status "availableReplicas" }}, {{ printf "available:%d" .Status.availableReplicas | redBoldIf (not (eq $spec_replicas .Status.availableReplicas)) }}{{ end }}
        {{- if hasKey .Status "fullyLabeledReplicas" }}, {{ printf "fullyLabeled:%d" .Status.fullyLabeledReplicas | redBoldIf (not (eq $spec_replicas .Status.fullyLabeledReplicas)) }}{{ end }}
        {{- with .Status.unavailableReplicas }}, {{ printf "unavailable:%d" . | red | bold }}{{ end }}
        {{- with .Status.collisionCount }}, {{ printf "collisions:%d" . | red | bold }}{{ end }}
    {{- end }}
{{- end -}}

{{- define "status_summary_line" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- if (.Object | default dict).inline }}
        {{- .Kind | bold }}/{{ .Name }}
    {{- else }}
        {{- .Kind | cyan | bold }}/{{ .Name | cyan }}
    {{- end }}
    {{- with .Namespace }} -n {{ . }}{{ end }}
    {{- with .Metadata.creationTimestamp }}, created {{ . | colorAgo }} ago{{ end }}
    {{- if .Metadata.ownerReferences }} by {{ range $index, $ownerReference := .Metadata.ownerReferences }}
        {{- if $index }},{{ end }}{{ $ownerReference.kind | bold }}/{{ $ownerReference.name }}
    {{- end }}{{ end }}
    {{- with .Metadata.generation }}, gen:{{ . }}{{ end }}
    {{- if .Status.startTime }}
        {{- $created := .Metadata.creationTimestamp | toDate "2006-01-02T15:04:05Z" }}
        {{- $started := .Status.startTime | toDate "2006-01-02T15:04:05Z" }}
        {{- $startedIn := $started.Sub $created}}
        {{- if gt ($startedIn.Seconds | int) 0 }}, started after {{ $startedIn.Seconds | ago }}{{ end }}
    {{- end }}
    {{- if .Status.completionTime }}
        {{- $started := .Status.startTime | toDate "2006-01-02T15:04:05Z" -}}
        {{- $completed := .Status.completionTime | toDate "2006-01-02T15:04:05Z" -}}
        {{- $ranfor := $completed.Sub $started }} and {{ "completed" | green }} in {{ $ranfor | colorDuration }}
    {{- end }}
    {{- with .Status.phase }} {{ if $.Object.nodeContext }}{{ . | redIf (eq . "Pending" ) }}{{else}}{{ . | colorKeyword }}{{ end }}{{ end }}
    {{- /* .status.state is used by e.g. Ambassador */ -}}
    {{- with .Status.state }} {{ . | colorKeyword }}{{ end }}
    {{- with .Status.reason }} {{ . | colorKeyword }}{{ end }}
{{- end -}}

{{- define "kstatus_summary" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- with $.KStatus }}
        {{- /*gotype: sigs.k8s.io/cli-utils/pkg/kstatus/status.Result*/}}
        {{- .Status.String | colorKeyword | nindent 2 }}: {{ .Message }}
        {{- range .Conditions }}
            {{- $notHealthy := eq .Status "True"  }}
            {{- .Type.String | redIf $notHealthy | bold | nindent 4 }}:
            {{- with .Reason }} {{ . | redBoldIf $notHealthy }}{{ end }}
            {{- with .Message }}, {{ . | redIf $notHealthy }}{{ end }}
        {{- end }}
    {{- end }}
{{- end -}}

{{- define "finalizer_details_on_termination"}}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- if and .Metadata.deletionTimestamp .Metadata.finalizers }}
        {{- "Finalizers" | red | nindent 4 }}: {{ .Metadata.finalizers | join ", " }}
    {{- end}}
{{- end -}}

{{- define "observed_generation_summary" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- if and .Metadata.generation .Status.observedGeneration }}
        {{- if ne .Metadata.generation .Status.observedGeneration }}
            {{- "Observed generation" | nindent 2 }}({{ .Status.observedGeneration | toString | red | bold }}) doesn't match generation({{ .Metadata.generation | toString | red | bold }})
            {{- "This usually means related controller has not yet reconciled this resource!" | yellow | nindent 4}}
        {{- end }}
    {{- end }}
{{- end -}}

{{- define "conditions_summary" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- range .StatusConditions }}
  {{ template "condition_summary" . }}
    {{- end }}
{{- end -}}

{{- define "condition_summary" }}
    {{- $notHealthy := not (isStatusConditionHealthy .) }}
    {{- .type | redIf $notHealthy | bold }}
    {{- with .reason }} {{ . | redBoldIf $notHealthy }}{{ end }}
    {{- with .message }}, {{ . | redIf $notHealthy }}{{ end }}
    {{- with .lastTransitionTime }} for {{ . | colorAgo }}{{ end }}
    {{- if .lastUpdateTime }}
        {{- if ne (.lastUpdateTime | colorAgo) (.lastTransitionTime | colorAgo) -}}
            , last update was {{ .lastUpdateTime | colorAgo }} ago
        {{- end }}
    {{- end }}
    {{- if .lastProbeTime}}
        {{- if ne (.lastProbeTime | colorAgo) (.lastTransitionTime | colorAgo) -}}
            , last probe was {{ .lastProbeTime | colorAgo }} ago
        {{- end }}
    {{- end }}
{{- end -}}

{{- define "events" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- if .Config.GetBool "include-events" }}
        {{- with .KubeGetEvents }}{{ if .Object }}{{ if .Object.items }}
            {{- "Events:" | nindent 2 }}
            {{- range .Object.items }}
                {{- $.Include "event" . | nindent 4 }}
            {{- end }}
        {{- end }}{{ end }}{{ end }}
    {{- end }}
{{- end -}}

{{- define "owners" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject */ -}}
    {{- if .Config.GetBool "include-owners" }}
        {{- with .KubeGetOwners }}
            {{- "Owners:" | nindent 2 }}
            {{- range . }}{{ if .Object }}
                {{- $.IncludeRenderableObject . | nindent 4 }}
            {{- end }}{{ end }}
        {{- end }}
    {{- end }}
{{- end }}

{{- define "application_details" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- if .Config.GetBool "include-application-details" }}
        {{- $labels := .Labels }}
        {{- $annotations := .Annotations }}
        {{- $clusterService := get $labels "kubernetes.io/cluster-service" }}
        {{- $addonManager := get $labels "addonmanager.kubernetes.io/mode" }}
        {{- $managedBy := coalesce (get $labels "app.kubernetes.io/managed-by") $labels.heritage }}
        {{- $createdBy := get $labels "app.kubernetes.io/created-by" }}
        {{- $name := coalesce (get $labels "helm.sh/chart") $labels.chart (get $labels "app.kubernetes.io/name") $labels.app (get $labels "k8s-app") (get $labels "kubernetes.io/name") $labels.ame | default "" }}
        {{- $partOf := get $labels "app.kubernetes.io/part-of" }}
        {{- $component := get $labels "app.kubernetes.io/component" }}
        {{- $instance := coalesce (get $annotations "meta.helm.sh/release-name") $labels.release (get $labels "app.kubernetes.io/instance") }}
        {{- $releaseNamespace := get $annotations "meta.helm.sh/release-namespace" }}
        {{- $namespace := eq $releaseNamespace .Namespace | ternary "" $releaseNamespace }}
        {{- $version := coalesce (get $labels "app.kubernetes.io/version") $labels.version }}
        {{- if or $clusterService $addonManager $managedBy $createdBy $name $partOf $component $instance $namespace $version }}
            {{- "Managed" | nindent 2 }}
            {{- with $addonManager }} by {{ "AddonManager" | bold | yellow }} in {{ . | redBoldIf (eq . "Reconcile") }} mode{{ end }}
            {{- with $clusterService }} as a {{ "cluster-service" | yellow | bold }}{{end}}
            {{- with $managedBy }} by {{ . | bold }}{{ end }}
            {{- with $createdBy }} (created by {{ . | bold }}){{ end }}
            {{- with $instance }} with {{ . | bold }} {{ eq $managedBy "Helm" | ternary "release" "instance" }}{{end}}
            {{- with $namespace }} in {{ . | bold | yellow }} namespace (different than the current resource){{end}}
            {{- with $name }} by {{ . | bold }} {{ eq $managedBy "Helm" | ternary "chart" "application" }}{{ end }}
            {{- with $partOf }}{{ if not (contains $partOf $name) }} as part of {{ . | bold }}{{ end }}{{ end }}
            {{- with $component }} within component {{ . | bold }}{{ end }}
            {{- with $version }}{{ if not (contains $version $name) }} using version {{ . | bold }}{{ end }}{{ end }}
        {{- end -}}
    {{- end }}
{{- end -}}

{{- define "rollout_diffs_flag_help" }}
    {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}}
    {{- with not (.Config.GetBool "include-rollout-diffs") }}
      {{- " Use the `--include-rollout-diffs` flag to see the diff between each rollout!" | blue }}
    {{- end }}
{{- end }}