github.com/openshift/installer@v1.4.17/pkg/utils/baremetal/cache.go (about)

     1  package baremetal
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/sirupsen/logrus"
     8  	apierrors "k8s.io/apimachinery/pkg/api/errors"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    11  	"k8s.io/apimachinery/pkg/runtime"
    12  	"k8s.io/apimachinery/pkg/watch"
    13  	"k8s.io/client-go/dynamic"
    14  )
    15  
    16  // BmhCacheListerWatcher is an object that wraps the listing and wrapping
    17  // functionality for baremetal host resources.
    18  type BmhCacheListerWatcher struct {
    19  	Resource   dynamic.ResourceInterface
    20  	RetryWatch bool
    21  }
    22  
    23  // List returns a list of baremetal hosts as dynamic objects.
    24  func (bc BmhCacheListerWatcher) List(options metav1.ListOptions) (runtime.Object, error) {
    25  	list, err := bc.Resource.List(context.TODO(), options)
    26  	if apierrors.IsNotFound(err) {
    27  		logrus.Debug("    baremetalhost resource not yet available, will retry")
    28  		return &unstructured.UnstructuredList{}, nil
    29  	}
    30  
    31  	return list, err
    32  }
    33  
    34  // Watch starts a watch over baremetal hosts.
    35  func (bc BmhCacheListerWatcher) Watch(options metav1.ListOptions) (watch.Interface, error) {
    36  	w, err := bc.Resource.Watch(context.TODO(), options)
    37  	if apierrors.IsNotFound(err) && bc.RetryWatch {
    38  		logrus.Debug("    baremetalhost resource not yet available, will retry")
    39  		// When the Resource isn't installed yet, we can encourage the caller to keep
    40  		// retrying by supplying an empty watcher.  In the case of
    41  		// UntilWithSync, the caller also checks how long it takes to create the
    42  		// watch.  To avoid errors, we introduce an artificial delay of one
    43  		// second.
    44  		w := watch.NewEmptyWatch()
    45  		time.Sleep(time.Second)
    46  		return w, nil
    47  	}
    48  	return w, err
    49  }