Skip to content

Commit

Permalink
Autorun postflight checks (#429)
Browse files Browse the repository at this point in the history
* postflight checks triggerred on qliksense install, updated docs, added a postflight all command
  • Loading branch information
ashwathishiva authored Jun 18, 2020
1 parent 6899a7b commit b6ae0c9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 5 deletions.
9 changes: 7 additions & 2 deletions cmd/qliksense/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ func installCmd(q *qliksense.Qliksense) *cobra.Command {
}

if filePath != "" {
return apply(q, cmd, opts)
if err := apply(q, cmd, opts); err != nil {
return err
}
} else {
return q.InstallQK8s(version, opts)
if err1 := q.InstallQK8s(version, opts); err1 != nil {
return err1
}
}
return AllPostflightChecks(q).Execute()
},
}

Expand Down
38 changes: 37 additions & 1 deletion cmd/qliksense/postflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func postflightCmd(q *qliksense.Qliksense) *cobra.Command {
return postflightCmd
}

func pfMigrationCheck(q *qliksense.Qliksense) *cobra.Command {
func postflightMigrationCheck(q *qliksense.Qliksense) *cobra.Command {
out := ansi.NewColorableStdout()
postflightOpts := &postflight.PostflightOptions{}
var postflightMigrationCmd = &cobra.Command{
Expand Down Expand Up @@ -58,3 +58,39 @@ func pfMigrationCheck(q *qliksense.Qliksense) *cobra.Command {
f.BoolVarP(&postflightOpts.Verbose, "verbose", "v", false, "verbose mode")
return postflightMigrationCmd
}

func AllPostflightChecks(q *qliksense.Qliksense) *cobra.Command {
out := ansi.NewColorableStdout()
postflightOpts := &postflight.PostflightOptions{}
var postflightAllChecksCmd = &cobra.Command{
Use: "all",
Short: "perform all checks",
Long: `perform all postflight checks`,
Example: `qliksense postflight all`,
RunE: func(cmd *cobra.Command, args []string) error {
pf := &postflight.QliksensePostflight{Q: q, P: postflightOpts, CG: &api.ClientGoUtils{Verbose: postflightOpts.Verbose}}

// run all postflight checks
fmt.Printf("Running all postflight checks...\n\n")
namespace, kubeConfigContents, err := pf.CG.LoadKubeConfigAndNamespace()
if err != nil {
fmt.Fprintf(out, "%s\n", Red("Unable to run all postflight checks"))
fmt.Printf("Error: %v\n", err)
return nil
}
if namespace == "" {
namespace = "default"
}
if err = pf.RunAllPostflightChecks(namespace, kubeConfigContents, postflightOpts); err != nil {
fmt.Fprintf(out, "%s\n", Red("1 or more preflight checks have FAILED"))
fmt.Printf("Completed running all postflight checks")
return nil
}
fmt.Fprintf(out, "%s\n", Green("All postflight checks have PASSED"))
return nil
},
}
f := postflightAllChecksCmd.Flags()
f.BoolVarP(&postflightOpts.Verbose, "verbose", "v", false, "verbose mode")
return postflightAllChecksCmd
}
3 changes: 2 additions & 1 deletion cmd/qliksense/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ func rootCmd(p *qliksense.Qliksense) *cobra.Command {

// add postflight command
postflightCmd := postflightCmd(p)
postflightCmd.AddCommand(pfMigrationCheck(p))
postflightCmd.AddCommand(postflightMigrationCheck(p))
postflightCmd.AddCommand(AllPostflightChecks(p))

cmd.AddCommand(postflightCmd)
return cmd
Expand Down
18 changes: 18 additions & 0 deletions docs/postflight_checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ Flags:
-v, --verbose verbose mode
```

### Run all postflight checks
This command runs all the postflight checks available.

```shell
$ qliksense postflight all
Running all postflight checks...

Postflight db migration check...
Logs from pod: qliksense-users-6977cb7788-qlgmv
{"caller":"main.go:39","environment":"qseok","error":"error parsing uri: scheme must be \"mongodb\" or \"mongodb+srv\"","level":"error","message":"failed to connect to ","timestamp":"2020-06-17T04:10:11.7891913Z","version":""}
To view more logs in this context, please run the command: kubectl logs -n test_ns qliksense-users-6977cb7788-qlgmv migration
PASSED

All postflight checks have PASSED
```

### DB migration check
This command checks init containers for successful database migrarion completions, and reports failure, if any to the user.

Expand All @@ -29,5 +45,7 @@ An example run of this check produces an output as shown below:
$ qliksense postflight db-migration-check
Logs from pod: qliksense-users-6977cb7788-cxxwh
{"caller":"main.go:39","environment":"qseok","error":"error parsing uri: scheme must be \"mongodb\" or \"mongodb+srv\"","level":"error","message":"failed to connect to ","timestamp":"2020-06-01T01:07:18.4170507Z","version":""}
To view more logs in this context, please run the command: kubectl logs -n test_ns qliksense-users-6977cb7788-qlgmv migration
PASSED
Postflight db_migration_check completed
```
31 changes: 31 additions & 0 deletions pkg/postflight/all_postflight_checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package postflight

import (
"fmt"

. "github.com/logrusorgru/aurora"
ansi "github.com/mattn/go-colorable"
"github.com/pkg/errors"
)

func (qp *QliksensePostflight) RunAllPostflightChecks(namespace string, kubeConfigContents []byte, preflightOpts *PostflightOptions) error {
checkCount := 0
totalCount := 0

out := ansi.NewColorableStdout()
// Postflight db migration check
if err := qp.DbMigrationCheck(namespace, kubeConfigContents); err != nil {
fmt.Fprintf(out, "%s\n", Red("FAILED"))
fmt.Printf("Error: %v\n\n", err)
} else {
fmt.Fprintf(out, "%s\n\n", Green("PASSED"))
checkCount++
}
totalCount++

if checkCount == totalCount {
// All postflight checks were successful
return nil
}
return errors.New("1 or more postflight checks have FAILED")
}
3 changes: 2 additions & 1 deletion pkg/postflight/db_migration_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
const initContainerNameToCheck = "migration"

func (p *QliksensePostflight) DbMigrationCheck(namespace string, kubeConfigContents []byte) error {

fmt.Printf("Postflight db migration check... \n")
p.CG.LogVerboseMessage("\n----------------------------------- \n")
clientset, _, err := p.CG.GetK8SClientSet(kubeConfigContents, "")
if err != nil {
err = fmt.Errorf("unable to create a kubernetes client: %v", err)
Expand Down

0 comments on commit b6ae0c9

Please sign in to comment.