Skip to content

Commit fadbf1d

Browse files
authored
fix: set hostsubnetprefix for overlay (#1414)
1 parent deb0c0c commit fadbf1d

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

cni/network/invoker_cns.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ import (
2020
)
2121

2222
var (
23-
errEmtpyHostSubnetPrefix = errors.New("empty host subnet prefix not allowed")
24-
errEmptyCNIArgs = errors.New("empty CNI cmd args not allowed")
25-
errInvalidArgs = errors.New("invalid arg(s)")
23+
errEmptyCNIArgs = errors.New("empty CNI cmd args not allowed")
24+
errInvalidArgs = errors.New("invalid arg(s)")
2625
)
2726

2827
type CNSIPAMInvoker struct {
@@ -100,7 +99,7 @@ func (invoker *CNSIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, erro
10099
log.Printf("[cni-invoker-cns] Received info %+v for pod %v", info, podInfo)
101100

102101
ncgw := net.ParseIP(info.ncGatewayIPAddress)
103-
if ncgw == nil {
102+
if ncgw == nil && invoker.ipamMode != util.V4Overlay {
104103
return IPAMAddResult{}, errors.Wrap(errInvalidArgs, "%w: Gateway address "+info.ncGatewayIPAddress+" from response is invalid")
105104
}
106105

@@ -133,29 +132,26 @@ func (invoker *CNSIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, erro
133132
},
134133
}
135134

135+
// get the name of the primary IP address
136+
_, hostIPNet, err := net.ParseCIDR(info.hostSubnet)
137+
if err != nil {
138+
return IPAMAddResult{}, fmt.Errorf("unable to parse hostSubnet: %w", err)
139+
}
140+
141+
addResult.hostSubnetPrefix = *hostIPNet
142+
136143
// set subnet prefix for host vm
137144
// setHostOptions will execute if IPAM mode is not v4 overlay
138145
if invoker.ipamMode != util.V4Overlay {
139-
if err := setHostOptions(&addResult.hostSubnetPrefix, ncipnet, addConfig.options, &info); err != nil {
146+
if err := setHostOptions(ncipnet, addConfig.options, &info); err != nil {
140147
return IPAMAddResult{}, err
141148
}
142149
}
143150

144151
return addResult, nil
145152
}
146153

147-
func setHostOptions(hostSubnetPrefix, ncSubnetPrefix *net.IPNet, options map[string]interface{}, info *IPv4ResultInfo) error {
148-
// get the name of the primary IP address
149-
_, hostIPNet, err := net.ParseCIDR(info.hostSubnet)
150-
if err != nil {
151-
return err
152-
}
153-
154-
if hostSubnetPrefix == nil {
155-
return errEmtpyHostSubnetPrefix
156-
}
157-
158-
*hostSubnetPrefix = *hostIPNet
154+
func setHostOptions(ncSubnetPrefix *net.IPNet, options map[string]interface{}, info *IPv4ResultInfo) error {
159155

160156
// get the host ip
161157
hostIP := net.ParseIP(info.hostPrimaryIP)

cni/network/invoker_cns_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func Test_setHostOptions(t *testing.T) {
305305
for _, tt := range tests {
306306
tt := tt
307307
t.Run(tt.name, func(t *testing.T) {
308-
err := setHostOptions(tt.args.hostSubnetPrefix, tt.args.ncSubnetPrefix, tt.args.options, &tt.args.info)
308+
err := setHostOptions(tt.args.ncSubnetPrefix, tt.args.options, &tt.args.info)
309309
if tt.wantErr {
310310
require.Error(err)
311311
return

0 commit comments

Comments
 (0)