forked from aws/amazon-vpc-cni-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
windows: add support for port mapping in vpc-bridge plugin
Presently, the vpc-bridge plugin silently ignores any port mapping between container to host for the configured endpoint. As per the current CNI convention, plugins can request that the runtime insert this dynamic configuration by explicitly listing their capabilities in the network configuration. Dynamic information (i.e. data that a runtime fills out) should be placed in a runtimeConfig section. Reference: https://www.cni.dev/docs/conventions/#dynamic-plugin-specific-fields-capabilities--runtime-configuration `portMappings` is one such capability. This change adds the support for creating NAT port mappings between container and host ports as configured by the runtime.
- Loading branch information
Showing
7 changed files
with
234 additions
and
92 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
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,43 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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 vpc | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// protocolTCP indicates TCP protocol number for port mapping. | ||
protocolTCP uint32 = 6 | ||
// protocolUDP indicates UDP protocol number for port mapping. | ||
protocolUDP uint32 = 17 | ||
) | ||
|
||
// ProtocolToNumber converts the protocol to it's assigned number. | ||
// Reference: https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml | ||
func ProtocolToNumber(protocol string) (uint32, error) { | ||
var protocolNumber uint32 | ||
switch strings.ToUpper(protocol) { | ||
case "TCP": | ||
protocolNumber = protocolTCP | ||
case "UDP": | ||
protocolNumber = protocolUDP | ||
default: | ||
// Protocol number 256 is invalid and therefore, returned with the error. | ||
return 256, fmt.Errorf("unsupported protocol for portmapping: %s", protocol) | ||
} | ||
|
||
return protocolNumber, nil | ||
} |
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,53 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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. | ||
|
||
//go:build unit_test | ||
|
||
package vpc | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestProtocolToNumberTCP tests the conversion of TCP protocol to number | ||
// when the protocol is either in upper or lower case. | ||
func TestProtocolToNumberTCP(t *testing.T) { | ||
protocolToTest := []string{"TCP", "tcp"} | ||
|
||
for _, testProtocol := range protocolToTest { | ||
actualProtocolNumber, err := ProtocolToNumber(testProtocol) | ||
assert.Equal(t, protocolTCP, actualProtocolNumber) | ||
assert.NoError(t, err) | ||
} | ||
} | ||
|
||
// TestProtocolToNumberUDP tests the conversion of UDP protocol to number | ||
// when the protocol is either in upper or lower case. | ||
func TestProtocolToNumberUDP(t *testing.T) { | ||
protocolToTest := []string{"UDP", "udp"} | ||
|
||
for _, testProtocol := range protocolToTest { | ||
actualProtocolNumber, err := ProtocolToNumber(testProtocol) | ||
assert.Equal(t, protocolUDP, actualProtocolNumber) | ||
assert.NoError(t, err) | ||
} | ||
} | ||
|
||
// TestProtocolToNumberFailure tests the failure case with invalid protocol. | ||
func TestProtocolToNumberFailure(t *testing.T) { | ||
actualProtocolNumber, err := ProtocolToNumber("ICMP") | ||
assert.Equal(t, uint32(256), actualProtocolNumber) | ||
assert.Error(t, err) | ||
} |
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
Oops, something went wrong.