Skip to content

Commit 29c87cb

Browse files
authored
Merge pull request kubernetes#80482 from mars1024/bugfix/cni_validation
add CNI config validation to getDefaultCNINetwork
2 parents 5df8781 + 9903cb3 commit 29c87cb

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

pkg/kubelet/dockershim/network/cni/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ go_library(
1919
"//pkg/kubelet/container:go_default_library",
2020
"//pkg/kubelet/dockershim/network:go_default_library",
2121
"//pkg/util/bandwidth:go_default_library",
22+
"//pkg/util/slice:go_default_library",
2223
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
2324
"//staging/src/k8s.io/cri-api/pkg/apis/runtime/v1alpha2:go_default_library",
2425
"//vendor/github.com/containernetworking/cni/libcni:go_default_library",

pkg/kubelet/dockershim/network/cni/cni.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package cni
1919
import (
2020
"context"
2121
"encoding/json"
22-
"errors"
2322
"fmt"
2423
"math"
2524
"sort"
@@ -36,6 +35,7 @@ import (
3635
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
3736
"k8s.io/kubernetes/pkg/kubelet/dockershim/network"
3837
"k8s.io/kubernetes/pkg/util/bandwidth"
38+
utilslice "k8s.io/kubernetes/pkg/util/slice"
3939
utilexec "k8s.io/utils/exec"
4040
)
4141

@@ -46,6 +46,13 @@ const (
4646
// defaultSyncConfigPeriod is the default period to sync CNI config
4747
// TODO: consider making this value configurable or to be a more appropriate value.
4848
defaultSyncConfigPeriod = time.Second * 5
49+
50+
// supported capabilities
51+
// https://github.com/containernetworking/cni/blob/master/CONVENTIONS.md
52+
portMappingsCapability = "portMappings"
53+
ipRangesCapability = "ipRanges"
54+
bandwidthCapability = "bandwidth"
55+
dnsCapability = "dns"
4956
)
5057

5158
type cniNetworkPlugin struct {
@@ -69,6 +76,7 @@ type cniNetwork struct {
6976
name string
7077
NetworkConfig *libcni.NetworkConfigList
7178
CNIConfig libcni.CNI
79+
Capabilities []string
7280
}
7381

7482
// cniPortMapping maps to the standard CNI portmapping Capability
@@ -149,9 +157,11 @@ func getDefaultCNINetwork(confDir string, binDirs []string) (*cniNetwork, error)
149157
case err != nil:
150158
return nil, err
151159
case len(files) == 0:
152-
return nil, fmt.Errorf("No networks found in %s", confDir)
160+
return nil, fmt.Errorf("no networks found in %s", confDir)
153161
}
154162

163+
cniConfig := &libcni.CNIConfig{Path: binDirs}
164+
155165
sort.Strings(files)
156166
for _, confFile := range files {
157167
var confList *libcni.NetworkConfigList
@@ -185,16 +195,24 @@ func getDefaultCNINetwork(confDir string, binDirs []string) (*cniNetwork, error)
185195
continue
186196
}
187197

198+
// Before using this CNI config, we have to validate it to make sure that
199+
// all plugins of this config exist on disk
200+
caps, err := cniConfig.ValidateNetworkList(context.TODO(), confList)
201+
if err != nil {
202+
klog.Warningf("Error validating CNI config %v: %v", confList, err)
203+
continue
204+
}
205+
188206
klog.V(4).Infof("Using CNI configuration file %s", confFile)
189207

190-
network := &cniNetwork{
208+
return &cniNetwork{
191209
name: confList.Name,
192210
NetworkConfig: confList,
193-
CNIConfig: &libcni.CNIConfig{Path: binDirs},
194-
}
195-
return network, nil
211+
CNIConfig: cniConfig,
212+
Capabilities: caps,
213+
}, nil
196214
}
197-
return nil, fmt.Errorf("No valid networks found in %s", confDir)
215+
return nil, fmt.Errorf("no valid networks found in %s", confDir)
198216
}
199217

200218
func (plugin *cniNetworkPlugin) Init(host network.Host, hairpinMode kubeletconfig.HairpinMode, nonMasqueradeCIDR string, mtu int) error {
@@ -236,18 +254,13 @@ func (plugin *cniNetworkPlugin) setDefaultNetwork(n *cniNetwork) {
236254

237255
func (plugin *cniNetworkPlugin) checkInitialized() error {
238256
if plugin.getDefaultNetwork() == nil {
239-
return errors.New("cni config uninitialized")
257+
return fmt.Errorf("cni config uninitialized")
240258
}
241259

242-
// If the CNI configuration has the ipRanges capability, we need a PodCIDR assigned
243-
for _, p := range plugin.getDefaultNetwork().NetworkConfig.Plugins {
244-
if p.Network.Capabilities["ipRanges"] {
245-
if plugin.podCidr == "" {
246-
return errors.New("no PodCIDR set")
247-
}
248-
break
249-
}
260+
if utilslice.ContainsString(plugin.getDefaultNetwork().Capabilities, ipRangesCapability, nil) && plugin.podCidr == "" {
261+
return fmt.Errorf("cni config needs ipRanges but no PodCIDR set")
250262
}
263+
251264
return nil
252265
}
253266

@@ -395,12 +408,12 @@ func (plugin *cniNetworkPlugin) buildCNIRuntimeConf(podName string, podNs string
395408
})
396409
}
397410
rt.CapabilityArgs = map[string]interface{}{
398-
"portMappings": portMappingsParam,
411+
portMappingsCapability: portMappingsParam,
399412
}
400413

401414
ingress, egress, err := bandwidth.ExtractPodBandwidthResources(annotations)
402415
if err != nil {
403-
return nil, fmt.Errorf("Error reading pod bandwidth annotations: %v", err)
416+
return nil, fmt.Errorf("failed to get pod bandwidth from annotations: %v", err)
404417
}
405418
if ingress != nil || egress != nil {
406419
bandwidthParam := cniBandwidthEntry{}
@@ -415,11 +428,11 @@ func (plugin *cniNetworkPlugin) buildCNIRuntimeConf(podName string, podNs string
415428
bandwidthParam.EgressRate = int(egress.Value())
416429
bandwidthParam.EgressBurst = math.MaxInt32 // no limit
417430
}
418-
rt.CapabilityArgs["bandwidth"] = bandwidthParam
431+
rt.CapabilityArgs[bandwidthCapability] = bandwidthParam
419432
}
420433

421434
// Set the PodCIDR
422-
rt.CapabilityArgs["ipRanges"] = [][]cniIPRange{{{Subnet: plugin.podCidr}}}
435+
rt.CapabilityArgs[ipRangesCapability] = [][]cniIPRange{{{Subnet: plugin.podCidr}}}
423436

424437
// Set dns capability args.
425438
if dnsOptions, ok := options["dns"]; ok {
@@ -429,7 +442,7 @@ func (plugin *cniNetworkPlugin) buildCNIRuntimeConf(podName string, podNs string
429442
return nil, fmt.Errorf("failed to unmarshal dns config %q: %v", dnsOptions, err)
430443
}
431444
if dnsParam := buildDNSCapabilities(&dnsConfig); dnsParam != nil {
432-
rt.CapabilityArgs["dns"] = *dnsParam
445+
rt.CapabilityArgs[dnsCapability] = *dnsParam
433446
}
434447
}
435448

0 commit comments

Comments
 (0)