Skip to content

Commit

Permalink
feat: Add status handler
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <gaoce@caicloud.io>
  • Loading branch information
gaocegege committed May 17, 2019
1 parent 5b74ade commit 0b05e64
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
8 changes: 6 additions & 2 deletions pkg/controller/v1alpha2/trial/trial_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ func Add(mgr manager.Manager) error {

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
return &ReconcileTrial{
r := &ReconcileTrial{
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
ManagerClient: managerclient.New(),
}
r.updateStatusHandler = r.updateStatus
return r
}

// add adds a new Controller to mgr with r as the reconcile.Reconciler
Expand Down Expand Up @@ -116,6 +118,8 @@ type ReconcileTrial struct {
scheme *runtime.Scheme

managerclient.ManagerClient
// updateStatusHandler is defined for test purpose.
updateStatusHandler updateStatusFunc
}

// Reconcile reads that state of the cluster for a Trial object and makes changes based on the state read
Expand Down Expand Up @@ -177,7 +181,7 @@ func (r *ReconcileTrial) Reconcile(request reconcile.Request) (reconcile.Result,
logger.Error(err, "Update trial status in DB error")
return reconcile.Result{}, err
}
err = r.Status().Update(context.TODO(), instance)
err = r.updateStatusHandler(instance)
if err != nil {
logger.Error(err, "Update trial instance status error")
return reconcile.Result{}, err
Expand Down
17 changes: 17 additions & 0 deletions pkg/controller/v1alpha2/trial/trial_controller_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package trial

import (
"context"

trialsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/trial/v1alpha2"
)

type updateStatusFunc func(instance *trialsv1alpha2.Trial) error

func (r *ReconcileTrial) updateStatus(instance *trialsv1alpha2.Trial) error {
err := r.Status().Update(context.TODO(), instance)
if err != nil {
return err
}
return nil
}
4 changes: 3 additions & 1 deletion pkg/controller/v1alpha2/trial/trial_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func TestCreateTFJobTrial(t *testing.T) {
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
ManagerClient: mc,
updateStatusHandler: func(instance *trialsv1alpha2.Trial) error {
return nil
},
})
g.Expect(add(mgr, recFn)).NotTo(gomega.HaveOccurred())

Expand Down Expand Up @@ -84,7 +87,6 @@ func TestCreateTFJobTrial(t *testing.T) {
if err := k8syaml.NewYAMLOrJSONDecoder(buf, bufSize).Decode(tfJob); err != nil {
t.Errorf("Expected nil, got %v", err)
}
println(tfJob.GetName())
g.Eventually(func() error { return c.Get(context.TODO(), tfJobKey, tfJob) }, timeout).
Should(gomega.Succeed())

Expand Down

0 comments on commit 0b05e64

Please sign in to comment.