Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
Sync the informer cache on each test. (#9)
Browse files Browse the repository at this point in the history
This change should have been made a long time ago. On test start up,
sync the informer caches right before we start to run any checks. This
ensures that cluster precondition checks are reliable.

Signed-off-by: James Peach <[email protected]>
  • Loading branch information
jpeach authored Jul 28, 2020
1 parent b155e1c commit 3853a9b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
22 changes: 22 additions & 0 deletions pkg/driver/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ type ObjectDriver interface {
// watchers.
InformOn(gvr schema.GroupVersionResource) error

// WaitForCacheSync waits until all the informers created
// by the driver have synced.
WaitForCacheSync(timeout time.Duration) error

// Watch registers an event handler to receive events from
// all the informers managed by the driver.
Watch(cache.ResourceEventHandler) func()
Expand Down Expand Up @@ -217,6 +221,24 @@ func (o *objectDriver) InformOn(gvr schema.GroupVersionResource) error {
return nil
}

func (o *objectDriver) WaitForCacheSync(timeout time.Duration) error {
var synced []cache.InformerSynced

for _, i := range o.informerPool {
synced = append(synced, i.Informer().HasSynced)
}

stopChan := make(chan struct{})
timer := time.AfterFunc(timeout, func() { close(stopChan) })
defer timer.Stop()

if !cache.WaitForCacheSync(stopChan, synced...) {
return errors.New("informer cache sync timed out")
}

return nil
}

func (o *objectDriver) Apply(obj *unstructured.Unstructured) (*OperationResult, error) {
obj = obj.DeepCopy() // Copy in case we set the namespace.
gvk := obj.GetObjectKind().GroupVersionKind()
Expand Down
11 changes: 9 additions & 2 deletions pkg/test/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ func PreserveObjectsOpt() RunOpt {
})
}

// WatchResourceOpt disables automatic object deletion.
// WatchResourceOpt adds an explicit informer for the given resource.
func WatchResourceOpt(gvr schema.GroupVersionResource) RunOpt {
return RunOpt(func(tc *testContext) {
tc.watchedResources = append(tc.watchedResources, gvr)
})
}

// DryRunOpt enables Kuberentes dry-run mode (TODO).
// DryRunOpt enables Kubernetes dry-run mode (TODO).
func DryRunOpt() RunOpt {
return RunOpt(func(tc *testContext) {
tc.dryRun = true
Expand Down Expand Up @@ -188,6 +188,13 @@ func Run(testDoc *doc.Document, opts ...RunOpt) error {
tc.objectDriver.InformOn(gvr)
}

// Let the informers sync. For most environments, this
// timeout is far too long. Eventually we can make it a flag
// to tune it down.
if err := tc.objectDriver.WaitForCacheSync(5 * time.Minute); err != nil {
return err
}

if err := storeResourceVersions(tc.kubeDriver, tc.regoDriver); err != nil {
return err
}
Expand Down

0 comments on commit 3853a9b

Please sign in to comment.