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 b69e061
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 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

0 comments on commit b69e061

Please sign in to comment.