From daec030cdfb543e2314b0e13d7a55b0d5901de0a Mon Sep 17 00:00:00 2001 From: Zhongcheng Lao Date: Fri, 22 Mar 2019 13:33:11 +0800 Subject: [PATCH] Render asset template when enabling/disabling addon --- cmd/minikube/cmd/config/util.go | 33 +++++++- pkg/minikube/assets/addons.go | 11 +++ pkg/minikube/bootstrapper/kubeadm/kubeadm.go | 16 ++-- .../bootstrapper/kubeadm/kubeadm_test.go | 79 ++----------------- pkg/minikube/constants/constants.go | 2 +- test/integration/util/util.go | 1 + 6 files changed, 57 insertions(+), 85 deletions(-) diff --git a/cmd/minikube/cmd/config/util.go b/cmd/minikube/cmd/config/util.go index 586981df1e34..e5b0986c9e1a 100644 --- a/cmd/minikube/cmd/config/util.go +++ b/cmd/minikube/cmd/config/util.go @@ -18,6 +18,7 @@ package config import ( "fmt" + "os" "strconv" "strings" @@ -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" ) @@ -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) } } diff --git a/pkg/minikube/assets/addons.go b/pkg/minikube/assets/addons.go index d882492ddfc0..6728192edbdf 100644 --- a/pkg/minikube/assets/addons.go +++ b/pkg/minikube/assets/addons.go @@ -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 +} diff --git a/pkg/minikube/bootstrapper/kubeadm/kubeadm.go b/pkg/minikube/bootstrapper/kubeadm/kubeadm.go index e4ec43e60549..b5b95415ba45 100644 --- a/pkg/minikube/bootstrapper/kubeadm/kubeadm.go +++ b/pkg/minikube/bootstrapper/kubeadm/kubeadm.go @@ -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") } @@ -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") } @@ -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 @@ -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) { diff --git a/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go b/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go index 35cbb49bf681..6e777e3c7a92 100644 --- a/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go +++ b/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go @@ -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", @@ -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", @@ -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}, }, } @@ -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 @@ -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) - } - } }) } } diff --git a/pkg/minikube/constants/constants.go b/pkg/minikube/constants/constants.go index a31f657f3e73..3972ea318ebf 100644 --- a/pkg/minikube/constants/constants.go +++ b/pkg/minikube/constants/constants.go @@ -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 diff --git a/test/integration/util/util.go b/test/integration/util/util.go index 83cbc67f8b8d..6b2ec9b437ec 100644 --- a/test/integration/util/util.go +++ b/test/integration/util/util.go @@ -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)