Git Product home page Git Product logo

Comments (7)

Peefy avatar Peefy commented on July 17, 2024 1

The kcl code should be

envs = option("envs",type="dict", help="环境变量")

Here's my example:
image

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024 1

In KCL the option key need to be offered a positional argument instead of keyword argument. For the latter, we will support it in the next version.

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024 1

In KCL the option key need to be offered a positional argument instead of keyword argument. For the latter, we will support it in the next version.

Many parameters did not use the key syntax, but this one did, which seems a bit unfortunate.
Thank you for your support.

from kcl-go.

Peefy avatar Peefy commented on July 17, 2024

You can use a json format like

	var envs = `
	{
		"aa":"bb"
	}
	`

Note that a valid JSON string does not have a comma at the end of the line.

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

You can use a json format like

	var envs = `
	{
		"aa":"bb"
	}
	`

Note that a valid JSON string does not have a comma at the end of the line.

It’s still not working.

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

full code
main.go

package main

import (
	"fmt"

	"kcl-lang.io/kcl-go/pkg/kcl"
	"kcl-lang.io/kcl-go/pkg/loader"
)

func main() {
	//yaml := kcl.MustRun("kubernetes.k", kcl.WithCode(code), kcl.WithOptions("metadata.name=huangyf", "metadata.lables=111")).GetRawYamlResult()
	//fmt.Println(yaml)
	ops, err := loader.ListFileOptions("amd64.k")
	// ops, err := loader.ListFileOptions("./myk8s/main.k")
	if err != nil {
		panic(err)
	}
	fmt.Println(ops)
	for _, op := range ops {
		//fmt.Println(op.Name)
		//fmt.Println(op.Type)
		//fmt.Println(op.Required)
		//fmt.Println(op.DefaultValue)
		fmt.Printf("变量名为%s类型是%v默认值为%v是否必填%v\n", op.Name, op.Type, op.DefaultValue, op.Required)
	}
	var envs = `
	{
		"aa":"bb"
	}
	`
	fmt.Println(kcl.MustRun("amd64.k", kcl.WithOptions("app=nginx", "image=aaa", fmt.Sprintf("envs=%s", envs))).GetRawYamlResult())
}

const code = `

metadata = {
	name = "nginx"
	labels = "app"
}

apiVersion = "apps/v1"
kind = "Deployment"
spec = {
    replicas = 3
    selector.matchLabels = metadata.labels
    template.metadata.labels = metadata.labels
    template.spec.containers = [
        {
            name = metadata.name
            image = "${metadata.name}:1.14.2"
            ports = [{ containerPort = 80 }]
        }
    ]
}
`

amd64.k

_image = option("image", help="镜像地址", required=True)
_app : str = option("app", help="应用名称", required=True)
_namespace : str = option("namespace", help="命名空间") or "default"
envs = option(key="envs",type="dict", help="环境变量")
MQTT_BROKER_URL = option("MQTT_BROKER_URL", help="MQTT_BROKER_URL", required=False) or "tcp://1111"
__EDGEMANAGER__CLOUDBROKERADDR = option("__EDGEMANAGER__CLOUDBROKERADDR", help="__EDGEMANAGER__CLOUDBROKERADDR", required=False) or "tcp://1111"
__EDGEMANAGER__NODEID = option("__EDGEMANAGER__NODEID", help="__EDGEMANAGER__NODEID", required=False) or "tcp://1111"
__EDGEMANAGER__CACERTHTTPSERVER = option("__EDGEMANAGER__CACERTHTTPSERVER", help="__EDGEMANAGER__CACERTHTTPSERVER", required=False) or "tcp://1111"
_static_envs = [
                            # {
                            #     name = "MQTT_BROKER_URL"
                            #     value = MQTT_BROKER_URL
                            # }
                            # {
                            #     name = "__EDGEMANAGER__CLOUDBROKERADDR"
                            #     value = __EDGEMANAGER__CLOUDBROKERADDR
                            # }
                            # {
                            #     name = "__EDGEMANAGER__NODEID"
                            #     value = __EDGEMANAGER__NODEID
                            # }
                            # {
                            #     name = "__EDGEMANAGER__CACERTHTTPSERVER"
                            #     value = __EDGEMANAGER__CACERTHTTPSERVER
                            # }
]
# print(envs)
# print(_app)
# assert envs != None , "envs is required"
if envs:
    _dy_env = [{
        name: k
        value: v
    } for k,v in envs]
else:
    _dy_env = []
{
    apiVersion = "apps/v1"
    kind = "Deployment"
    metadata = {
        name = _app
        namespace = _namespace
    }
    spec = {
        replicas = 1
        selector = {
            matchLabels = {
                app = _app
            }
        }
        template = {
            metadata = {
                labels = {
                    app = _app
                }
            }
            spec = {
                containers = [
                    {
                        name = _app
                        image = _image
                        env = _static_envs +  _dy_env
                        ports = [
                            {
                                containerPort = 80
                            }
                        ]
                        volumeMounts = [
                            {
                                mountPath = "/data"
                                name = "data"
                            }
                        ]
                    }
                ]
                volumes = [
                    {
                        name = "data"
                        persistentVolumeClaim = {
                            claimName = _app
                        }
                    }
                ]
            }
        }
    }
}
# if envs:
#     _a=[{name: k, value: envs[k]} for k in envs]
# print(_a)

from kcl-go.

reckless-huang avatar reckless-huang commented on July 17, 2024

What is the difference between them?

envs = option("envs",type="dict", help="环境变量")
envs = option(key="envs",type="dict", help="环境变量")

from kcl-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.