Afsal M A:
Can someone create a NetworkPolicy with the below details ?
Create a NetworkPolicy named allow-port-from-namespace in the existing namespace echo. Ensure that the new NetworkPolicy allows Pods in namespace my-app to connect to port 8080 of Pods in namespace echo.
Ensure the below
* does not allow access to Pods, which don't listen on port 8080
* does not allow access from Pods, which are not in namespace my-app
Afsal M A:
default labels are created when a namespace is created - in Kubernetes v1.21 <https://github.com/kubernetes/kubernetes/issues/88253#issuecomment-798793953>
To verify it, try creating a new namespace then run “kubectl get ns (namespace name) -o yaml”, you will see the name coloumn in that. That name is what we have selected in the above solution.
Daniel Henson:
Ah! I see. I missed that above. In that case, why not apply a label to the namespace yourself, so that your network policy can select it?
Daniel Henson:
So the rule of thumb for the exam is that if the question is asking you to do something, it is already possible to do it. In other words, if they expect you to select a namespace for a network policy, that namespace will already be labeled appropriately.
In fact, in some instances you are told explicitly not to change existing resources. Also keep in mind that the exam is running v1.22, so namespaces will be labeled by default.
I think you replied to the wrong daniel. However, we’re all here to learn, so while this is probably above my current kcad course (or I’m just not at that part yet), i’ll try to answer the question.
If I take a quick look at kubectl explain networkpolicies.networking.k8s.io.spec.ingress.from, we can see the following entries:
So it looks like providing a namespaceSelector with empty podSelector will yield the same result as providing it without podSelector.
Also, be sure to note that there’s a difference in combining a namespaceSelector together with a podSelector, and providing a namespaceSelector AND podSelector. As can be read here Network Policies | Kubernetes.