@@ -17,6 +17,13 @@ import (
1717 "k8s.io/apimachinery/pkg/api/resource"
1818 "k8s.io/apimachinery/pkg/runtime"
1919 metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
20+ "k8s.io/utils/ptr"
21+ )
22+
23+ const (
24+ statsKindAppplication = "app"
25+ statsKindWorkerJob = "worker-job"
26+ statsKindScheduledJob = "scheduled-job"
2027)
2128
2229type applicationsCmd struct {
@@ -189,6 +196,24 @@ func printDNSDetailsTabRow(items []util.DNSDetail, get *Cmd, out io.Writer) erro
189196 return nil
190197}
191198
199+ func sizeForWorkerJob (release * apps.Release , workerJobName string ) * apps.ApplicationSize {
200+ for _ , wj := range release .Spec .ForProvider .Config .WorkerJobs {
201+ if wj .Name == workerJobName {
202+ return wj .Size
203+ }
204+ }
205+ return nil
206+ }
207+
208+ func sizeForScheduledJob (release * apps.Release , scheduledJobName string ) * apps.ApplicationSize {
209+ for _ , sj := range release .Spec .ForProvider .Config .ScheduledJobs {
210+ if sj .Name == scheduledJobName {
211+ return sj .Size
212+ }
213+ }
214+ return nil
215+ }
216+
192217func (cmd * applicationsCmd ) printStats (ctx context.Context , c * api.Client , appList []apps.Application , get * Cmd , out io.Writer ) error {
193218 scheme := runtime .NewScheme ()
194219 if err := metricsv1beta1 .AddToScheme (scheme ); err != nil {
@@ -200,55 +225,96 @@ func (cmd *applicationsCmd) printStats(ctx context.Context, c *api.Client, appLi
200225 return err
201226 }
202227 w := tabwriter .NewWriter (out , 0 , 0 , 3 , ' ' , 0 )
203- get .writeHeader (w , "NAME" , "REPLICA" , "STATUS" , "CPU" , "CPU%" , "MEMORY" , "MEMORY%" , "RESTARTS" , "LASTEXITCODE" )
228+ get .writeHeader (w , "NAME" , "TYPE" , "REPLICA" , "STATUS" , "CPU" , "CPU%" , "MEMORY" , "MEMORY%" , "RESTARTS" , "LASTEXITCODE" )
229+
230+ type statsObservation struct {
231+ kind string
232+ name string
233+ size apps.ApplicationSize
234+ apps.ReplicaObservation
235+ }
204236
205237 for _ , app := range appList {
206238 rel , err := util .ApplicationLatestRelease (ctx , c , api .ObjectName (& app ))
207239 if err != nil {
208- format .PrintWarningf ("unable to get replicas for app %s\n " , c .Name (app .Name ))
240+ format .PrintWarningf ("unable to get latest release for app %s\n " , c .Name (app .Name ))
209241 continue
210242 }
211243
212- replicas := rel .Status .AtProvider .ReplicaObservation
213- workers := []apps.ReplicaObservation {}
244+ var observations []statsObservation
245+ appSize := rel .Spec .ForProvider .Config .Size
246+
247+ // we first gather the normal application replicas
248+ for _ , appReplica := range rel .Status .AtProvider .ReplicaObservation {
249+ observations = append (observations , statsObservation {
250+ kind : statsKindAppplication ,
251+ name : app .Name ,
252+ size : appSize ,
253+ ReplicaObservation : appReplica ,
254+ })
255+ }
256+ // we now handle the worker jobs
214257 for _ , wjs := range rel .Status .AtProvider .WorkerJobStatus {
215- workers = append (workers , wjs .ReplicaObservation ... )
258+ wjSize := sizeForWorkerJob (rel , wjs .Name )
259+ if wjSize == nil {
260+ wjSize = ptr .To (appSize )
261+ }
262+ for _ , replicaObs := range wjs .ReplicaObservation {
263+ observations = append (observations , statsObservation {
264+ kind : statsKindWorkerJob ,
265+ name : wjs .Name ,
266+ ReplicaObservation : replicaObs ,
267+ size : * wjSize ,
268+ })
269+ }
216270 }
217-
271+ // ..and finally the scheduled jobs
218272 for _ , sjs := range rel .Status .AtProvider .ScheduledJobStatus {
219- workers = append (workers , sjs .ReplicaObservation ... )
273+ sjSize := sizeForScheduledJob (rel , sjs .Name )
274+ if sjSize == nil {
275+ sjSize = ptr .To (appSize )
276+ }
277+ for _ , replicaObs := range sjs .ReplicaObservation {
278+ observations = append (observations , statsObservation {
279+ kind : statsKindScheduledJob ,
280+ name : sjs .Name ,
281+ ReplicaObservation : replicaObs ,
282+ size : * sjSize ,
283+ })
284+ }
220285 }
221286
222- for _ , replica := range append ( replicas , workers ... ) {
287+ for _ , statsObservation := range observations {
223288 podMetrics := metricsv1beta1.PodMetrics {}
224- if err := runtimeClient .Get (ctx , api .NamespacedName (replica .ReplicaName , app .Namespace ), & podMetrics ); err != nil {
225- format .PrintWarningf ("unable to get metrics for replica %s\n " , replica .ReplicaName )
289+ if err := runtimeClient .Get (ctx , api .NamespacedName (statsObservation .ReplicaName , app .Namespace ), & podMetrics ); err != nil {
290+ format .PrintWarningf ("unable to get metrics for replica %s\n " , statsObservation .ReplicaName )
226291 }
227292
228- appResources := apps .AppResources [app . Status . AtProvider . Size ]
293+ maxResources := apps .AppResources [statsObservation . size ]
229294 // We expect exactly one container, fall back to [util.NoneText] if that's
230295 // not the case. The container might simply not have any metrics yet.
231296 cpuUsage , cpuPercentage := util .NoneText , util .NoneText
232297 memoryUsage , memoryPercentage := util .NoneText , util .NoneText
233298 if len (podMetrics .Containers ) == 1 {
234299 cpu := podMetrics .Containers [0 ].Usage [corev1 .ResourceCPU ]
235300 cpuUsage = formatQuantity (corev1 .ResourceCPU , cpu )
236- cpuPercentage = formatPercentage (cpu .MilliValue (), appResources .Cpu ().MilliValue ())
301+ cpuPercentage = formatPercentage (cpu .MilliValue (), maxResources .Cpu ().MilliValue ())
237302 memory := podMetrics .Containers [0 ].Usage [corev1 .ResourceMemory ]
238303 memoryUsage = formatQuantity (corev1 .ResourceMemory , memory )
239- memoryPercentage = formatPercentage (memory .MilliValue (), appResources .Memory ().MilliValue ())
304+ memoryPercentage = formatPercentage (memory .MilliValue (), maxResources .Memory ().MilliValue ())
240305 }
241306
242307 get .writeTabRow (
243- w , app .Namespace , app .Name ,
244- replica .ReplicaName ,
245- string (replica .Status ),
308+ w , app .Namespace , statsObservation .name ,
309+ statsObservation .kind ,
310+ statsObservation .ReplicaName ,
311+ string (statsObservation .Status ),
246312 cpuUsage ,
247313 cpuPercentage ,
248314 memoryUsage ,
249315 memoryPercentage ,
250- formatRestartCount (replica ),
251- formatExitCode (replica ),
316+ formatRestartCount (statsObservation . ReplicaObservation ),
317+ formatExitCode (statsObservation . ReplicaObservation ),
252318 )
253319 }
254320 }
0 commit comments