-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add VolumeAttachment collector #946
Merged
k8s-ci-robot
merged 1 commit into
kubernetes:master
from
c445:jenserat/volumeattachments
Oct 17, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# PersistentVolume Metrics | ||
|
||
| Metric name| Metric type | Labels/tags | Status | | ||
| ---------- | ----------- | ----------- | ----------- | | ||
| kube_volumeattachment_info | Gauge | `volumeattachment`=<volumeattachment-name> <br> `attacher`=<attacher-name> <br> `nodeName`=<node-name> | EXPERIMENTAL | | ||
| kube_volumeattachment_created | Gauge | `volumeattachment`=<volumeattachment-name> | EXPERIMENTAL | | ||
| kube_volumeattachment_labels | Gauge | `volumeattachment`=<volumeattachment-name> <br> `label_VOLUMEATTACHMENT_LABEL`=<VOLUMEATTACHMENT_LABEL> | EXPERIMENTAL | | ||
| kube_volumeattachment_spec_source_persistentvolume | Gauge | `volumeattachment`=<volumeattachment-name> <br> `volumename`=<persistentvolume-name> | EXPERIMENTAL | | ||
| kube_volumeattachment_status_attached | Gauge | `volumeattachment`=<volumeattachment-name> | EXPERIMENTAL | | ||
| kube_volumeattachment_status_attachment_metadata | Gauge | `volumeattachment`=<volumeattachment-name> <br> `metadata_METADATA_KEY`=<METADATA_VALUE> | EXPERIMENTAL | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ rules: | |
- storage.k8s.io | ||
resources: | ||
- storageclasses | ||
- volumeattachments | ||
verbs: | ||
- list | ||
- watch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ rules: | |
- storage.k8s.io | ||
resources: | ||
- storageclasses | ||
- volumeattachments | ||
verbs: | ||
- list | ||
- watch | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package store | ||
|
||
import ( | ||
storagev1beta1 "k8s.io/api/storage/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/watch" | ||
clientset "k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/cache" | ||
|
||
"k8s.io/kube-state-metrics/pkg/metric" | ||
) | ||
|
||
var ( | ||
descVolumeAttachmentLabelsName = "kube_volumeattachment_labels" | ||
descVolumeAttachmentLabelsHelp = "Kubernetes labels converted to Prometheus labels." | ||
descVolumeAttachmentLabelsDefaultLabels = []string{"volumeattachment"} | ||
|
||
volumeAttachmentMetricFamilies = []metric.FamilyGenerator{ | ||
{ | ||
Name: descVolumeAttachmentLabelsName, | ||
Type: metric.Gauge, | ||
Help: descVolumeAttachmentLabelsHelp, | ||
GenerateFunc: wrapVolumeAttachmentFunc(func(va *storagev1beta1.VolumeAttachment) *metric.Family { | ||
labelKeys, labelValues := kubeLabelsToPrometheusLabels(va.Labels) | ||
return &metric.Family{ | ||
Metrics: []*metric.Metric{ | ||
{ | ||
LabelKeys: labelKeys, | ||
LabelValues: labelValues, | ||
Value: 1, | ||
}, | ||
}, | ||
} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_volumeattachment_info", | ||
Type: metric.Gauge, | ||
Help: "Information about volumeattachment.", | ||
GenerateFunc: wrapVolumeAttachmentFunc(func(va *storagev1beta1.VolumeAttachment) *metric.Family { | ||
return &metric.Family{ | ||
Metrics: []*metric.Metric{ | ||
{ | ||
LabelKeys: []string{"attacher", "nodeName"}, | ||
LabelValues: []string{va.Spec.Attacher, va.Spec.NodeName}, | ||
Value: 1, | ||
}, | ||
}, | ||
} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_volumeattachment_created", | ||
Type: metric.Gauge, | ||
Help: "Unix creation timestamp", | ||
GenerateFunc: wrapVolumeAttachmentFunc(func(va *storagev1beta1.VolumeAttachment) *metric.Family { | ||
if !va.CreationTimestamp.IsZero() { | ||
m := metric.Metric{ | ||
LabelKeys: nil, | ||
LabelValues: nil, | ||
Value: float64(va.CreationTimestamp.Unix()), | ||
} | ||
return &metric.Family{Metrics: []*metric.Metric{&m}} | ||
} | ||
return &metric.Family{Metrics: []*metric.Metric{}} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_volumeattachment_spec_source_persistentvolume", | ||
Type: metric.Gauge, | ||
Help: "PersistentVolume source reference.", | ||
GenerateFunc: wrapVolumeAttachmentFunc(func(va *storagev1beta1.VolumeAttachment) *metric.Family { | ||
if va.Spec.Source.PersistentVolumeName != nil { | ||
return &metric.Family{ | ||
Metrics: []*metric.Metric{ | ||
{ | ||
LabelKeys: []string{"volumename"}, | ||
LabelValues: []string{*va.Spec.Source.PersistentVolumeName}, | ||
Value: 1, | ||
}, | ||
}, | ||
} | ||
} | ||
return &metric.Family{} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_volumeattachment_status_attached", | ||
Type: metric.Gauge, | ||
Help: "Information about volumeattachment.", | ||
GenerateFunc: wrapVolumeAttachmentFunc(func(va *storagev1beta1.VolumeAttachment) *metric.Family { | ||
return &metric.Family{ | ||
Metrics: []*metric.Metric{ | ||
{ | ||
LabelKeys: nil, | ||
LabelValues: nil, | ||
Value: boolFloat64(va.Status.Attached), | ||
}, | ||
}, | ||
} | ||
}), | ||
}, | ||
{ | ||
Name: "kube_volumeattachment_status_attachment_metadata", | ||
Type: metric.Gauge, | ||
Help: "volumeattachment metadata.", | ||
GenerateFunc: wrapVolumeAttachmentFunc(func(va *storagev1beta1.VolumeAttachment) *metric.Family { | ||
labelKeys, labelValues := mapToPrometheusLabels(va.Status.AttachmentMetadata, "metadata") | ||
return &metric.Family{ | ||
Metrics: []*metric.Metric{ | ||
{ | ||
LabelKeys: labelKeys, | ||
LabelValues: labelValues, | ||
Value: 1, | ||
}, | ||
}, | ||
} | ||
}), | ||
}, | ||
} | ||
) | ||
|
||
func wrapVolumeAttachmentFunc(f func(*storagev1beta1.VolumeAttachment) *metric.Family) func(interface{}) *metric.Family { | ||
return func(obj interface{}) *metric.Family { | ||
va := obj.(*storagev1beta1.VolumeAttachment) | ||
|
||
metricFamily := f(va) | ||
|
||
for _, m := range metricFamily.Metrics { | ||
m.LabelKeys = append(descVolumeAttachmentLabelsDefaultLabels, m.LabelKeys...) | ||
m.LabelValues = append([]string{va.Name}, m.LabelValues...) | ||
} | ||
|
||
return metricFamily | ||
} | ||
} | ||
|
||
func createVolumeAttachmentListWatch(kubeClient clientset.Interface, _ string) cache.ListerWatcher { | ||
return &cache.ListWatch{ | ||
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { | ||
return kubeClient.StorageV1beta1().VolumeAttachments().List(opts) | ||
}, | ||
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) { | ||
return kubeClient.StorageV1beta1().VolumeAttachments().Watch(opts) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package store | ||
|
||
import ( | ||
storagev1beta1 "k8s.io/api/storage/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"testing" | ||
|
||
"k8s.io/kube-state-metrics/pkg/metric" | ||
) | ||
|
||
func TestVolumeAttachmentStore(t *testing.T) { | ||
const metadata = ` | ||
# HELP kube_volumeattachment_created Unix creation timestamp | ||
# HELP kube_volumeattachment_info Information about volumeattachment. | ||
# HELP kube_volumeattachment_labels Kubernetes labels converted to Prometheus labels. | ||
# HELP kube_volumeattachment_spec_source_persistentvolume PersistentVolume source reference. | ||
# HELP kube_volumeattachment_status_attached Information about volumeattachment. | ||
# HELP kube_volumeattachment_status_attachment_metadata volumeattachment metadata. | ||
# TYPE kube_volumeattachment_created gauge | ||
# TYPE kube_volumeattachment_info gauge | ||
# TYPE kube_volumeattachment_labels gauge | ||
# TYPE kube_volumeattachment_spec_source_persistentvolume gauge | ||
# TYPE kube_volumeattachment_status_attached gauge | ||
# TYPE kube_volumeattachment_status_attachment_metadata gauge | ||
` | ||
|
||
var ( | ||
volumename = "pvc-44f6ff3f-ba9b-49c4-9b95-8b01c4bd4bab" | ||
cases = []generateMetricsTestCase{ | ||
{ | ||
Obj: &storagev1beta1.VolumeAttachment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Generation: 2, | ||
Name: "csi-5ff16a1ad085261021e21c6cb3a6defb979a8794f25a4f90f6285664cff37224", | ||
Labels: map[string]string{ | ||
"app": "foobar", | ||
}, | ||
}, | ||
Spec: storagev1beta1.VolumeAttachmentSpec{ | ||
Attacher: "cinder.csi.openstack.org", | ||
NodeName: "node1", | ||
Source: storagev1beta1.VolumeAttachmentSource{ | ||
PersistentVolumeName: &volumename, | ||
InlineVolumeSpec: nil, | ||
}, | ||
}, | ||
Status: storagev1beta1.VolumeAttachmentStatus{ | ||
Attached: true, | ||
AttachmentMetadata: map[string]string{ | ||
"DevicePath": "/dev/sdd", | ||
}, | ||
}, | ||
}, | ||
Want: metadata + ` | ||
kube_volumeattachment_info{attacher="cinder.csi.openstack.org",nodeName="node1",volumeattachment="csi-5ff16a1ad085261021e21c6cb3a6defb979a8794f25a4f90f6285664cff37224"} 1 | ||
kube_volumeattachment_labels{label_app="foobar",volumeattachment="csi-5ff16a1ad085261021e21c6cb3a6defb979a8794f25a4f90f6285664cff37224"} 1 | ||
kube_volumeattachment_spec_source_persistentvolume{volumeattachment="csi-5ff16a1ad085261021e21c6cb3a6defb979a8794f25a4f90f6285664cff37224",volumename="pvc-44f6ff3f-ba9b-49c4-9b95-8b01c4bd4bab"} 1 | ||
kube_volumeattachment_status_attached{volumeattachment="csi-5ff16a1ad085261021e21c6cb3a6defb979a8794f25a4f90f6285664cff37224"} 1 | ||
kube_volumeattachment_status_attachment_metadata{metadata_DevicePath="/dev/sdd",volumeattachment="csi-5ff16a1ad085261021e21c6cb3a6defb979a8794f25a4f90f6285664cff37224"} 1 | ||
`, | ||
MetricNames: []string{ | ||
"kube_volumeattachment_labels", | ||
"kube_volumeattachment_info", | ||
"kube_volumeattachment_created", | ||
"kube_volumeattachment_spec_source_persistentvolume", | ||
"kube_volumeattachment_status_attached", | ||
"kube_volumeattachment_status_attachment_metadata", | ||
}, | ||
}, | ||
} | ||
) | ||
for i, c := range cases { | ||
c.Func = metric.ComposeMetricGenFuncs(volumeAttachmentMetricFamilies) | ||
c.Headers = metric.ExtractMetricFamilyHeaders(volumeAttachmentMetricFamilies) | ||
if err := c.run(); err != nil { | ||
t.Errorf("unexpected collecting result in %vth run:\n%s", i, err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: storage.k8s.io/v1 | ||
kind: VolumeAttachment | ||
metadata: | ||
name: volumeattachment | ||
spec: | ||
attacher: attacher | ||
nodeName: minikube | ||
source: | ||
persistentVolumeName: persistentvolume |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use
storagev1
for VolumeAttachment. It has been promoted to v1 since 1.13 if I am not mistaken.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should even support the watching and listing of VolumeAttachments created under
v1beta1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it graduated to GA in 1.13 (as also stated in the PR). I started with
storagev1
originally, but it failed in a Kubernetes 1.14 cluster -- kube-state-metrics got v1beta1 objects returned and paniced.On the other hand, following the compatibility chart from the README we need to support Kubernetes 1.11? In this case, only
storagev1beta1
is available.