Skip to main content

71 posts tagged with "KCL"

View All Tags

· 5 min read

KCL is a constraint-based record and functional language hosted by Cloud Native Computing Foundation (CNCF) that enhances the writing of complex configurations, 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.

This section will update the KCL language community's latest developments every two weeks, including features, website updates, and the latest community news, helping everyone better understand the KCL community!

KCL Website: https://kcl-lang.io

Overview

Thank you to all contributors for their outstanding work over the past two weeks (11.24 - 12.07 2023). Here is an overview of the key content:

📦 Module Update

The number of KCL models has increased to 240, mainly including models related to Crossplane Provider and libraries related to JSON merging operations.

🔧 Toolchain Update

  • Documentation Tool Updates
    • Support documentation generation for third-party libraries that models depend on, such as the k8s module.
  • Validation Tool Updates
    • Support validation results and error localization to YAML/JSON files, outputting error line and column number information.
  • Import Tool Updates
    • Support mapping OpenAPI multiplyOf specification to KCL multiplyof function for validation.
    • Support outputting YAML Stream format Kubernetes CRD files into multiple KCL files.
    • Optimize KCL code generation by removing empty check statements.

🏄 SDK Update

In addition to the existing Go and Python SDKs in KCL, a new Rust SDK has been added (without LLVM dependency), which includes APIs for KCL file compilation, validation, testing, and code formatting.

💻 IDE Updates

  • Developer Experience
    • Support incremental parsing and asynchronous compilation to enhance performance.
  • Bug Fixes
    • Fixed the issue where string interpolation variables in assert statements cannot be navigated.
    • Fixed the issue where exceptional triggering of function completion in strings.
    • Fixed the issue with alias semantic check and completion in import statements.
    • Fixed the issue with check expression completion in schemas.

📒 Documentation Updates

Special Thanks

The following are listed in no particular order:

  • Thanks to @professorabhay for supporting KCL testing diff function 🙌 https://github.com/kcl-lang/kcl/issues/940
  • Thanks to @patrycju, @Callum Lyall, @Even Solberg, @Matt Gowie, and @ShiroDN for their valuable feedback and discussions during the promotion and usage of KCL 🙌

Using Kubernetes Strategy Merge Patch to Update Configurations in KCL

In the current version of KCL, various attribute operators are supported to update and override configurations. However, the capability is relatively atomic and cannot cover the typical configuration strategy scenarios in cloud-native environments.

For Kubernetes configurations, it is common to use the JSON Merge Patch and Strategy Merge Patch capabilities natively supported by Kubernetes e.g., using tools such as kubectl patch, kustomize, and other patching capabilities supported by cloud-native configuration and policy tools.

To avoid repeatedly using KCL attribute operators to write configuration patch template codes when dealing with Kubernetes configurations, we provide the Kubernetes Strategy Merge Patch library for updating Kubernetes configurations. This library supports all merging strategies defined by native Kubernetes objects, such as overwriting, modifying, and adding items to list objects. Here is how to use it:

Create a new project and add the Strategy Merge Patch library dependency:

kcl mod init && kcl mod add strategic_merge_patch

Write the configuration patch code in main.k (using the labels, replicas, and container attributes of a Deployment template as an example):

import strategic_merge_patch as s

original = {
apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
name = "my-deployment"
labels.app = "my-app"
}
spec: {
replicas = 3
template.spec.containers = [
{
name = "my-container-1"
image = "my-image-1"
}
{
name = "my-container-2"
image = "my-image-2"
}
]
}
}
patch = {
apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
name = "my-deployment"
labels.version = "v1"
}
spec: {
replicas = 4
template.spec.containers = [
{
name = "my-container-1"
image = "my-new-image-1"
}
{
name = "my-container-3"
image = "my-image-3"
}
]
}
}
got = s.merge(original, patch)

Run the command to get the output:

kcl run

The output will be:

original:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
labels:
app: my-app
spec:
replicas: 3
template:
spec:
containers:
- name: my-container-1
image: my-image-1
- name: my-container-2
image: my-image-2
patch:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
labels:
version: v1
spec:
replicas: 4
template:
spec:
containers:
- name: my-container-1
image: my-new-image-1
- name: my-container-3
image: my-image-3
got:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
labels:
app: my-app
version: v1
spec:
replicas: 4
template:
spec:
containers:
- name: my-container-1
image: my-new-image-1
- name: my-container-2
image: my-image-2
- name: my-container-3
image: my-image-3

As seen in the output, the labels, replicas, and container fields of the Deployment template have all been updated with the correct values. For more documentation and usage examples, please refer to the document.

Resources

❤️ Thanks to all KCL users and community members for their valuable feedback and suggestions in the community. See here to join us!

For more resources, please refer to

· 9 min read

Introduction

The KCL team is pleased to announce that KCL v0.7.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.

  • The new KCL CLI integrates KCL package management, doc, test and other peripheral ecosystems.

  • The rich KCL third-party library market artifacthub.io provides more than 200 KCL third-party libraries for you to choose from.

KCL v0.7.0 is now available for download at KCL v0.7.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.7.0 and recent developments in the KCL community to readers.

Language Updates

⭐️ New KCL CLI

When compiling, use kcl, when downloading packages, use kpm, if you have a KCL model that you want to send to the cluster, you also need to use kusion, kcl is the compilation command, kpm run can also be compiled, I also found kusion compile in the kusion command line, do you have the same confusion, what is the relationship between these tools? How do I use them?

For this reason, we provide you with a new KCL CLI, the goal is to include the KCL ecosystem together, to provide you with a unified and concise operation page, everything, one-click direct.

The new KCL CLI will continue to use kcl as the command prefix, and currently provides multiple sub-commands including compilation, package management, and formatting tools.

cli-help

🔧 Diagnostic Information Optimization

We have tried to add repair suggestions in some error messages. If you are frustrated by the KCL compilation failure, you may wish to listen to the compiler's suggestions.

import sub as s1

The_first_kcl_program = s.The_first_kcl_program

Let's listen to what the compiler says. You may have written s1 as s by mistake.

did you mean

KCL cannot find the third-party library used in the package? Try kcl mod add, if it still doesn't work, we have prepared more than 200 KCL models for you on artifacthub.io, come and have a look, there is always one that suits you!

try-kcl-mod-add

🚀 Language Writing Experience Optimization

Removed indentation check in some code blocks

In some code blocks, whether the indentation is aligned has become less important.

If your code is written like this

schema TestIndent:
name: str
msg: str
id: int

test_indent = TestIndent {
name = "test"
msg = "test"
id = 1
}

In the past, you may have encountered a lot of red errors, but now this is not a mistake, kcl fmt will help you tidy it up.

kcl-fmt

Lambda expression type annotation

In the new version of KCL, we have added type annotations for lambda expressions, and you can write lambda with type annotations in the new version of KCL.

schema Test:
name: str

identity: (Test) -> bool = lambda res: Test -> bool {
res.name == "hello"
}

c = identity(Test { name = "hello" })

🏄 API Updates

🐞 Other Updates and Bug Fixes

IDE & Toolchain Updates

IDE Updates

KCL IDE supports goto reference and renaming of symbols

IDE supports goto reference of symbols, using goto reference or find all references:

find-ref

IDE supports renaming of symbols:

rename

IDE supports formatting of import statements and union types

We have optimized the behavior of blank lines between import statements and other code blocks (formatted as one blank line) and the behavior of spaces between union types (formatted as separated by |):

fmt

KCL IDE has added a lot of completion prompts

We have added a lot of completion prompts for the configuration definition, which simplifies the user's mind of writing configuration based on the model and improves the efficiency of configuration editing. In addition, the parameter completion when calling the built-in function is enhanced. Talk is cheap, let's take a look at the effect directly:

