-
Notifications
You must be signed in to change notification settings - Fork 260
fix: Use PortMappingPolicySetting #689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ashvindeodhar
merged 1 commit into
Azure:master
from
adelina-t:usePortMappingPolicySettings
Nov 9, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -3,19 +3,18 @@ package policy | |
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/Azure/azure-container-networking/log" | ||
| "github.com/Microsoft/hcsshim" | ||
| "github.com/Microsoft/hcsshim/hcn" | ||
| ) | ||
|
|
||
| const ( | ||
| // protocolTcp indicates tcp protocol id for portmapping | ||
| protocolTcp = 6 | ||
| // ProtocolTcp indicates tcp protocol id for portmapping | ||
| ProtocolTcp = 6 | ||
|
|
||
| // protocolUdp indicates udp protocol id for portmapping | ||
| protocolUdp = 17 | ||
| // ProtocolUdp indicates udp protocol id for portmapping | ||
| ProtocolUdp = 17 | ||
|
|
||
| // CnetAddressSpace indicates constant for the key string | ||
| CnetAddressSpace = "cnetAddressSpace" | ||
|
|
@@ -27,13 +26,6 @@ type KVPairRoutePolicy struct { | |
| NeedEncap json.RawMessage `json:"NeedEncap"` | ||
| } | ||
|
|
||
| type KVPairPortMapping struct { | ||
| Type CNIPolicyType `json:"Type"` | ||
| ExternalPort uint16 `json:"ExternalPort"` | ||
| InternalPort uint16 `json:"InternalPort"` | ||
| Protocol string `json:"Protocol"` | ||
| } | ||
|
|
||
| type KVPairOutBoundNAT struct { | ||
| Type CNIPolicyType `json:"Type"` | ||
| ExceptionList json.RawMessage `json:"ExceptionList"` | ||
|
|
@@ -64,6 +56,13 @@ func SerializePolicies(policyType CNIPolicyType, policies []Policy, epInfoData m | |
| jsonPolicies = append(jsonPolicies, serializedOutboundNatPolicy) | ||
| } | ||
| } | ||
| } else if isPolicyTypeNAT := IsPolicyTypeNAT(policy); isPolicyTypeNAT { | ||
| // NATPolicy comes as a HNSv2 type, it needs to be converted to HNSv1 | ||
| if serializedNatPolicy, err := SerializeNATPolicy(policy); err != nil { | ||
| log.Printf("Failed to serialize NatPolicy") | ||
| } else { | ||
| jsonPolicies = append(jsonPolicies, serializedNatPolicy) | ||
| } | ||
| } else { | ||
| jsonPolicies = append(jsonPolicies, policy.Data) | ||
| } | ||
|
|
@@ -117,6 +116,45 @@ func IsPolicyTypeOutBoundNAT(policy Policy) bool { | |
| return false | ||
| } | ||
|
|
||
| // IsPolicyTypeNAT returns true if the policy type is NAT | ||
| func IsPolicyTypeNAT(policy Policy) bool { | ||
| if policy.Type == EndpointPolicy { | ||
| var endpointPolicy hcn.EndpointPolicy | ||
| if err := json.Unmarshal(policy.Data, &endpointPolicy); err != nil { | ||
| return false | ||
| } | ||
| if endpointPolicy.Type == hcn.PortMapping { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func SerializeNATPolicy(policy Policy) (json.RawMessage, error) { | ||
| var ( | ||
| endpointPolicy hcn.EndpointPolicy | ||
| portMappingPolicy hcn.PortMappingPolicySetting | ||
| ) | ||
| if err := json.Unmarshal(policy.Data, &endpointPolicy); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := json.Unmarshal(endpointPolicy.Settings, &portMappingPolicy); err != nil { | ||
| return nil, err | ||
| } | ||
| natPolicy := hcsshim.NatPolicy{ | ||
| Type: "NAT", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use constant here.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| InternalPort: portMappingPolicy.InternalPort, | ||
| ExternalPort: portMappingPolicy.ExternalPort, | ||
| } | ||
| switch portMappingPolicy.Protocol { | ||
| case ProtocolTcp: | ||
| natPolicy.Protocol = "TCP" | ||
| case ProtocolUdp: | ||
| natPolicy.Protocol = "UDP" | ||
| } | ||
| return json.Marshal(natPolicy) | ||
| } | ||
|
|
||
| // SerializeOutBoundNATPolicy formulates OutBoundNAT policy and returns serialized json | ||
| func SerializeOutBoundNATPolicy(policy Policy, epInfoData map[string]interface{}) (json.RawMessage, error) { | ||
| outBoundNatPolicy := hcsshim.OutboundNatPolicy{} | ||
|
|
@@ -169,9 +207,9 @@ func GetPolicyType(policy Policy) CNIPolicyType { | |
| } | ||
|
|
||
| // Check if the type if Port mapping / NAT | ||
| var dataPortMapping KVPairPortMapping | ||
| var dataPortMapping hcn.EndpointPolicy | ||
| if err := json.Unmarshal(policy.Data, &dataPortMapping); err == nil { | ||
| if dataPortMapping.Type == PortMappingPolicy { | ||
| if dataPortMapping.Type == hcn.PortMapping { | ||
| return PortMappingPolicy | ||
| } | ||
| } | ||
|
|
@@ -308,37 +346,12 @@ func GetHcnRoutePolicy(policy Policy) (hcn.EndpointPolicy, error) { | |
|
|
||
| // GetHcnPortMappingPolicy returns port mapping policy. | ||
| func GetHcnPortMappingPolicy(policy Policy) (hcn.EndpointPolicy, error) { | ||
| portMappingPolicy := hcn.EndpointPolicy{ | ||
ashvindeodhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Type: hcn.PortMapping, | ||
| } | ||
|
|
||
| var dataPortMapping KVPairPortMapping | ||
| if err := json.Unmarshal(policy.Data, &dataPortMapping); err != nil { | ||
| var portMappingPolicy hcn.EndpointPolicy | ||
| if err := json.Unmarshal(policy.Data, &portMappingPolicy); err != nil { | ||
| return portMappingPolicy, | ||
| fmt.Errorf("Invalid policy: %+v. Expecting PortMapping policy. Error: %v", policy, err) | ||
| } | ||
|
|
||
| portMappingPolicySetting := &hcn.PortMappingPolicySetting{ | ||
| InternalPort: dataPortMapping.InternalPort, | ||
| ExternalPort: dataPortMapping.ExternalPort, | ||
| } | ||
|
|
||
| protocol := strings.ToUpper(strings.TrimSpace(dataPortMapping.Protocol)) | ||
| switch protocol { | ||
| case "TCP": | ||
| portMappingPolicySetting.Protocol = protocolTcp | ||
| case "UDP": | ||
| portMappingPolicySetting.Protocol = protocolUdp | ||
| default: | ||
| return portMappingPolicy, fmt.Errorf("Invalid protocol: %s for port mapping", protocol) | ||
| } | ||
|
|
||
| portMappingPolicySettingBytes, err := json.Marshal(portMappingPolicySetting) | ||
| if err != nil { | ||
| return portMappingPolicy, err | ||
| } | ||
|
|
||
| portMappingPolicy.Settings = portMappingPolicySettingBytes | ||
| portMappingPolicy.Type = hcn.PortMapping | ||
|
|
||
| return portMappingPolicy, nil | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also share what all testing has been performed? Just containerd or docker has been tested to make HnsV1 is working as earlier too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please confirm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically the portmapping scenario