@@ -251,139 +251,8 @@ func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k
251251 cc = updateExistingConfigFromFlags (cmd , existing )
252252 } else {
253253 klog .Info ("no existing cluster config was found, will generate one from the flags " )
254- sysLimit , containerLimit , err := memoryLimits (drvName )
255- if err != nil {
256- klog .Warningf ("Unable to query memory limits: %+v" , err )
257- }
258-
259- mem := suggestMemoryAllocation (sysLimit , containerLimit , viper .GetInt (nodes ))
260- if cmd .Flags ().Changed (memory ) {
261- var err error
262- mem , err = pkgutil .CalculateSizeInMB (viper .GetString (memory ))
263- if err != nil {
264- exit .Message (reason .Usage , "Generate unable to parse memory '{{.memory}}': {{.error}}" , out.V {"memory" : viper .GetString (memory ), "error" : err })
265- }
266- if driver .IsKIC (drvName ) && mem > containerLimit {
267- exit .Message (reason .Usage , "{{.driver_name}} has only {{.container_limit}}MB memory but you specified {{.specified_memory}}MB" , out.V {"container_limit" : containerLimit , "specified_memory" : mem , "driver_name" : driver .FullName (drvName )})
268- }
269- } else {
270- validateRequestedMemorySize (mem , drvName )
271- klog .Infof ("Using suggested %dMB memory alloc based on sys=%dMB, container=%dMB" , mem , sysLimit , containerLimit )
272- }
273-
274- diskSize , err := pkgutil .CalculateSizeInMB (viper .GetString (humanReadableDiskSize ))
275- if err != nil {
276- exit .Message (reason .Usage , "Generate unable to parse disk size '{{.diskSize}}': {{.error}}" , out.V {"diskSize" : viper .GetString (humanReadableDiskSize ), "error" : err })
277- }
278-
279- repository := viper .GetString (imageRepository )
280- mirrorCountry := strings .ToLower (viper .GetString (imageMirrorCountry ))
281- if strings .ToLower (repository ) == "auto" || (mirrorCountry != "" && repository == "" ) {
282- found , autoSelectedRepository , err := selectImageRepository (mirrorCountry , semver .MustParse (strings .TrimPrefix (k8sVersion , version .VersionPrefix )))
283- if err != nil {
284- exit .Error (reason .InetRepo , "Failed to check main repository and mirrors for images" , err )
285- }
286-
287- if ! found {
288- if autoSelectedRepository == "" {
289- exit .Message (reason .InetReposUnavailable , "None of the known repositories are accessible. Consider specifying an alternative image repository with --image-repository flag" )
290- } else {
291- out .WarningT ("None of the known repositories in your location are accessible. Using {{.image_repository_name}} as fallback." , out.V {"image_repository_name" : autoSelectedRepository })
292- }
293- }
254+ cc = generateNewConfigFromFlags (cmd , k8sVersion , drvName )
294255
295- repository = autoSelectedRepository
296- }
297-
298- if cmd .Flags ().Changed (imageRepository ) || cmd .Flags ().Changed (imageMirrorCountry ) {
299- out .Styled (style .Success , "Using image repository {{.name}}" , out.V {"name" : repository })
300- }
301-
302- // Backwards compatibility with --enable-default-cni
303- chosenCNI := viper .GetString (cniFlag )
304- if viper .GetBool (enableDefaultCNI ) && ! cmd .Flags ().Changed (cniFlag ) {
305- klog .Errorf ("Found deprecated --enable-default-cni flag, setting --cni=bridge" )
306- chosenCNI = "bridge"
307- }
308- // networkPlugin cni deprecation warning
309- chosenNetworkPlugin := viper .GetString (networkPlugin )
310- if chosenNetworkPlugin == "cni" {
311- out .WarningT ("With --network-plugin=cni, you will need to provide your own CNI. See --cni flag as a user-friendly alternative" )
312- }
313-
314- if ! (driver .IsKIC (drvName ) || driver .IsKVM (drvName )) && viper .GetString (network ) != "" {
315- out .WarningT ("--network flag is only valid with the docker/podman and KVM drivers, it will be ignored" )
316- }
317-
318- checkNumaCount (k8sVersion )
319-
320- cc = config.ClusterConfig {
321- Name : ClusterFlagValue (),
322- KeepContext : viper .GetBool (keepContext ),
323- EmbedCerts : viper .GetBool (embedCerts ),
324- MinikubeISO : viper .GetString (isoURL ),
325- KicBaseImage : viper .GetString (kicBaseImage ),
326- Network : viper .GetString (network ),
327- Memory : mem ,
328- CPUs : viper .GetInt (cpus ),
329- DiskSize : diskSize ,
330- Driver : drvName ,
331- ListenAddress : viper .GetString (listenAddress ),
332- HyperkitVpnKitSock : viper .GetString (vpnkitSock ),
333- HyperkitVSockPorts : viper .GetStringSlice (vsockPorts ),
334- NFSShare : viper .GetStringSlice (nfsShare ),
335- NFSSharesRoot : viper .GetString (nfsSharesRoot ),
336- DockerEnv : config .DockerEnv ,
337- DockerOpt : config .DockerOpt ,
338- InsecureRegistry : insecureRegistry ,
339- RegistryMirror : registryMirror ,
340- HostOnlyCIDR : viper .GetString (hostOnlyCIDR ),
341- HypervVirtualSwitch : viper .GetString (hypervVirtualSwitch ),
342- HypervUseExternalSwitch : viper .GetBool (hypervUseExternalSwitch ),
343- HypervExternalAdapter : viper .GetString (hypervExternalAdapter ),
344- KVMNetwork : viper .GetString (kvmNetwork ),
345- KVMQemuURI : viper .GetString (kvmQemuURI ),
346- KVMGPU : viper .GetBool (kvmGPU ),
347- KVMHidden : viper .GetBool (kvmHidden ),
348- KVMNUMACount : viper .GetInt (kvmNUMACount ),
349- DisableDriverMounts : viper .GetBool (disableDriverMounts ),
350- UUID : viper .GetString (uuid ),
351- NoVTXCheck : viper .GetBool (noVTXCheck ),
352- DNSProxy : viper .GetBool (dnsProxy ),
353- HostDNSResolver : viper .GetBool (hostDNSResolver ),
354- HostOnlyNicType : viper .GetString (hostOnlyNicType ),
355- NatNicType : viper .GetString (natNicType ),
356- StartHostTimeout : viper .GetDuration (waitTimeout ),
357- ExposedPorts : viper .GetStringSlice (ports ),
358- SSHIPAddress : viper .GetString (sshIPAddress ),
359- SSHUser : viper .GetString (sshSSHUser ),
360- SSHKey : viper .GetString (sshSSHKey ),
361- SSHPort : viper .GetInt (sshSSHPort ),
362- KubernetesConfig : config.KubernetesConfig {
363- KubernetesVersion : k8sVersion ,
364- ClusterName : ClusterFlagValue (),
365- Namespace : viper .GetString (startNamespace ),
366- APIServerName : viper .GetString (apiServerName ),
367- APIServerNames : apiServerNames ,
368- APIServerIPs : apiServerIPs ,
369- DNSDomain : viper .GetString (dnsDomain ),
370- FeatureGates : viper .GetString (featureGates ),
371- ContainerRuntime : viper .GetString (containerRuntime ),
372- CRISocket : viper .GetString (criSocket ),
373- NetworkPlugin : chosenNetworkPlugin ,
374- ServiceCIDR : viper .GetString (serviceCIDR ),
375- ImageRepository : repository ,
376- ExtraOptions : config .ExtraOptions ,
377- ShouldLoadCachedImages : viper .GetBool (cacheImages ),
378- CNI : chosenCNI ,
379- NodePort : viper .GetInt (apiServerPort ),
380- },
381- MultiNodeRequested : viper .GetInt (nodes ) > 1 ,
382- }
383- cc .VerifyComponents = interpretWaitFlag (* cmd )
384- if viper .GetBool (createMount ) && driver .IsKIC (drvName ) {
385- cc .ContainerVolumeMounts = []string {viper .GetString (mountString )}
386- }
387256 cnm , err := cni .New (cc )
388257 if err != nil {
389258 return cc , config.Node {}, errors .Wrap (err , "cni" )
@@ -418,6 +287,147 @@ func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k
418287 return createNode (cc , kubeNodeName , existing )
419288}
420289
290+ // generateNewConfigFromFlags generate a config.ClusterConfig based on flags
291+ func generateNewConfigFromFlags (cmd * cobra.Command , k8sVersion string , drvName string ) config.ClusterConfig {
292+ var cc config.ClusterConfig
293+
294+ sysLimit , containerLimit , err := memoryLimits (drvName )
295+ if err != nil {
296+ klog .Warningf ("Unable to query memory limits: %+v" , err )
297+ }
298+
299+ mem := suggestMemoryAllocation (sysLimit , containerLimit , viper .GetInt (nodes ))
300+ if cmd .Flags ().Changed (memory ) || viper .IsSet (memory ) {
301+ var err error
302+ mem , err = pkgutil .CalculateSizeInMB (viper .GetString (memory ))
303+ if err != nil {
304+ exit .Message (reason .Usage , "Generate unable to parse memory '{{.memory}}': {{.error}}" , out.V {"memory" : viper .GetString (memory ), "error" : err })
305+ }
306+ if driver .IsKIC (drvName ) && mem > containerLimit {
307+ exit .Message (reason .Usage , "{{.driver_name}} has only {{.container_limit}}MB memory but you specified {{.specified_memory}}MB" , out.V {"container_limit" : containerLimit , "specified_memory" : mem , "driver_name" : driver .FullName (drvName )})
308+ }
309+ } else {
310+ validateRequestedMemorySize (mem , drvName )
311+ klog .Infof ("Using suggested %dMB memory alloc based on sys=%dMB, container=%dMB" , mem , sysLimit , containerLimit )
312+ }
313+
314+ diskSize , err := pkgutil .CalculateSizeInMB (viper .GetString (humanReadableDiskSize ))
315+ if err != nil {
316+ exit .Message (reason .Usage , "Generate unable to parse disk size '{{.diskSize}}': {{.error}}" , out.V {"diskSize" : viper .GetString (humanReadableDiskSize ), "error" : err })
317+ }
318+
319+ repository := viper .GetString (imageRepository )
320+ mirrorCountry := strings .ToLower (viper .GetString (imageMirrorCountry ))
321+ if strings .ToLower (repository ) == "auto" || (mirrorCountry != "" && repository == "" ) {
322+ found , autoSelectedRepository , err := selectImageRepository (mirrorCountry , semver .MustParse (strings .TrimPrefix (k8sVersion , version .VersionPrefix )))
323+ if err != nil {
324+ exit .Error (reason .InetRepo , "Failed to check main repository and mirrors for images" , err )
325+ }
326+
327+ if ! found {
328+ if autoSelectedRepository == "" {
329+ exit .Message (reason .InetReposUnavailable , "None of the known repositories are accessible. Consider specifying an alternative image repository with --image-repository flag" )
330+ } else {
331+ out .WarningT ("None of the known repositories in your location are accessible. Using {{.image_repository_name}} as fallback." , out.V {"image_repository_name" : autoSelectedRepository })
332+ }
333+ }
334+
335+ repository = autoSelectedRepository
336+ }
337+
338+ if cmd .Flags ().Changed (imageRepository ) || cmd .Flags ().Changed (imageMirrorCountry ) {
339+ out .Styled (style .Success , "Using image repository {{.name}}" , out.V {"name" : repository })
340+ }
341+
342+ // Backwards compatibility with --enable-default-cni
343+ chosenCNI := viper .GetString (cniFlag )
344+ if viper .GetBool (enableDefaultCNI ) && ! cmd .Flags ().Changed (cniFlag ) {
345+ klog .Errorf ("Found deprecated --enable-default-cni flag, setting --cni=bridge" )
346+ chosenCNI = "bridge"
347+ }
348+ // networkPlugin cni deprecation warning
349+ chosenNetworkPlugin := viper .GetString (networkPlugin )
350+ if chosenNetworkPlugin == "cni" {
351+ out .WarningT ("With --network-plugin=cni, you will need to provide your own CNI. See --cni flag as a user-friendly alternative" )
352+ }
353+
354+ if ! (driver .IsKIC (drvName ) || driver .IsKVM (drvName )) && viper .GetString (network ) != "" {
355+ out .WarningT ("--network flag is only valid with the docker/podman and KVM drivers, it will be ignored" )
356+ }
357+
358+ checkNumaCount (k8sVersion )
359+
360+ cc = config.ClusterConfig {
361+ Name : ClusterFlagValue (),
362+ KeepContext : viper .GetBool (keepContext ),
363+ EmbedCerts : viper .GetBool (embedCerts ),
364+ MinikubeISO : viper .GetString (isoURL ),
365+ KicBaseImage : viper .GetString (kicBaseImage ),
366+ Network : viper .GetString (network ),
367+ Memory : mem ,
368+ CPUs : viper .GetInt (cpus ),
369+ DiskSize : diskSize ,
370+ Driver : drvName ,
371+ ListenAddress : viper .GetString (listenAddress ),
372+ HyperkitVpnKitSock : viper .GetString (vpnkitSock ),
373+ HyperkitVSockPorts : viper .GetStringSlice (vsockPorts ),
374+ NFSShare : viper .GetStringSlice (nfsShare ),
375+ NFSSharesRoot : viper .GetString (nfsSharesRoot ),
376+ DockerEnv : config .DockerEnv ,
377+ DockerOpt : config .DockerOpt ,
378+ InsecureRegistry : insecureRegistry ,
379+ RegistryMirror : registryMirror ,
380+ HostOnlyCIDR : viper .GetString (hostOnlyCIDR ),
381+ HypervVirtualSwitch : viper .GetString (hypervVirtualSwitch ),
382+ HypervUseExternalSwitch : viper .GetBool (hypervUseExternalSwitch ),
383+ HypervExternalAdapter : viper .GetString (hypervExternalAdapter ),
384+ KVMNetwork : viper .GetString (kvmNetwork ),
385+ KVMQemuURI : viper .GetString (kvmQemuURI ),
386+ KVMGPU : viper .GetBool (kvmGPU ),
387+ KVMHidden : viper .GetBool (kvmHidden ),
388+ KVMNUMACount : viper .GetInt (kvmNUMACount ),
389+ DisableDriverMounts : viper .GetBool (disableDriverMounts ),
390+ UUID : viper .GetString (uuid ),
391+ NoVTXCheck : viper .GetBool (noVTXCheck ),
392+ DNSProxy : viper .GetBool (dnsProxy ),
393+ HostDNSResolver : viper .GetBool (hostDNSResolver ),
394+ HostOnlyNicType : viper .GetString (hostOnlyNicType ),
395+ NatNicType : viper .GetString (natNicType ),
396+ StartHostTimeout : viper .GetDuration (waitTimeout ),
397+ ExposedPorts : viper .GetStringSlice (ports ),
398+ SSHIPAddress : viper .GetString (sshIPAddress ),
399+ SSHUser : viper .GetString (sshSSHUser ),
400+ SSHKey : viper .GetString (sshSSHKey ),
401+ SSHPort : viper .GetInt (sshSSHPort ),
402+ KubernetesConfig : config.KubernetesConfig {
403+ KubernetesVersion : k8sVersion ,
404+ ClusterName : ClusterFlagValue (),
405+ Namespace : viper .GetString (startNamespace ),
406+ APIServerName : viper .GetString (apiServerName ),
407+ APIServerNames : apiServerNames ,
408+ APIServerIPs : apiServerIPs ,
409+ DNSDomain : viper .GetString (dnsDomain ),
410+ FeatureGates : viper .GetString (featureGates ),
411+ ContainerRuntime : viper .GetString (containerRuntime ),
412+ CRISocket : viper .GetString (criSocket ),
413+ NetworkPlugin : chosenNetworkPlugin ,
414+ ServiceCIDR : viper .GetString (serviceCIDR ),
415+ ImageRepository : repository ,
416+ ExtraOptions : config .ExtraOptions ,
417+ ShouldLoadCachedImages : viper .GetBool (cacheImages ),
418+ CNI : chosenCNI ,
419+ NodePort : viper .GetInt (apiServerPort ),
420+ },
421+ MultiNodeRequested : viper .GetInt (nodes ) > 1 ,
422+ }
423+ cc .VerifyComponents = interpretWaitFlag (* cmd )
424+ if viper .GetBool (createMount ) && driver .IsKIC (drvName ) {
425+ cc .ContainerVolumeMounts = []string {viper .GetString (mountString )}
426+ }
427+
428+ return cc
429+ }
430+
421431// setCNIConfDir sets kubelet's '--cni-conf-dir' flag to custom CNI Config Directory path (same used also by CNI Deployment) to avoid conflicting CNI configs.
422432// ref: https://github.com/kubernetes/minikube/issues/10984
423433// Note: currently, this change affects only Kindnet CNI (and all multinodes using it), but it can be easily expanded to other/all CNIs if needed.
0 commit comments