Skip to content

Commit 0d1ce9f

Browse files
committed
feat: add GlobalPodSecurity config plumbing
1 parent bd3841a commit 0d1ce9f

7 files changed

Lines changed: 118 additions & 0 deletions

File tree

cni/azure-linux.conflist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"type":"azure-vnet",
77
"mode":"transparent",
88
"ipsToRouteViaHost":["169.254.20.10"],
9+
"globalPodSecurity":false,
910
"ipam":{
1011
"type":"azure-vnet-ipam"
1112
}

cni/azure-windows.conflist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"plugins": [
66
{
77
"type": "azure-vnet",
8+
"globalPodSecurity": false,
89
"capabilities": {
910
"portMappings": true,
1011
"dns": true

cni/netconfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type NetworkConfig struct {
7373
DisableHairpinOnHostInterface bool `json:"disableHairpinOnHostInterface,omitempty"`
7474
DisableIPTableLock bool `json:"disableIPTableLock,omitempty"`
7575
DisableAsyncDelete bool `json:"disableAsyncDelete,omitempty"`
76+
GlobalPodSecurity bool `json:"globalPodSecurity,omitempty"`
7677
CNSUrl string `json:"cnsurl,omitempty"`
7778
ExecutionMode string `json:"executionMode,omitempty"`
7879
IPAM IPAM `json:"ipam,omitempty"`

cni/netconfig_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2017 Microsoft. All rights reserved.
2+
// MIT License
3+
4+
package cni
5+
6+
import (
7+
"encoding/json"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestNetworkConfigGlobalPodSecurity(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
jsonInput string
18+
expected bool
19+
}{
20+
{
21+
name: "GlobalPodSecurity set to true",
22+
jsonInput: `{"globalPodSecurity": true}`,
23+
expected: true,
24+
},
25+
{
26+
name: "GlobalPodSecurity set to false",
27+
jsonInput: `{"globalPodSecurity": false}`,
28+
expected: false,
29+
},
30+
{
31+
name: "GlobalPodSecurity omitted defaults to false",
32+
jsonInput: `{"name": "test"}`,
33+
expected: false,
34+
},
35+
}
36+
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
var nwCfg NetworkConfig
40+
err := json.Unmarshal([]byte(tt.jsonInput), &nwCfg)
41+
require.NoError(t, err)
42+
assert.Equal(t, tt.expected, nwCfg.GlobalPodSecurity)
43+
})
44+
}
45+
}

cni/network/network.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ func (plugin *NetPlugin) createEpInfo(opt *createEpInfoOpt) (*network.EndpointIn
743743
IPsToRouteViaHost: opt.nwCfg.IPsToRouteViaHost,
744744
EnableSnatOnHost: opt.nwCfg.EnableSnatOnHost,
745745
EnableMultiTenancy: opt.nwCfg.MultiTenancy,
746+
GlobalPodSecurity: opt.nwCfg.GlobalPodSecurity,
746747
EnableInfraVnet: opt.enableInfraVnet,
747748
EnableSnatForDns: opt.enableSnatForDNS,
748749
PODName: opt.k8sPodName,

cni/network/network_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,3 +1849,70 @@ func TestValidateArgs(t *testing.T) {
18491849
})
18501850
}
18511851
}
1852+
1853+
func TestCreateEpInfoGlobalPodSecurity(t *testing.T) {
1854+
p, _ := cni.NewPlugin("name", "0.3.0")
1855+
1856+
tests := []struct {
1857+
name string
1858+
globalPodSecurity bool
1859+
}{
1860+
{
1861+
name: "GlobalPodSecurity enabled",
1862+
globalPodSecurity: true,
1863+
},
1864+
{
1865+
name: "GlobalPodSecurity disabled",
1866+
globalPodSecurity: false,
1867+
},
1868+
}
1869+
1870+
for _, tt := range tests {
1871+
t.Run(tt.name, func(t *testing.T) {
1872+
testNwCfg := &cni.NetworkConfig{
1873+
Master: eth0IfName,
1874+
GlobalPodSecurity: tt.globalPodSecurity,
1875+
}
1876+
infraSeen := false
1877+
plugin := &NetPlugin{
1878+
Plugin: p,
1879+
nm: acnnetwork.NewMockNetworkmanager(acnnetwork.NewMockEndpointClient(nil)),
1880+
netClient: &InterfaceGetterMock{
1881+
interfaces: []net.Interface{
1882+
{
1883+
Name: eth0IfName,
1884+
},
1885+
},
1886+
},
1887+
}
1888+
opt := &createEpInfoOpt{
1889+
nwCfg: testNwCfg,
1890+
ipamAddConfig: &IPAMAddConfig{
1891+
nwCfg: testNwCfg,
1892+
args: &cniSkel.CmdArgs{
1893+
ContainerID: "test-container",
1894+
Netns: "test-netns",
1895+
IfName: eth0IfName,
1896+
},
1897+
},
1898+
args: &cniSkel.CmdArgs{
1899+
ContainerID: "test-container",
1900+
Netns: "test-netns",
1901+
IfName: eth0IfName,
1902+
},
1903+
ifInfo: &acnnetwork.InterfaceInfo{
1904+
NICType: cns.InfraNIC,
1905+
HostSubnetPrefix: net.IPNet{
1906+
IP: net.ParseIP("10.0.0.0"),
1907+
Mask: net.CIDRMask(24, 32),
1908+
},
1909+
},
1910+
infraSeen: &infraSeen,
1911+
}
1912+
1913+
epInfo, err := plugin.createEpInfo(opt)
1914+
require.NoError(t, err)
1915+
assert.Equal(t, tt.globalPodSecurity, epInfo.GlobalPodSecurity)
1916+
})
1917+
}
1918+
}

network/endpoint.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type endpoint struct {
4646
EnableSnatOnHost bool
4747
EnableInfraVnet bool
4848
EnableMultitenancy bool
49+
GlobalPodSecurity bool
4950
AllowInboundFromHostToNC bool
5051
AllowInboundFromNCToHost bool
5152
NetworkContainerID string
@@ -81,6 +82,7 @@ type EndpointInfo struct {
8182
EnableSnatOnHost bool
8283
EnableInfraVnet bool
8384
EnableMultiTenancy bool
85+
GlobalPodSecurity bool
8486
EnableSnatForDns bool
8587
AllowInboundFromHostToNC bool
8688
AllowInboundFromNCToHost bool

0 commit comments

Comments
 (0)