diff options
author | John Cai <jcai@gitlab.com> | 2021-11-09 23:44:07 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2021-11-10 07:08:08 +0300 |
commit | aa0b2cb03061e7a1f7f20e708ce876cec514b2ec (patch) | |
tree | 0176d30dfbc60031110e5ea5c2a71fd18c114364 | |
parent | 8d7e242576249154697fed7876cd9fe2a31ffdc3 (diff) |
praefect: allow check subcommand to be skippedjc-skip-startup-check
Since the startup checks can potentiall take a while, we want to allow
the option to skip the startup checks via an environment variable. This
way, the startup tooling such as omnibus can set this environment variable
when it needs to skip the startup healthchecks before starting Praefect.
Changelog: added
-rw-r--r-- | cmd/praefect/subcmd_check.go | 13 | ||||
-rw-r--r-- | cmd/praefect/subcmd_check_test.go | 27 |
2 files changed, 37 insertions, 3 deletions
diff --git a/cmd/praefect/subcmd_check.go b/cmd/praefect/subcmd_check.go index d84bef069..7d49ae50a 100644 --- a/cmd/praefect/subcmd_check.go +++ b/cmd/praefect/subcmd_check.go @@ -8,14 +8,18 @@ import ( "io" "time" + "gitlab.com/gitlab-org/gitaly/v14/internal/helper/env" "gitlab.com/gitlab-org/gitaly/v14/internal/praefect" "gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config" ) const ( - checkCmdName = "check" + skipChecksVar = "PRAEFECT_SKIP_STARTUP_CHECKS" + checkCmdName = "check" ) +var errFatalChecksFailed = errors.New("checks failed") + type checkSubcommand struct { w io.Writer checkFuncs []praefect.CheckFunc @@ -39,9 +43,12 @@ func (cmd *checkSubcommand) FlagSet() *flag.FlagSet { return fs } -var errFatalChecksFailed = errors.New("checks failed") - func (cmd *checkSubcommand) Exec(flags *flag.FlagSet, cfg config.Config) error { + if skipChecks, _ := env.GetBool(skipChecksVar, false); skipChecks { + fmt.Fprintf(cmd.w, "Skipping startup checks.\n") + return nil + } + var allChecks []*praefect.Check for _, checkFunc := range cmd.checkFuncs { allChecks = append(allChecks, checkFunc(cfg)) diff --git a/cmd/praefect/subcmd_check_test.go b/cmd/praefect/subcmd_check_test.go index ec9108a54..ddc01f8be 100644 --- a/cmd/praefect/subcmd_check_test.go +++ b/cmd/praefect/subcmd_check_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/gitaly/v14/internal/praefect" "gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config" + "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper" ) func TestCheckSubcommand_Exec(t *testing.T) { @@ -119,3 +120,29 @@ func TestCheckSubcommand_Exec(t *testing.T) { }) } } + +func TestCheckSubcommand_Skip(t *testing.T) { + var cfg config.Config + var stdout bytes.Buffer + + var functionRan bool + checks := []praefect.CheckFunc{ + func(cfg config.Config) *praefect.Check { + return &praefect.Check{ + Name: "check 1", + Run: func(ctx context.Context) error { + functionRan = true + return nil + }, + Severity: praefect.Fatal, + } + }, + } + checkCmd := checkSubcommand{w: &stdout, checkFuncs: checks} + cleanup := testhelper.ModifyEnvironment(t, skipChecksVar, "true") + defer cleanup() + + assert.Nil(t, checkCmd.Exec(flag.NewFlagSet("", flag.PanicOnError), cfg)) + assert.False(t, functionRan) + assert.Equal(t, "Skipping startup checks.\n", stdout.String()) +} |