Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions clusterloader2/cmd/clusterloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
_ "k8s.io/perf-tests/clusterloader2/pkg/measurement/common/dns"
_ "k8s.io/perf-tests/clusterloader2/pkg/measurement/common/network"
_ "k8s.io/perf-tests/clusterloader2/pkg/measurement/common/network-policy"
_ "k8s.io/perf-tests/clusterloader2/pkg/measurement/common/network-policy/network-policy-soak"
_ "k8s.io/perf-tests/clusterloader2/pkg/measurement/common/probes"
_ "k8s.io/perf-tests/clusterloader2/pkg/measurement/common/slos"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ rules:
- apiGroups: ["networking.k8s.io"]
resources: ["networkpolicies"]
verbs: ["get"]
- apiGroups: ["cilium.io"]
resources: ["ciliumclusterwidenetworkpolicies"]
verbs: ["get", "list", "watch"]
- apiGroups: ["cilium.io"]
resources: ["ciliumnetworkpolicies"]
verbs: ["get", "list", "watch"]

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ spec:
name: npdelaymetrics
protocol: TCP
imagePullPolicy: Always
image: gcr.io/k8s-staging-perf-tests/network-policy-enforcement-latency/policy-creation-enforcement-latency:v0.0.1
# image: gcr.io/k8s-staging-perf-tests/network-policy-enforcement-latency/policy-creation-enforcement-latency:v0.0.1
image: docker.io/sanamsarath/policy-creation-enforcement-latency:v0.0.1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we using custom image here? If that's needed to add new commands, then we should publish it to ghcr and use image from there instead of docker.io

