Skip to content

Commit

Permalink
feat(cli): add timeout check for demo cmd (#667)
Browse files Browse the repository at this point in the history
* feat(cli): add timeout check for `demo` cmd

* move timeout duration to flags

* update

* add timeout flag and shorthand
  • Loading branch information
lizzy-0323 authored Dec 16, 2024
1 parent f3bb57f commit a82d783
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
9 changes: 8 additions & 1 deletion internal/cli/cluster/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const (
FLAG_SYSTEM_MEMORY = "system_memory"

// Flag for demo cluster
FLAG_WAIT = "wait"
FLAG_WAIT = "wait"
FLAG_TIMEOUT = "timeout"
)

// Default values for cluster management
Expand Down Expand Up @@ -88,6 +89,8 @@ const (

// Default values for wait flag
DEFAULT_WAIT = false
// Default timeout
DEFAULT_TIMEOUT = 30
)

// Default cluster type for easier cluster creation
Expand All @@ -107,4 +110,8 @@ const (
SHORTHAND_ZONES = "z"
SHORTHAND_NAMESPACE = "n"
SHORTHAND_PASSWD = "p"

// Shorthand for demo cluster creation
SHORTHAND_WAIT = "w"
SHORTHAND_TIMEOUT = "t"
)
73 changes: 47 additions & 26 deletions internal/cli/cmd/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ func NewCmd() *cobra.Command {
tenantOptions := tenant.NewCreateOptions()
logger := utils.GetDefaultLoggerInstance()
pf := demo.NewPromptFactory()
clusterTickerDuration := 2 * time.Second
tenantTickerDuration := 1 * time.Second
var clusterType string
var timeoutDuration time.Duration
var wait bool
var err error
var prompt any
Expand Down Expand Up @@ -105,7 +108,7 @@ func NewCmd() *cobra.Command {
logger.Fatalln(err)
}
logger.Printf("Creating OBCluster instance: %s", clusterOptions.ClusterName)
waitForClusterReady(cmd.Context(), obcluster, logger, 2*time.Second)
waitForClusterReady(cmd.Context(), obcluster, logger, timeoutDuration, clusterTickerDuration)
logger.Printf("Run `echo $(kubectl get secret %s -n %s -o jsonpath='{.data.password}'|base64 --decode)` to get cluster secrets", obcluster.Spec.UserSecrets.Root, obcluster.Namespace)
},
PostRun: func(cmd *cobra.Command, args []string) {
Expand All @@ -115,53 +118,71 @@ func NewCmd() *cobra.Command {
logger.Fatalln(err)
}
logger.Printf("Creating OBTenant instance: %s", tenantOptions.TenantName)
waitForTenantReady(cmd.Context(), obtenant, logger, 1*time.Second)
waitForTenantReady(cmd.Context(), obtenant, logger, timeoutDuration, tenantTickerDuration)
logger.Printf("Run `echo $(kubectl get secret %s -n %s -o jsonpath='{.data.password}'|base64 --decode)` to get tenant secrets", obtenant.Spec.Credentials.Root, obtenant.Namespace)
},
}
// TODO: if w is set, wait for the cluster and tenant ready
cmd.Flags().BoolVarP(&wait, cluster.FLAG_WAIT, "w", cluster.DEFAULT_WAIT, "wait for the cluster and tenant ready")
// TODO: if w is set, wait for cluster and tenant ready, not implemented yet
cmd.Flags().BoolVarP(&wait, cluster.FLAG_WAIT, cluster.SHORTHAND_WAIT, cluster.DEFAULT_WAIT, "wait for the cluster and tenant ready")
cmd.Flags().DurationVarP(&timeoutDuration, cluster.FLAG_TIMEOUT, cluster.SHORTHAND_TIMEOUT, cluster.DEFAULT_TIMEOUT*time.Minute, "timeout duration for waiting for the cluster and tenant ready")
return cmd
}

