add ProvsK3sBuild doc

This commit is contained in:
Michael Jerger 2023-06-13 17:22:44 +02:00
parent aa91765915
commit da1aaae40c
5 changed files with 149 additions and 11 deletions

View file

@ -74,6 +74,7 @@ classDiagram
link DevopsImageBuild "./doc/DevopsImageBuild.md" link DevopsImageBuild "./doc/DevopsImageBuild.md"
link DevopsTerraformBuild "./doc/DevopsTerraformBuild.md" link DevopsTerraformBuild "./doc/DevopsTerraformBuild.md"
link ReleaseMixin "./doc/ReleaseMixin.md" link ReleaseMixin "./doc/ReleaseMixin.md"
link ProvsK3sBuild "doc/ProvsK3sBuild.md"
``` ```
@ -101,6 +102,7 @@ export PATH=$PATH:~/.local/bin
* [DigitaloceanProvider](doc/DevopsTerraformBuildWithDigitaloceanProvider.md) * [DigitaloceanProvider](doc/DevopsTerraformBuildWithDigitaloceanProvider.md)
* [HetznerProvider](doc/DevopsTerraformBuildWithHetznerProvider.md) * [HetznerProvider](doc/DevopsTerraformBuildWithHetznerProvider.md)
* [ReleaseMixin](./doc/ReleaseMixin.md) * [ReleaseMixin](./doc/ReleaseMixin.md)
* [ProvsK3sBuild](doc/ProvsK3sBuild.md)

143
doc/ProvsK3sBuild.md Normal file
View file

@ -0,0 +1,143 @@
# ProvsK3sBuild
Sets up a single-node k3s cluster using provs.
```mermaid
classDiagram
class ProvsK3sBuild {
def update_runtime_config(dns_record) - after applying terraform update the dns records
write_provs_config() - generate the provs config
provs_apply(dry_run=False) - run provs
}
```
## Input
| name | description | default |
| ----------------------------- | ----------------------------------------------------------------- | --------- |
| k3s_provision_user | the user used to provision k3s | "root" |
| k3s_letsencrypt_email | email address used for letsencrypt | |
| k3s_letsencrypt_endpoint | letsencrypt endpoint. Valid values are staging, prod | "staging" |
| k3s_app_filename_to_provision | an k8s manifest to apply imediately after k3s setup was sucessful | |
| k3s_enable_echo | provision the echo app on k3s. Valid values are true, false | "false" |
| k3s_provs_template | use a individual template for provs config | None |
### Credentials Mapping defaults
```python
[]
```
## Example Usage
### build.py
```python
from os import environ
from pybuilder.core import task, init
from ddadevops import *
name = 'my-project'
MODULE = 'my-module'
PROJECT_ROOT_PATH = '..'
class MyBuild(DevopsTerraformBuild, ProvsK3sBuild):
pass
@init
def initialize(project):
project.build_depends_on("ddadevops>=4.0.0")
stage = environ["STAGE"]
if stage == "test":
tmp_letsencrypt_endpoint = "staging"
elif stage == "prod":
tmp_letsencrypt_endpoint = "prod"
config = {
"credentials_mapping": [
{
"gopass_path": environ.get("DIGITALOCEAN_TOKEN_KEY_PATH", None),
"name": "do_api_key",
},
{
"gopass_path": environ.get("HETZNER_API_KEY_PATH", None),
"name": "hetzner_api_key",
},
],
"name": name,
"module": MODULE,
"stage": stage,
"project_root_path": PROJECT_ROOT_PATH,
"build_types": ["TERRAFORM", "C4K", "K3S"],
"mixin_types": [],
"tf_provider_types": ["DIGITALOCEAN", "HETZNER"],
"tf_use_workspace": False,
"tf_terraform_semantic_version": "1.4.2",
"do_as_backend": True,
"do_bucket": "my-configuration",
"k3s_app_filename_to_provision": "my_k8s_application_manifest.yaml",
"k3s_letsencrypt_endpoint": tmp_letsencrypt_endpoint,
"k3s_letsencrypt_email": "admin@my.doamin",
}
build = MyBuild(project, config)
build.initialize_build_dir()
@task
def plan(project):
build = get_devops_build(project)
build.plan()
@task
def tf_apply(project):
build = get_devops_build(project)
build.apply(True)
@task
def show_config(project):
build = get_devops_build(project)
build.apply(True)
out_json = build.read_output_json()
build.update_runtime_config(
DnsRecord(
fqdn=out_json["fqdn"]["value"],
ipv4=out_json["ipv4"]["value"],
ipv6=out_json["ipv6"]["value"],
)
)
build.write_provs_config()
build.provs_apply(dry_run=True)
@task
def apply(project):
build = get_devops_build(project)
build.apply(True)
out_json = build.read_output_json()
build.update_runtime_config(
DnsRecord(
fqdn=out_json["fqdn"]["value"],
ipv4=out_json["ipv4"]["value"],
ipv6=out_json["ipv6"]["value"],
)
)
build.write_provs_config()
time.sleep(5)
build.provs_apply()
@task
def destroy(project):
build = get_devops_build(project)
build.destroy(True)
```
### call the build
```bash
pyb apply
```

View file

@ -1,6 +1,6 @@
# ReleaseMixin # ReleaseMixin
Support for Releases following the trunk-based-release flow (see https://trunkbaseddevelopment.com/) Support for releases following the trunk-based-release flow (see https://trunkbaseddevelopment.com/)
```mermaid ```mermaid
classDiagram classDiagram

View file

@ -24,9 +24,9 @@ CONFIG_ECHO = """echo: $echo
class K3s(Validateable): class K3s(Validateable):
def __init__(self, inp: dict): def __init__(self, inp: dict):
self.k3s_provision_user = inp.get("k3s_provision_user") self.k3s_provision_user = inp.get("k3s_provision_user", "root")
self.k3s_letsencrypt_email = inp.get("k3s_letsencrypt_email") self.k3s_letsencrypt_email = inp.get("k3s_letsencrypt_email")
self.k3s_letsencrypt_endpoint = inp.get("k3s_letsencrypt_endpoint") self.k3s_letsencrypt_endpoint = inp.get("k3s_letsencrypt_endpoint", "staging")
self.k3s_app_filename_to_provision = inp.get("k3s_app_filename_to_provision") self.k3s_app_filename_to_provision = inp.get("k3s_app_filename_to_provision")
self.k3s_enable_echo = inp.get("k3s_enable_echo", "false") self.k3s_enable_echo = inp.get("k3s_enable_echo", "false")
self.k3s_provs_template = inp.get("k3s_provs_template", None) self.k3s_provs_template = inp.get("k3s_provs_template", None)

View file

@ -4,14 +4,7 @@ from .devops_build import DevopsBuild
class ProvsK3sBuild(DevopsBuild): class ProvsK3sBuild(DevopsBuild):
def __init__(self, project, config): def __init__(self, project, inp):
inp = config.copy()
inp["name"] = project.name
inp["module"] = config.get("module")
inp["stage"] = config.get("stage")
inp["project_root_path"] = config.get("project_root_path")
inp["build_types"] = config.get("build_types", [])
inp["mixin_types"] = config.get("mixin_types", [])
super().__init__(project, inp) super().__init__(project, inp)
self.execution_api = ExecutionApi() self.execution_api = ExecutionApi()
devops = self.devops_repo.get_devops(self.project) devops = self.devops_repo.get_devops(self.project)