Skip to main content

72 posts tagged with "KCL"

View All Tags

· 14 min read

Introduction

The KCL team is pleased to announce that KCL v0.8.0 is now available! This release has brought three key updates to everyone: Language, Tools, and Integrations.

  • Use KCL language, tools and IDE extensions with more complete features and fewer errors to improve code writing experience and efficiency.

  • More comprehensive and rich community ecosystem integration, improving operation and maintenance experience.

  • More comprehensive KCL third-party dependencies, easier integration with cloud-native ecosystem.

KCL v0.8.0 is now available for download at KCL v0.8.0 Release Page or KCL Official Website.

KCL is an open-source, constraint-based record and functional language hosted by Cloud Native Computing Foundation (CNCF). KCL improves the writing of numerous complex configurations, such as cloud-native scenarios, through its mature programming language technology and practice. It is dedicated to building better modularity, scalability, and stability around configurations, simpler logic writing, faster automation, and great built-in or API-driven integrations.

This blog will introduce the content of KCL v0.8.0 and recent developments in the KCL community to readers.

Language Updates

🚗 Grammar and Semantics Updates

KCL supports show-hidden

KCL v0.8.0 adds support for --show-hidden to display private variables.

The main.k

a = {_b = 1}

Compile by kcl run main.k --show-hidden.

a:
_b: 1

KCL supports arguments and keyword arguments union

KCL v0.8.0 adds support for arguments and keyword arguments union. Schema instances with arguments will union arguments during union operations.

schema Person[separator]:
firstName: str = "John"
lastName: str
fullName: str = firstName + separator + lastName

x = Person(" ") {lastName = "Doe"}

y = Person("-") {lastName = "Doe1"}

z = x | y

The compiled result is as follows:

x:
firstName: John
lastName: Doe
fullName: John Doe
y:
firstName: John
lastName: Doe1
fullName: John-Doe1
z:
firstName: John
lastName: Doe1
fullName: John-Doe1

KCL supports scalar yaml stream output

In v0.8.0, by using yaml_stream method, you can output the result of yaml scalar.

import manifests

x0 = 1
x1 = 2
manifests.yaml_stream([x0, x1])

The compiled result is as follows:

1
---
2

KCL removes the __settings__ attribute in the compiled output

In v0.8.0, the __settings__ attribute is removed from the compiled output.

schema Person:
__settings__: {str:str} = {"output_type": "STANDALONE"}
name?: str
age?: int
school?: str

a = Person{
name: "a",
}

The compiled result is as follows.

a:
name: a

KCL supports the key and value evaluation in the config expression

In v0.8.0, KCL supports the key and value evaluation in the config expression.

_data = {
"a": 'foo'
"b": 'bar'
}

r0 = [{v = k} for k, v in _data]
r1 = [{k = v} for k, v in _data]
r2 = [{k.foo = v} for k, v in _data]
r3 = [[k] for k, v in _data]
r4 = [[k, v] for k, v in _data]

The compiled result is as follows.

r0:
- foo: a
- bar: b
r1:
- a: foo
- b: bar
r2:
- a:
foo: foo
- b:
foo: bar
r3:
- - a
- - b
r4:
- - a
- foo
- - b
- bar

🚀 Diagnostic Optimization

KCL uses the elif keyword in the if block, not else if.

Compile the following KCL program:

if True: a = 1
else if False: b = 1

The diagnostic information in KCL has added suggestions for error correction:

error[E1001]: InvalidSyntax
--> main.k:2:6
|
2 | else if False: b = 1
| ^ 'else if' here is invalid in KCL, consider using the 'elif' keyword
|

🚀 Language writing experience optimization

KCL standard library adds file system access functions

KCL has added methods to access the file system. In v0.8.0, it supports methods such as read, glob, etc. to access the file system.

By using the read, you can read the contents of a file as a string.

import file

a = file.read("hello.txt")

Add the following content to the hello.txt file:

Hello World !

The compilation result is as follows:

a: Hello World !

By combining the json.decode method, you can easily deserialize a JSON file.

