@@ -30,19 +30,31 @@ import (
3030 "k8s.io/minikube/pkg/minikube/assets"
3131 "k8s.io/minikube/pkg/minikube/command"
3232 "k8s.io/minikube/pkg/minikube/config"
33+ "k8s.io/minikube/pkg/minikube/constants"
3334 "k8s.io/minikube/pkg/minikube/driver"
3435 "k8s.io/minikube/pkg/minikube/vmpath"
3536)
3637
3738const (
3839 // DefaultPodCIDR is the default CIDR to use in minikube CNI's.
3940 DefaultPodCIDR = "10.244.0.0/16"
41+
42+ // DefaultConfDir is the default CNI Config Directory path
43+ DefaultConfDir = "/etc/cni/net.d"
44+ // CustomConfDir is the custom CNI Config Directory path used to avoid conflicting CNI configs
45+ // ref: https://github.com/kubernetes/minikube/issues/10984 and https://github.com/kubernetes/minikube/pull/11106
46+ CustomConfDir = "/etc/cni/net.mk"
4047)
4148
4249var (
43- // CustomCNIConfDir is the custom CNI Config Directory path used to avoid conflicting CNI configs
44- // ref: https://github.com/kubernetes/minikube/issues/10984
45- CustomCNIConfDir = "/etc/cni/net.mk"
50+ // ConfDir is the CNI Config Directory path that can be customised, defaulting to DefaultConfDir
51+ ConfDir = DefaultConfDir
52+
53+ // Network is the network name that CNI should use (eg, "kindnet").
54+ // Currently, only crio (and podman) can use it, so that setting custom ConfDir is not necessary.
55+ // ref: https://github.com/cri-o/cri-o/issues/2121 (and https://github.com/containers/podman/issues/2370)
56+ // ref: https://github.com/cri-o/cri-o/blob/master/docs/crio.conf.5.md#crionetwork-table
57+ Network = ""
4658)
4759
4860// Runner is the subset of command.Runner this package consumes
@@ -72,38 +84,40 @@ type tmplInput struct {
7284}
7385
7486// New returns a new CNI manager
75- func New (cc config.ClusterConfig ) (Manager , error ) {
87+ func New (cc * config.ClusterConfig ) (Manager , error ) {
7688 if cc .KubernetesConfig .NetworkPlugin != "" && cc .KubernetesConfig .NetworkPlugin != "cni" {
7789 klog .Infof ("network plugin configured as %q, returning disabled" , cc .KubernetesConfig .NetworkPlugin )
7890 return Disabled {}, nil
7991 }
8092
8193 klog .Infof ("Creating CNI manager for %q" , cc .KubernetesConfig .CNI )
8294
83- // respect user-specified custom CNI Config Directory, if any
84- userCNIConfDir := cc .KubernetesConfig .ExtraOptions .Get ("cni-conf-dir" , "kubelet" )
85- if userCNIConfDir != "" {
86- CustomCNIConfDir = userCNIConfDir
87- }
88-
95+ var cnm Manager
96+ var err error
8997 switch cc .KubernetesConfig .CNI {
9098 case "" , "auto" :
91- return chooseDefault (cc ), nil
99+ cnm = chooseDefault (* cc )
92100 case "false" :
93- return Disabled {cc : cc }, nil
101+ cnm = Disabled {cc : * cc }
94102 case "kindnet" , "true" :
95- return KindNet {cc : cc }, nil
103+ cnm = KindNet {cc : * cc }
96104 case "bridge" :
97- return Bridge {cc : cc }, nil
105+ cnm = Bridge {cc : * cc }
98106 case "calico" :
99- return Calico {cc : cc }, nil
107+ cnm = Calico {cc : * cc }
100108 case "cilium" :
101- return Cilium {cc : cc }, nil
109+ cnm = Cilium {cc : * cc }
102110 case "flannel" :
103- return Flannel {cc : cc }, nil
111+ cnm = Flannel {cc : * cc }
104112 default :
105- return NewCustom (cc , cc .KubernetesConfig .CNI )
113+ cnm , err = NewCustom (* cc , cc .KubernetesConfig .CNI )
106114 }
115+
116+ if err := configureCNI (cc , cnm ); err != nil {
117+ klog .Errorf ("unable to set CNI Config Directory: %v" , err )
118+ }
119+
120+ return cnm , err
107121}
108122
109123// IsDisabled checks if CNI is disabled
@@ -183,3 +197,33 @@ func applyManifest(cc config.ClusterConfig, r Runner, f assets.CopyableFile) err
183197
184198 return nil
185199}
200+
201+ // configureCNI - to avoid conflicting CNI configs, it sets:
202+ // - for crio: 'cni_default_network' config param via cni.Network
203+ // - for containerd and docker: kubelet's '--cni-conf-dir' flag to custom CNI Config Directory path (same used also by CNI Deployment).
204+ // ref: https://github.com/kubernetes/minikube/issues/10984 and https://github.com/kubernetes/minikube/pull/11106
205+ // 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.
206+ // Note2: Cilium does not need workaround as they automatically restart pods after CNI is successfully deployed.
207+ func configureCNI (cc * config.ClusterConfig , cnm Manager ) error {
208+ if _ , kindnet := cnm .(KindNet ); kindnet {
209+ // crio only needs CNI network name; hopefully others (containerd, docker and kubeadm/kubelet) will follow eventually
210+ if cc .KubernetesConfig .ContainerRuntime == constants .CRIO {
211+ Network = "kindnet"
212+ return nil
213+ }
214+ // for containerd and docker: auto-set custom CNI via kubelet's 'cni-conf-dir' param, if not user-specified
215+ eo := fmt .Sprintf ("kubelet.cni-conf-dir=%s" , CustomConfDir )
216+ if ! cc .KubernetesConfig .ExtraOptions .Exists (eo ) {
217+ klog .Infof ("auto-setting extra-config to %q" , eo )
218+ if err := cc .KubernetesConfig .ExtraOptions .Set (eo ); err != nil {
219+ return fmt .Errorf ("failed auto-setting extra-config %q: %v" , eo , err )
220+ }
221+ ConfDir = CustomConfDir
222+ klog .Infof ("extra-config set to %q" , eo )
223+ } else {
224+ // respect user-specified custom CNI Config Directory
225+ ConfDir = cc .KubernetesConfig .ExtraOptions .Get ("cni-conf-dir" , "kubelet" )
226+ }
227+ }
228+ return nil
229+ }
0 commit comments