-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/cluster: raise error if host-ports are configured again with same…
… addr and protocol
- Loading branch information
1 parent
49c8845
commit 024b73f
Showing
3 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package common | ||
|
||
// AddressPortProtocolMap is used to keep track of declared listen address, | ||
// port and protocol in node extra port mappings. | ||
type AddressPortProtocolMap map[string]map[int32]map[string]struct{} | ||
|
||
// IsAddrPortProtocolReconfigured checks if a combination of listen address, port and protocol exists in the given map. | ||
// Also takes care of special cases like 0.0.0.0 wildcard address. | ||
func IsAddrPortProtocolReconfigured(addrPortProtocolMap AddressPortProtocolMap, addr string, port int32, protocol string) bool { | ||
existsInMap := func(addr string, port int32, protocol string) bool { | ||
var ok bool | ||
if _, ok = addrPortProtocolMap[addr]; ok { | ||
if _, ok = addrPortProtocolMap[addr][port]; ok { | ||
if _, ok = addrPortProtocolMap[addr][port][protocol]; ok { | ||
ok = true | ||
} | ||
} | ||
} | ||
return ok | ||
} | ||
|
||
// base case - direct existence check for listen address, port and protocol | ||
if existsInMap(addr, port, protocol) { | ||
return true | ||
} | ||
|
||
wildcardAddress := "0.0.0.0" | ||
// iterate over all addrs, to check if any non-wildcard addr was declared with same port and protocol. | ||
if addr == wildcardAddress { | ||
for key := range addrPortProtocolMap { | ||
if key != wildcardAddress && existsInMap(key, port, protocol) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// check if port and protocol combination is already defined for 0.0.0.0 address | ||
return existsInMap(wildcardAddress, port, protocol) | ||
|
||
} | ||
|
||
// AddAddrPortProtocolToMap adds combination of listen address, port and protocol to the map. | ||
func AddAddrPortProtocolToMap(addrPortProtocolMap AddressPortProtocolMap, addr string, port int32, protocol string) { | ||
var ok bool | ||
if _, ok = addrPortProtocolMap[addr]; ok { | ||
if _, ok = addrPortProtocolMap[addr][port]; ok { | ||
if _, ok = addrPortProtocolMap[addr][port]; ok { | ||
addrPortProtocolMap[addr][port][protocol] = struct{}{} | ||
} | ||
} else { | ||
addrPortProtocolMap[addr][port] = make(map[string]struct{}) | ||
addrPortProtocolMap[addr][port][protocol] = struct{}{} | ||
} | ||
} else { | ||
addrPortProtocolMap[addr] = make(map[int32]map[string]struct{}) | ||
addrPortProtocolMap[addr][port] = make(map[string]struct{}) | ||
addrPortProtocolMap[addr][port][protocol] = struct{}{} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"reflect" | ||
"sigs.k8s.io/kind/pkg/internal/assert" | ||
"testing" | ||
) | ||
|
||
func TestAddAddrPortProtocolToMap(t *testing.T) { | ||
addrPortProtocolMap := make(AddressPortProtocolMap) | ||
|
||
AddAddrPortProtocolToMap(addrPortProtocolMap, "127.0.0.1", int32(80), "UDP") | ||
assert.BoolEqual(t, true, reflect.DeepEqual(addrPortProtocolMap, AddressPortProtocolMap{ | ||
"127.0.0.1": { | ||
int32(80): { | ||
"UDP": struct{}{}, | ||
}, | ||
}, | ||
})) | ||
|
||
AddAddrPortProtocolToMap(addrPortProtocolMap, "127.0.0.1", int32(80), "TCP") | ||
assert.BoolEqual(t, true, reflect.DeepEqual(addrPortProtocolMap, AddressPortProtocolMap{ | ||
"127.0.0.1": { | ||
int32(80): { | ||
"UDP": struct{}{}, | ||
"TCP": struct{}{}, | ||
}, | ||
}, | ||
})) | ||
|
||
AddAddrPortProtocolToMap(addrPortProtocolMap, "0.0.0.0", int32(5000), "UDP") | ||
assert.BoolEqual(t, true, reflect.DeepEqual(addrPortProtocolMap, AddressPortProtocolMap{ | ||
"127.0.0.1": { | ||
int32(80): { | ||
"UDP": struct{}{}, | ||
"TCP": struct{}{}, | ||
}, | ||
}, | ||
"0.0.0.0": { | ||
int32(5000): { | ||
"UDP": struct{}{}, | ||
}, | ||
}, | ||
})) | ||
|
||
AddAddrPortProtocolToMap(addrPortProtocolMap, "0.0.0.0", int32(3000), "SCTP") | ||
assert.BoolEqual(t, true, reflect.DeepEqual(addrPortProtocolMap, AddressPortProtocolMap{ | ||
"127.0.0.1": { | ||
int32(80): { | ||
"UDP": struct{}{}, | ||
"TCP": struct{}{}, | ||
}, | ||
}, | ||
"0.0.0.0": { | ||
int32(5000): { | ||
"UDP": struct{}{}, | ||
}, | ||
int32(3000): { | ||
"SCTP": struct{}{}, | ||
}, | ||
}, | ||
})) | ||
} | ||
|
||
func TestIsAddrPortProtocolRedeclared(t *testing.T) { | ||
addrPortProtocolMap := make(AddressPortProtocolMap) | ||
|
||
testCases := []struct { | ||
addr string | ||
port int | ||
protocol string | ||
expected bool | ||
}{ | ||
{"127.0.0.1", 80, "UDP", false}, | ||
{"127.0.0.1", 80, "TCP", false}, | ||
|
||
// expected true: same addr, port and protocol | ||
{"127.0.0.1", 80, "UDP", true}, | ||
|
||
// should return true: subset of 0.0.0.0[127.0.0.1] is already defined for same port and protocol | ||
{"0.0.0.0", 80, "UDP", true}, | ||
|
||
// should return true: subset of 0.0.0.0[127.0.0.1] is already defined for same port and protocol | ||
{"0.0.0.0", 80, "TCP", true}, | ||
|
||
{"0.0.0.0", 3000, "UDP", false}, | ||
{"0.0.0.0", 3000, "TCP", false}, | ||
|
||
// should return true: same addr, port and protocol | ||
{"0.0.0.0", 3000, "UDP", true}, | ||
|
||
// should return true: same port and protocol is already defined for wildcard interface - 0.0.0.0 | ||
{"127.0.0.1", 3000, "UDP", true}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
assert.BoolEqual(t, testCase.expected, | ||
IsAddrPortProtocolReconfigured(addrPortProtocolMap, testCase.addr, int32(testCase.port), testCase.protocol)) | ||
|
||
AddAddrPortProtocolToMap(addrPortProtocolMap, testCase.addr, int32(testCase.port), testCase.protocol) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters