Skip to content

Commit

Permalink
Render asset template when enabling/disabling addon
Browse files Browse the repository at this point in the history
  • Loading branch information
laozc committed Mar 22, 2019
1 parent 7ae0741 commit daec030
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 85 deletions.
33 changes: 31 additions & 2 deletions cmd/minikube/cmd/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"fmt"
"os"
"strconv"
"strings"

Expand All @@ -26,6 +27,7 @@ import (
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/storageclass"
)
Expand Down Expand Up @@ -123,15 +125,42 @@ func EnableOrDisableAddon(name string, val string) error {
if err != nil {
return errors.Wrap(err, "command runner")
}

cfg, err := config.Load()
if err != nil && !os.IsNotExist(err) {
exit.WithCode(exit.Data, "Unable to load config: %v", err)
}

data := assets.GenerateTemplateData(cfg.KubernetesConfig)
if enable {
for _, addon := range addon.Assets {
if err := cmd.Copy(addon); err != nil {
var addonFile assets.CopyableFile
if addon.IsTemplate() {
addonFile, err = addon.Evaluate(data)
if err != nil {
return errors.Wrapf(err, "evaluate bundled addon %s asset", addon.GetAssetName())
}

} else {
addonFile = addon
}
if err := cmd.Copy(addonFile); err != nil {
return errors.Wrapf(err, "enabling addon %s", addon.AssetName)
}
}
} else {
for _, addon := range addon.Assets {
if err := cmd.Remove(addon); err != nil {
var addonFile assets.CopyableFile
if addon.IsTemplate() {
addonFile, err = addon.Evaluate(data)
if err != nil {
return errors.Wrapf(err, "evaluate bundled addon %s asset", addon.GetAssetName())
}

} else {
addonFile = addon
}
if err := cmd.Remove(addonFile); err != nil {
return errors.Wrapf(err, "disabling addon %s", addon.AssetName)
}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/minikube/assets/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,14 @@ func addMinikubeDirToAssets(basedir, vmpath string, assets *[]CopyableFile) erro
}
return nil
}

// GenerateTemplateData generates template data for template assets
func GenerateTemplateData(cfg config.KubernetesConfig) interface{} {
opts := struct {
ImageRepository string
}{
ImageRepository: cfg.ImageRepository,
}

return opts
}
16 changes: 8 additions & 8 deletions pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (k *Bootstrapper) UpdateCluster(cfg config.KubernetesConfig) error {
if err != nil {
return errors.Wrap(err, "runtime")
}
kubeadmCfg, opts, err := generateConfig(cfg, r)
kubeadmCfg, err := generateConfig(cfg, r)
if err != nil {
return errors.Wrap(err, "generating kubeadm cfg")
}
Expand Down Expand Up @@ -480,7 +480,7 @@ func (k *Bootstrapper) UpdateCluster(cfg config.KubernetesConfig) error {
return errors.Wrap(err, "downloading binaries")
}

if err := addAddons(&files, opts); err != nil {
if err := addAddons(&files, assets.GenerateTemplateData(cfg)); err != nil {
return errors.Wrap(err, "adding addons")
}

Expand All @@ -501,22 +501,22 @@ sudo systemctl start kubelet
return nil
}

func generateConfig(k8s config.KubernetesConfig, r cruntime.Manager) (string, interface{}, error) {
func generateConfig(k8s config.KubernetesConfig, r cruntime.Manager) (string, error) {
version, err := ParseKubernetesVersion(k8s.KubernetesVersion)
if err != nil {
return "", nil, errors.Wrap(err, "parsing kubernetes version")
return "", errors.Wrap(err, "parsing kubernetes version")
}

// parses a map of the feature gates for kubeadm and component
kubeadmFeatureArgs, componentFeatureArgs, err := ParseFeatureArgs(k8s.FeatureGates)
if err != nil {
return "", nil, errors.Wrap(err, "parses feature gate config for kubeadm and component")
return "", errors.Wrap(err, "parses feature gate config for kubeadm and component")
}

// generates a map of component to extra args for apiserver, controller-manager, and scheduler
extraComponentConfig, err := NewComponentExtraArgs(k8s.ExtraOptions, version, componentFeatureArgs)
if err != nil {
return "", nil, errors.Wrap(err, "generating extra component config for kubeadm")
return "", errors.Wrap(err, "generating extra component config for kubeadm")
}

// In case of no port assigned, use util.APIServerPort
Expand Down Expand Up @@ -571,10 +571,10 @@ func generateConfig(k8s config.KubernetesConfig, r cruntime.Manager) (string, in
configTmpl = configTmplV1Beta1
}
if err := configTmpl.Execute(&b, opts); err != nil {
return "", nil, err
return "", err
}

return b.String(), opts, nil
return b.String(), nil
}

func maybeDownloadAndCache(binary, version string) (string, error) {
Expand Down
79 changes: 5 additions & 74 deletions pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,10 @@ ExecStart=/usr/bin/kubelet --allow-privileged=true --authorization-mode=Webhook

func TestGenerateConfig(t *testing.T) {
tests := []struct {
description string
cfg config.KubernetesConfig
expectedCfg string
expectedOpts interface{}
shouldErr bool
description string
cfg config.KubernetesConfig
expectedCfg string
shouldErr bool
}{
{
description: "no extra args",
Expand All @@ -148,37 +147,6 @@ nodeName: minikube
apiServerExtraArgs:
admission-control: "Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota"
`,
expectedOpts: struct {
CertDir string
ServiceCIDR string
AdvertiseAddress string
APIServerPort int
KubernetesVersion string
EtcdDataDir string
NodeName string
CRISocket string
ImageRepository string
ExtraArgs []ComponentExtraArgs
FeatureArgs map[string]bool
NoTaintMaster bool
}{
CertDir: util.DefaultCertPath,
ServiceCIDR: util.DefaultServiceCIDR,
AdvertiseAddress: "192.168.1.100",
APIServerPort: 8443,
KubernetesVersion: "v1.10.0",
EtcdDataDir: "/data/minikube",
NodeName: "minikube",
ExtraArgs: []ComponentExtraArgs{
{
Component: "apiServer",
Options: map[string]string{
"admission-control": "Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota",
},
},
},
FeatureArgs: map[string]bool{},
NoTaintMaster: true},
},
{
description: "extra args all components",
Expand Down Expand Up @@ -471,38 +439,6 @@ imageRepository: docker-proxy-image.io/google_containers
apiServerExtraArgs:
admission-control: "Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota"
`,
expectedOpts: struct {
CertDir string
ServiceCIDR string
AdvertiseAddress string
APIServerPort int
KubernetesVersion string
EtcdDataDir string
NodeName string
CRISocket string
ImageRepository string
ExtraArgs []ComponentExtraArgs
FeatureArgs map[string]bool
NoTaintMaster bool
}{
CertDir: util.DefaultCertPath,
ServiceCIDR: util.DefaultServiceCIDR,
AdvertiseAddress: "192.168.1.100",
APIServerPort: 8443,
KubernetesVersion: "v1.10.0",
EtcdDataDir: "/data/minikube",
NodeName: "minikube",
ExtraArgs: []ComponentExtraArgs{
{
Component: "apiServer",
Options: map[string]string{
"admission-control": "Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota",
},
},
},
ImageRepository: "docker-proxy-image.io/google_containers",
FeatureArgs: map[string]bool{},
NoTaintMaster: true},
},
}

Expand All @@ -513,7 +449,7 @@ apiServerExtraArgs:
}

t.Run(test.description, func(t *testing.T) {
gotCfg, gotOpts, err := generateConfig(test.cfg, runtime)
gotCfg, err := generateConfig(test.cfg, runtime)
if err != nil && !test.shouldErr {
t.Errorf("got unexpected error generating config: %v", err)
return
Expand All @@ -529,11 +465,6 @@ apiServerExtraArgs:
if diff := cmp.Diff(gotSplit, wantSplit); diff != "" {
t.Errorf("unexpected diff: (-want +got)\n%s\ngot: %s\n", diff, gotCfg)
}
if test.expectedOpts != nil {
if diff := cmp.Diff(test.expectedOpts, gotOpts); diff != "" {
t.Errorf("opts differ: (-want +got)\n%s", diff)
}
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/minikube/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ const (
// DefaultMsize is the default number of bytes to use for 9p packet payload
DefaultMsize = 262144
// DefaultMountVersion is the default 9p version to use for mount
DefaultMountVersion = "9p2000.L"
DefaultMountVersion = "9p2000.L"
)

// GetKubernetesReleaseURL gets the location of a kubernetes client
Expand Down
1 change: 1 addition & 0 deletions test/integration/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func (m *MinikubeRunner) RunDaemon2(command string) (*exec.Cmd, *bufio.Reader, *
}
return cmd, bufio.NewReader(stdoutPipe), bufio.NewReader(stderrPipe)
}

// SSH returns the output of running a command using SSH
func (m *MinikubeRunner) SSH(command string) (string, error) {
path, _ := filepath.Abs(m.BinaryPath)
Expand Down

0 comments on commit daec030

Please sign in to comment.