Skip to content

Commit

Permalink
Add tests for new operator-sdk related code (#122)
Browse files Browse the repository at this point in the history
* Add tests for new operator-sdk related code

Signed-off-by: Juraci Paixão Kröhling <[email protected]>

* Clarified TestNewJaegerInstance, removed extra 'handled' flag

Signed-off-by: Juraci Paixão Kröhling <[email protected]>
  • Loading branch information
jpkrohling authored Nov 22, 2018
1 parent 5fbff10 commit 96ccc24
Show file tree
Hide file tree
Showing 8 changed files with 1,969 additions and 11 deletions.
8 changes: 6 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 21 additions & 9 deletions pkg/controller/jaeger/jaeger_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ var _ reconcile.Reconciler = &ReconcileJaeger{}
type ReconcileJaeger struct {
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver
client client.Client
scheme *runtime.Scheme
client client.Client
scheme *runtime.Scheme
strategyChooser func(*v1alpha1.Jaeger) Controller
}

// Reconcile reads that state of the cluster for a Jaeger object and makes changes based on the state read
Expand Down Expand Up @@ -91,7 +92,7 @@ func (r *ReconcileJaeger) Reconcile(request reconcile.Request) (reconcile.Result
instance.APIVersion = fmt.Sprintf("%s/%s", v1alpha1.SchemeGroupVersion.Group, v1alpha1.SchemeGroupVersion.Version)
instance.Kind = "Jaeger"

ctrl := NewController(context.Background(), instance)
ctrl := r.runStrategyChooser(instance)

// wait for all the dependencies to succeed
if err := r.handleDependencies(ctrl); err != nil {
Expand All @@ -114,20 +115,32 @@ func (r *ReconcileJaeger) Reconcile(request reconcile.Request) (reconcile.Result

// we store back the changed CR, so that what is stored reflects what is being used
if err := r.client.Update(context.Background(), instance); err != nil {
log.Errorf("failed to update %v", instance)
log.WithError(err).Error("failed to update")
return reconcile.Result{}, err
}

return reconcile.Result{}, nil
}

func (r *ReconcileJaeger) runStrategyChooser(instance *v1alpha1.Jaeger) Controller {
if nil == r.strategyChooser {
return defaultStrategyChooser(instance)
}

return r.strategyChooser(instance)
}

func defaultStrategyChooser(instance *v1alpha1.Jaeger) Controller {
return NewController(context.Background(), instance)
}

func (r *ReconcileJaeger) handleCreate(ctrl Controller) (bool, error) {
objs := ctrl.Create()
created := false
for _, obj := range objs {
err := r.client.Create(context.Background(), obj)
if err != nil && !apierrors.IsAlreadyExists(err) {
log.Errorf("failed to create %v", obj)
log.WithError(err).Error("failed to create")
return false, err
}

Expand All @@ -142,9 +155,8 @@ func (r *ReconcileJaeger) handleCreate(ctrl Controller) (bool, error) {
func (r *ReconcileJaeger) handleUpdate(ctrl Controller) error {
objs := ctrl.Update()
for _, obj := range objs {
log.Debugf("Updating %v", obj)
if err := r.client.Update(context.Background(), obj); err != nil {
log.Errorf("failed to update %v", obj)
log.WithError(err).Error("failed to update")
return err
}
}
Expand All @@ -156,7 +168,7 @@ func (r *ReconcileJaeger) handleDependencies(ctrl Controller) error {
for _, dep := range ctrl.Dependencies() {
err := r.client.Create(context.Background(), &dep)
if err != nil && !apierrors.IsAlreadyExists(err) {
log.Errorf("failed to create %v", dep.Name)
log.WithError(err).Error("failed to create")
return err
}

Expand All @@ -166,7 +178,7 @@ func (r *ReconcileJaeger) handleDependencies(ctrl Controller) error {
batch := &batchv1.Job{}
err = r.client.Get(context.Background(), types.NamespacedName{Name: dep.Name, Namespace: dep.Namespace}, batch)
if err != nil {
log.Errorf("failed to get the status of the dependency %v", dep.Name)
log.WithField("dependency", dep.Name).WithError(err).Error("failed to get the status of the dependency")
return false, err
}

Expand Down
Loading

0 comments on commit 96ccc24

Please sign in to comment.