Skip to content

Commit 005e576

Browse files
authored
Adds a secret skip preflight environment variable (#2364)
* Adds a secret skip preflight environment variable TL;DR ----- Supports the `SKIP_HOST_PREFLIGHTS` environment variable to skip the preflight checks. Thjis is not documented and not for general use. Details ------- Offers a "hidden" way to skip preflight checks by setting the environment variable `SKIP_HOST_PREFLIGHTS`. I'm adding the flag to support our Instruqt labs, since the Instruqt environment require a wildcard DNS entry that causes the host preflights to fail. I decided on this approach to avoid teaching any bad habits by telling the user to use `--ignore-host-preflights` when teaching them about the Embedded Cluster. I wanted instead to avoid the checks on their behalf without them realized it. This variable is not deliberately not documented to avoid similar encouragement of bad habits. * Reformats code * Restores missing brace * Moves to the Linux pre install per @sgalsaleh review * Checks specific values per @sgalsaleh review * Fixes failing test
1 parent f3a25db commit 005e576

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

cmd/installer/cli/install.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,10 @@ func preRunInstallCommon(cmd *cobra.Command, flags *InstallCmdFlags, rc runtimec
449449
}
450450

451451
func preRunInstallLinux(cmd *cobra.Command, flags *InstallCmdFlags, rc runtimeconfig.RuntimeConfig) error {
452+
if !cmd.Flags().Changed("skip-host-preflights") && (os.Getenv("SKIP_HOST_PREFLIGHTS") == "1" || os.Getenv("SKIP_HOST_PREFLIGHTS") == "true") {
453+
flags.skipHostPreflights = true
454+
}
455+
452456
if os.Getuid() != 0 {
453457
return fmt.Errorf("install command must be run as root")
454458
}

cmd/installer/cli/install_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414
"github.com/replicatedhq/embedded-cluster/pkg/prompts"
1515
"github.com/replicatedhq/embedded-cluster/pkg/prompts/plain"
1616
"github.com/replicatedhq/embedded-cluster/pkg/release"
17+
"github.com/replicatedhq/embedded-cluster/pkg/runtimeconfig"
1718
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
19+
"github.com/spf13/cobra"
1820
"github.com/stretchr/testify/assert"
1921
"github.com/stretchr/testify/require"
2022
)
@@ -607,3 +609,97 @@ func Test_verifyProxyConfig(t *testing.T) {
607609
})
608610
}
609611
}
612+
613+
func Test_preRunInstall_SkipHostPreflightsEnvVar(t *testing.T) {
614+
tests := []struct {
615+
name string
616+
envVarValue string
617+
flagValue *bool // nil means not set, true/false means explicitly set
618+
expectedSkipPreflights bool
619+
}{
620+
{
621+
name: "env var set to 1, no flag",
622+
envVarValue: "1",
623+
flagValue: nil,
624+
expectedSkipPreflights: true,
625+
},
626+
{
627+
name: "env var set to true, no flag",
628+
envVarValue: "true",
629+
flagValue: nil,
630+
expectedSkipPreflights: true,
631+
},
632+
{
633+
name: "env var set, flag explicitly false (flag takes precedence)",
634+
envVarValue: "1",
635+
flagValue: boolPtr(false),
636+
expectedSkipPreflights: false,
637+
},
638+
{
639+
name: "env var set, flag explicitly true",
640+
envVarValue: "1",
641+
flagValue: boolPtr(true),
642+
expectedSkipPreflights: true,
643+
},
644+
{
645+
name: "env var not set, no flag",
646+
envVarValue: "",
647+
flagValue: nil,
648+
expectedSkipPreflights: false,
649+
},
650+
{
651+
name: "env var not set, flag explicitly false",
652+
envVarValue: "",
653+
flagValue: boolPtr(false),
654+
expectedSkipPreflights: false,
655+
},
656+
{
657+
name: "env var not set, flag explicitly true",
658+
envVarValue: "",
659+
flagValue: boolPtr(true),
660+
expectedSkipPreflights: true,
661+
},
662+
}
663+
664+
for _, tt := range tests {
665+
t.Run(tt.name, func(t *testing.T) {
666+
// Set up environment variable
667+
if tt.envVarValue != "" {
668+
t.Setenv("SKIP_HOST_PREFLIGHTS", tt.envVarValue)
669+
}
670+
671+
// Create a mock cobra command to simulate flag behavior
672+
cmd := &cobra.Command{}
673+
flags := &InstallCmdFlags{}
674+
675+
// Add the flag to the command (similar to addInstallFlags)
676+
cmd.Flags().BoolVar(&flags.skipHostPreflights, "skip-host-preflights", false, "Skip host preflight checks")
677+
678+
// Set the flag if explicitly provided in test
679+
if tt.flagValue != nil {
680+
err := cmd.Flags().Set("skip-host-preflights", fmt.Sprintf("%t", *tt.flagValue))
681+
require.NoError(t, err)
682+
}
683+
684+
// Create a minimal runtime config for the test
685+
rc := runtimeconfig.New(nil)
686+
687+
// Call preRunInstall (this would normally require root, but we're just testing the flag logic)
688+
// We expect this to fail due to non-root execution, but we can check the flag value before it fails
689+
err := preRunInstallLinux(cmd, flags, rc)
690+
691+
// The function will fail due to non-root check, but we can verify the flag was set correctly
692+
// by checking the flag value before the root check fails
693+
assert.Equal(t, tt.expectedSkipPreflights, flags.skipHostPreflights)
694+
695+
// We expect an error due to non-root execution
696+
assert.Error(t, err)
697+
assert.Contains(t, err.Error(), "install command must be run as root")
698+
})
699+
}
700+
}
701+
702+
// Helper function to create bool pointer
703+
func boolPtr(b bool) *bool {
704+
return &b
705+
}

0 commit comments

Comments
 (0)