@@ -10,8 +10,26 @@ import (
1010 corev1 "k8s.io/api/core/v1"
1111 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212 ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
13+
14+ machinev1beta1 "github.com/openshift/api/machine/v1beta1"
15+ autoscalingv1beta1 "github.com/openshift/cluster-autoscaler-operator/pkg/apis/autoscaling/v1beta1"
1316)
1417
18+ type MachineSetCount struct {
19+ Name string `json:"name"`
20+ Current int `json:"current"`
21+ Min int `json:"min"`
22+ Max int `json:"max"`
23+ }
24+
25+ type WorkloadNodeCount struct {
26+ Workload string `json:"workload"`
27+ Current int `json:"current"`
28+ Min int `json:"min"`
29+ Max int `json:"max"`
30+ MachineSets []MachineSetCount `json:"machine_sets"`
31+ }
32+
1533type PodLifecycleMetricsEvent struct {
1634 PodName string `json:"pod_name,omitempty"`
1735 Namespace string `json:"namespace,omitempty"`
@@ -31,22 +49,30 @@ type PodLifecycleMetricsEvent struct {
3149 InitContainerRestarts int `json:"init_container_restarts,omitempty"`
3250 InitContainerLastError string `json:"init_container_last_error,omitempty"`
3351 Timestamp time.Time `json:"timestamp,omitempty"`
52+
53+ WorkloadCapacity WorkloadNodeCount `json:"workload_capacity,omitempty"`
3454}
3555
3656func (e * PodLifecycleMetricsEvent ) SetTimestamp (ts time.Time ) {
3757 e .Timestamp = ts
3858}
3959
4060type PodLifecyclePlugin struct {
41- ctx context.Context
42- logger * logrus.Entry
43- mu sync.Mutex
44- events []PodLifecycleMetricsEvent
45- client ctrlruntimeclient.Client
61+ ctx context.Context
62+ logger * logrus.Entry
63+ mu sync.Mutex
64+ events []PodLifecycleMetricsEvent
65+ client ctrlruntimeclient.Client
66+ autoscalers []autoscalingv1beta1.MachineAutoscaler
4667}
4768
48- func NewPodLifecyclePlugin (ctx context.Context , logger * logrus.Entry , client ctrlruntimeclient.Client ) * PodLifecyclePlugin {
49- return & PodLifecyclePlugin {ctx : ctx , logger : logger .WithField ("plugin" , "pods" ), client : client }
69+ func NewPodLifecyclePlugin (ctx context.Context , logger * logrus.Entry , client ctrlruntimeclient.Client , autoscalers []autoscalingv1beta1.MachineAutoscaler ) * PodLifecyclePlugin {
70+ return & PodLifecyclePlugin {
71+ ctx : ctx ,
72+ logger : logger .WithField ("plugin" , "pods" ),
73+ client : client ,
74+ autoscalers : autoscalers ,
75+ }
5076}
5177
5278func (p * PodLifecyclePlugin ) Name () string {
@@ -70,7 +96,10 @@ func (p *PodLifecyclePlugin) Record(ev MetricsEvent) {
7096 e .CreationTime = & pod .CreationTimestamp .Time
7197 e .StartTime = & pod .Status .StartTime .Time
7298 e .CompletionTime = getPodCompletionTime (pod )
73- e .CIWorkload = pod .Labels ["ci-workload" ]
99+ e .CIWorkload = pod .Labels [CIWorkloadLabel ]
100+ if e .CIWorkload != "" {
101+ e .WorkloadCapacity = p .getWorkloadCounts (e .CIWorkload )
102+ }
74103
75104 // Only set pod phase if not already set by caller (preserves success/failure determination)
76105 if e .PodPhase == "" {
@@ -125,6 +154,41 @@ func (p *PodLifecyclePlugin) Events() []MetricsEvent {
125154 return out
126155}
127156
157+ func (p * PodLifecyclePlugin ) getMinMax (machineSetName string ) (int , int ) {
158+ for _ , autoscaler := range p .autoscalers {
159+ if autoscaler .Spec .ScaleTargetRef .Name == machineSetName {
160+ return int (autoscaler .Spec .MinReplicas ), int (autoscaler .Spec .MaxReplicas )
161+ }
162+ }
163+ return 0 , 0
164+ }
165+
166+ func (p * PodLifecyclePlugin ) getWorkloadCounts (workload string ) WorkloadNodeCount {
167+ ret := WorkloadNodeCount {Workload : workload }
168+ machineSetList := & machinev1beta1.MachineSetList {}
169+ if err := p .client .List (p .ctx , machineSetList ); err != nil {
170+ p .logger .WithError (err ).Warn ("Failed to list MachineSets" )
171+ return WorkloadNodeCount {}
172+ }
173+
174+ for _ , ms := range machineSetList .Items {
175+ msWorkload := ms .Spec .Template .Spec .ObjectMeta .Labels [CIWorkloadLabel ]
176+ if msWorkload != workload {
177+ continue
178+ }
179+
180+ current := int (ms .Status .Replicas )
181+ min , max := p .getMinMax (ms .Name )
182+
183+ ret .Current += current
184+ ret .Min += min
185+ ret .Max += max
186+ ret .MachineSets = append (ret .MachineSets , MachineSetCount {Name : ms .Name , Current : current , Min : min , Max : max })
187+ }
188+
189+ return ret
190+ }
191+
128192func getPodCompletionTime (pod * corev1.Pod ) * time.Time {
129193 var end metav1.Time
130194 for _ , status := range pod .Status .ContainerStatuses {
0 commit comments