Skip to content

Commit

Permalink
ClusterToObjectsMapper: use namespace in client.List
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer buringerst@vmware.com
  • Loading branch information
sbueringer committed Oct 6, 2021
1 parent 18ecf48 commit 22bbc07
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ require (
sigs.k8s.io/controller-runtime v0.10.2
sigs.k8s.io/yaml v1.2.0
)

replace sigs.k8s.io/controller-runtime => /Users/buringerst/code/src/sigs.k8s.io/controller-runtime
35 changes: 34 additions & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -489,15 +490,30 @@ func ClusterToObjectsMapper(c client.Client, ro client.ObjectList, scheme *runti
return nil, err
}

isNamespaced, err := isAPINamespaced(gvk, c.RESTMapper())
if err != nil {
return nil, err
}

return func(o client.Object) []ctrl.Request {
cluster, ok := o.(*clusterv1.Cluster)
if !ok {
return nil
}

listOpts := []client.ListOption{
client.MatchingLabels{
clusterv1.ClusterLabelName: cluster.Name,
},
}

if isNamespaced {
listOpts = append(listOpts, client.InNamespace(cluster.Namespace))
}

list := &unstructured.UnstructuredList{}
list.SetGroupVersionKind(gvk)
if err := c.List(context.TODO(), list, client.MatchingLabels{clusterv1.ClusterLabelName: cluster.Name}); err != nil {
if err := c.List(context.TODO(), list, listOpts...); err != nil {
return nil
}

Expand All @@ -511,6 +527,23 @@ func ClusterToObjectsMapper(c client.Client, ro client.ObjectList, scheme *runti
}, nil
}

// isAPINamespaced detects if a GroupVersionKind is namespaced.
func isAPINamespaced(gk schema.GroupVersionKind, restmapper meta.RESTMapper) (bool, error) {
restMapping, err := restmapper.RESTMapping(schema.GroupKind{Group: gk.Group, Kind: gk.Kind})
if err != nil {
return false, fmt.Errorf("failed to get restmapping: %w", err)
}

switch restMapping.Scope.Name() {
case "":
return false, errors.New("Scope cannot be identified. Empty scope returned")
case meta.RESTScopeNameRoot:
return false, nil
default:
return true, nil
}
}

// ObjectReferenceToUnstructured converts an object reference to an unstructured object.
func ObjectReferenceToUnstructured(in corev1.ObjectReference) *unstructured.Unstructured {
out := &unstructured.Unstructured{}
Expand Down
18 changes: 15 additions & 3 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import (
"github.com/blang/semver"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
Expand Down Expand Up @@ -673,8 +674,19 @@ func TestClusterToObjectsMapper(t *testing.T) {

for _, tc := range table {
tc.objects = append(tc.objects, cluster)
client := fake.NewClientBuilder().WithObjects(tc.objects...).Build()
f, err := ClusterToObjectsMapper(client, tc.input, scheme.Scheme)

scheme := runtime.NewScheme()
_ = clusterv1.AddToScheme(scheme)

restMapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{clusterv1.GroupVersion})

// Add tc.input gvk to the restMapper.
gvk, err := apiutil.GVKForObject(tc.input, scheme)
g.Expect(err).ToNot(HaveOccurred())
restMapper.Add(gvk, meta.RESTScopeNamespace)

client := fake.NewClientBuilder().WithObjects(tc.objects...).WithRESTMapper(restMapper).Build()
f, err := ClusterToObjectsMapper(client, tc.input, scheme)
g.Expect(err != nil, err).To(Equal(tc.expectError))
g.Expect(f(cluster)).To(ConsistOf(tc.output))
}
Expand Down

0 comments on commit 22bbc07

Please sign in to comment.