command:
- sh
- -c
Expand All @@ -46,6 +47,8 @@ spec:
-MaxTargets={{.MaxTargets}}
-MetricsPort={{.MetricsPort}}
-AllowPolicyName={{.AllowPolicyName}}
-np_type={{.NetworkPolicy_Type}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's follow proper format like the original arguments. For example, -NetworkPolicyType instead of np_type. Also, remove _ for consistency in naming, like NetworkPolicyType instead of NetworkPolicy_Type

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this repo have a style guide?

-np_namespace={{.Namespace}} # change this value according to the namespace where the policy is created, verify policy-egress-allow-target-pods.yaml
resources:
requests:
cpu: 200m
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,61 @@
{{ if eq .NetworkPolicy_Type "cnp" }}
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: {{.Name}}
namespace: {{.ClientNamespace}} # since this policy is egress, the namespace is the client namespace
labels:
type: {{.TypeLabelValue}}
spec:
endpointSelector:
matchLabels:
type: {{.TypeLabelValue}}
egress:
- toEndpoints:
- matchLabels:
net-pol-test: {{.TargetLabelValue}}
{{if .OnlyTargetNamespace}}
k8s:io.kubernetes.pod.namespace: {{.TargetNamespace}}
{{end}}
{{if .L7Enabled}}
toPorts:
- ports:
- port: "{{.TargetPort}}"
protocol: TCP
rules:
http:
- method: GET
path: /
{{end}}
{{else if .NetworkPolicy_Type "ccnp" }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite follow the if else here. Can you use indentation those to make the condition flow clearer?

apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
name: {{.Name}}
labels:
type: {{.TypeLabelValue}}
spec:
endpointSelector:
matchLabels:
type: {{.TypeLabelValue}}
egress:
- toEndpoints:
- matchLabels:
net-pol-test: {{.TargetLabelValue}}
{{if .OnlyTargetNamespace}}
k8s:io.kubernetes.pod.namespace: {{.TargetNamespace}}
{{end}}
{{if .L7Enabled}}
toPorts:
- ports:
- port: "{{.TargetPort}}"
protocol: TCP
rules:
http:
- method: GET
path: /
{{end}}
{{else}}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
Expand All @@ -22,4 +80,8 @@ spec:
kubernetes.io/metadata.name: {{.TargetNamespace}}
{{else}}
namespaceSelector: {}
ports:
- port: "{{.TargetPort}}"
protocol: TCP
{{end}}
{{end}}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"golang.org/x/time/rate"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"k8s.io/perf-tests/clusterloader2/pkg/framework"
Expand Down Expand Up @@ -111,6 +112,12 @@ type networkPolicyEnforcementMeasurement struct {
// testClientNodeSelectorValue is value key for the node label on which the
// test client pods should run.
testClientNodeSelectorValue string
// np type is the type of network policy to be created, default is k8s.
npType string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

netPolicyType instead of npType. We should avoid unclear variable names

// l7Enabled is a flag to enable L7 policies.
l7Enabled bool
// l7 port
l7Port int
}

// Execute - Available actions:
Expand Down Expand Up @@ -215,6 +222,21 @@ func (nps *networkPolicyEnforcementMeasurement) initializeMeasurement(config *me
return fmt.Errorf("cannot initialize the %q, no namespaces with prefix %q exist", networkPolicyEnforcementName, testNamespacePrefix)
}

// Get the network policy type
if nps.npType, err = util.GetStringOrDefault(config.Params, "networkPolicyType", "k8s"); err != nil {
return fmt.Errorf("failed to get network policy type, error: %v", err)
}

// Get the L7 enabled flag
if nps.l7Enabled, err = util.GetBoolOrDefault(config.Params, "l7Enabled", false); err != nil {
klog.Infof("Failed to get L7 enabled flag, error: %v", err)
}

// Get the L7 port
if nps.l7Port, err = util.GetIntOrDefault(config.Params, "l7Port", 80); err != nil {
klog.Infof("Failed to get L7 port, error: %v", err)
}

return nil
}

Expand Down Expand Up @@ -386,9 +408,10 @@ func (nps *networkPolicyEnforcementMeasurement) createPolicyAllowAPIServer() err

func (nps *networkPolicyEnforcementMeasurement) createPolicyToTargetPods(policyName, targetNamespace, testType string, allowForTargetPods bool) error {
templateMap := map[string]interface{}{
"Name": policyName,
"Namespace": nps.testClientNamespace,
"TypeLabelValue": testType,
"Name": policyName,
"Namespace": nps.testClientNamespace,
"TypeLabelValue": testType,
"ClientNamespace": nps.testClientNamespace,
}

if allowForTargetPods {
Expand All @@ -404,6 +427,14 @@ func (nps *networkPolicyEnforcementMeasurement) createPolicyToTargetPods(policyN
templateMap["OnlyTargetNamespace"] = false
}

templateMap["NetworkPolicy_Type"] = nps.npType

// if L7 enabled, then add the L7 parameters to templateMap
if nps.l7Enabled {
templateMap["L7Enabled"] = nps.l7Enabled
templateMap["TargetPort"] = nps.l7Port
}

if err := nps.framework.ApplyTemplatedManifests(manifestsFS, policyEgressTargetPodsFilePath, templateMap); err != nil {
return fmt.Errorf("error while creating allow egress to pods network policy: %v", err)
}
Expand All @@ -420,6 +451,7 @@ func (nps *networkPolicyEnforcementMeasurement) createTestClientDeployments(temp
templateMap["Name"] = fmt.Sprintf("%s-%s-%d", testType, netPolicyTestClientName, i)
templateMap["TargetNamespace"] = ns
templateMap["AllowPolicyName"] = fmt.Sprintf("%s-%d", allowPolicyName, i)
templateMap["NetworkPolicy_Type"] = nps.npType

if err := nps.framework.ApplyTemplatedManifests(manifestsFS, deploymentFilePath, templateMap); err != nil {
return fmt.Errorf("error while creating test client deployment: %v", err)
Expand Down Expand Up @@ -528,13 +560,55 @@ func (nps *networkPolicyEnforcementMeasurement) deleteClusterRoleAndBinding() er
return nps.k8sClient.RbacV1().ClusterRoleBindings().Delete(context.TODO(), netPolicyTestClientName, metav1.DeleteOptions{})
}

func (nps *networkPolicyEnforcementMeasurement) deleteNetworkPolicies() error {
klog.V(2).Infof("Deleting Cilium network policies for measurement %q", networkPolicyEnforcementName)

dynamicClient := nps.framework.GetDynamicClients().GetClient()

switch nps.npType {
case "k8s":
return nil
case "ccnp":
// Define the GVR for CiliumClusterwideNetworkPolicy
ccnpGVR := schema.GroupVersionResource{
Group: "cilium.io",
Version: "v2",
Resource: "ciliumclusterwidenetworkpolicies",
}

if err := dynamicClient.Resource(ccnpGVR).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: fmt.Sprintf("type=%s", policyCreationTest)}); err != nil {
klog.Warningf("failed to delete CiliumClusterwideNetworkPolicy of type %s, error: %v", policyCreationTest, err)
}

if err := dynamicClient.Resource(ccnpGVR).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: fmt.Sprintf("type=%s", podCreationTest)}); err != nil {
klog.Warningf("failed to delete CiliumClusterwideNetworkPolicy of type %s, error: %v", podCreationTest, err)
}
case "cnp":
// Define the GVR for CiliumNetworkPolicy
cnpGVR := schema.GroupVersionResource{
Group: "cilium.io",
Version: "v2",
Resource: "ciliumnetworkpolicies",
}

if err := dynamicClient.Resource(cnpGVR).Namespace(nps.testClientNamespace).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
klog.Warningf("failed to delete CiliumNetworkPolicy in ns:%s, error: %v", nps.testClientNamespace, err)
}
}
return nil
}

func (nps *networkPolicyEnforcementMeasurement) cleanUp() error {
if nps.k8sClient == nil {
return fmt.Errorf("cleanup skipped - the measurement is not running")
}

if err := nps.deleteClusterRoleAndBinding(); err != nil {
return err
klog.Warningf("Failed to delete ClusterRole and ClusterRoleBinding, error: %v", err)
}

if err := nps.deleteNetworkPolicies(); err != nil {
klog.Warningf("Failed to delete network policies, error: %v", err)
}

klog.V(2).Infof("Deleting namespace %q for measurement %q", nps.testClientNamespace, networkPolicyEnforcementName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{.Name}}
namespace: {{.Namespace}}
labels:
{{ (StructuralData .ClientLabelKey)}}: {{.ClientLabelValue}}
spec:
podSelector:
matchLabels:
{{ (StructuralData .ClientLabelKey)}}: {{.ClientLabelValue}}
policyTypes:
- Egress
egress:
- ports:
- port: 443
protocol: TCP
- port: 80
protocol: TCP
to:
- ipBlock:
cidr: {{.KubeAPIServerIP}}/32
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
{{ (StructuralData .ClientLabelKey)}}: {{.ClientLabelValue}}
name: {{.UniqueName}}
namespace: {{.ClientNamespace}}
spec:
replicas: {{.Replicas}}
selector:
matchLabels:
{{ (StructuralData .ClientLabelKey)}}: {{.ClientLabelValue}}
template:
metadata:
labels:
{{ (StructuralData .ClientLabelKey)}}: {{.ClientLabelValue}}
spec:
nodeSelector:
node: "client"
serviceAccountName: {{.ClientName}}-sa
containers:
- image: acnpublic.azurecr.io/sanamsarath/netloader:0.7
name: netloader
args:
- "--dest_labelSelector={{.TargetLabelKey}}={{.TargetLabelValue}}"
- "--namespace={{.TargetNamespace}}"
- "--duration={{.Duration}}"
- "--interval=1"
- "--workers={{.Workers}}"
- "--destPort={{.TargetPort}}"
- "--destPath={{.TargetPath}}"
resources:
requests:
cpu: "20m"
memory: "40Mi"
tolerations:
- key: "node.kubernetes.io/not-ready"
operator: "Exists"
effect: "NoExecute"
tolerationSeconds: 900
- key: "node.kubernetes.io/unreachable"
operator: "Exists"
effect: "NoExecute"
tolerationSeconds: 900
- key: "slo"
operator: "Equal"
value: "true"
effect: "NoSchedule"

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{.Name}}-cr
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: {{.Name}}-crb
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: {{.Name}}-cr
subjects:
- kind: ServiceAccount
name: {{.Name}}-sa
namespace: {{.Namespace}}
Loading