Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
patrick-hermann-sva opened this issue Jul 21, 2024 · 7 comments

Comments

@patrick-hermann-sva
Copy link

patrick-hermann-sva commented Jul 21, 2024

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
Copy link
Contributor

Peefy commented Jul 22, 2024

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
Copy link
Author

patrick-hermann-sva commented Jul 22, 2024

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
Copy link
Contributor

Peefy commented Jul 22, 2024

-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
Copy link
Contributor

Peefy commented Jul 22, 2024

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
Copy link
Author

patrick-hermann-sva commented Jul 22, 2024

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
Copy link
Contributor

Peefy commented Jul 22, 2024

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
Copy link
Contributor

Peefy commented Jul 22, 2024

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

image

@Peefy Peefy changed the title How to set __META_APP_NAME, META_ENV_TYPE_NAME and META_CLUSTER_NAME? How to set __META_APP_NAME, META_ENV_TYPE_NAME and META_CLUSTER_NAME in Konfig? Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants