<p>kubernetes V19</p>
<p>Create a new NetworkPolicy named allow-port-from-namespace that allows Pods in the existing namespace internal to connect to port 80 of other Pods in the same namespace.</p>
<p>Ensure that the new NetworkPolicy:</p>
<p>does not allow access to Pods not listening on port 80
does not allow access from Pods not in namespace internal</p>
<p>i need to know if i can do it without adding a labels to namspace and pod or not ?</p>

Inforedaster
<p>In <a href="https://kubernetes.io/docs/concepts/services-networking/network-policies/#isolated-and-non-isolated-pods" rel="nofollow noreferrer">k8s networkpolicy docs</a> you read:</p>
<blockquote>
<p>By default, pods are non-isolated; they accept traffic from any
source.</p>
<p>Pods become isolated by having a NetworkPolicy that selects them. Once
there is any NetworkPolicy in a namespace selecting a particular pod,
that pod will reject any connections that are not allowed by any
NetworkPolicy. (Other pods in the namespace that are not selected by
any NetworkPolicy will continue to accept all traffic.)</p>
<p>Network policies do not conflict; they are additive. If any policy or
policies select a pod, the pod is restricted to what is allowed by the
union of those policies' ingress/egress rules. Thus, order of
evaluation does not affect the policy result</p>
</blockquote>
<p>This means that once you assign(select) a pod with network policy you never set deny rules because everyting is denied by default. You only specify allow rules.</p>
<p>This beeing explained lets go back to k8s docs where you can read the following:</p>
<blockquote>
<p>There are four kinds of selectors that can be specified in an ingress
from section or egress to section:</p>
<p><strong>podSelector</strong>: This selects particular Pods in the same namespace as the
NetworkPolicy which should be allowed as ingress sources or egress
destinations.</p>
<p><strong>namespaceSelector</strong>: This selects particular namespaces for which all
Pods should be allowed as ingress sources or egress destinations.</p>
<p><strong>namespaceSelector</strong> and <strong>podSelector</strong>: A single to/from entry that
specifies both namespaceSelector and podSelector selects particular
Pods within particular namespaces
[...]</p>
</blockquote>
<p>I am not going to paste all docs here, check the rest <a href="https://kubernetes.io/docs/concepts/services-networking/network-policies/#behavior-of-to-and-from-selectors" rel="nofollow noreferrer">here</a>.</p>
<hr />
<p>Now to answer you question: <code>&quot;I need to know if i can do it without adding a labels to namspace and pod or not ?&quot;</code></p>
<p>What you should notice in the docs metioned above is that you can only target namespace and pods using labels.</p>
<p>And when you don't use namespace label selector, the selector dafaults to the namespace where networkpolicy is deployed.</p>
<p>So, yes, you can do it without adding a labels to a namespace as long as you deploy network policy in the namespace you want to target. And you can also do it without adding labels to a pod as long as this is the only pod in the namespace.</p>
