Skip to content

Commit

Permalink
Add minikube config get/set/unset commands
Browse files Browse the repository at this point in the history
This introduces the minikube config get/set/unset command. At a basic level, it allows a more user friendly interface for setting minikube config values, but it also allows us to run validations and callbacks before and after the values have been set.
  • Loading branch information
r2d4 committed Sep 5, 2016
1 parent a4e767a commit 2b662e0
Show file tree
Hide file tree
Showing 19 changed files with 948 additions and 26 deletions.
146 changes: 146 additions & 0 deletions cmd/minikube/cmd/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"encoding/json"
"io"

"os"

"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/constants"
)

type configFile interface {
io.ReadWriter
}

type setFn func(string, string) error
type MinikubeConfig map[string]interface{}

type Setting struct {
name string
set func(MinikubeConfig, string, string) error
validations []setFn
callbacks []setFn
}

// These are all the settings that are configurable
// and their validation and callback fn run on Set
var settings []Setting = []Setting{
{
name: "vm-driver",
set: SetString,
validations: []setFn{IsValidDriver},
callbacks: []setFn{RequiresRestartMsg},
},
{
name: "cpus",
set: SetInt,
validations: []setFn{IsPositive},
callbacks: []setFn{RequiresRestartMsg},
},
{
name: "disk-size",
set: SetString,
validations: []setFn{IsValidDiskSize},
callbacks: []setFn{RequiresRestartMsg},
},
{
name: "host-only-cidr",
set: SetString,
validations: []setFn{IsValidCIDR},
},
{
name: "memory",
set: SetInt,
validations: []setFn{IsPositive},
callbacks: []setFn{RequiresRestartMsg},
},
{
name: "show-libmachine-logs",
set: SetBool,
},
{
name: "log_dir",
set: SetString,
validations: []setFn{IsValidPath},
},
{
name: "kubernetes-version",
set: SetString,
},
}

var ConfigCmd = &cobra.Command{
Use: "config SUBCOMMAND [flags]",
Short: "Modify minikube config",
Long: `config modifies minikube config files using subcommands like "minikube config set vm-driver kvm"`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

// Reads in the JSON minikube config
func ReadConfig() MinikubeConfig {
f, err := os.Open(constants.ConfigFile)
if err != nil {
if os.IsNotExist(err) {
return make(map[string]interface{})
}
glog.Errorf("Could not open file %s: %s", constants.ConfigFile, err)
}
var m MinikubeConfig
m, err = decode(f)
if err != nil {
glog.Errorf("Could not decode config %s: %s", constants.ConfigFile, err)
}

return m
}

// Writes a minikube config to the JSON file
func WriteConfig(m MinikubeConfig) {
f, err := os.Create(constants.ConfigFile)
if err != nil {
glog.Errorf("Could not open file %s: %s", constants.ConfigFile, err)
}
defer f.Close()
err = encode(f, m)
if err != nil {
glog.Errorf("Error encoding config %s: %s", constants.ConfigFile, err)
}
}

func decode(r io.Reader) (MinikubeConfig, error) {
var data MinikubeConfig
err := json.NewDecoder(r).Decode(&data)
return data, err
}

func encode(w io.Writer, m MinikubeConfig) error {
b, err := json.MarshalIndent(m, "", " ")
if err != nil {
return err
}

_, err = w.Write(b)

return err
}
83 changes: 83 additions & 0 deletions cmd/minikube/cmd/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"bytes"
"reflect"
"testing"
)

type configTestCase struct {
data string
config map[string]interface{}
}

var configTestCases = []configTestCase{
{
data: `{
"memory": 2
}`,
config: map[string]interface{}{
"memory": 2,
},
},
{
data: `{
"ReminderWaitPeriodInHours": 99,
"cpus": 4,
"disk-size": "20g",
"log_dir": "/etc/hosts",
"show-libmachine-logs": true,
"v": 5,
"vm-driver": "kvm"
}`,
config: map[string]interface{}{
"vm-driver": "kvm",
"cpus": 4,
"disk-size": "20g",
"v": 5,
"show-libmachine-logs": true,
"log_dir": "/etc/hosts",
"ReminderWaitPeriodInHours": 99,
},
},
}

func TestReadConfig(t *testing.T) {
for _, tt := range configTestCases {
r := bytes.NewBufferString(tt.data)
config, err := decode(r)
if reflect.DeepEqual(config, tt.config) || err != nil {
t.Errorf("Did not read config correctly,\n\n wanted %+v, \n\n got %+v", tt.config, config)
}
}
}

func TestWriteConfig(t *testing.T) {
var b bytes.Buffer
for _, tt := range configTestCases {
err := encode(&b, tt.config)
if err != nil {
t.Errorf("Error encoding: %s", err)
}
if b.String() != tt.data {
t.Errorf("Did not write config correctly, \n\n expected:\n %+v \n\n actual:\n %+v", tt.data, b.String())
}
b.Reset()
}
}
49 changes: 49 additions & 0 deletions cmd/minikube/cmd/config/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"fmt"
"github.com/spf13/cobra"
"os"
)

var configGetCmd = &cobra.Command{
Use: "get PROPERTY_NAME",
Short: "Gets the value of PROPERTY_NAME from the minikube config file",
Long: "Returns the value of PROPERTY_NAME from the minikube config file. Can be overwritten at runtime by flags or environmental variables.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "usage: minikube config get PROPERTY_NAME")
os.Exit(1)
}

val := get(args[0])
if val != "" {
fmt.Fprintln(os.Stdout, val)
}
},
}

func init() {
ConfigCmd.AddCommand(configGetCmd)
}

func get(name string) string {
m := ReadConfig()
return fmt.Sprintf("%v", m[name])
}
75 changes: 75 additions & 0 deletions cmd/minikube/cmd/config/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"fmt"
"github.com/spf13/cobra"
"os"
)

var configSetCmd = &cobra.Command{
Use: "set PROPERTY_NAME PROPERTY_VALUE",
Short: "Sets an individual value in a minikube config file",
Long: `Sets the PROPERTY_NAME config value to PROPERTY_VALUE
These values can be overwritten by flags or environment variables at runtime.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "usage: minikube config set PROPERTY_NAME PROPERTY_VALUE")
os.Exit(1)
}
err := set(args[0], args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
},
}

func init() {
ConfigCmd.AddCommand(configSetCmd)
}

func set(name string, value string) error {
s, err := findSetting(name)
if err != nil {
return err
}
// Validate the new value
err = run(name, value, s.validations)
if err != nil {
return err
}

// Set the value
config := ReadConfig()
err = s.set(config, name, value)
if err != nil {
return err
}

// Run any callbacks for this property
err = run(name, value, s.callbacks)
if err != nil {
return err
}

// Write the value
WriteConfig(config)

return nil
}
26 changes: 26 additions & 0 deletions cmd/minikube/cmd/config/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import "testing"

func TestNotFound(t *testing.T) {
err := set("nonexistant", "10")
if err == nil {
t.Fatalf("Set did not return error for unknown property")
}
}
Loading

0 comments on commit 2b662e0

Please sign in to comment.