Skip to content

Commit

Permalink
Remove error return from ListResources signature
Browse files Browse the repository at this point in the history
The internal implementation of ListResources can no longer
return an error so modify the signature accordingly to simplify
usage.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Jul 20, 2023
1 parent b12d33e commit 8979282
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 32 deletions.
25 changes: 5 additions & 20 deletions pkg/syncer/broker/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,27 +321,12 @@ func (s *Syncer) Start(stopCh <-chan struct{}) error {
}
}

lister := func(s syncer.Interface) []runtime.Object {
list, err := s.ListResources()
if err != nil {
logger.Error(err, "Unable to reconcile - error listing resources")
return nil
}

return list
}

for i := 0; i < len(s.syncers); i += 2 {
localSyncer := s.syncers[i]
remoteSyncer := s.syncers[i+1]

remoteSyncer.Reconcile(func() []runtime.Object {
return lister(localSyncer)
})

localSyncer.Reconcile(func() []runtime.Object {
return lister(remoteSyncer)
})
remoteSyncer.Reconcile(localSyncer.ListResources)
localSyncer.Reconcile(remoteSyncer.ListResources)
}

return nil
Expand All @@ -364,13 +349,13 @@ func (s *Syncer) GetLocalResource(name, namespace string, ofType runtime.Object)
return ls.GetResource(name, namespace) //nolint:wrapcheck // OK to return the error as is.
}

func (s *Syncer) ListLocalResources(ofType runtime.Object) ([]runtime.Object, error) {
func (s *Syncer) ListLocalResources(ofType runtime.Object) []runtime.Object {
ls, found := s.localSyncers[reflect.TypeOf(ofType)]
if !found {
return nil, fmt.Errorf("no Syncer found for %#v", ofType)
panic(fmt.Errorf("no Syncer found for %#v", ofType))
}

return ls.ListResources() //nolint:wrapcheck // OK to return the error as is.
return ls.ListResources()
}

func (s *Syncer) ListLocalResourcesBySelector(ofType runtime.Object, selector labels.Selector) []runtime.Object {
Expand Down
3 changes: 1 addition & 2 deletions pkg/syncer/broker/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,7 @@ var _ = Describe("Broker Syncer", func() {
test.CreateResource(localClient, resource)
test.AwaitResource(brokerClient, resource.GetName())

list, err := syncer.ListLocalResources(resource)
Expect(err).To(Succeed())
list := syncer.ListLocalResources(resource)
Expect(list).To(HaveLen(1))
Expect(list[0]).To(BeAssignableToTypeOf(&corev1.Pod{}))
Expect(&list[0].(*corev1.Pod).Spec).To(Equal(&resource.Spec))
Expand Down
10 changes: 3 additions & 7 deletions pkg/syncer/resource_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8slabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -310,8 +309,8 @@ func (r *resourceSyncer) GetResource(name, namespace string) (runtime.Object, bo
return converted, true, nil
}

func (r *resourceSyncer) ListResources() ([]runtime.Object, error) {
return r.ListResourcesBySelector(k8slabels.Everything()), nil
func (r *resourceSyncer) ListResources() []runtime.Object {
return r.ListResourcesBySelector(k8slabels.Everything())
}

func (r *resourceSyncer) ListResourcesBySelector(selector k8slabels.Selector) []runtime.Object {
Expand All @@ -330,10 +329,7 @@ func (r *resourceSyncer) ListResourcesBySelector(selector k8slabels.Selector) []
continue
}

converted, err := r.convert(obj.(*unstructured.Unstructured))
utilruntime.Must(err)

retObjects = append(retObjects, converted)
retObjects = append(retObjects, r.convertNoError(obj.(*unstructured.Unstructured)))
}

return retObjects
Expand Down
3 changes: 1 addition & 2 deletions pkg/syncer/resource_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,8 +872,7 @@ func testListResources() {
})

It("should return all the resources", func() {
list, err := d.syncer.ListResources()
Expect(err).To(Succeed())
list := d.syncer.ListResources()
assertResourceList(list, d.resource, resource2)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Interface interface {
Start(stopCh <-chan struct{}) error
AwaitStopped()
GetResource(name, namespace string) (runtime.Object, bool, error)
ListResources() ([]runtime.Object, error)
ListResources() []runtime.Object
ListResourcesBySelector(selector labels.Selector) []runtime.Object
Reconcile(resourceLister func() []runtime.Object)
}

0 comments on commit 8979282

Please sign in to comment.