CentaurusInfra / mizar

Mizar – Experimental, High Scale and High Performance Cloud Network https://mizar.readthedocs.io
https://mizar.readthedocs.io
GNU General Public License v2.0
112 stars 50 forks source link

Convert Network Policy Rules into json format and pass it as a string through gRPC #464

Open clu2xlu opened 3 years ago

clu2xlu commented 3 years ago

Mizar and Arktos communicates through gRPC protocol. The desired Network policy gRPC message should be:

message BuiltinsNetworkPolicyMessage {
  string name = 1;
  string tenant = 2;
  string namespace = 3; 
  string spec = 4;
}

https://github.com/clu2xlu/arktos/blob/poc/pkg/controller/mizar/builtins.proto#L75-L79

However, in go, the network policy obj's Spec is not in String format. e.g. policy.Spec cannot be converted to string directly. https://github.com/clu2xlu/arktos/blob/poc/pkg/controller/mizar/util.go#L125

For this task, you will need to use NetworkPolicySpec, Ingress and Egress to convert network policy's spec into a json string

The desired spec json strong looks like this:

{'podSelector': {'matchLabels': {'run': 'pod0'}}, 'ingress': [{'ports': [{'protocol': 'TCP', 'port': 8000}, {'protocol': 'TCP', 'port': 5976}], 'from': [{'podSelector': {'matchLabels': {'run': 'pod1'}}}]}], 'egress': [{'ports': [{'protocol': 'TCP', 'port': 8000}], 'to': [{'podSelector': {'matchLabels': {'run': 'pod2'}}}]}], 'policyTypes': ['Ingress', 'Egress']}

it's yaml is

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: test-network-policy
  namespace: default
spec:
  podSelector:
    matchLabels:
      run: pod0
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          run: pod1
    ports:
    - protocol: TCP
      port: 8000
    - protocol: TCP
      port: 5976
  egress:
  - to:
    - podSelector:
        matchLabels:
          run: pod2
    ports:
    - protocol: TCP
      port: 8000

NOTE: please use my branch for now:

For mizar: use branch poc https://github.com/clu2xlu/mizar/tree/poc

For arktos: use branch poc https://github.com/clu2xlu/arktos/tree/poc