Skip to content

Commit

Permalink
Virtual kubelet: do not start reflection with 0 workers
Browse files Browse the repository at this point in the history
  • Loading branch information
giorio94 authored and adamjensenbot committed Jun 17, 2022
1 parent 7640770 commit 57e429d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
32 changes: 31 additions & 1 deletion pkg/virtualKubelet/reflection/generic/reflector.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
)

var _ manager.Reflector = (*reflector)(nil)
var _ manager.Reflector = (*dummyreflector)(nil)

// NamespacedReflectorFactoryFunc represents the function type to create a new NamespacedReflector.
type NamespacedReflectorFactoryFunc func(*options.NamespacedOpts) manager.NamespacedReflector
Expand All @@ -59,8 +60,18 @@ type reflector struct {
fallbackFactory FallbackReflectorFactoryFunc
}

// NewReflector returns a new reflector to implement the reflection towards a remote clusters.
// NewReflector returns a new reflector to implement the reflection towards a remote clusters, of a dummy one if no workers are specified.
func NewReflector(name string, namespaced NamespacedReflectorFactoryFunc, fallback FallbackReflectorFactoryFunc, workers uint) manager.Reflector {
if workers == 0 {
// Return a dummy reflector in case no workers are specified, to avoid starting the working queue and registering the infromers.
return &dummyreflector{name: name}
}

return newReflector(name, namespaced, fallback, workers)
}

// newReflector returns a new reflector to implement the reflection towards a remote clusters.
func newReflector(name string, namespaced NamespacedReflectorFactoryFunc, fallback FallbackReflectorFactoryFunc, workers uint) manager.Reflector {
return &reflector{
name: name,
workers: workers,
Expand Down Expand Up @@ -258,3 +269,22 @@ func NamespacedKeyer(namespace string) func(metadata metav1.Object) types.Namesp
func WithoutFallback() FallbackReflectorFactoryFunc {
return func(ro *options.ReflectorOpts) manager.FallbackReflector { return nil }
}

type dummyreflector struct{ name string }

// Start starts the dummy reflector (no-op).
func (dr *dummyreflector) Start(ctx context.Context, opts *options.ReflectorOpts) {
klog.Infof("Skipping starting the %v reflector, as no workers are configured", dr.name)
}

// StartNamespace starts the reflection for the given namespace (no-op).
func (dr *dummyreflector) StartNamespace(opts *options.NamespacedOpts) {
klog.Infof("Skipping starting the %v reflection between local namespace %q and remote namespace %q, as no workers are configured",
dr.name, opts.LocalNamespace, opts.RemoteNamespace)
}

// StopNamespace stops the reflection for the given namespace (no-op).
func (dr *dummyreflector) StopNamespace(local, remote string) {
klog.Infof("Skipping stopping the %v reflection between local namespace %q and remote namespace %q, as no workers are configured",
dr.name, local, remote)
}
20 changes: 19 additions & 1 deletion pkg/virtualKubelet/reflection/generic/reflector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -62,10 +63,27 @@ var _ = Describe("Reflector tests", func() {
})

Context("a new reflector is created", func() {
BeforeEach(func() { workers = 10 })
JustBeforeEach(func() {
rfl = NewReflector(reflectorName, NewFakeNamespacedReflector, NewFakeFallbackReflector, workers)
})

When("no workers are specified", func() {
BeforeEach(func() { workers = 0 })
It("should return a dummy reflector", func() { Expect(rfl).To(PointTo(BeAssignableToTypeOf(dummyreflector{}))) })
})

When("at least one worker is specified", func() {
BeforeEach(func() { workers = 1 })
It("should return a real reflector", func() { Expect(rfl).To(PointTo(BeAssignableToTypeOf(reflector{}))) })
})
})

Context("a new real reflector is created", func() {
BeforeEach(func() { workers = 10 })
JustBeforeEach(func() {
// Here, we use the internal function, to retrieve the real reflector also in case no workers are set.
rfl = newReflector(reflectorName, NewFakeNamespacedReflector, NewFakeFallbackReflector, workers)
})
It("should return a non nil reflector", func() { Expect(rfl).ToNot(BeNil()) })
It("should correctly populate the reflector fields", func() {
Expect(rfl.(*reflector).name).To(BeIdenticalTo(reflectorName))
Expand Down

0 comments on commit 57e429d

Please sign in to comment.