Skip to content

Commit

Permalink
Merge pull request #25 from xiezhang7/read-items-pointer-master
Browse files Browse the repository at this point in the history
Support reading kubernetes resource lists that hold resource item pointers in the array
  • Loading branch information
bmozaffa committed Oct 24, 2019
2 parents 942a3f9 + 5d91538 commit 7e918ca
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
30 changes: 28 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion pkg/resource/read/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,22 @@ func (this *resourceReader) List(listObject runtime.Object) ([]resource.Kubernet
}
itemsValue := reflect.Indirect(reflect.ValueOf(listObject)).FieldByName("Items")
for index := 0; index < itemsValue.Len(); index++ {
item := itemsValue.Index(index).Addr().Interface().(resource.KubernetesResource)
item := addr(itemsValue.Index(index)).Interface().(resource.KubernetesResource)
if this.ownerObject == nil || isOwner(this.ownerObject, item) {
resources = append(resources, item)
}
}
return resources, nil
}

func addr(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Ptr {
return v
}

return v.Addr()
}

func isOwner(owner metav1.Object, res resource.KubernetesResource) bool {
for _, ownerRef := range res.GetOwnerReferences() {
if ownerRef.UID == owner.GetUID() {
Expand Down
30 changes: 28 additions & 2 deletions pkg/resource/read/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package read
import (
"context"
"fmt"
monv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -18,6 +19,8 @@ func TestListObjects(t *testing.T) {
scheme := runtime.NewScheme()
err := corev1.SchemeBuilder.AddToScheme(scheme)
assert.Nil(t, err, "Expect no errors building scheme")
err = monv1.SchemeBuilder.AddToScheme(scheme)
assert.Nil(t, err, "Expect no errors building scheme")
client := fake.NewFakeClientWithScheme(scheme)
services := getServices(2)
for index := range services {
Expand All @@ -27,11 +30,15 @@ func TestListObjects(t *testing.T) {
for index := range pods {
assert.Nil(t, client.Create(context.TODO(), &pods[index]), "Expect no errors mock creating objects")
}
serviceMonitors := getServiceMonitors(2)
for index := range serviceMonitors {
assert.Nil(t, client.Create(context.TODO(), &serviceMonitors[index]), "Expect no errors mock creating objects")
}

reader := New(client).WithNamespace(namespace)
objectMap, err := reader.ListAll(&corev1.ServiceList{}, &corev1.PodList{})
objectMap, err := reader.ListAll(&corev1.ServiceList{}, &corev1.PodList{}, &monv1.ServiceMonitorList{})
assert.Nil(t, err, "Expect no errors listing objects")
assert.Len(t, objectMap, 2, "Expect two object types found")
assert.Len(t, objectMap, 3, "Expect two object types found")

listedServices := objectMap[reflect.TypeOf(corev1.Service{})]
assert.Len(t, listedServices, 2, "Expect to find 2 services")
Expand All @@ -45,6 +52,12 @@ func TestListObjects(t *testing.T) {
assert.Equal(t, &expectedPods[0], listedPods[0])
assert.Equal(t, &expectedPods[1], listedPods[1])
assert.Equal(t, &expectedPods[2], listedPods[2])

listedServiceMonitors := objectMap[reflect.TypeOf(monv1.ServiceMonitor{})]
assert.Len(t, listedServiceMonitors, 2, "Expect to find 2 servicemonitors")
expectedServiceMonitors := getServiceMonitors(2)
assert.Equal(t, &expectedServiceMonitors[0], listedServiceMonitors[0])
assert.Equal(t, &expectedServiceMonitors[1], listedServiceMonitors[1])
}

func TestLoadObject(t *testing.T) {
Expand Down Expand Up @@ -85,3 +98,16 @@ func getPods(count int) []corev1.Pod {
}
return pods
}

func getServiceMonitors(count int) []monv1.ServiceMonitor {
servicemonitors := make([]monv1.ServiceMonitor, count)
for index := range servicemonitors {
servicemonitors[index] = monv1.ServiceMonitor{
ObjectMeta: v1.ObjectMeta{
Name: fmt.Sprintf("servicemonitor-%d", index+1),
Namespace: namespace,
},
}
}
return servicemonitors
}

0 comments on commit 7e918ca

Please sign in to comment.