Hi! Scenario for example: We need to create a network policy from namespace A . . .

Kratika:
Hi! Scenario for example:

We need to create a network policy from namespace A to Namespace B where there is no pods available and no labels available for namespaces except default label like “http://kubernetes.io/metadata.name|kubernetes.io/metadata.name”, How do you achieve this?

Can any one please provide me the spec for the same as am stuck with this one?

1 Like

Adeola Adefolaju:
I think you may need to label the namespaces. $ k label ns A project=test and $ k label ns B project=test

Marko Eremija:
also take a look here: https://kubernetes.io/docs/reference/labels-annotations-taints/#kubernetes-io-metadata-name

Kratika:
Thanks for the response @Adeola Adefolaju and @Marko Eremija

But how do you create network policy as network policy is asking for podSelector where there is no pod Exists

Marko Eremija:
IIRC, podSelector can be an empty dictionary :thinking_face:

Marko Eremija:
i.e. podSelector: {}

Adeola Adefolaju:
yes

Adeola Adefolaju:
Try this:

Adeola Adefolaju:
But you must have created the namespaces and their labels before creating the netpol

Adeola Adefolaju:
@unnivkn could you help confirm please? Thank you.

Kratika:
@Adeola Adefolaju in the given spec I don’t see source namespace or any label for the same right?

Adeola Adefolaju:
yes because I created it before creating the yaml, sorry. $ k create ns a and $ k create ns b. Then label them: $ k label ns a project=test and $ k label ns b project=test

Adeola Adefolaju:
You can also confirm.

k get ns --show-labels

Kratika:
Spec:
podSelector: {}
namespaceSelector:
matchLabels:
project: test
policyTypes:

  • Ingress
    ingress:

Do you mean to say policy will come just like above if we apply label as you suggested? @Adeola Adefolaju @unnivkn

unnivkn:
Hi @Kratika may I know from where you got this question… first of all, this question not looks a standard one to me… however, what ever the guys told above looks good to me… I always recommend to take the standard netpol template from the doc & modify it as per our requirement. fyI: https://kubernetes.io/docs/concepts/services-networking/network-policies/