func-completion

conf-completion

And for the model design, we have also added a quick generation of docstring to reduce the boilerplate of typing by hand:

gen-docstring

Performance

  • KCL has designed and restructured a new semantic model, as well as an API that supports nearest symbol search and symbol semantic information query.
  • The migration of IDE completion, jump, and hover functions to new semantic models significantly reduces the difficulty and code volume of IDE development.
  • The KCL compiler supports syntax incremental parsing and semantic incremental checking, which improves the performance of KCL compilation, construction, and IDE plugin usage in most scenarios by 5-10 times.

KCL IDE other updates and bug fixes

  • KCL IDE extension for IntelliJ 2023.2+
  • Fix the problem that the language service virtual file system related bug: the file dimension change will cause the language service to crash and must be restarted to recover, which has now been fixed.
  • Support import statement completion of external package dependencies introduced by package management tools
  • Fix the display position of the function parameter undefined type error

Test Tool Updates

Are you worried that your KCL program is written incorrectly? Why not come and test it? This update provides a new KCL test tool.

The new KCL test tool supports writing unit tests using KCL lambda and executing tests using the tool.

You can write your test cases through lambda expressions in the file with the suffix _test.k.

import .app

# Convert the `App` model into Kubernetes Deployment and Service Manifests
test_kubernetesRender = lambda {
a = app.App {
name = "app"
containers.ngnix = {
image = "ngnix"
ports = [{containerPort = 80}]
}
service.ports = [{ port = 80 }]
}
deployment_got = kubernetesRender(a)
assert deployment_got[0].kind == "Deployment"
assert deployment_got[1].kind == "Service"
}

You can run this test case and view the test results through kcl test.

After the test is passed, you will get the following results:

test-pass

If your test fails, kcl test will output error information to help you troubleshoot the problem.

test-failed

KCL Package Management

The update command is added to the kcl mod command. The update command is used to automatically update local dependencies. kcl mod update will automatically download the missing third-party libraries for you. For details, please refer to: https://github.com/kcl-lang/kpm/pull/212

KCL Import Tool

The KCL Import tool supports one-click generation of KCL configuration/models from YAML/JSON/CRD/Terraform Schema, realizing automated migration.

If you have the following yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

You can convert it to a KCL program through the command kcl import test.yaml.

"""
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""

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

More details: https://kcl-lang.io/docs/user_docs/guides/working-with-k8s/adapt-from-kubernetes

Community Integrations & Extensions Updates

KCL Marketplace with ArtifactHub

Through the integration of artifacthub.io, we have built a KCL third-party library market, where you can share your KCL programs with us. You can also choose freely and find the KCL third-party library that suits you!

Open the homepage of artifacthub.io, search for the keyword you need directly, and you can see the relevant content about the KCL third-party library!

artifachub-index

Click on the third-party library homepage, you can view the detailed content and related documents about the third-party library.

pkg-page

If you don't know how to use these third-party libraries, the button on the right can bring up the installation page for you.

install-pkg

Welcome to contribute your third-party libraries to the KCL community on artifacthub.io and make the KCL community more colorful!

Contributing to KCL Marketplace: https://kcl-lang.io/docs/user_docs/guides/package-management/how-to/publish_pkg_to_ah

Other Updates

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

Document Updates

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

Community Updates

KCL Officially Becomes CNCF Sandbox Project

🎉 🎉 🎉 On September 20, 2023, the KCL project passed the evaluation of the Technical Oversight Committee of the Cloud Native Computing Foundation (CNCF), the world's top open source foundation, and officially became a CNCF sandbox project.

More details - https://kcl-lang.io/blog/2023-09-19-kcl-joining-cncf-sandbox/

Next Steps

We expect to release KCL v0.8.0 in February 2024. For more details, please refer to KCL 2024 Roadmap and KCL v0.8.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. 👏👏👏