Add the following content to the hello.json file:

{
"name": "John",
"age": 10
}

KCL program:

import file
import json

_a = json.decode(file.read("hello.json"))

name = _a.name
age = _a.age

The compilation result is as follows:

name: John
age: 10

More details - https://kcl-lang.io/zh-CN/docs/reference/model/file/

KCL Compiler supports the use of environment variable KCL_CACHE_PATH to specify the cache path

KCL Compiler will cache the generated code to the directory specified by the environment variable KCL_CACHE_PATH. If not specified, it will be generated to the project root directory.

KCL Plugin System supports using Golang to write KCL plugins

KCL Plugin System supports using Golang to write KCL plugins. The following is an example of using Golang to define the hello plugin.

package hello_plugin

import (
"kcl-lang.io/kcl-go/pkg/plugin"
)

func init() {
plugin.RegisterPlugin(plugin.Plugin{
Name: "hello",
MethodMap: map[string]plugin.MethodSpec{
"add": {
Body: func(args *plugin.MethodArgs) (*plugin.MethodResult, error) {
v := args.IntArg(0) + args.IntArg(1)
return &plugin.MethodResult{V: v}, nil
},
},
},
})
}

With the hello plugin, you can use the add method in the KCL program.

package main

import (
"fmt"

"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kcl-go/pkg/native" // Import the native API
_ "kcl-lang.io/kcl-go/pkg/plugin/hello_plugin" // Import the hello plugin
)

func main() {
// Note we use `native.MustRun` here instead of `kcl.MustRun`, because it needs the cgo feature.
yaml := native.MustRun("main.k", kcl.WithCode(code)).GetRawYamlResult()
fmt.Println(yaml)
}

