Enable/disable addons works per profile

However, this means that addon information won't persist beyond a 'minikube delete'
pull/6124/head
Priya Wadhwa 2019-12-18 21:35:46 -08:00
parent ba8b723978
commit e72cb80cbf
7 changed files with 85 additions and 9 deletions

View File

@ -18,6 +18,8 @@ package config
import (
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/addons"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/out"
)
@ -32,7 +34,7 @@ var addonsDisableCmd = &cobra.Command{
}
addon := args[0]
err := Set(addon, "false")
err := addons.Set(addon, "false", config.CurrentProfile())
if err != nil {
exit.WithError("disable failed", err)
}

View File

@ -18,6 +18,8 @@ package config
import (
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/addons"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/out"
)
@ -32,7 +34,7 @@ var addonsEnableCmd = &cobra.Command{
}
addon := args[0]
err := Set(addon, "true")
err := addons.Set(addon, "true", config.CurrentProfile())
if err != nil {
exit.WithError("enable failed", err)
}

View File

@ -17,6 +17,7 @@ limitations under the License.
package addons
import (
"fmt"
"os"
"strconv"
@ -35,12 +36,68 @@ import (
// defaultStorageClassProvisioner is the name of the default storage class provisioner
const defaultStorageClassProvisioner = "standard"
func Set(name, value, profile string) error {
a, valid := addonIsValid(name)
if !valid {
return errors.Errorf("%s is not a valid addon", name)
}
// Validate the new value
if err := run(name, value, profile, a.validations); err != nil {
return err
}
// Set the value
c, err := config.Load(profile)
if err != nil {
return err
}
if err := a.set(c, name, value); err != nil {
return err
}
// Run any callbacks for this property
if err := run(name, value, profile, a.callbacks); err != nil {
return err
}
// Write the value
return config.Write(profile, c)
}
// Runs all the validation or callback functions and collects errors
func run(name, value, profile string, fns []setFn) error {
var errors []error
for _, fn := range fns {
err := fn(name, value, profile)
if err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
return fmt.Errorf("%v", errors)
}
return nil
}
func addonIsValid(name string) (*Addon, bool) {
for _, a := range Addons {
if a.name == name {
return a, true
}
}
return nil, false
}
// SetBool sets a bool value
func SetBool(m config.MachineConfig, name string, val string) error {
func SetBool(m *config.MachineConfig, name string, val string) error {
b, err := strconv.ParseBool(val)
if err != nil {
return err
}
if m.Addons == nil {
m.Addons = map[string]bool{}
}
m.Addons[name] = b
return nil
}

View File

@ -23,13 +23,13 @@ type setFn func(string, string, string) error
// Addon represents an addon
type Addon struct {
name string
set func(config.MachineConfig, string, string) error
setMap func(config.MachineConfig, string, map[string]interface{}) error
set func(*config.MachineConfig, string, string) error
validations []setFn
callbacks []setFn
}
var Addons = []Addon{
// Addons is a list of all addons
var Addons = []*Addon{
{
name: "addon-manager",
set: SetBool,

View File

@ -45,7 +45,7 @@ func IsValidAddon(name, val, profile string) error {
// IsContainerdRuntime is a validator which returns an error if the current runtime is not containerd
func IsContainerdRuntime(_, _, profile string) error {
config, err := config.Load(config.ProfileFilePath(profile))
config, err := config.Load(profile)
if err != nil {
return fmt.Errorf("config.Load: %v", err)
}

View File

@ -55,8 +55,7 @@ func (a *Addon) Name() string {
// IsEnabled checks if an Addon is enabled for the current profile
func (a *Addon) IsEnabled() (bool, error) {
fmt.Printf("Checking if addon %s is enabled for profile %s", a.Name(), config.CurrentProfile())
config, err := config.Load(config.ProfileFilePath(config.CurrentProfile()))
config, err := config.Load(config.CurrentProfile())
if err == nil {
if status, ok := config.Addons[a.Name()]; ok {
return status, nil

View File

@ -127,9 +127,14 @@ func Load(profile string) (*MachineConfig, error) {
return DefaultLoader.LoadConfigFromFile(profile)
}
func Write(profile string, cc *MachineConfig) error {
return DefaultLoader.WriteConfigToFile(profile, cc)
}
// Loader loads the kubernetes and machine config based on the machine profile name
type Loader interface {
LoadConfigFromFile(profile string, miniHome ...string) (*MachineConfig, error)
WriteConfigToFile(profileName string, cc *MachineConfig, miniHome ...string) error
}
type simpleConfigLoader struct{}
@ -156,3 +161,14 @@ func (c *simpleConfigLoader) LoadConfigFromFile(profileName string, miniHome ...
}
return &cc, nil
}
func (c *simpleConfigLoader) WriteConfigToFile(profileName string, cc *MachineConfig, miniHome ...string) error {
// Move to profile package
path := ProfileFilePath(profileName, miniHome...)
contents, err := json.MarshalIndent(cc, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(path, contents, 0644)
}