forked from openshift/insights-operator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release-4.11] OCPBUGS-7872: Collect info about monitoring pods pv (o…
…penshift#773) * Add new gatherer and sample file * Add unit tests for new monitoring PVs gatherer * Test golangci fix to handle modern versioning changes on docs
- Loading branch information
Showing
6 changed files
with
261 additions
and
3 deletions.
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
70 changes: 70 additions & 0 deletions
70
docs/insights-archive-sample/config/persistentvolumes/monitoring-persistent-volume.json
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,70 @@ | ||
{ | ||
"metadata": { | ||
"name": "pvc-99ffaeb3-8ff8-4137-a1fc-0bf72e7209a5", | ||
"uid": "17122aab-411b-4a71-ae35-c13caac23492", | ||
"resourceVersion": "20098", | ||
"creationTimestamp": "2023-02-20T14:44:30Z", | ||
"labels": { | ||
"topology.kubernetes.io/region": "us-west-2", | ||
"topology.kubernetes.io/zone": "us-west-2c" | ||
}, | ||
"annotations": { | ||
"kubernetes.io/createdby": "aws-ebs-dynamic-provisioner", | ||
"pv.kubernetes.io/bound-by-controller": "yes", | ||
"pv.kubernetes.io/provisioned-by": "kubernetes.io/aws-ebs" | ||
}, | ||
"finalizers": [ | ||
"kubernetes.io/pv-protection" | ||
] | ||
}, | ||
"spec": { | ||
"capacity": { | ||
"storage": "20Gi" | ||
}, | ||
"awsElasticBlockStore": { | ||
"volumeID": "aws://us-west-2c/vol-07ecf570b7adfedda", | ||
"fsType": "ext4" | ||
}, | ||
"accessModes": [ | ||
"ReadWriteOnce" | ||
], | ||
"claimRef": { | ||
"kind": "PersistentVolumeClaim", | ||
"namespace": "openshift-monitoring", | ||
"name": "prometheus-data-prometheus-k8s-1", | ||
"uid": "99ffaeb3-8ff8-4137-a1fc-0bf72e7209a5", | ||
"apiVersion": "v1", | ||
"resourceVersion": "19914" | ||
}, | ||
"persistentVolumeReclaimPolicy": "Delete", | ||
"storageClassName": "gp2", | ||
"volumeMode": "Filesystem", | ||
"nodeAffinity": { | ||
"required": { | ||
"nodeSelectorTerms": [ | ||
{ | ||
"matchExpressions": [ | ||
{ | ||
"key": "topology.kubernetes.io/region", | ||
"operator": "In", | ||
"values": [ | ||
"us-west-2" | ||
] | ||
}, | ||
{ | ||
"key": "topology.kubernetes.io/zone", | ||
"operator": "In", | ||
"values": [ | ||
"us-west-2c" | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"status": { | ||
"phase": "Bound" | ||
} | ||
} |
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,88 @@ | ||
package clusterconfig | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/openshift/insights-operator/pkg/record" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
coreV1 "k8s.io/client-go/kubernetes/typed/core/v1" | ||
) | ||
|
||
// GatherMonitoringPVs Collects Persistent Volumes from openshift-monitoring namespace | ||
// which matches with ConfigMap configuration yaml | ||
// | ||
// ### API Reference | ||
// - https://github.com/kubernetes/client-go/blob/master/kubernetes/typed/core/v1/configmap.go | ||
// - https://github.com/kubernetes/client-go/blob/master/kubernetes/typed/core/v1/persistentvolume.go | ||
// | ||
// ### Sample data | ||
// - docs/insights-archive-sample/config/persistentvolumes/monitoring-persistent-volume.json | ||
// | ||
// ### Location in archive | ||
// - `config/persistentvolumes/{persistent_volume_name}.json` | ||
// | ||
// ### Config ID | ||
// `clusterconfig/monitoring_persistent_volumes` | ||
// | ||
// ### Released version | ||
// - 4.14 | ||
// | ||
// ### Backported versions | ||
// - 4.13 | ||
// | ||
// ### Changes | ||
// None | ||
func (g *Gatherer) GatherMonitoringPVs(ctx context.Context) ([]record.Record, []error) { | ||
kubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
mg := MonitoringPVGatherer{client: kubeClient.CoreV1()} | ||
|
||
return mg.gather(ctx) | ||
} | ||
|
||
type MonitoringPVGatherer struct { | ||
client coreV1.CoreV1Interface | ||
} | ||
|
||
// gather returns the persistent volumes found as records for its gathering | ||
// and a collection of errors | ||
func (mg MonitoringPVGatherer) gather(ctx context.Context) ([]record.Record, []error) { | ||
const Namespace = "openshift-monitoring" | ||
const PrometheusDefault = "prometheus-k8s" | ||
|
||
pvcList, err := mg.client.PersistentVolumeClaims(Namespace).List(ctx, metaV1.ListOptions{}) | ||
if err != nil { | ||
return []record.Record{}, []error{err} | ||
} | ||
|
||
var records []record.Record | ||
var errors []error | ||
|
||
pvInterface := mg.client.PersistentVolumes() | ||
for i := range pvcList.Items { | ||
pvcName := pvcList.Items[i].Name | ||
|
||
if strings.Contains(pvcName, PrometheusDefault) { | ||
pvName := pvcList.Items[i].Spec.VolumeName | ||
|
||
pv, err := pvInterface.Get(ctx, pvName, metaV1.GetOptions{}) | ||
if err != nil { | ||
errors = append(errors, err) | ||
continue | ||
} | ||
|
||
records = append(records, record.Record{ | ||
Name: fmt.Sprintf("config/persistentvolumes/%s", pv.Name), | ||
Item: record.ResourceMarshaller{Resource: pv}, | ||
}) | ||
} | ||
} | ||
|
||
return records, errors | ||
} |
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,99 @@ | ||
package clusterconfig | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
kubefake "k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func Test_GatherMonitoring_gather(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
pvc *corev1.PersistentVolumeClaim | ||
pv *corev1.PersistentVolume | ||
assertErrorsNumber int | ||
assertRecordNumber int | ||
assertRecord bool | ||
assertError bool | ||
}{ | ||
{ | ||
name: "Existent Persistent Volume within the namespace is gathered", | ||
pvc: &corev1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "prometheus-k8s-0", Namespace: "openshift-monitoring"}, | ||
Spec: corev1.PersistentVolumeClaimSpec{VolumeName: "test"}, | ||
}, | ||
pv: &corev1.PersistentVolume{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "test"}, | ||
Status: corev1.PersistentVolumeStatus{Phase: "Available"}, | ||
Spec: corev1.PersistentVolumeSpec{ | ||
PersistentVolumeSource: corev1.PersistentVolumeSource{}, | ||
}, | ||
}, | ||
assertErrorsNumber: 0, | ||
assertRecordNumber: 1, | ||
assertRecord: true, | ||
}, | ||
{ | ||
name: "Existent Persistent Volume with unmatching prefix is not gathered", | ||
pvc: &corev1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "mockFail", Namespace: "openshift-monitoring"}, | ||
Spec: corev1.PersistentVolumeClaimSpec{VolumeName: "test"}, | ||
}, | ||
pv: &corev1.PersistentVolume{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "test"}, | ||
Status: corev1.PersistentVolumeStatus{Phase: "Available"}, | ||
Spec: corev1.PersistentVolumeSpec{ | ||
PersistentVolumeSource: corev1.PersistentVolumeSource{}, | ||
}, | ||
}, | ||
assertErrorsNumber: 0, | ||
assertRecordNumber: 0, | ||
}, | ||
{ | ||
name: "Non-existent Persistent Volume within the namespace throws an error", | ||
pvc: &corev1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "prometheus-k8s-0", Namespace: "openshift-monitoring"}, | ||
Spec: corev1.PersistentVolumeClaimSpec{VolumeName: "test"}, | ||
}, | ||
pv: &corev1.PersistentVolume{}, | ||
assertErrorsNumber: 1, | ||
assertRecordNumber: 0, | ||
assertError: true, | ||
}, | ||
{ | ||
name: "Non-existent Persistent Volume Claim does not throw any error", | ||
pvc: &corev1.PersistentVolumeClaim{}, | ||
pv: &corev1.PersistentVolume{}, | ||
assertErrorsNumber: 0, | ||
assertRecordNumber: 0, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
// Given | ||
coreclient := kubefake.NewSimpleClientset([]runtime.Object{testCase.pvc, testCase.pv}...) | ||
gatherer := MonitoringPVGatherer{client: coreclient.CoreV1()} | ||
|
||
// When | ||
records, errors := gatherer.gather(context.Background()) | ||
|
||
// Assert | ||
assert.Len(t, records, testCase.assertRecordNumber) | ||
if testCase.assertRecord { | ||
assert.Equal(t, "config/persistentvolumes/test", records[0].Name) | ||
} | ||
|
||
assert.Len(t, errors, testCase.assertErrorsNumber) | ||
if testCase.assertError { | ||
assert.Error(t, errors[0]) | ||
assert.Contains(t, errors[0].Error(), "not found") | ||
} | ||
}) | ||
} | ||
} |