Skip to main content

· 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:

· 4 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 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.

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

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

Overview

Thanks to to all contributors for their outstanding work over the past twenty days (2024.02.02 - 2024.02.22). Here is an overview of the key content:

📦 Module Updates

The JSON Schema module 0.0.4 has released, fixing type definition errors. You can update or add dependencies by running the following command:

kcl mod add jsonschema:0.0.4

🏄 Language Updates

KCL has released preview version 0.8.0, mainly including the following updates:

  • Added the file system library for reading KCL module information and system files, including the read, glob, workdir, and modpath functions. See more details in this issue
  • Optimized syntax error messages for unexpected tokens.
  • Removed output for unexpected internal built-in type attributes in schema objects.
  • Fixed variable calculation in unexpected dictionary generation expressions where the key is the same as the loop variable.
  • Fixed errors in defining string identifiers within schemas, such as "$if".

🔧 Toolchain Updates

  • kcl run supports using the -H parameter to output hidden fields starting with _.
  • kcl run supports running remote Git repository code directly.
  • Introduce the kcl mod graph subcommand used to output module dependency graphs.
  • Fixed formatting errors when there is an if statement within an else block.

💻 IDE Updates

  • Enhanced autocompletion and hover documentation for built-in functions and system libraries
  • Fixed issues with navigating and autocompleting if statements symbols within configuration blocks
  • Added quick fix feature for variable reference errors

🎁 API Updates

  • The OverrideFile API has added path for querying and modifying configurations, such as a["b"].c
  • The Run API has added the WithShowHidden and the WithTypePath flags.

🚀 Plugin System Updates

In addition to using Python for KCL plugin functions, it now supports using Go to write plugin functions for KCL, which is very simple to use.

  • Define a plugin (using a hello plugin containing the add function as an example)
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
},
},
},
})
}
  • Use the plugin
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
`

🚢 Integration Updates

  • Released initial version of Ansible KCL module, supporting basic execution of KCL code, with other functionalities being improved
  • Optimized Git Source functionality for KCL FluxCD Controller, with OCI Source functionality in progress

Special Thanks

The following are listed in no particular order:

  • Thanks to @octonawish-akcodes and @d4v1d03 for their continuous contributions to KCL FAQ documentation and KCL IDE functionality 🙌
  • Thanks to @octonawish-akcodes for the contribution to the Ansible KCL Module
  • Thanks to @AkashKumar7902 and @Vanshikav123 for the contribution to the KCL package management tool functionality 🙌
  • Thanks to @StevenLeiZhang for the contribution to KCL documentation and KCL plugins
  • Thanks to @TheChinBot, @Evgeny Shepelyuk, @yonas, @steeling, @vtomilov, @Fdall, @CloudZero357, @bozaro, @starkers, @MrGuoRanDuo and @FLAGLORD, among others, for their valuable feedback and suggestions while using KCL recently. 🙌

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