Skip to content

Commit 10c212d

Browse files
committed
sdn: rationalize data directories between kubelet, CNI, and SDN
Instead of defining things in a couple places, just use the Kubelet CNI driver conf dir define. If that's overridden via the kubelet --cni-conf-dir argument, use that instead. Also pass a known IPAM data dir to the host-local plugin, and use that in the metrics code, to protect against upstream changes to the defaults.
1 parent ed29904 commit 10c212d

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

pkg/cmd/server/kubernetes/network/sdn_linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"k8s.io/client-go/tools/record"
1111
kinternalclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
1212
kinternalinformers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion"
13+
kcni "k8s.io/kubernetes/pkg/kubelet/network/cni"
1314
"k8s.io/kubernetes/pkg/proxy/apis/kubeproxyconfig"
1415

1516
configapi "github.com/openshift/origin/pkg/cmd/server/api"
@@ -35,6 +36,11 @@ func NewSDNInterfaces(options configapi.NodeConfig, networkClient networkclient.
3536
}
3637
}
3738

39+
cniConfDir := kcni.DefaultNetDir
40+
if val, ok := options.KubeletArguments["cni-conf-dir"]; ok && len(val) == 1 {
41+
cniConfDir = val[0]
42+
}
43+
3844
// dockershim + kube CNI driver delegates hostport handling to plugins,
3945
// while CRI-O handles hostports itself. Thus we need to disable the
4046
// SDN's hostport handling when run under CRI-O.
@@ -49,6 +55,7 @@ func NewSDNInterfaces(options configapi.NodeConfig, networkClient networkclient.
4955
Hostname: options.NodeName,
5056
SelfIP: options.NodeIP,
5157
RuntimeEndpoint: runtimeEndpoint,
58+
CNIConfDir: cniConfDir,
5259
MTU: options.NetworkConfig.MTU,
5360
NetworkClient: networkClient,
5461
KClient: kubeClient,

pkg/network/node/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func updateARPMetrics() {
167167

168168
func updatePodIPMetrics() {
169169
numAddrs := 0
170-
items, err := ioutil.ReadDir("/var/lib/cni/networks/openshift-sdn/")
170+
items, err := ioutil.ReadDir(hostLocalDataDir + "/networks/openshift-sdn/")
171171
if err != nil && os.IsNotExist(err) {
172172
// Don't log an error if the directory doesn't exist (eg, no pods started yet)
173173
return

pkg/network/node/node.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ import (
4747
)
4848

4949
const (
50-
cniDirPath = "/etc/cni/net.d"
5150
openshiftCNIFile = "80-openshift-network.conf"
51+
hostLocalDataDir = "/var/lib/cni"
5252
)
5353

5454
type osdnPolicy interface {
@@ -75,6 +75,7 @@ type OsdnNodeConfig struct {
7575
RuntimeEndpoint string
7676
MTU uint32
7777
EnableHostports bool
78+
CNIConfDir string
7879

7980
NetworkClient networkclient.Interface
8081
KClient kclientset.Interface
@@ -102,6 +103,7 @@ type OsdnNode struct {
102103
useConnTrack bool
103104
iptablesSyncPeriod time.Duration
104105
mtu uint32
106+
cniDirPath string
105107

106108
// Synchronizes operations on egressPolicies
107109
egressPoliciesLock sync.Mutex
@@ -154,7 +156,7 @@ func New(c *OsdnNodeConfig) (network.NodeInterface, error) {
154156

155157
// If our CNI config file exists, remove it so that kubelet doesn't think
156158
// we're ready yet
157-
os.Remove(filepath.Join(cniDirPath, openshiftCNIFile))
159+
os.Remove(filepath.Join(c.CNIConfDir, openshiftCNIFile))
158160

159161
if err := c.setNodeIP(); err != nil {
160162
return nil, err
@@ -184,6 +186,7 @@ func New(c *OsdnNodeConfig) (network.NodeInterface, error) {
184186
kubeInformers: c.KubeInformers,
185187
networkInformers: c.NetworkInformers,
186188
egressIP: newEgressIPWatcher(oc, c.SelfIP, c.MasqueradeBit),
189+
cniDirPath: c.CNIConfDir,
187190

188191
runtimeEndpoint: c.RuntimeEndpoint,
189192
// 2 minutes is the current default value used in kubelet
@@ -380,7 +383,7 @@ func (node *OsdnNode) Start() error {
380383
}
381384
}
382385

383-
if err := os.MkdirAll(cniDirPath, 0755); err != nil {
386+
if err := os.MkdirAll(node.cniDirPath, 0755); err != nil {
384387
return err
385388
}
386389

@@ -396,7 +399,7 @@ func (node *OsdnNode) Start() error {
396399

397400
// Write our CNI config file out to disk to signal to kubelet that
398401
// our network plugin is ready
399-
return ioutil.WriteFile(filepath.Join(cniDirPath, openshiftCNIFile), []byte(`
402+
return ioutil.WriteFile(filepath.Join(node.cniDirPath, openshiftCNIFile), []byte(`
400403
{
401404
"cniVersion": "0.2.0",
402405
"name": "openshift-sdn",

pkg/network/node/pod.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
2727
kcontainer "k8s.io/kubernetes/pkg/kubelet/container"
2828
knetwork "k8s.io/kubernetes/pkg/kubelet/network"
29+
kcni "k8s.io/kubernetes/pkg/kubelet/network/cni"
2930
kubehostport "k8s.io/kubernetes/pkg/kubelet/network/hostport"
3031
kbandwidth "k8s.io/kubernetes/pkg/util/bandwidth"
3132
utildbus "k8s.io/kubernetes/pkg/util/dbus"
@@ -42,6 +43,7 @@ import (
4243

4344
const (
4445
podInterfaceName = knetwork.DefaultInterfaceName
46+
cniBinPath = kcni.DefaultCNIDir
4547
)
4648

4749
type podHandler interface {
@@ -114,9 +116,10 @@ func getIPAMConfig(clusterNetworks []common.ClusterNetwork, localSubnet string)
114116
}
115117

116118
type hostLocalIPAM struct {
117-
Type string `json:"type"`
118-
Subnet cnitypes.IPNet `json:"subnet"`
119-
Routes []cnitypes.Route `json:"routes"`
119+
Type string `json:"type"`
120+
Subnet cnitypes.IPNet `json:"subnet"`
121+
Routes []cnitypes.Route `json:"routes"`
122+
DataDir string `json:"dataDir"`
120123
}
121124

122125
type cniNetworkConfig struct {
@@ -153,7 +156,8 @@ func getIPAMConfig(clusterNetworks []common.ClusterNetwork, localSubnet string)
153156
Name: "openshift-sdn",
154157
Type: "openshift-sdn",
155158
IPAM: &hostLocalIPAM{
156-
Type: "host-local",
159+
Type: "host-local",
160+
DataDir: hostLocalDataDir,
157161
Subnet: cnitypes.IPNet{
158162
IP: nodeNet.IP,
159163
Mask: nodeNet.Mask,
@@ -410,7 +414,7 @@ func createIPAMArgs(netnsPath string, action cniserver.CNICommand, id string) *i
410414
ContainerID: id,
411415
NetNS: netnsPath,
412416
IfName: podInterfaceName,
413-
Path: "/opt/cni/bin",
417+
Path: cniBinPath,
414418
}
415419
}
416420

@@ -421,7 +425,7 @@ func (m *podManager) ipamAdd(netnsPath string, id string) (*cni020.Result, net.I
421425
}
422426

423427
args := createIPAMArgs(netnsPath, cniserver.CNI_ADD, id)
424-
r, err := invoke.ExecPluginWithResult("/opt/cni/bin/host-local", m.ipamConfig, args)
428+
r, err := invoke.ExecPluginWithResult(cniBinPath+"/host-local", m.ipamConfig, args)
425429
if err != nil {
426430
return nil, nil, fmt.Errorf("failed to run CNI IPAM ADD: %v", err)
427431
}
@@ -441,7 +445,7 @@ func (m *podManager) ipamAdd(netnsPath string, id string) (*cni020.Result, net.I
441445
// Run CNI IPAM release for the container
442446
func (m *podManager) ipamDel(id string) error {
443447
args := createIPAMArgs("", cniserver.CNI_DEL, id)
444-
err := invoke.ExecPluginWithoutResult("/opt/cni/bin/host-local", m.ipamConfig, args)
448+
err := invoke.ExecPluginWithoutResult(cniBinPath+"/host-local", m.ipamConfig, args)
445449
if err != nil {
446450
return fmt.Errorf("failed to run CNI IPAM DEL: %v", err)
447451
}

0 commit comments

Comments
 (0)