Skip to content

Commit

Permalink
Write data restore completion event using dynamic client (#601)
Browse files Browse the repository at this point in the history
Signed-off-by: Emruz Hossain <emruz@appscode.com>
  • Loading branch information
Emruz Hossain committed Oct 1, 2020
1 parent 60ada14 commit f27cb72
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pkg/controller/initializer/stash/restorebatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ func (c Controller) processRestoreBatch(key string) error {
// Note that you also have to check the uid if you have a local controlled resource, which
// is dependent on the actual instance, to detect that a Job was recreated with the same name
rb := obj.(*v1beta1.RestoreBatch).DeepCopy()
rb.GetObjectKind().SetGroupVersionKind(v1beta1.SchemeGroupVersion.WithKind(v1beta1.ResourceKindRestoreBatch))
ri, err := c.extractRestoreInfo(rb)
if err != nil {
log.Errorln("failed to extract restore invoker info. Reason: ", err)
return err
}
return c.setInitializationCondition(ri)
return c.setRestoreCompletionCondition(ri)
}
return nil
}
3 changes: 2 additions & 1 deletion pkg/controller/initializer/stash/restoresession.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ func (c *Controller) processRestoreSession(key string) error {
// Note that you also have to check the uid if you have a local controlled resource, which
// is dependent on the actual instance, to detect that a Job was recreated with the same name
rs := obj.(*v1beta1.RestoreSession).DeepCopy()
rs.GetObjectKind().SetGroupVersionKind(v1beta1.SchemeGroupVersion.WithKind(v1beta1.ResourceKindRestoreSession))
ri, err := c.extractRestoreInfo(rs)
if err != nil {
log.Errorln("failed to extract restore invoker info. Reason: ", err)
return err
}
return c.setInitializationCondition(ri)
return c.setRestoreCompletionCondition(ri)
}
return nil
}
36 changes: 32 additions & 4 deletions pkg/controller/initializer/stash/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"kubedb.dev/apimachinery/apis/kubedb"
api "kubedb.dev/apimachinery/apis/kubedb/v1alpha1"
"kubedb.dev/apimachinery/client/clientset/versioned/scheme"

"github.com/appscode/go/log"
"github.com/appscode/go/types"
Expand All @@ -32,6 +33,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/reference"
kmapi "kmodules.xyz/client-go/api/v1"
core_util "kmodules.xyz/client-go/core/v1"
"kmodules.xyz/client-go/discovery"
Expand Down Expand Up @@ -92,7 +94,7 @@ func (c *Controller) extractRestoreInfo(invoker interface{}) (*restoreInfo, erro
return ri, nil
}

func (c *Controller) setInitializationCondition(ri *restoreInfo) error {
func (c *Controller) setRestoreCompletionCondition(ri *restoreInfo) error {
if ri == nil {
return fmt.Errorf("invalid restore information. it must not be nil")
}
Expand All @@ -112,15 +114,15 @@ func (c *Controller) setInitializationCondition(ri *restoreInfo) error {
if ri.phase == v1beta1.RestoreSucceeded {
dbCond.Status = kmapi.ConditionTrue
dbCond.Reason = api.DatabaseSuccessfullyInitialized
dbCond.Message = fmt.Sprintf("Database has been initialized successfully by %s %s/%s",
dbCond.Message = fmt.Sprintf("Successfully restored data by initializer %s %s/%s",
ri.invoker.Kind,
ri.do.Namespace,
ri.invoker.Name,
)
} else {
dbCond.Status = kmapi.ConditionFalse
dbCond.Reason = api.FailedToInitializeDatabase
dbCond.Message = fmt.Sprintf("Database failed to be initialized successfully by %s %s/%s."+
dbCond.Message = fmt.Sprintf("Failed to restore data by initializer %s %s/%s."+
"\nRun 'kubectl describe %s %s -n %s' for more details.",
ri.invoker.Kind,
ri.do.Namespace,
Expand All @@ -132,7 +134,12 @@ func (c *Controller) setInitializationCondition(ri *restoreInfo) error {
}

// Add "DatabaseInitialized" dmcond to the respective database CR
return ri.do.SetCondition(dbCond)
err := ri.do.SetCondition(dbCond)
if err != nil {
return err
}
// Write data restore completion event to the respective database CR
return c.writeRestoreCompletionEvent(ri.do, dbCond)
}

func (c *Controller) identifyTarget(members []v1beta1.RestoreTargetSpec, namespace string) (*v1beta1.RestoreTarget, error) {
Expand Down Expand Up @@ -253,3 +260,24 @@ func targetOfGroupKind(target v1beta1.TargetRef, group, kind string) (bool, erro
}
return gv.Group == group && target.Kind == kind, nil
}

func (c *Controller) writeRestoreCompletionEvent(do dmcond.DynamicOptions, cond kmapi.Condition) error {
// Get the database CR
resp, err := do.Client.Resource(do.GVR).Namespace(do.Namespace).Get(context.TODO(), do.Name, metav1.GetOptions{})
if err != nil {
return err
}
// Create database CR's reference
ref, err := reference.GetReference(scheme.Scheme, resp)
if err != nil {
return err
}

eventType := core.EventTypeNormal
if cond.Status != kmapi.ConditionTrue {
eventType = core.EventTypeWarning
}
// create event
c.Recorder.Eventf(ref, eventType, cond.Reason, cond.Message)
return nil
}

0 comments on commit f27cb72

Please sign in to comment.