kcl-lang / konfig

KCL Kubernetes Config Abstraction & Composition Module
https://kcl-lang.io
Apache License 2.0
9 stars 8 forks source link

How to set __META_APP_NAME, META_ENV_TYPE_NAME and META_CLUSTER_NAME in Konfig? #16

Open patrick-hermann-sva opened 3 months ago

patrick-hermann-sva commented 3 months ago

General Question

How to set __META_APP_NAME, META_ENV_TYPE_NAME and META_CLUSTER_NAME?

i have a structure of base and dev folders which are rendering fine (kpm run k8s/dev/) for my defined server (deployment, configMaps ...),. Sadly i cannot overwrite the namespace or env (for the deployment, for e.g. configmaps it can be refrenced by the imported packages). i did some research for those variables but do not know how to set them. (i read sth about project.yaml and stack.yaml - but i could not find information about the structure of it or references).

rendered deployment (server)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bla
  namespace: sampleapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: sampleapp
      app.kubernetes.io/env: prod
      app.kubernetes.io/instance: sampleapp-prod
      app.k8s.io/component: bla
# ..

could you maybe help with a hint? :-)

Peefy commented 3 months ago

The definitions of the variables are as follows:

__META_APP_NAME = option("app") or "sampleapp"
__META_ENV_TYPE_NAME = option("env") or "prod"
__META_CLUSTER_NAME = option("cluster") or Undefined

Thus you can use -D app=override_app -D env=override_env -D cluster=override_cluster in KCL CLI to set them.

patrick-hermann-sva commented 3 months ago

thank you @Peefy !

sadly this dosen't work for my kpm run command (env, app stay default for the -D overwrites)

is there a way to overwrite those values as code for an env e.g. directly in the dev main.k ? my idea is to have different envs defined (based on base) and do want to read the values from files not via cmd flags..

# dev/main.k? 
## somehow? 
app="myapp"
env="dev"

my structure is:

ls k8s/base/
base.k

ls k8s/dev/
kcl.mod  kcl.mod.lock  main.k  

I'm using the oci packages (did not clone the whole konfig repo).

my kcl.mod does look like this:

[package]
name = "dev"
edition = "v0.9.0"
version = "0.0.1"

[dependencies]
konfig = "0.5.0"
k8s = "1.30"

[profile]
entries = ["../base/base.k", "main.k", "${konfig:KCL_MOD}/models/kube/render/render.k"]
Peefy commented 3 months ago

-D works well for me at the latest kcl cli version: v0.9.3. https://github.com/kcl-lang/cli

Besides, If you want to read these configurations from a file, you can write code like the following code in the konfig server backend. https://github.com/kcl-lang/modules/blob/main/konfig/models/kube/backend/server_backend.k

name = file.read("name.txt") or matadata.__META_APP_NAME
Peefy commented 3 months ago

Or you can use a kcl.yaml file to set the -D parameters like this: https://www.kcl-lang.io/docs/reference/lang/tour#top-level-argument

KCL CLI will read the file automaticly.

patrick-hermann-sva commented 3 months ago

maybe more precise - i do not know how to set app and env in the code :-/

image

name does work but the other 2 not

Peefy commented 3 months ago

I see. If you want to set it up in the Server model and konfig does not yet support that, you can modify the konfig code or submit a PR to meet your needs.

Peefy commented 3 months ago

Or you can set the podMetadata attribute to merge more app and env labels like this:

image