The KCL team is happy to announce that version 0.4.4 is now available!This release mainly adds the ability to customize the output of YAML Manifests to the KCL language. Users can customize the style of YAML output by writing code and calling system functions without understanding complex schema settings semantics; in addition, this release provides the latest KCL Python SDK It can be used for Python users to directly integrate KCL; at the same time, we have greatly reduced the size of the KCL installation package, and the average installation package size has been reduced to one-fifth of the previous version, and it includes a number of compiler error message optimizations and bug fixes.You can find KCL release page Get more detailed release information and links to KCL binary downloads.

KCL is an open-source constraint-based recording and functional language. It expects to improve the writing of a large number of complex configurations and policies through mature programming language technologies and practices, and is committed to building better modularity, scalability and stability around configurations. , easier logic writing, faster automation integration and good ecological scalability.

This article will introduce readers to the recent developments in the KCL community.

New features​

Custom YAML format output​

In previous KCL versions, the style of YAML output was hard-coded in the KCL compiler, and the user could set the __settings__ Meta properties are set to different values ​​to determine the YAML output style, which brings high complexity and memory cost, so in version 0.4.4 we provide a system library function for developers to customize YAML more easily Output style, the signature of this function is as follows:


manifests.yaml_stream(values: [any], opts: {str:} = {
    sort_keys = False
    ignore_private = True
    ignore_none = False
    sep = "---"
})

The function of this function is to serialize the list of KCL objects into --- Delimiter style YAML output, which has two arguments:

  • values – a list of KCL objects
  • opts – YAML serialization options
    • sort_keys: Whether to sort the serialized results lexicographically by property names (default is False).
    • ignore_private: Whether to ignore names ending in _ Attributes starting with the serialized output (defaults to True).
    • ignore_none: whether to ignore the value None properties (defaults to False).
    • sep: What kind of delimiter to choose between multiple YAML documents (the default is "---").

Let’s illustrate with an example:


import manifests

schema Deployment:
    apiVersion: str = "v1"
    kind: str = "Deployment"
    metadata: {str:} = {
        name = "deploy"
    }
    spec: {str:} = {
        replica = 2
    }

schema Service:
    apiVersion: str = "v1"
    kind: str = "Service"
    metadata: {str:} = {
         name = "svc"
    }
    spec: {str:} = {}    
        
deployments = [Deployment {}, Deployment {}]
services = [Service {}, Service {}]

manifests.yaml_stream(deployments + services)

First we pass import keyword import manifests module and define 2 Deployment and 2 Service resources, when we want to use YAML stream and use --- When outputting these 4 resources sequentially in the format as delimiter, we can combine them into one KCL list and use it as manifests.yaml_stream functional values Formal parameters are passed in (if there is no special requirement, the opts parameter can generally use the default value), and the final YAML output is:


apiVersion: v1
kind: Deployment
metadata:
  name: deploy
spec:
  replica: 2
---
apiVersion: v1
kind: Deployment
metadata:
  name: deploy
spec:
  replica: 2
---
apiVersion: v1
kind: Service
metadata:
  name: svc
---
apiVersion: v1
kind: Service
metadata:
  name: svc

For more information, please refer to: https://github.com/KusionStack/KCLVM/issues/94

Python SDK​

In addition to existing KCL Go SDK, KCL Python SDK is also added in this release. Using the Python SDK requires you to have a local Python version higher than 3.7.3 and the pip package management tool. You can install it and get help information through the following commands


$ python3 -m pip install kclvm && python3 -m kclvm --help

command line tools​

write named main.k The KCL file:


name = "kcl"
age = 1

schema Person:
    name: str = "kcl"
    age: int = 1

x0 = Person {}
x1 = Person {
    age = 101
}

Execute the following command and get the output:


$ python3 -m kclvm hello.k
name: kcl
age: 1
x0:
  name: kcl
  age: 1
x1:
  name: kcl
  age: 101

API​

In addition, we can also execute the KCL file through Python code

write named main.py The python file:


import kclvm.program.exec as kclvm_exec
import kclvm.vm.planner as planner

print(planner.plan(kclvm_exec.Run(["hello.k"]).filter_by_path_selector()))

Execute the following command and get the output:


$ python3 main.py
name: kcl
age: 1
x0:
  name: kcl
  age: 1
x1:
  name: kcl
  age: 101

It can be seen that the same output can be obtained through the command line tool and the API.

At present, the KCL Python SDK is still in the early preview version, and the KCL team will continue to update and provide more functions in the future. For more information, please refer to: https://github.com/KusionStack/kclvm-py

Installation volume optimization​

In the new KCL version, we stripped the built-in Python3 of KCL, reducing the size of KCL binary compressed package from an average of 200M to 35M, users can download and use KCL faster, and Python Plugin becomes an option, if you want To enable the KCL Python plugin, an additional requirement is that you need to have a local version of Python higher than 3.7.3 and the pip package management tool. For more details, please refer to https://github.com/KusionStack/kcl-plugin

Bug fixes​

Function call error message optimization​

In version 0.4.4, KCL optimizes the error message output when the number of function parameters does not match, and supports displaying the function name and the number of parameters that do not match


schema Foo[x: int]:
    bar?: int = x

f = lambda x {
    x + 1
}

foo = Foo(1,2,3)  # Error: "Foo" takes 1 positional argument but 3 were given
f(1,2)  # Error: "f" takes 1 positional argument but 2 were given

For more information, please refer to: https://github.com/KusionStack/KCLVM/issues/299

Interpolation triple quote string formatting error fix​

In the previous KCL version, formatting the following code would mistakenly format triple quotes with string interpolation into single quote strings and cause compilation errors. We have fixed it in version 0.4.4


# Before KCL v0.4.4, variable "bar" will be formatted as:
#
# foo = 1
# bar = "
# ${foo}
# "
foo = 1
bar = """
${foo}
"""

Copy

For more information, please refer to: https://github.com/KusionStack/KCLVM/issues/294

Other bug fixes​

For more bug fixes, see: https://github.com/KusionStack/KCLVM/milestone/2?closed=1

KCL website Preliminary establishment and improvement of Kubernetes scene-related documents.

For more website information see https://kcl-lang.github.io/

Community dynamics​

The KCL community has added three external contributors @my-vegetable-has-exploded, @possible-fqz, @orangebees, thank them for their enthusiastic and active participation in the contribution

next step​

It is expected that by the end of January 2023, we will release the KCL v0.4.5 version, and the expected key evolution includes

  • Continuous optimization of language user interface, continuous improvement of experience and resolution of user pain points
  • More scenarios and ecosystems such as Kubernetes and CI/CD Pipeline scenarios KCL support and document updates
  • KCL Windows version support
  • KCL package management tool kpm released
  • KCL new playground support

For more details, please refer to KCL v0.4.5 Milestone

Frequently Asked Questions and Answers​

For frequently asked questions and answers, see: https://kcl-lang.github.io/docs/user_docs/support/

Other resources​

Welcome to join our community for communication 👏👏👏: https://github.com/KusionStack/community

#KCL #v044 #released #Custom #YAML #Manifests #output #Python #SDK #News Fast Delivery

Leave a Comment

Your email address will not be published. Required fields are marked *