generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 24
enable network policies strict mode #268
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
Open
aojea
wants to merge
4
commits into
kubernetes-sigs:main
Choose a base branch
from
aojea:strict_mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+676
−49
Open
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
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| apiVersion: networking.k8s.io/v1 | ||
| kind: NetworkPolicy | ||
| metadata: | ||
| name: allow-all-ingress | ||
| spec: | ||
| podSelector: {} | ||
| ingress: | ||
| - {} | ||
| policyTypes: | ||
| - Ingress |
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| apiVersion: networking.k8s.io/v1 | ||
| kind: NetworkPolicy | ||
| metadata: | ||
| name: default-deny-all | ||
| spec: | ||
| podSelector: {} | ||
| policyTypes: | ||
| - Ingress | ||
| - Egress |
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
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
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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package dataplane | ||
|
|
||
| import ( | ||
| "github.com/vishvananda/netlink" | ||
| "golang.org/x/sys/unix" | ||
| v1 "k8s.io/api/core/v1" | ||
| "k8s.io/klog/v2" | ||
| "sigs.k8s.io/kube-network-policies/pkg/network" | ||
| ) | ||
|
|
||
| var ( | ||
| mapIPFamilyToString = map[uint8]v1.IPFamily{ | ||
| unix.AF_INET: v1.IPv4Protocol, | ||
| unix.AF_INET6: v1.IPv6Protocol, | ||
| } | ||
| mapProtocolToString = map[uint8]v1.Protocol{ | ||
| unix.IPPROTO_TCP: v1.ProtocolTCP, | ||
| unix.IPPROTO_UDP: v1.ProtocolUDP, | ||
| unix.IPPROTO_SCTP: v1.ProtocolSCTP, | ||
| } | ||
| ) | ||
|
|
||
| func PacketFromFlow(flow *netlink.ConntrackFlow) *network.Packet { | ||
| if flow == nil { | ||
| return nil | ||
| } | ||
| packet := network.Packet{ | ||
| SrcIP: flow.Forward.SrcIP, | ||
| DstIP: flow.Reverse.SrcIP, | ||
| SrcPort: int(flow.Forward.SrcPort), | ||
| DstPort: int(flow.Reverse.SrcPort), | ||
| } | ||
|
|
||
| if family, ok := mapIPFamilyToString[flow.FamilyType]; ok { | ||
| packet.Family = family | ||
| } else { | ||
| klog.InfoS("Unknown IP family", "family", flow.FamilyType, "flow", flow) | ||
| return nil | ||
| } | ||
|
|
||
| if protocol, ok := mapProtocolToString[flow.Forward.Protocol]; ok { | ||
| packet.Proto = protocol | ||
| } else { | ||
| klog.InfoS("Unknown protocol", "protocol", flow.Forward.Protocol, "flow", flow) | ||
| return nil | ||
| } | ||
|
|
||
| return &packet | ||
| } | ||
|
|
||
| // generateLabelMask creates a 16-byte (128-bit) mask with a single bit set at the | ||
| // specified bitIndex. | ||
| // If the bit index is out of the valid range [0, 127], it returns a 16-byte | ||
| // slice of all zeros. | ||
| // This function implements a Big Endia 128-bit layout. This means the | ||
| // most significant byte (containing bits 127-120) is at index 0 of the | ||
| // slice, and the least significant *byte* (containing bits 7-0) is at | ||
| // index 15. | ||
| func generateLabelMask(bitIndex int) []byte { | ||
| labelMask := make([]byte, 16) | ||
| if bitIndex < 0 || bitIndex > 127 { | ||
| return labelMask | ||
| } | ||
|
|
||
| arrayIndex := len(labelMask) - (bitIndex / 8) - 1 | ||
| bitPos := uint(bitIndex % 8) | ||
| mask := uint8(1) << bitPos | ||
| labelMask[arrayIndex] = mask | ||
| return labelMask | ||
| } | ||
|
|
||
| // clearLabelBit clears a specific bit in a 16-byte (128-bit) label and returns | ||
| // a new 16-byte slice with the modified label. The original slice (currentLabel) | ||
| // is not modified. | ||
| // If currentLabel is not 16 bytes long, it returns a new, empty 16-byte slice. | ||
| // If bitIndex is out of the valid range [0, 127], it returns a copy of the | ||
| // original label. | ||
| func clearLabelBit(currentLabel []byte, bitIndex int) []byte { | ||
| newLabel := make([]byte, 16) | ||
| if len(currentLabel) != 16 { | ||
| return newLabel | ||
| } | ||
|
|
||
| copy(newLabel, currentLabel) | ||
| if bitIndex < 0 || bitIndex > 127 { | ||
| return newLabel | ||
| } | ||
| arrayIndex := len(newLabel) - (bitIndex / 8) - 1 | ||
| bitPos := uint(bitIndex % 8) | ||
| zeroMask := ^(uint8(1) << bitPos) | ||
| newLabel[arrayIndex] &= zeroMask | ||
| return newLabel | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.