6
6
"github.com/gophercloud/gophercloud"
7
7
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/quotasets"
8
8
"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
9
- "github.com/gophercloud/gophercloud/pagination"
10
9
"github.com/prometheus/client_golang/prometheus"
11
10
corev1 "k8s.io/api/core/v1"
12
11
"k8s.io/client-go/kubernetes"
@@ -93,39 +92,48 @@ func registerCinderMetrics() {
93
92
// PublishCinderMetrics makes the list request to the blockstorage api and passes
94
93
// the result to a publish function.
95
94
func PublishCinderMetrics (client * gophercloud.ServiceClient , clientset * kubernetes.Clientset , tenantID string ) error {
95
+ // first step: gather the data
96
+
96
97
// get the cinder pvs to add metadata
97
98
pvs , err := getPVsByCinderID (clientset )
98
99
if err != nil {
99
100
return err
100
101
}
101
102
102
- // cinderQuotaVolumes and CinderQuotaVolumesGigabytes are not dynamic and do not need to be reset
103
- cinderVolumeCreated .Reset ()
104
- cinderVolumeUpdatedAt .Reset ()
105
- cinderVolumeStatus .Reset ()
106
- cinderVolumeSize .Reset ()
107
- cinderVolumeAttachedAt .Reset ()
108
-
109
- // get all volumes and publish them
103
+ // get all volumes from openstack
110
104
mc := newOpenStackMetric ("volume" , "list" )
111
- pager := volumes .List (client , volumes.ListOpts {})
112
- err = pager .EachPage (func (page pagination.Page ) (bool , error ) {
113
- return publishVolumesPage (page , pvs )
114
- })
105
+ pages , err := volumes .List (client , volumes.ListOpts {}).AllPages ()
115
106
if mc .Observe (err ) != nil {
116
107
// only warn, maybe the next list will work.
117
108
klog .Warningf ("Unable to list volumes: %v" , err )
118
109
return err
119
110
}
111
+ volumesList , err := volumes .ExtractVolumes (pages )
112
+ if err != nil {
113
+ return err
114
+ }
120
115
116
+ // get quotas form openstack
121
117
mc = newOpenStackMetric ("volume_quotasets_usage" , "get" )
122
- q , err := quotasets .GetUsage (client , tenantID ).Extract ()
118
+ quotas , err := quotasets .GetUsage (client , tenantID ).Extract ()
123
119
if mc .Observe (err ) != nil {
124
120
// only warn, maybe the next get will work.
125
121
klog .Warningf ("Unable to get quotas: %v" , err )
126
122
return err
127
123
}
128
- publishCinderQuotas (q )
124
+
125
+ // second step: reset the old metrics
126
+ // cinderQuotaVolumes and CinderQuotaVolumesGigabytes are not dynamic and do not need to be reset
127
+ cinderVolumeCreated .Reset ()
128
+ cinderVolumeUpdatedAt .Reset ()
129
+ cinderVolumeStatus .Reset ()
130
+ cinderVolumeSize .Reset ()
131
+ cinderVolumeAttachedAt .Reset ()
132
+
133
+ // third step: publish the metrics
134
+ publishVolumes (volumesList , pvs )
135
+
136
+ publishCinderQuotas (quotas )
129
137
return nil
130
138
}
131
139
@@ -142,21 +150,15 @@ func publishCinderQuotas(q quotasets.QuotaUsageSet) {
142
150
cinderQuotaVolumesGigabyte .WithLabelValues ("allocated" ).Set (float64 (q .Gigabytes .Allocated ))
143
151
}
144
152
145
- // publishVolumesPage iterates over a page, the result of a list request
146
- func publishVolumesPage (page pagination.Page , pvs map [string ]corev1.PersistentVolume ) (bool , error ) {
147
- vList , err := volumes .ExtractVolumes (page )
148
- if err != nil {
149
- return false , err
150
- }
151
-
153
+ // publishVolumes iterates over a page, the result of a list request
154
+ func publishVolumes (vList []volumes.Volume , pvs map [string ]corev1.PersistentVolume ) {
152
155
for _ , v := range vList {
153
156
if pv , ok := pvs [v .ID ]; ok {
154
157
publishVolumeMetrics (v , & pv )
155
158
} else {
156
159
publishVolumeMetrics (v , nil )
157
160
}
158
161
}
159
- return true , nil
160
162
}
161
163
162
164
// publishVolumeMetrics extracts data from a volume and exposes the metrics via prometheus
0 commit comments