addons: Fail when enabling KVM addons on non-KVM cluster

pull/19215/head
Steven Powell 2024-07-04 09:53:25 -05:00 committed by Medya Ghazizadeh
parent 177f4f2a00
commit cb892bc45b
3 changed files with 50 additions and 13 deletions

View File

@ -71,7 +71,7 @@ var Addons = []*Addon{
{
name: "gvisor",
set: SetBool,
validations: []setFn{IsRuntimeContainerd},
validations: []setFn{isRuntimeContainerd},
callbacks: []setFn{EnableOrDisableAddon, verifyAddonStatus},
},
{
@ -127,11 +127,13 @@ var Addons = []*Addon{
{
name: "nvidia-driver-installer",
set: SetBool,
validations: []setFn{isKVMDriverForNVIDIA},
callbacks: []setFn{EnableOrDisableAddon},
},
{
name: "nvidia-gpu-device-plugin",
set: SetBool,
validations: []setFn{isKVMDriverForNVIDIA},
callbacks: []setFn{EnableOrDisableAddon},
},
{
@ -204,7 +206,7 @@ var Addons = []*Addon{
{
name: "csi-hostpath-driver",
set: SetBool,
validations: []setFn{IsVolumesnapshotsEnabled},
validations: []setFn{isVolumesnapshotsEnabled},
callbacks: []setFn{EnableOrDisableAddon, verifyAddonStatus},
},
{

View File

@ -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"
)
@ -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
@ -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.
@ -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) {

View File

@ -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 {
@ -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)
}
}
}