Skip to content

Commit 423a255

Browse files
authored
feat: docs how to use raw policices with the PolicyResourceManager (#37)
1 parent 72da618 commit 423a255

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

docs/.custom_wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PNG
3939
PolicyResourceManager
4040
proxied
4141
proxying
42+
Pydantic
4243
Pygments
4344
plantuml
4445
productpage

docs/how-to/manage-custom-policies-with-policyresourcemanager.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,121 @@ For **unit-targeted policies** (`PolicyTargetType.unit`):
282282
Unit-targeted policies provide Layer 4 (TCP) access control to individual pods. They cannot restrict by HTTP methods, paths, or hosts - only by ports. This limitation comes from the underlying Istio service mesh implementation. Use unit policies when you need to access individual units directly, such as for metrics scraping from each pod.
283283
```
284284

285+
## Using raw policy objects
286+
287+
For advanced use cases where `MeshPolicy` doesn't provide enough flexibility, you can pass pre-built policy objects directly to the `PolicyResourceManager` using the `raw_policies` parameter.
288+
289+
### When to use raw policies
290+
291+
Use `raw_policies` when you need:
292+
- Mesh-specific features not exposed by the mesh-agnostic `MeshPolicy` abstraction
293+
- Full control over the native policy specification
294+
- Direct translation of existing mesh-specific policies to your charm
295+
296+
```{note}
297+
`MeshPolicy` is designed to be mesh-agnostic. If your policy requirements are specific to a particular mesh implementation, `raw_policies` gives you direct access to the underlying policy format.
298+
```
299+
300+
### Available policy types
301+
302+
Currently, the following raw policy types are supported:
303+
304+
**For Istio mesh:**
305+
- `AuthorizationPolicy` - available from `lightkube_extensions.types`
306+
307+
### Building raw policies for Istio
308+
309+
Import the `AuthorizationPolicy` type and spec models:
310+
311+
```python
312+
from lightkube.models.meta_v1 import ObjectMeta
313+
from lightkube_extensions.types import AuthorizationPolicy
314+
from charmed_service_mesh_helpers.models import (
315+
AuthorizationPolicySpec,
316+
From,
317+
Operation,
318+
PolicyTargetReference,
319+
Rule,
320+
Source,
321+
To,
322+
)
323+
```
324+
325+
```{note}
326+
The `AuthorizationPolicy` resource type is provided by `lightkube_extensions`, while the spec data models (`AuthorizationPolicySpec`, `Rule`, etc.) are provided by `charmed_service_mesh_helpers`. There are ongoing plans to consolidate the service mesh library offerings into a single, unified Python package.
327+
```
328+
329+
Create an `AuthorizationPolicy` using the data models:
330+
331+
```python
332+
def _get_raw_policies(self) -> list[AuthorizationPolicy]:
333+
"""Return raw AuthorizationPolicy objects."""
334+
policy = AuthorizationPolicy(
335+
metadata=ObjectMeta(
336+
name="my-custom-policy",
337+
namespace=self.model.name,
338+
),
339+
spec=AuthorizationPolicySpec(
340+
targetRefs=[
341+
PolicyTargetReference(
342+
kind="Service",
343+
group="",
344+
name="target-service",
345+
)
346+
],
347+
rules=[
348+
Rule(
349+
from_=[
350+
From(
351+
source=Source(
352+
principals=[
353+
f"cluster.local/ns/{self.model.name}/sa/source-app"
354+
]
355+
)
356+
)
357+
],
358+
to=[
359+
To(
360+
operation=Operation(
361+
ports=["8080"],
362+
methods=["GET", "POST"],
363+
paths=["/api/*"],
364+
)
365+
)
366+
],
367+
)
368+
],
369+
).model_dump(by_alias=True, exclude_unset=True, exclude_none=True),
370+
)
371+
return [policy]
372+
```
373+
374+
```{note}
375+
The `AuthorizationPolicySpec` is a Pydantic model. Use `.model_dump(by_alias=True, exclude_unset=True, exclude_none=True)` to convert it to the dict format expected by `AuthorizationPolicy.spec`.
376+
```
377+
378+
### Reconciling raw policies
379+
380+
Pass `raw_policies` to `reconcile()` alongside or instead of `MeshPolicy` objects:
381+
382+
```python
383+
def _reconcile_policies(self, event):
384+
prm = self._get_policy_manager()
385+
mesh_type = self._mesh.mesh_type()
386+
387+
# Use both MeshPolicy and raw policies together
388+
prm.reconcile(
389+
self._get_custom_policies(),
390+
mesh_type,
391+
raw_policies=self._get_raw_policies(),
392+
)
393+
394+
# Or use only raw policies
395+
prm.reconcile([], mesh_type, raw_policies=self._get_raw_policies())
396+
```
397+
398+
The `PolicyResourceManager` will apply the configured labels to raw policies and manage them alongside any `MeshPolicy`-generated policies.
399+
285400
## Best practices
286401

287402
### Combining ServiceMeshConsumer and PolicyResourceManager

0 commit comments

Comments
 (0)