Skip to content

Commit

Permalink
addons: Fail when enabling KVM addons on non-KVM cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
spowelljr authored and medyagh committed Jul 9, 2024
1 parent 177f4f2 commit cb892bc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
18 changes: 10 additions & 8 deletions pkg/addons/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var Addons = []*Addon{
{
name: "gvisor",
set: SetBool,
validations: []setFn{IsRuntimeContainerd},
validations: []setFn{isRuntimeContainerd},
callbacks: []setFn{EnableOrDisableAddon, verifyAddonStatus},
},
{
Expand Down Expand Up @@ -125,14 +125,16 @@ var Addons = []*Addon{
callbacks: []setFn{EnableOrDisableAddon, verifyAddonStatus},
},
{
name: "nvidia-driver-installer",
set: SetBool,
callbacks: []setFn{EnableOrDisableAddon},
name: "nvidia-driver-installer",
set: SetBool,
validations: []setFn{isKVMDriverForNVIDIA},
callbacks: []setFn{EnableOrDisableAddon},
},
{
name: "nvidia-gpu-device-plugin",
set: SetBool,
callbacks: []setFn{EnableOrDisableAddon},
name: "nvidia-gpu-device-plugin",
set: SetBool,
validations: []setFn{isKVMDriverForNVIDIA},
callbacks: []setFn{EnableOrDisableAddon},
},
{
name: "olm",
Expand Down Expand Up @@ -204,7 +206,7 @@ var Addons = []*Addon{
{
name: "csi-hostpath-driver",
set: SetBool,
validations: []setFn{IsVolumesnapshotsEnabled},
validations: []setFn{isVolumesnapshotsEnabled},
callbacks: []setFn{EnableOrDisableAddon, verifyAddonStatus},
},
{
Expand Down
17 changes: 13 additions & 4 deletions pkg/addons/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/out"
)

Expand All @@ -45,8 +46,7 @@ const volumesnapshotsDisabledMsg = `[WARNING] For full functionality, the 'csi-h
You can enable 'volumesnapshots' addon by running: 'minikube addons enable volumesnapshots'
`

// IsRuntimeContainerd is a validator which returns an error if the current runtime is not containerd
func IsRuntimeContainerd(cc *config.ClusterConfig, _, _ string) error {
func isRuntimeContainerd(cc *config.ClusterConfig, _, _ string) error {
r, err := cruntime.New(cruntime.Config{Type: cc.KubernetesConfig.ContainerRuntime})
if err != nil {
return err
Expand All @@ -58,9 +58,9 @@ func IsRuntimeContainerd(cc *config.ClusterConfig, _, _ string) error {
return nil
}

// IsVolumesnapshotsEnabled is a validator that prints out a warning if the volumesnapshots addon
// isVolumesnapshotsEnabled is a validator that prints out a warning if the volumesnapshots addon
// is disabled (does not return any errors!)
func IsVolumesnapshotsEnabled(cc *config.ClusterConfig, _, value string) error {
func isVolumesnapshotsEnabled(cc *config.ClusterConfig, _, value string) error {
isCsiDriverEnabled, _ := strconv.ParseBool(value)
// assets.Addons[].IsEnabled() returns the current status of the addon or default value.
// config.AddonList contains list of addons to be enabled.
Expand All @@ -74,6 +74,15 @@ func IsVolumesnapshotsEnabled(cc *config.ClusterConfig, _, value string) error {
return nil
}

func isKVMDriverForNVIDIA(cc *config.ClusterConfig, name, _ string) error {
if driver.IsKVM(cc.Driver) {
return nil
}
out.Ln("")
out.FailureT("The {{.addon}} addon is only supported with the KVM driver.\n\nFor GPU setup instructions see: https://minikube.sigs.k8s.io/docs/tutorials/nvidia/", out.V{"addon": name})
return fmt.Errorf("%s addon is only supported with the KVM driver", name)
}

// isAddonValid returns the addon, true if it is valid
// otherwise returns nil, false
func isAddonValid(name string) (*Addon, bool) {
Expand Down
28 changes: 27 additions & 1 deletion pkg/addons/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ limitations under the License.

package addons

import "testing"
import (
"testing"

"k8s.io/minikube/pkg/minikube/config"
)

func TestIsAddonValid(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -76,3 +80,25 @@ func TestContains(t *testing.T) {
})
}
}

func TestIsKVMDriverForNVIDIA(t *testing.T) {
tests := []struct {
cc *config.ClusterConfig
wantError bool
}{
{
cc: &config.ClusterConfig{Driver: "kvm"},
},
{
cc: &config.ClusterConfig{Driver: "docker"},
wantError: true,
},
}

for _, tc := range tests {
err := isKVMDriverForNVIDIA(tc.cc, "", "")
if gotError := (err != nil); gotError != tc.wantError {
t.Errorf("isKVMDriverForNVIDIA(%v) got error %t (%v), want error %t", tc.cc, gotError, err, tc.wantError)
}
}
}

0 comments on commit cb892bc

Please sign in to comment.