Skip to content

Commit

Permalink
docs: add omegaconf docs + change site name (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoVoges committed Aug 3, 2023
1 parent 72cc5ab commit d8c25b2
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CNAME
Original file line number Diff line number Diff line change
@@ -1 +1 @@
kapitan.dev
nexenio.kapitan.dev
2 changes: 1 addition & 1 deletion docs/CNAME
Original file line number Diff line number Diff line change
@@ -1 +1 @@
kapitan.dev
nexenio.kapitan.dev
254 changes: 254 additions & 0 deletions docs/pages/inventory/omegaconf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
## OmegaConf

With version `0.33.0` we are introducing a new inventory backend as an alternative to reclass.
Here are all of the differences to using reclass, new features and some instructions, how to migrate your inventory.

!!! warning

OmegaConf is currently in experimental mode. If you encouter unexpected errors or bugs, please let us know and create an [issue](https://github.com/kapicorp/kapitan/issues/new/choose).

### Differences to reclass

#### Supported:
* compose-node-name
* key overwrite prefix '`~`'
* interpolations
* relative class names
* init class
* nested interpolations
* escaped interpolations

#### Not (yet) supported
* exports
* inventory queries
* interpolation to yaml-keys containing '`.`' (the delimiter itself)

#### Syntax changes

OmegaConf uses the default yaml-path notation using dots (`.`) as key delimiter.

### New Functionalities

All of [OmegaConfs native functionalities](https://omegaconf.readthedocs.io/en/2.3_branch/grammar.html#the-omegaconf-grammar) are supported.

#### General
* relative interpolation
* list accessing
* mandatory values
* Resolvers and Custom Resolvers

#### Resolvers

Resolvers are the main benefits of using OmegaConf.
You can define any behavior with a python function that gets executed with the given input parameters.

We provide some basic resolvers:

* OmegaConf
* `oc.env`: access a environment variable
* `oc.select`: provide a default value for an interpolation
* `oc.dict.keys`: get the keys of a dictionary object as a list
* `oc.dict.values`: get the values of dictionary object as a list
* Utilities
* `key`: get the name of the nodes key
* `fullkey`: get the full name of the key
* `parentkey`: get the name of the nodes parent key
* `relpath`: takes an absolute path and convert it to relative
* `tag`: creates an escaped interpolation
* Casting and Merging
* `dict`: cast a dict inside a list into the actual dict
* `list`: put a dict inside a list with that dict
* `merge`: merge objects

#### Usage

Using a resolver is as simple as an interpolation: `yamlkey: ${resolver:input}`

#### Custom Resolver

You can write your own resolvers and are able to achieve any behavior you want.

In your inventory you have to create a file `resolvers.py`.
You should define a function `pass_resolvers()` that returns a dictionary with the resolvers name and the respective function pointer.

Now you can start writing python functions with your custom .

#### Example

```python
# inventory/resolvers.py

def concat(input1, input2):
return input1 + input2

def add_ten(input):
assert isinstance(input, int)
return input + 10

def default_value():
return "DEFAULT"

def split_dots(input: str):
return input.split(".")

# required function
def pass_resolvers():
return {"concat": concat, "plus_ten": add_ten, "default": default_value, "split", split_dots}
```

If we render a file the result would be:

```yaml
string: ${concat:Hello, World} # --> Hello World
int: ${plus_ten:90} # --> 100
default: ${default:} # --> DEFAULT
list: ${split:hello.world} # --> yaml list [hello, world]
```
### Access the feature
To access the feature you have to use a kapitan version >=0.33.0.
Use the flag `--omegaconf` in your command to indicate to use OmegaConf as backend. To specify that you want to use it everytime, add this to your `.kapitan` file:
```yaml
inventory_backend:
omegaconf: true
```

If this is your first time running you have to specify `--migrate` to adapt to OmegaConfs syntax.

!!! danger

Please backup your inventory, if you're running `--migrate` or make sure you are able to revert the changes using source control.
Also check your inventory if it contains some yaml errors like duplicate keys or wrong yaml types. The command will not change anything if some errors occur.

The migration consists of the following steps:
* replacing the delimiter '`:`' with '`.`' in interpolations
* replacing meta interpolations '`_reclass_`' to '`_meta_`'
* replacing escaped interpolations `\${content}` to resolver `${tag:content}`

### Examples

One important usecase with this is the definition of default values and overwriting them with specific target/component values.


```yaml
# inventory/classes/templates/deployment.yml
parameters:
# define default values using the 'relpath' resolver
deployment:
namespace: ${target_name}
component_name: \${parentkey:} # hack to get the components name
labels:
app.kubernetes.io/name: ${relpath:deployment.namespace} # gets resolved relatively
image: ??? # OmegaConf mandatory value (has to be set in target)
pull_policy: Always
image_pull_secrets:
- name: default-secret
service_port: 8080 # default value
service:
type: ClusterIP
selector:
app: ${target_name}
ports:
http:
service_port: ${relpath:deployment.service_port} # allows us to overwrite this in another key
```

```yaml
# inventory/targets/example.yml
classes:
- templates.deployment
parameters:
target_name: ${_meta_.name.short}
components:
# merge each component with a deployment
backend: ${merge:${deployment},${backend}}
keycloak: ${merge:${deployment},${keycloak}}
keycloak-copy: ${merge:${keycloak},${keycloak-copy}} # merge with another component to specify even more
# components config (would be in their own classes)
# backend config
backend:
image: backend:latest
# keycloak config
keycloak:
namespace: example1
image: keycloak:latest
env:
[...]
# keycloak-copy config (inherits env and namespace from keycloak)
keycloak-copy:
namespace: example2
```

This would generate the following components definition:

```yaml
components:
backend:
component_name: deployment
image: backend:latest
image_pull_secrets:
- name: default-secret
labels:
app.kubernetes.io/name: example
namespace: example
ports:
http:
service_port: 8080
pull_policy: Always
service:
selector:
app: example
type: ClusterIP
service_port: 8080
keycloak:
component_name: deployment
image: keycloak:latest
image_pull_secrets:
- name: default-secret
labels:
app.kubernetes.io/name: example1
namespace: example1
ports:
http:
service_port: 8080
pull_policy: Always
service:
selector:
app: example
type: ClusterIP
service_port: 8080
keycloak-copy:
component_name: deployment
image: keycloak:latest
image_pull_secrets:
- name: default-secret
labels:
app.kubernetes.io/name: example1
namespace: example2
ports:
http:
service_port: 8080
pull_policy: Always
service:
selector:
app: example
type: ClusterIP
service_port: 8080
```
3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# dependencies:
# pip install mkdocs mkdocs-material pymdown-extensions
site_name: "Kapitan: Keep your ship together"
site_url: "https://kapitan.dev"
site_url: "https://nexenio.kapitan.dev"
strict: true
plugins:
- tags:
Expand Down Expand Up @@ -92,6 +92,7 @@ nav:
- Classes: pages/inventory/classes.md
- Parameters Interpolation: pages/inventory/parameters_interpolation.md
- Advanced: pages/inventory/advanced.md
- OmegaConf: pages/inventory/omegaconf.md

- Input Types:
- Introduction: pages/input_types/introduction.md
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ description = "Generic templated configuration management for Kubernetes, Terraf
authors = [
"Ricardo Amaro <[email protected]>"
]
documentation = "https://kapitan.dev/"
repository = "https://github.com/kapicorp/kapitan"
documentation = "https://nexenio.kapitan.dev/"
repository = "https://github.com/nexenio/kapitan"
readme = "README.md"
keywords = ["jsonnet", "kubernetes", "reclass", "jinja"]
classifiers = [
Expand Down

0 comments on commit d8c25b2

Please sign in to comment.