@@ -17,6 +17,7 @@ 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"
2021)
2122
2223type applicationsCmd struct {
@@ -189,6 +190,24 @@ func printDNSDetailsTabRow(items []util.DNSDetail, get *Cmd, out io.Writer) erro
189190 return nil
190191}
191192
193+ func sizeForWorkerJob (release * apps.Release , workerJobName string ) * apps.ApplicationSize {
194+ for _ , wj := range release .Spec .ForProvider .Config .WorkerJobs {
195+ if wj .Name == workerJobName {
196+ return wj .Size
197+ }
198+ }
199+ return nil
200+ }
201+
202+ func sizeForScheduledJob (release * apps.Release , scheduledJobName string ) * apps.ApplicationSize {
203+ for _ , sj := range release .Spec .ForProvider .Config .ScheduledJobs {
204+ if sj .Name == scheduledJobName {
205+ return sj .Size
206+ }
207+ }
208+ return nil
209+ }
210+
192211func (cmd * applicationsCmd ) printStats (ctx context.Context , c * api.Client , appList []apps.Application , get * Cmd , out io.Writer ) error {
193212 scheme := runtime .NewScheme ()
194213 if err := metricsv1beta1 .AddToScheme (scheme ); err != nil {
@@ -202,53 +221,89 @@ func (cmd *applicationsCmd) printStats(ctx context.Context, c *api.Client, appLi
202221 w := tabwriter .NewWriter (out , 0 , 0 , 3 , ' ' , 0 )
203222 get .writeHeader (w , "NAME" , "REPLICA" , "STATUS" , "CPU" , "CPU%" , "MEMORY" , "MEMORY%" , "RESTARTS" , "LASTEXITCODE" )
204223
224+ type statsObservation struct {
225+ name string
226+ size apps.ApplicationSize
227+ apps.ReplicaObservation
228+ }
229+
205230 for _ , app := range appList {
206231 rel , err := util .ApplicationLatestRelease (ctx , c , api .ObjectName (& app ))
207232 if err != nil {
208- format .PrintWarningf ("unable to get replicas for app %s\n " , c .Name (app .Name ))
233+ format .PrintWarningf ("unable to get latest release for app %s\n " , c .Name (app .Name ))
209234 continue
210235 }
211236
212- replicas := rel .Status .AtProvider .ReplicaObservation
213- workers := []apps.ReplicaObservation {}
237+ var observations []statsObservation
238+ appSize := rel .Spec .ForProvider .Config .Size
239+
240+ // we first gather the normal application replicas
241+ for _ , appReplica := range rel .Status .AtProvider .ReplicaObservation {
242+ observations = append (observations , statsObservation {
243+ name : app .Name ,
244+ size : appSize ,
245+ ReplicaObservation : appReplica ,
246+ })
247+ }
248+ // we now handle the worker jobs
214249 for _ , wjs := range rel .Status .AtProvider .WorkerJobStatus {
215- workers = append (workers , wjs .ReplicaObservation ... )
250+ wjSize := sizeForWorkerJob (rel , wjs .Name )
251+ if wjSize == nil {
252+ wjSize = ptr .To (appSize )
253+ }
254+ for _ , replicaObs := range wjs .ReplicaObservation {
255+ observations = append (observations , statsObservation {
256+ name : wjs .Name ,
257+ ReplicaObservation : replicaObs ,
258+ size : * wjSize ,
259+ })
260+ }
216261 }
217-
262+ // ..and finally the scheduled jobs
218263 for _ , sjs := range rel .Status .AtProvider .ScheduledJobStatus {
219- workers = append (workers , sjs .ReplicaObservation ... )
264+ sjSize := sizeForScheduledJob (rel , sjs .Name )
265+ if sjSize == nil {
266+ sjSize = ptr .To (appSize )
267+ }
268+ for _ , replicaObs := range sjs .ReplicaObservation {
269+ observations = append (observations , statsObservation {
270+ name : sjs .Name ,
271+ ReplicaObservation : replicaObs ,
272+ size : * sjSize ,
273+ })
274+ }
220275 }
221276
222- for _ , replica := range append ( replicas , workers ... ) {
277+ for _ , statsObservation := range observations {
223278 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 )
279+ if err := runtimeClient .Get (ctx , api .NamespacedName (statsObservation .ReplicaName , app .Namespace ), & podMetrics ); err != nil {
280+ format .PrintWarningf ("unable to get metrics for replica %s\n " , statsObservation .ReplicaName )
226281 }
227282
228- appResources := apps .AppResources [app . Status . AtProvider . Size ]
283+ maxResources := apps .AppResources [statsObservation . size ]
229284 // We expect exactly one container, fall back to [util.NoneText] if that's
230285 // not the case. The container might simply not have any metrics yet.
231286 cpuUsage , cpuPercentage := util .NoneText , util .NoneText
232287 memoryUsage , memoryPercentage := util .NoneText , util .NoneText
233288 if len (podMetrics .Containers ) == 1 {
234289 cpu := podMetrics .Containers [0 ].Usage [corev1 .ResourceCPU ]
235290 cpuUsage = formatQuantity (corev1 .ResourceCPU , cpu )
236- cpuPercentage = formatPercentage (cpu .MilliValue (), appResources .Cpu ().MilliValue ())
291+ cpuPercentage = formatPercentage (cpu .MilliValue (), maxResources .Cpu ().MilliValue ())
237292 memory := podMetrics .Containers [0 ].Usage [corev1 .ResourceMemory ]
238293 memoryUsage = formatQuantity (corev1 .ResourceMemory , memory )
239- memoryPercentage = formatPercentage (memory .MilliValue (), appResources .Memory ().MilliValue ())
294+ memoryPercentage = formatPercentage (memory .MilliValue (), maxResources .Memory ().MilliValue ())
240295 }
241296
242297 get .writeTabRow (
243- w , app .Namespace , app . Name ,
244- replica .ReplicaName ,
245- string (replica .Status ),
298+ w , app .Namespace , statsObservation . name ,
299+ statsObservation .ReplicaName ,
300+ string (statsObservation .Status ),
246301 cpuUsage ,
247302 cpuPercentage ,
248303 memoryUsage ,
249304 memoryPercentage ,
250- formatRestartCount (replica ),
251- formatExitCode (replica ),
305+ formatRestartCount (statsObservation . ReplicaObservation ),
306+ formatExitCode (statsObservation . ReplicaObservation ),
252307 )
253308 }
254309 }
0 commit comments