Skip to content

Commit

Permalink
feat(guide): code review
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Skalski <skalskimarcin33@gmail.com>
  • Loading branch information
Automaat committed Oct 18, 2024
1 parent f4305c2 commit cd09772
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/_data/docs_nav_kuma_2.9.x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ items:
url: /guides/migration-to-the-new-policies/
- text: Progressively rolling in strict MTLS
url: /guides/progressively-rolling-in-strict-mtls/
- text: Producer and consumer policies
url: /guides/consumer-producer-policies
- title: Reference
group: true
items:
Expand Down
91 changes: 85 additions & 6 deletions app/_src/guides/consumer-producer-policies.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ title: Producer and Consumer policies
---

With namespace scoped policies in {{site.mesh_product_name}} you can have fine-grained control over policies and how they apply to
your workloads. To fully utilize power of namespace scoped policies we need to get familiar with producer consumer model.
your workloads. Moreover, this empowers app owners to take advantage of Kubernetes RBAC for policy configuration.

To fully utilize power of namespace scoped policies we need to get familiar with producer consumer model.
This guide will help you get comfortable with producer consumer model.

## Prerequisites
Expand Down Expand Up @@ -91,9 +93,32 @@ You should see something similar to:
{"counter":"1","zone":"local","err":null}
```

At this moment our setup looks like this:

{% mermaid %}
flowchart LR

subgraph first-consumer-ns
first-consumer
end

subgraph second-consumer-ns
second-consumer
end

subgraph kuma-demo-ns
kuma-demo
redis
end

kuma-demo --> redis
first-consumer --> kuma-demo
second-consumer --> kuma-demo
{% endmermaid %}

## Namespace scoped policies

Now that we have our setup we can start playing with policies. Let's create a simple MeshTimeout policy in `kuma-demo` namespace:
Now that we have our setup we can start playing with policies. Let's create a simple [MeshTimeout](/docs/{{ page.version }}/policies/meshtimeout/) policy in `kuma-demo` namespace:

```shell
echo "apiVersion: kuma.io/v1alpha1
Expand Down Expand Up @@ -138,7 +163,7 @@ This label indicates the policy role. Possible values of this label are:

- `system` - policies applied in system namespace
- `workload-owner` - policies that don't specify `spec.from` and `spec.to` sections or specify only `spec.from` section
- `consumer` - policies targeting MeshServices from different namespace or targeting MeshService by labels
- `consumer` - policies targeting `MeshServices` from different namespace in `spec.to` section or targeting `MeshService` by labels
- `producer` - policies defined in the same namespace as the MeshService they are targeting in their `spec.to[]`.

### Producer consumer model
Expand Down Expand Up @@ -175,6 +200,36 @@ We should see the same results when making requests from second-consumer namespa
kubectl exec -n second-consumer consumer -- curl -s -XPOST demo-app.kuma-demo:5000/increment -H "x-set-response-delay-ms: 2000"
```

Output:

```
{"counter":"4","zone":"local","err":null}
```

Producer policy will be applied on all traffic to `kuma-demo` as we can see on this diagram:

{% mermaid %}
flowchart LR

subgraph first-consumer-ns

Check failure on line 214 in app/_src/guides/consumer-producer-policies.md

View workflow job for this annotation

GitHub Actions / Lint docs

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'subgraph'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'subgraph'?", "location": {"path": "app/_src/guides/consumer-producer-policies.md", "range": {"start": {"line": 214, "column": 1}}}, "severity": "ERROR"}
first-consumer
end

subgraph second-consumer-ns

Check failure on line 218 in app/_src/guides/consumer-producer-policies.md

View workflow job for this annotation

GitHub Actions / Lint docs

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'subgraph'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'subgraph'?", "location": {"path": "app/_src/guides/consumer-producer-policies.md", "range": {"start": {"line": 218, "column": 1}}}, "severity": "ERROR"}
second-consumer
end

subgraph kuma-demo-ns

Check failure on line 222 in app/_src/guides/consumer-producer-policies.md

View workflow job for this annotation

GitHub Actions / Lint docs

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'subgraph'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'subgraph'?", "location": {"path": "app/_src/guides/consumer-producer-policies.md", "range": {"start": {"line": 222, "column": 1}}}, "severity": "ERROR"}
kuma-demo
redis
end

kuma-demo --> redis
first-consumer --producer-timeout--> kuma-demo
second-consumer --producer-timeout--> kuma-demo
{% endmermaid %}


## Utilizing consumer policy

Until now, we were relying on producer policy created previously. Let's assume that we don't mind waiting a little longer
Expand All @@ -193,8 +248,8 @@ spec:
to:
- targetRef:
kind: MeshService
name: demo-app
namespace: kuma-demo
labels:
k8s.kuma.io/service-name: demo-app
default:
http:
requestTimeout: 3s" | kubectl apply -f -
Expand All @@ -219,6 +274,29 @@ We should still see timeouts:
upstream request timeout
```

To better visualize this we can look again at our diagram and see that only traffic from `first-consumer` will be affected.

{% mermaid %}
flowchart LR

subgraph first-consumer-ns

Check failure on line 282 in app/_src/guides/consumer-producer-policies.md

View workflow job for this annotation

GitHub Actions / Lint docs

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'subgraph'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'subgraph'?", "location": {"path": "app/_src/guides/consumer-producer-policies.md", "range": {"start": {"line": 282, "column": 1}}}, "severity": "ERROR"}
first-consumer
end

subgraph second-consumer-ns

Check failure on line 286 in app/_src/guides/consumer-producer-policies.md

View workflow job for this annotation

GitHub Actions / Lint docs

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'subgraph'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'subgraph'?", "location": {"path": "app/_src/guides/consumer-producer-policies.md", "range": {"start": {"line": 286, "column": 1}}}, "severity": "ERROR"}
second-consumer
end

subgraph kuma-demo-ns

Check failure on line 290 in app/_src/guides/consumer-producer-policies.md

View workflow job for this annotation

GitHub Actions / Lint docs

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'subgraph'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'subgraph'?", "location": {"path": "app/_src/guides/consumer-producer-policies.md", "range": {"start": {"line": 290, "column": 1}}}, "severity": "ERROR"}
kuma-demo
redis
end

kuma-demo --> redis
first-consumer --consumer-timeout--> kuma-demo
second-consumer --producer-timeout--> kuma-demo
{% endmermaid %}

## What we've learnt?

- How to apply policy on a namespace
Expand All @@ -228,4 +306,5 @@ upstream request timeout

## Next steps

- Read more about [producer/consumer policies](TODO)
- Read more about [producer/consumer policies](/docs/{{ page.version }}/policies/introduction)
- Check out [Federate zone control plane](/docs/{{ page.version }}/guides/federate/) guide

0 comments on commit cd09772

Please sign in to comment.