Skip to content

Commit

Permalink
preflight: Add filter API
Browse files Browse the repository at this point in the history
Currently, generating the list of preflight checks used by
CleanUpHost/SetupHost and StartPreflightChecks is quite convoluted. We
have multiple Check[] arrays, which are picked or not depending on
various parameters (distro, network mode, experimental, ...).

When adding a new check, one needs to take all of this into account
before their check is used at the expected time.

This patch introduces a new API which uses the labels added to  each
individual Check definition.
getAllPreflightChecks/getPreflightChecks will then be modified to make
use of it, and automatically select the tests which should be used
depending on the labels which are set.
  • Loading branch information
cfergeau authored and praveenkumar committed Jun 3, 2021
1 parent 1ef71c9 commit 668a0cb
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pkg/crc/preflight/labels.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package preflight

import (
"runtime"

"github.com/code-ready/crc/pkg/crc/network"
)

type LabelName uint32

const (
Expand Down Expand Up @@ -31,3 +37,57 @@ var (
)

type labels map[LabelName]LabelValue

type preflightFilter map[LabelName]LabelValue

func newFilter() preflightFilter {
switch runtime.GOOS {
case "darwin":
return preflightFilter{Os: Darwin}
case "linux":
return preflightFilter{Os: Linux}
case "windows":
return preflightFilter{Os: Windows}
}
// Should not happen
return preflightFilter{Os: Linux}
}

func (filter preflightFilter) SetNetworkMode(networkMode network.Mode) {
switch networkMode {
case network.SystemNetworkingMode:
filter[NetworkMode] = System
case network.UserNetworkingMode:
filter[NetworkMode] = User
}
}

/* This will iterate over 'checks' and only keep the checks which match the filter:
* - if a key is present in the filter and not in the check labels, the check is kept
* - if a key is present in the check labels, but not in the filter, the check is kept
* - if a key is present both in the filter and in the check labels, the check
* is kept only if they have the same value, it is dropped if their values differ
*/
func (filter preflightFilter) Apply(checks []Check) []Check {
var filteredChecks []Check

for _, check := range checks {
if !skipCheck(check, filter) {
filteredChecks = append(filteredChecks, check)
}

}

return filteredChecks
}

func skipCheck(check Check, filter preflightFilter) bool {
for filterKey, filterValue := range filter {
checkValue, present := check.labels[filterKey]
if present && checkValue != filterValue {
return true
}
}

return false
}
8 changes: 8 additions & 0 deletions pkg/crc/preflight/preflight_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ const (
Disabled
)

func (filter preflightFilter) SetTray(enable bool) {
if version.IsMacosInstallPathSet() && enable {
filter[Tray] = Enabled
} else {
filter[Tray] = Disabled
}
}

// We want all preflight checks including
// - experimental checks
// - tray checks when using an installer, regardless of tray enabled or not
Expand Down
16 changes: 16 additions & 0 deletions pkg/crc/preflight/preflight_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,22 @@ const (
SystemdResolved
)

func (filter preflightFilter) SetSystemdResolved(usingSystemdResolved bool) {
if usingSystemdResolved {
filter[DNS] = SystemdResolved
} else {
filter[DNS] = Dnsmasq
}
}

func (filter preflightFilter) SetDistro(distro *linux.OsRelease) {
if distroIsLike(distro, linux.Ubuntu) {
filter[Distro] = UbuntuLike
} else {
filter[Distro] = Other
}
}

// We want all preflight checks
// - matching the current distro
// - matching the networking daemon in use (NetworkManager or systemd-resolved) regardless of user/system networking
Expand Down
8 changes: 8 additions & 0 deletions pkg/crc/preflight/preflight_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ const (
Disabled
)

func (filter preflightFilter) SetTray(enable bool) {
if version.IsMsiBuild() && enable {
filter[Tray] = Enabled
} else {
filter[Tray] = Disabled
}
}

// We want all preflight checks including
// - experimental checks
// - tray checks when using an installer, regardless of tray enabled or not
Expand Down

0 comments on commit 668a0cb

Please sign in to comment.