kcl-lang / kcl

KCL Programming Language (CNCF Sandbox Project). https://kcl-lang.io
https://kcl-lang.io
Apache License 2.0
1.42k stars 110 forks source link

Have not get the | - + from """ #1407

Closed liangyuanpeng closed 3 weeks ago

liangyuanpeng commented 3 weeks ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

kind = "Cluster"
apiVersion = "kind.x-k8s.io/v1alpha4"
networking = {
     #https://kcl-lang.io/docs/user_docs/support/faq-kcl#39-how-to-set-default-value-for-option-function-in-kcl
    apiServerAddress = option("apiaddress",default="127.0.0.1")
}
nodes=[{
        role = "control-plane",
        image = option("kindimage",default="kindest/node:v1.30.0")
        kubeadmConfigPatches = [
            """
        kind: ClusterConfiguration
        apiServer:
        extraArgs:
            runtime-config: api/all=true 
            storage-media-type: option("storageMediaType",default="application/json")
        """
        ]
}]
qqq="""
kind: ClusterConfiguration
apiServer:
extraArgs:
    runtime-config: api/all=true 
    storage-media-type: option("storageMediaType",default="application/json")
"""
qqq2="""
hello
"""

I'm try to using kcl to manage kind config files and have not get the | from kubeadmConfigPatches.

$ kcl main.k 
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
  apiServerAddress: '127.0.0.1'
nodes:
- role: control-plane
  image: kindest/node:v1.30.0
  kubeadmConfigPatches:
  - "\n        kind: ClusterConfiguration\n        apiServer:\n        extraArgs:\n            runtime-config: api/all=true \n            storage-media-type: option(\"storageMediaType\",default=\"application/json\")\n        "
qqq: "\nkind: ClusterConfiguration\napiServer:\nextraArgs:\n    runtime-config: api/all=true \n    storage-media-type: option(\"storageMediaType\",default=\"application/json\")\n"
qqq2: |2

  hello

2. What did you expect to see? (Required)

$ kcl main.k 
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
  apiServerAddress: '127.0.0.1'
nodes:
- role: control-plane
  image: kindest/node:v1.30.0
  kubeadmConfigPatches:
  - |
    kind: ClusterConfiguration
    apiServer:
      extraArgs:
        runtime-config: api/all=true 
        storage-media-type: application/json
...

I'm not sure kcl does support this case? remind me if i missed something,Thanks.

3. What did you see instead (Required)

4. What is your KCL components version? (Required)

Peefy commented 3 weeks ago

Hello @liangyuanpeng Perhaps what you want is this piece of code.

kind = "Cluster"
apiVersion = "kind.x-k8s.io/v1alpha4"
networking = {
     #https://kcl-lang.io/docs/user_docs/support/faq-kcl#39-how-to-set-default-value-for-option-function-in-kcl
    apiServerAddress = option("apiaddress",default="127.0.0.1")
}
nodes=[{
        role = "control-plane",
        image = option("kindimage",default="kindest/node:v1.30.0")
        kubeadmConfigPatches = [
"""\
kind: ClusterConfiguration
apiServer:
  extraArgs:
    runtime-config: api/all=true
    storage-media-type: option("storageMediaType",default="application/json")
"""
        ]
}]
qqq="""\
kind: ClusterConfiguration
apiServer:
  extraArgs:
    runtime-config: api/all=true
    storage-media-type: option("storageMediaType",default="application/json")
"""
qqq2="""\
hello
"""

Please note that my additional task is to remove trailing spaces in multiple line strings

image

liangyuanpeng commented 3 weeks ago

@Peefy Awesome!! Thanks for quickly reply, and do you want to support set param on multiple line strings? I'm not sure if this is a reasonable request. Anyway, now it does what I want.

kind = "Cluster"
apiVersion = "kind.x-k8s.io/v1alpha4"
networking = {
     #https://kcl-lang.io/docs/user_docs/support/faq-kcl#39-how-to-set-default-value-for-option-function-in-kcl
    apiServerAddress = option("apiaddress",default="127.0.0.1")
}
nodes=[{
        role = "control-plane",
        image = option("kindimage",default="kindest/node:v1.30.0")
        kubeadmConfigPatches = [
"""\
kind: ClusterConfiguration
apiServer:
  extraArgs:
    runtime-config: api/all=true
    storage-media-type: ${REALLY_STORAGE_MEDIA_TYPE}
"""
        ]
}]

run command with:

REALLY_STORAGE_MEDIA_TYPE=application/json envsubst < main.k > main2.k
kcl main2.k