Skip to content
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

Network policy | For out-of-cluster access, use toEntities:world instead of toCIDR #486

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions src/networkpolicy/networkPolicy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package networkpolicy

import (
"net"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -234,7 +233,6 @@ type IcmpPortPair struct {

func getDst(log types.KnoxNetworkLog, services []types.Service, cidrBits int) (Dst, bool) {
var httpInfo string
var labels []string

// check HTTP
if log.HTTPMethod != "" && log.HTTPPath != "" {
Expand All @@ -255,35 +253,37 @@ func getDst(log types.KnoxNetworkLog, services []types.Service, cidrBits int) (D
}

if log.DstPodName == "" {
// check CIDR (out of cluster)
if libs.ContainsElement(log.DstReservedLabels, ReservedWorld) && log.DstIP != "" {
cidr := ""
if svc, valid := checkK8sService(log, services); valid {
// 1. check if the dst IP belongs to a service
log.DstNamespace = svc.Namespace
for k, v := range svc.Selector {
labels = append(labels, k+"="+v)
/*
// check CIDR (out of cluster)
if libs.ContainsElement(log.DstReservedLabels, ReservedWorld) && log.DstIP != "" {
cidr := ""
if svc, valid := checkK8sService(log, services); valid {
// 1. check if the dst IP belongs to a service
log.DstNamespace = svc.Namespace
for k, v := range svc.Selector {
labels = append(labels, k+"="+v)
}
} else {
// 3. else, handle it as cidr policy
log.DstNamespace = "reserved:cidr"
ipNetwork := log.DstIP + "/" + strconv.Itoa(cidrBits)
_, network, _ := net.ParseCIDR(ipNetwork)
cidr = network.String()
}
} else {
// 3. else, handle it as cidr policy
log.DstNamespace = "reserved:cidr"
ipNetwork := log.DstIP + "/" + strconv.Itoa(cidrBits)
_, network, _ := net.ParseCIDR(ipNetwork)
cidr = network.String()
}

dst := Dst{
Namespace: log.DstNamespace,
Additional: cidr,
Protocol: log.Protocol,
DstPort: log.DstPort,
ICMPType: log.ICMPType,
MatchLabels: strings.Join(labels, ","),
HTTP: httpInfo,
}
dst := Dst{
Namespace: log.DstNamespace,
Additional: cidr,
Protocol: log.Protocol,
DstPort: log.DstPort,
ICMPType: log.ICMPType,
MatchLabels: strings.Join(labels, ","),
HTTP: httpInfo,
}

return dst, true
}
return dst, true
}
*/

// reserved entities -> host, remote-node, kube-apiserver
if len(log.DstReservedLabels) > 0 {
Expand Down