Sha256: 57a53133cdab0e3a90f8e40249d1df9ab1f7fa9d576c9e69879562cb76741095

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

---
title: NetworkPolicy
categories: dsl
---

## Example

Here's an example of a NetworkPolicy.

.kubes/resources/web/network_policy.rb

```ruby
name "web"
labels(app: "backend")
namespace "backend"

matchLabels(app: "backend", role: "web")
fromNamespace(app: "frontend")
fromPod(app: "backend")
```

Produces:

.kubes/output/web/network_policy.yaml

```yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: web
  labels:
    app: backend
  namespace: backend
spec:
  podSelector:
    matchLabels:
      app: backend
      role: web
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          app: frontend
    - podSelector:
        matchLabels:
          app: backend
```

Note, the behavior of the from is an *or* since the namespaceSelector and podSelector are separate items.

## Example 2

If you need more control over the ingress selectors you can use the from method. He's an example:

.kubes/resources/web/network_policy.rb

```ruby
name "web"
labels(app: "backend")
namespace "backend"

matchLabels(app: "backend", role: "web")
from([
  { namespaceSelector: { matchLabels: { app: "frontend" } } },
  { namespaceSelector: { matchLabels: { app: "backend" } } }
])
```

Produces:

.kubes/output/web/network_policy.yaml

```yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: web
  labels:
    app: backend
  namespace: backend
spec:
  podSelector:
    matchLabels:
      app: backend
      role: web
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          app: frontend
    - namespaceSelector:
        matchLabels:
          app: backend
```

This will allow traffic from pods in either the frontend or backend namespaces to the backend pods.

## DSL Methods

Here's a list of more common methods:

* fromNamespace
* fromPod
* fromIpBlock
* toNamespace
* toPod
* toIpBlock
* from
* to

{% include dsl/methods.md name="network_policy" %}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kubes-0.2.2 docs/_docs/dsl/resources/network_policy.md
kubes-0.2.1 docs/_docs/dsl/resources/network_policy.md