kcl-lang / kcl

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

Have Repeat run the if conditional . #1410

Closed liangyuanpeng closed 1 week ago

liangyuanpeng commented 2 weeks ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

when i generate yaml from kcl, it have repeat run the if conditional .

kind.k

_nodes=[{
        role = "control-plane",
        image = option("kindimage",default="kindest/node:v1.30.0")
        extraMounts = [
            {
                hostPath = "/home/runner/work/lanactions/lanactions/config/apiserver-audit-policy.yaml",
                containerPath = "/etc/kubernetes/audit-policy/apiserver-audit-policy.yaml"
            }
        ]
        kubeadmConfigPatches = [
"""\
kind: ClusterConfiguration
etcd:
  external:
    endpoints: [${ETCD_ENDPOINTS}]
apiServer:
  extraArgs:
    runtime-config: api/all=true
    storage-media-type: ${REALLY_STORAGE_MEDIA_TYPE}
    audit-log-path: /var/log/audit/kube-apiserver-audit.log
    audit-policy-file: /etc/kubernetes/audit-policy/apiserver-audit-policy.yaml
  extraVolumes:
    - name: "audit-logs"
      hostPath: /var/log/audit
      mountPath: /var/log/audit
    - name: audit-policy
      hostPath: /etc/kubernetes/audit-policy
      mountPath: /etc/kubernetes/audit-policy
"""
        ]
}

]

_count = option("count",default="default")
if _count == 3:
    print("The value of a is", _count)
    _nodes+=[{
        role = "worker"
        image = option("kindimage",default="kindest/node:v1.30.0")
    }
    ]
elif _count == 5:
    _nodes+=[
        {
        role = "worker"
        image = option("kindimage",default="kindest/node:v1.30.0")
    },{
        role = "worker"
        image = option("kindimage",default="kindest/node:v1.30.0")
    },{
        role = "worker"
        image = option("kindimage",default="kindest/node:v1.30.0")
    },{
        role = "worker"
        image = option("kindimage",default="kindest/node:v1.30.0")
    },{
        role = "worker"
        image = option("kindimage",default="kindest/node:v1.30.0")
    }
    ]

#print("The value of a is", _count)
#print(_count==3)
#print(_count==5)

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="0.0.0.0")
    ipFamily = option("ipFamily",default="ipv4")
    kubeProxyMode = option("kubeProxyMode",default="iptables")
}
nodes=_nodes

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

do not run repeat if condition.

3. What did you see instead (Required)

You can see that print was executed repeatedly, and i just print once. Remind me if i missed something,Thanks.

$ kcl kind2.k -D count=3
The value of a is 3
The value of a is 3
The value of a is 3
The value of a is 3
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
  apiServerAddress: '0.0.0.0'
  ipFamily: ipv4
  kubeProxyMode: iptables
nodes:
- role: control-plane
  image: kindest/node:v1.30.0
  extraMounts:
  - hostPath: /home/runner/work/lanactions/lanactions/config/apiserver-audit-policy.yaml
    containerPath: /etc/kubernetes/audit-policy/apiserver-audit-policy.yaml
  kubeadmConfigPatches:
  - |
    kind: ClusterConfiguration
    etcd:
      external:
        endpoints: [http://192.168.66.2:2379]
    apiServer:
      extraArgs:
        runtime-config: api/all=true
        storage-media-type: application/json
        audit-log-path: /var/log/audit/kube-apiserver-audit.log
        audit-policy-file: /etc/kubernetes/audit-policy/apiserver-audit-policy.yaml
      extraVolumes:
        - name: "audit-logs"
          hostPath: /var/log/audit
          mountPath: /var/log/audit
        - name: audit-policy
          hostPath: /etc/kubernetes/audit-policy
          mountPath: /etc/kubernetes/audit-policy
- role: worker
  image: kindest/node:v1.30.0
- role: worker
  image: kindest/node:v1.30.0
- role: worker
  image: kindest/node:v1.30.0

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

$ kcl version
0.9.0-beta.1-linux-amd64
He1pa commented 2 weeks ago

Minimal reproduce step

_a = 1
if True:
    _a += 1
elif False:
    _a += 1
a=_a
Peefy commented 1 week ago

Closed by #1416, #1418 and #1423