add embeded-certs flag to config, decouple kubeconfig from viper

pull/5003/head
Medya Gh 2019-08-07 17:13:09 -07:00
parent 1e8961f385
commit 08e3e14263
6 changed files with 37 additions and 33 deletions

View File

@ -97,12 +97,12 @@ const (
uuid = "uuid"
vpnkitSock = "hyperkit-vpnkit-sock"
vsockPorts = "hyperkit-vsock-ports"
// embedCerts = "embed-certs" //TODO medya
noVTXCheck = "no-vtx-check"
downloadOnly = "download-only"
dnsProxy = "dns-proxy"
hostDNSResolver = "host-dns-resolver"
waitUntilHealthy = "wait"
embedCerts = "embed-certs"
noVTXCheck = "no-vtx-check"
downloadOnly = "download-only"
dnsProxy = "dns-proxy"
hostDNSResolver = "host-dns-resolver"
waitUntilHealthy = "wait"
)
var (
@ -134,20 +134,21 @@ func initMinikubeFlags() {
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
startCmd.Flags().Int(cpus, constants.DefaultCPUS, "Number of CPUs allocated to the minikube VM")
startCmd.Flags().String(memory, constants.DefaultMemorySize, "Amount of RAM allocated to the minikube VM (format: <number>[<unit>], where unit = b, k, m or g)")
startCmd.Flags().String(humanReadableDiskSize, constants.DefaultDiskSize, "Disk size allocated to the minikube VM (format: <number>[<unit>], where unit = b, k, m or g)")
startCmd.Flags().Int(cpus, constants.DefaultCPUS, "Number of CPUs allocated to the minikube VM.")
startCmd.Flags().String(memory, constants.DefaultMemorySize, "Amount of RAM allocated to the minikube VM (format: <number>[<unit>], where unit = b, k, m or g).")
startCmd.Flags().String(humanReadableDiskSize, constants.DefaultDiskSize, "Disk size allocated to the minikube VM (format: <number>[<unit>], where unit = b, k, m or g).")
startCmd.Flags().Bool(downloadOnly, false, "If true, only download and cache files for later use - don't install or start anything.")
startCmd.Flags().Bool(cacheImages, true, "If true, cache docker images for the current bootstrapper and load them into the machine. Always false with --vm-driver=none.")
startCmd.Flags().String(isoURL, constants.DefaultISOURL, "Location of the minikube iso")
startCmd.Flags().String(isoURL, constants.DefaultISOURL, "Location of the minikube iso.")
startCmd.Flags().Bool(keepContext, constants.DefaultKeepContext, "This will keep the existing kubectl context and will create a minikube context.")
startCmd.Flags().String(containerRuntime, "docker", "The container runtime to be used (docker, crio, containerd)")
startCmd.Flags().Bool(createMount, false, "This will start the mount daemon and automatically mount files into minikube")
startCmd.Flags().String(mountString, constants.DefaultMountDir+":"+constants.DefaultMountEndpoint, "The argument to pass the minikube mount command on start")
startCmd.Flags().String(criSocket, "", "The cri socket path to be used")
startCmd.Flags().String(networkPlugin, "", "The name of the network plugin")
startCmd.Flags().Bool(enableDefaultCNI, false, "Enable the default CNI plugin (/etc/cni/net.d/k8s.conf). Used in conjunction with \"--network-plugin=cni\"")
startCmd.Flags().Bool(waitUntilHealthy, true, "Wait until Kubernetes core services are healthy before exiting")
startCmd.Flags().Bool(embedCerts, constants.DefaultEmbedCerts, "if true, will embed the certs in kubeconfig.")
startCmd.Flags().String(containerRuntime, "docker", "The container runtime to be used (docker, crio, containerd).")
startCmd.Flags().Bool(createMount, false, "This will start the mount daemon and automatically mount files into minikube.")
startCmd.Flags().String(mountString, constants.DefaultMountDir+":"+constants.DefaultMountEndpoint, "The argument to pass the minikube mount command on start.")
startCmd.Flags().String(criSocket, "", "The cri socket path to be used.")
startCmd.Flags().String(networkPlugin, "", "The name of the network plugin.")
startCmd.Flags().Bool(enableDefaultCNI, false, "Enable the default CNI plugin (/etc/cni/net.d/k8s.conf). Used in conjunction with \"--network-plugin=cni\".")
startCmd.Flags().Bool(waitUntilHealthy, true, "Wait until Kubernetes core services are healthy before exiting.")
}
// initKubernetesFlags inits the commandline flags for kubernetes related options
@ -273,7 +274,7 @@ func runStart(cmd *cobra.Command, args []string) {
validateDriverVersion(viper.GetString(vmDriver))
k8sVersion, isUpgrade := getKubernetesVersion()
config, err := generateConfig(cmd, k8sVersion)
config, err := generateCfgFromFlags(cmd, k8sVersion)
if err != nil {
exit.WithError("Failed to generate config", err)
}
@ -329,7 +330,7 @@ func runStart(cmd *cobra.Command, args []string) {
}
func setupKubeconfig(host *host.Host, config *cfg.Config) (*kubeconfig.Setup, error) {
func setupKubeconfig(host *host.Host, config *cfg.Config) (*kubeconfig.KCS, error) {
addr, err := host.Driver.GetURL()
if err != nil {
exit.WithError("Failed to get host URL", err)
@ -414,7 +415,7 @@ func showVersionInfo(k8sVersion string, cr cruntime.Manager) {
}
}
func showKubectlConnectInfo(kubeconfig *kubeconfig.Setup) {
func showKubectlConnectInfo(kubeconfig *kubeconfig.KCS) {
if kubeconfig.KeepContext {
out.T(out.Kubectl, "To connect to this cluster, use: kubectl --context={{.name}}", out.V{"name": kubeconfig.ClusterName})
} else {
@ -572,8 +573,8 @@ func waitCacheImages(g *errgroup.Group) {
}
}
// generateConfig generates cfg.Config based on flags and supplied arguments
func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
// generateCfgFromFlags generates cfg.Config based on flags and supplied arguments
func generateCfgFromFlags(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
r, err := cruntime.New(cruntime.Config{Type: viper.GetString(containerRuntime)})
if err != nil {
return cfg.Config{}, err
@ -629,6 +630,7 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
cfg := cfg.Config{
MachineConfig: cfg.MachineConfig{
KeepContext: viper.GetBool(keepContext),
EmbedCerts: viper.GetBool(embedCerts),
MinikubeISO: viper.GetString(isoURL),
Memory: pkgutil.CalculateSizeInMB(viper.GetString(memory)),
CPUs: viper.GetInt(cpus),

View File

@ -67,7 +67,7 @@ func SetupCerts(cmd command.Runner, k8s config.KubernetesConfig) error {
copyableFiles = append(copyableFiles, certFile)
}
kubeCfgSetup := &kubeconfig.Setup{
kcs := &kubeconfig.KCS{
ClusterName: k8s.NodeName,
ClusterServerAddress: fmt.Sprintf("https://localhost:%d", k8s.NodePort),
ClientCertificate: path.Join(util.DefaultCertPath, "apiserver.crt"),
@ -77,7 +77,7 @@ func SetupCerts(cmd command.Runner, k8s config.KubernetesConfig) error {
}
kubeCfg := api.NewConfig()
err := kubeconfig.Populate(kubeCfgSetup, kubeCfg)
err := kubeconfig.Populate(kcs, kubeCfg)
if err != nil {
return errors.Wrap(err, "populating kubeconfig")
}

View File

@ -37,6 +37,7 @@ type Config struct {
// MachineConfig contains the parameters used to start a cluster.
type MachineConfig struct {
KeepContext bool // used by start and profile command to or not to switch kubectl's current context
EmbedCerts bool // used by kubeconfig.Setup
MinikubeISO string
Memory int
CPUs int

View File

@ -129,6 +129,8 @@ func MakeMiniPath(fileName ...string) string {
var MountProcessFileName = ".mount-process"
const (
// DefaultEmbedCerts is if the certs should be embedded in the kubeconfig file
DefaultEmbedCerts = false
// DefaultKeepContext is if we should keep context by default
DefaultKeepContext = false
// SHASuffix is the suffix of a SHA-256 checksum file

View File

@ -61,7 +61,7 @@ func (k *KCS) fileContent() string {
return k.kubeConfigFile.Load().(string)
}
// Populate populates an api.Config object with values from *Setup
// Populate populates an api.Config object with values from *KCS
func Populate(cfg *KCS, apiCfg *api.Config) error {
var err error
clusterName := cfg.ClusterName

View File

@ -28,7 +28,6 @@ import (
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/tools/clientcmd/api/latest"
@ -51,8 +50,8 @@ func Setup(clusterURL string, c *cfg.Config) (*KCS, error) {
ClientCertificate: constants.MakeMiniPath("client.crt"),
ClientKey: constants.MakeMiniPath("client.key"),
CertificateAuthority: constants.MakeMiniPath("ca.crt"),
KeepContext: viper.GetBool("keep-context"),
EmbedCerts: viper.GetBool("embed-certs"),
KeepContext: c.MachineConfig.KeepContext,
EmbedCerts: c.MachineConfig.EmbedCerts,
}
kcs.setPath(Path())
if err := update(kcs); err != nil {
@ -65,22 +64,22 @@ func Setup(clusterURL string, c *cfg.Config) (*KCS, error) {
// update reads config from disk, adds the minikube settings, and writes it back.
// activeContext is true when minikube is the CurrentContext
// If no CurrentContext is set, the given name will be used.
func update(cfg *KCS) error {
glog.Infoln("Using kubeconfig: ", cfg.fileContent())
func update(kcs *KCS) error {
glog.Infoln("Using kubeconfig: ", kcs.fileContent())
// read existing config or create new if does not exist
config, err := readOrNew(cfg.fileContent())
config, err := readOrNew(kcs.fileContent())
if err != nil {
return err
}
err = Populate(cfg, config)
err = Populate(kcs, config)
if err != nil {
return err
}
// write back to disk
if err := writeToFile(config, cfg.fileContent()); err != nil {
if err := writeToFile(config, kcs.fileContent()); err != nil {
return errors.Wrap(err, "writing kubeconfig")
}
return nil