55 "compress/gzip"
66 "encoding/base64"
77 "encoding/json"
8+ "fmt"
89 "io"
10+ "time"
911
1012 // helm v2 is long since deprecated
1113 // Unlike helm v3, it uses Protobuf encoding, so we can't use generic decoding without the message descriptors.
@@ -16,8 +18,10 @@ import (
1618 "github.com/pkg/errors"
1719 "github.com/rancher/apiserver/pkg/types"
1820 "github.com/rancher/norman/types/convert"
21+ rescommon "github.com/rancher/steve/pkg/resources/common"
1922 "github.com/rancher/wrangler/v3/pkg/data"
2023 "github.com/sirupsen/logrus"
24+ "k8s.io/apimachinery/pkg/util/duration"
2125)
2226
2327var (
@@ -61,11 +65,82 @@ func HandleHelmData(request *types.APIRequest, resource *types.RawResource) {
6165}
6266
6367func Pod (_ * types.APIRequest , resource * types.RawResource ) {
64- data := resource .APIObject .Data ()
65- fields := data .StringSlice ("metadata" , "fields" )
68+ objData := resource .APIObject .Data ()
69+ fields := objData .StringSlice ("metadata" , "fields" )
6670 if len (fields ) > 2 {
67- data .SetNested (convert .LowerTitle (fields [2 ]), "metadata" , "state" , "name" )
71+ objData .SetNested (convert .LowerTitle (fields [2 ]), "metadata" , "state" , "name" )
72+ }
73+
74+ if resource .Schema == nil {
75+ return
6876 }
77+
78+ cols := rescommon .GetColumnDefinitions (resource .Schema )
79+ for _ , col := range cols {
80+ if col .Name != "Restarts" {
81+ continue
82+ }
83+
84+ index := rescommon .GetIndexValueFromString (col .Field )
85+ if index == - 1 {
86+ continue
87+ }
88+
89+ rawFieldsRaw , ok := data .GetValue (objData , "metadata" , "fields" )
90+ if ! ok {
91+ continue
92+ }
93+
94+ rawFields , ok := rawFieldsRaw .([]interface {})
95+ if ! ok || index >= len (rawFields ) {
96+ continue
97+ }
98+
99+ valMap , ok := rawFields [index ].(map [string ]interface {})
100+ if ! ok {
101+ continue
102+ }
103+
104+ rawFields [index ] = FormatRestarts (valMap )
105+ objData .SetNested (rawFields , "metadata" , "fields" )
106+ }
107+ }
108+
109+ // FormatRestarts formats a restart map as a display string.
110+ func FormatRestarts (valMap map [string ]interface {}) string {
111+ var count int64
112+ var timestamp int64
113+
114+ if c , ok := valMap ["count" ]; ok {
115+ switch v := c .(type ) {
116+ case int64 :
117+ count = v
118+ case int :
119+ count = int64 (v )
120+ case float64 :
121+ count = int64 (v )
122+ }
123+ }
124+
125+ if t , ok := valMap ["timestamp" ]; ok {
126+ switch v := t .(type ) {
127+ case int64 :
128+ timestamp = v
129+ case int :
130+ timestamp = int64 (v )
131+ case float64 :
132+ timestamp = int64 (v )
133+ }
134+ }
135+
136+ if timestamp == 0 {
137+ return fmt .Sprintf ("%d" , count )
138+ }
139+
140+ t := time .UnixMilli (timestamp )
141+ dur := time .Since (t )
142+ humanDur := duration .HumanDuration (dur )
143+ return fmt .Sprintf ("%d (%s ago)" , count , humanDur )
69144}
70145
71146// decodeHelm3 receives a helm3 release data string, decodes the string data using the standard base64 library
0 commit comments