// waitForTenantReady wait for tenant ready, log the task status
func waitForClusterReady(ctx context.Context, obcluster *v1alpha1.OBCluster, logger *log.Logger, waitTime time.Duration) {
func waitForClusterReady(ctx context.Context, obcluster *v1alpha1.OBCluster, logger *log.Logger, timeoutDuration time.Duration, tickerDuration time.Duration) {
var err error
lastTask := ""
lastTaskStatus := ""
timeout := time.NewTimer(timeoutDuration)
ticker := time.NewTicker(tickerDuration)
defer ticker.Stop()
defer timeout.Stop()
logger.Println("Waiting for cluster ready...")
for obcluster.Status.Status != clusterstatus.Running {
time.Sleep(waitTime)
obcluster, err = clients.GetOBCluster(ctx, obcluster.Namespace, obcluster.Name)
if err != nil {
logger.Fatalln(err)
}
if obcluster.Status.OperationContext != nil && (lastTask != string(obcluster.Status.OperationContext.Task) || lastTaskStatus != string(obcluster.Status.OperationContext.TaskStatus)) {
logger.Printf("Task: %s, Status: %s", obcluster.Status.OperationContext.Task, obcluster.Status.OperationContext.TaskStatus)
lastTask = string(obcluster.Status.OperationContext.Task)
lastTaskStatus = string(obcluster.Status.OperationContext.TaskStatus)
select {
case <-ticker.C:
obcluster, err = clients.GetOBCluster(ctx, obcluster.Namespace, obcluster.Name)
if err != nil {
logger.Fatalln(err)
}
if obcluster.Status.OperationContext != nil && (lastTask != string(obcluster.Status.OperationContext.Task) || lastTaskStatus != string(obcluster.Status.OperationContext.TaskStatus)) {
logger.Printf("Task: %s, Status: %s", obcluster.Status.OperationContext.Task, obcluster.Status.OperationContext.TaskStatus)
lastTask = string(obcluster.Status.OperationContext.Task)
lastTaskStatus = string(obcluster.Status.OperationContext.TaskStatus)
timeout.Reset(timeoutDuration)
}
case <-timeout.C:
logger.Fatalf("Task: %s timeout", lastTask)
}
}
logger.Println("Cluster create successfully")
logger.Println("Create Cluster successfully")
}

// waitForTenantReady wait for tenant ready, log the task status
func waitForTenantReady(ctx context.Context, obtenant *v1alpha1.OBTenant, logger *log.Logger, waitTime time.Duration) {
func waitForTenantReady(ctx context.Context, obtenant *v1alpha1.OBTenant, logger *log.Logger, timeoutDuration time.Duration, tickerDuration time.Duration) {
var err error
lastTask := ""
lastTaskStatus := ""
timeout := time.NewTimer(timeoutDuration)
ticker := time.NewTicker(tickerDuration)
defer ticker.Stop()
defer timeout.Stop()
logger.Println("Waiting for tenant ready...")
for obtenant.Status.Status != tenantstatus.Running {
time.Sleep(waitTime)
obtenant, err = clients.GetOBTenant(ctx, types.NamespacedName{Namespace: obtenant.Namespace, Name: obtenant.Name})
if err != nil {
logger.Fatalln(err)
}
if obtenant.Status.OperationContext != nil && (lastTask != string(obtenant.Status.OperationContext.Task) || lastTaskStatus != string(obtenant.Status.OperationContext.TaskStatus)) {
logger.Printf("Task: %s, Status: %s", obtenant.Status.OperationContext.Task, obtenant.Status.OperationContext.TaskStatus)
lastTask = string(obtenant.Status.OperationContext.Task)
lastTaskStatus = string(obtenant.Status.OperationContext.TaskStatus)
select {
case <-ticker.C:
obtenant, err = clients.GetOBTenant(ctx, types.NamespacedName{Namespace: obtenant.Namespace, Name: obtenant.Name})
if err != nil {
logger.Fatalln(err)
}
if obtenant.Status.OperationContext != nil && (lastTask != string(obtenant.Status.OperationContext.Task) || lastTaskStatus != string(obtenant.Status.OperationContext.TaskStatus)) {
lastTask = string(obtenant.Status.OperationContext.Task)
lastTaskStatus = string(obtenant.Status.OperationContext.TaskStatus)
timeout.Reset(timeoutDuration)
}
case <-timeout.C:
logger.Fatalf("Task: %s timeout", lastTask)
}
}
logger.Println("Tenant create successfully")
logger.Println("Create Tenant successfully")
}

0 comments on commit a82d783

Please sign in to comment.