const code = `
import kcl_plugin.hello

name = "kcl"
three = hello.add(1,2) # 3

😸 KCL supports linux arm64

KCL v0.8.0 adds support for the Linux arm64 platform in the release product.

You can find the compressed package with the suffix linux-arm64 on the KCL Release Page

🏄 SDK & API Updates

Rust SDK

KCL Rust SDK provides a series of APIs for compiling, validating, testing, and formatting KCL files.

KCL Rust SDK: https://github.com/kcl-lang/lib

Java SDK

KCL Java SDK adds syntax tree, scope, symbol, and other syntax and semantic structure definitions and related query APIs.

Go SDK

  • KCL Doc supports output in OpenAPI format.
  • Go SDK add APIs for parsing.

API Update

  • KCL API adds API for validating JSON and YAML files.
  • Introducing syntax and semantic analysis APIs for analyzing KCL code.
  • Introducing a binary artifact build API for caching compilation results.
  • Introducing a binary artifact execution API for directly running compiled results, avoiding redundant compilation and improving performance.
  • Introducing a code generation API to programmatically implement KCL code generation instead of writing complex templates.

More updates can be found here.

🐞 Other Updates and Bug Fixes

  • Fixed compilation errors caused by using the -S compilation parameter in KCL CLI.
  • Corrected an issue in the kcl fmt tool where an extra newline was added at the end when formatting lambda expressions.
  • Resolved an error in Schema Doc code completion snippets.
  • Fixed a recursive check error for required properties in Schema objects.
  • Enhanced the robustness of Schema index signature type checks.
  • Addressed an issue where string identifiers like “$if” were not correctly defined within Schema.
  • Optimized error messages for unexpected tokens in syntax errors.
  • Rectified variable calculations in unexpected dictionary comprehension expressions where the key and loop variable were the same.

IDE & Toolchain Updates

IDE Updates

IDE semantic-level highlighting enhancement

KCL IDE previously only supported KCL syntax highlighting, as shown in the figure below:

old-ide

We have gradually introduced a new KCL semantic model in the past year. With the support of the new semantic model, KCL IDE now supports semantic-level highlighting. Code that is semantically related will be highlighted in the same way.

new-ide

For more information about the KCL semantic model, see: Unlocking Advanced Code Intelligence with the KCL Semantic Model

IDE supports completion of builtin methods

KCL IDE supports completion of builtin methods, as shown in the figure below:

builtin-completion

IDE supports quick fix for variable reference errors

KCL IDE supports quick fix for variable reference errors, as shown in the figure below:

quick-fix

IDE supports incremental parsing and asynchronous compilation

IDE supports incremental parsing and asynchronous compilation through the new semantic model, which improves the compilation speed and writing experience.

More details: https://kcl-lang.io/zh-CN/blog/2023-12-09-kcl-new-semantic-model

IDE LSP bug fixes

  • Fixed an issue where string interpolation variables in assert statements couldn’t navigate.
  • Corrected an exception-triggering issue in function autocompletion within strings.
  • Resolved an autocompletion error when a comment followed a string.
  • Fixed an issue where internal property symbols in schemas couldn’t navigate.
  • Addressed an exception in alias semantic checks and autocompletion for import statements.
  • Rectified autocompletion issues in check expressions within schemas.
  • Fixed autocompletion errors in nested schema definitions.
  • Resolved missing hover information for certain elements.
  • Ensured consistent symbol types for autocompletion across different syntaxes.
  • Distinguished between schema type and instance autocompletion symbols.
  • Standardized schema comment documentation autocompletion format.
  • Fixed issues where symbols within configuration block if statements couldn’t navigate or autocompletion was affected.

Vet Tool Updates

In v0.8.0 release, we have optimized the diagnostic of the KCL verification tool. In the work of using the KCL verification tool to verify json/yaml files, the abnormal location of the json file will be accurately located.

Take hello.json as an example, we will verify the content of the json file through the following main.k file.

{
"name": 10,
"age": 18,
"message": "This is Alice"
}

The main.k

schema User:
name: str
age: int
message?: str

We can verify the content of the json file through the following command.

kcl vet hello.json main.k

We can see the error location in the json file:

error[E2G22]: TypeError
--> test.json:2:5
|
2 | "name": 10,
| ^ expected str, got int(10)
|

--> main.k:2:5
|
2 | name: str
| ^ variable is defined here, its type is str, but got int(10)
|

KCL cli supports git repository as a compilation entry

By using the following command, you can use the KCL git repository as a compilation entry.

kcl run <git url>

kcl mod graph supports output KCL package dependency graph

You can use command kcl mod graph to output the KCL package dependency graph.

KCL Package Management

KCL Package Management supports adding git dependencies through commit

KCL Package Management adds the ability to add git repo dependencies through commit. For example, using https://github.com/KusionStack/catalog,add commit a29e3db as a dependency. You can add it through editing the kcl.mod file or directly through the command line.

Edit the kcl.mod file as follows:

[dependencies]
catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" }

Or add it through the command line:

kcl mod add --git https://github.com/KusionStack/catalog.git --commit a29e3db

KCL Package Management supports the dependency name with -

KCL Package Management supports the dependency name with -. For example, you can add the set-annotation as a dependency through the following command:

kcl mod add set-annotation

In the KCL program, you can reference it through set_annotation.

import set_annotation

KCL Import Tool

  • Support mapping OpenAPI multiplyOf specification to KCL multiplyof function for validation.
  • Support outputting multiple KCL files from YAML Stream-formatted Kubernetes CRD files.
  • Support generating validation expressions for OpenAPI allOf keyword.
  • Support generating validation expressions for KCL array and dictionary types using all/any.

Community Integrations & Extensions Updates

Flux KCL Controller Release

We have developed Flux KCL Controller to supports KCL integration with Flux. After installing Flux KCL Controller in the cluster, you can use the following resources to achieve continuous integration of KCL git repositories through FluxCD.

apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
name: kcl-deployment
namespace: source-system
spec:
interval: 30s
# The URL of the git repository
url: https://github.com/awesome-kusion/kcl-deployment.git
ref:
branch: main
---
apiVersion: krm.kcl.dev.fluxcd/v1alpha1
kind: KCLRun
metadata:
name: kcl-deployment
namespace: source-system
spec:
sourceRef:
kind: GitRepository
name: kcl-deployment

More details: https://kcl-lang.io/zh-CN/blog/2024-02-01-biweekly-newsletter/

CodeQL KCL Tool

We finished CodeQL KCL dbschema definition and data extraction for KCL syntax and semantics. We can use CodeQL to query KCL code for static analysis and scanning to improve code security.

More details: https://github.com/kcl-lang/codeql-kcl

KCL Modules Update

There are 303 KCL modules in the KCL v0.8.0 release on https://artifacthub.io, mainly including new models related to Crossplane Provider and libraries related to JSON merge operations.

kcl run oci://ghcr.io/kcl-lang/podinfo -D replicas=2
  • JSON Schema library released version 0.0.4, fixed type definition errors. You can execute the following command to update or add dependencies.
kcl mod add jsonschema:0.0.4

Other Updates

The full update and bugfix List of KCL v0.8.0 can be found at: https://github.com/kcl-lang/kcl/compare/v0.7.0...v0.8.0

Document Updates

The versioning semantic option is added to the KCL website. Currently, v0.4.x, v0.5.x, v0.6.x, v0.7.0 and v0.8.0 versions are supported.

Community Updates

KCL LFX Project KickOff

Congratulations to @AkashKumar7902, @octonawish-akcodes, @shashank-iitbhu for being selected for the CNCF KCL LFX project, and thanks to @Vanshikav123, @Amit Pandey for their active participation.

KCL on Crossplane Function Market

After the release of the combination function in Crossplane v1.14, the scope of using Crossplane to build cloud-native platforms has been rapidly expanded. The KCL team has followed up and proactively built a reusable function. The entire Crossplane ecosystem can now leverage the high-level experience and capabilities provided by KCL to build its own cloud-native platform.

More details: https://blog.crossplane.io/function-kcl/

Special Thanks

Thanks to the community friends for their contributions to KCL v0.8.0. The following list is in no particular order:

  • Thanks to @jakezhu9 for the continuous contribution to the kcl import tool 🤝
  • Thanks to @octonawish-akcodes for the continuous contributions to KCL code cleanup and FAQ documentation 🙌
  • Thanks to @satyazzz123 for contributing to the support of reading environment variables in KRM KCL 🙌
  • Thanks to @AkashKumar7902 for the contributions to the package management tool feature in KCL 🙌
  • Thanks to @UtkarshUmre for the contribution to the KCL linux-arm64 build CI 🙌
  • Thanks to @octonawish-akcodes and @d4v1d03 for the continuous contributions to KCL FAQ documentation and KCL IDE feature 🙌
  • Thanks to @octonawish-akcodes for the contribution to the Ansible KCL Module
  • Thanks to @AkashKumar7902 and @Vanshikav123 for the contributions to the package management tool feature in KCL 🙌
  • Thanks to @StevenLeiZhang for the contributions to KCL documentation and KCL plugin
  • Thanks to @patrycju, @Callum Lyall, @Matt Gowie, @ShiroDN, @FLAGLORD, @YiuTerran, @flyinox, @steeling, @Anoop, @Even Solberg, @Phillip Neumann, @Naxe, @rozaliev, @CloudZero357, @martingreber, @az, @Art3mK, @Erick, @TheChinBot, @Evgeny Shepelyuk, @yonas, @vtomilov, @Fdall, @bozaro, @starkers, and @MrGuoRanDuo for their valuable suggestions and feedback during the iteration process of KCL v0.8 🙌

Next Steps

We expect to release KCL v0.9.0 in May 2024. For more details, please refer to KCL 2024 Roadmap and KCL v0.9.0 Milestone. If you have more ideas and needs, please feel free to raise Issues or Discussions in the KCL Github repository, and welcome to join our community for discussion 🙌 🙌 🙌

Additional Resources

For more information, see KCL FAQ.

Resources

Thank all KCL users for their valuable feedback and suggestions during this version release. For more resources, please refer to:

See the community for ways to join us. 👏👏👏

· 4 min read

Content sourced from the original Crossplane official blog post: https://blog.crossplane.io/function-kcl

Introduction

KCL is a constraint-based record and functional language hosted by Cloud Native Computing Foundation (CNCF) that enhances the writing of complex configurations and polices, including those for cloud-native scenarios. With its advanced programming language technology and practices, KCL is dedicated to promoting better modularity, scalability, and stability for configurations. It enables simpler logic writing and offers ease of automation APIs and integration with homegrown systems.

Since the release of the beta version of composite functions in Crossplane v1.14, the potential for building cloud-native platforms using Crossplane has been rapidly expanding. The KCL team promptly followed up and actively built a reusable function. The entire Crossplane ecosystem can now utilize the high-level experience and capabilities provided by KCL to build its own cloud-native platform.

Additionally, Crossplane announced that the KCL function has become the first third-party function component to be released to the Upbound Marketplace, available at https://marketplace.upbound.io/providers/crossplane-contrib/function-kcl. The source code can be found at https://github.com/crossplane-contrib/function-kcl, and contributions and feedback are welcome.

You can install the function-kcl with the following command and start using it across the entire Crossplane control plane:

crossplane xpkg install function xpkg.upbound.io/crossplane-contrib/function-kcl:v0.2.0

The Crossplane team and community are thankful for the KCL team's significant contribution, and its substantial enhancement to the evolving Functions for Crossplane ecosystem!

crossplane-announcing

Crossplane, along with its composite model, allows developers to create higher-level abstractions that encapsulate and combine various types of cloud resources across different providers and services. Rendering these abstractions using composite functions can effectively enhance the template functionality of various provider resources while reducing the amount of YAML code required.

Combining KCL with Composition Functions offers several benefits:

  • Simplification of Complex Configurations: KCL provides a more concise syntax and structure as a DSL, reducing the complexity of configurations. When combined with Crossplane’s composite resources, you can create more intuitive and easy-to-understand configuration templates with loop and condition features, simplifying the definition and maintenance of resources instead of duplicate YAMLs.
  • Reusability and Modularity: KCL supports modularity and code reuse through OCI Registry, which means you can create reusable configuration components. Combined with Crossplane, this promotes the modularity of composite resources, increases the reuse of configurations, and reduces errors.
  • Automation and Policy-Driven: You can use KCL’s powerful features to write policies and constraints that, combined with Crossplane’s declarative resource management, can be automatically enforced, ensuring compliance within the cloud environment.

Getting Started

There are two ways to combine KCL and Crossplane:

  • One involves using KCL to write Crossplane composite functions and install them for use in a cluster, still using YAML to define the schema and input required by the app team. KCL is used to write rendering logic to Crossplane Managed Resource to interface with different cloud platforms or Kubernetes clusters. Note: This approach can install KCL functions for use in the cluster and also use the crossplane beta render command to complete Managed Resource rendering directly on the client.

crossplane-kcl-func

  • The other method involves using KCL to entirely provide abstractions for application developers on the client side and generating Crossplane managed resources to deploy to a cluster, providing a unified programmable access layer for Kubernetes. Specific usage defines the schema input required by the app team using KCL Schema and writes the logic to render it to Crossplane Managed Resource to interface with different cloud platforms or Kubernetes clusters.

kcl-on-crossplane

For detailed instructions on both approaches, please refer to the Crossplane official blog content: https://blog.crossplane.io/function-kcl

crossplane-kcl-blog

Both of these methods require a Registry to facilitate the work. The final choice between them may depend on your operational habits and environmental costs. Regardless of the chosen method, we recommend maintaining KCL code in Git for better GitOps implementation and better IDE experience and reusable modules, such as using the Crossplane AWS Module: https://github.com/kcl-lang/modules/tree/main/crossplane-provider-aws

Summary

The function-kcl project is now donated to the Crossplane community, and we encourage the entire community to test it and try using KCL (the latest advanced language experience provided by Crossplane Functions) to build a cloud-native control plane. We welcome contributions and feedback from the community on the GitHub repository. Let us know your thoughts! https://github.com/crossplane-contrib/function-kcl

For more resources, please refer to: