Skip to main content

· 6 min read

Introduction

In previous updates, we have added package management capability to the KCL language, solving the issue of how to obtain and publish third-party libraries. However, a new problem arises during the development process where teammates are often unsure which library to use to address their specific problem. Before using a third-party library through the package management tool, it is necessary to select the appropriate library based on the specific requirements of the cloud-native operations and the capabilities of various third-party libraries.

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.

Therefore, we have leveraged the ArtifactHub project under CNCF to build a marketplace for KCL third-party libraries. In this marketplace, users with specific needs can freely choose the third-party libraries they require, while those who have ideas and are willing to share can also contribute and share their libraries in this marketplace. Next, we will use a simple example of publishing an application to familiarize ourselves with how to retrieve KCL third-party libraries on ArtifactHub and develop KCL programs based on the library documentation.

Prerequisites

Start minikube using the following command:

minikube start --cache-images=true

If the Ingress controller is not installed in your minikube, you can use the following command to install it:

minikube addons enable ingress

HelloWorld Example

First, let's prepare a simple JavaScript application:

const express = require("express");
const app = express();
const port = 8080;

// Define a route that responds to GET requests
app.get("/", (req, res) => {
res.send("Welcome to my web application!");
});

// Start the server
app.listen(port, () => {
console.log(`Web app listening at http://localhost:${port}`);
});

Prepare an image for this application and upload it to the image registry:

FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "app.js" ]

In this example, we will use the image ghcr.io/test/my-web-app:1.0 to demonstrate the specific content. With the preparation work completed, we can now use KCL to write the corresponding release code.

Use ArtifactHub to search for the modules you need

First, I want to publish my my-web-app application, and I need to use a third-party library for Kubernetes to accomplish my task. By searching for the keyword k8s on ArtifactHub, you can see the currently available third-party libraries for k8s.

index-page

On the homepage of the k8s module, you can find the documentation and more detailed information about the k8s module.

k8s-page

The INSTALL button on the right provides you with the installation method for the k8s third-party library.

k8s-install-page

Since we are using k8s as a dependency for our package, we can install it using the following command:

kcl mod init my-kubernetes-config && cd my-kubernetes-config && kcl mod add k8s:1.28

Next, we need to write the deployment, service, and ingress sections for the application release. We will use the Deployment, Service, and Ingress schemas from the k8s module we just added. For more information about schemas, you can refer to: https://kcl-lang.io/docs/next/reference/lang/tour#schema

If you are not familiar with these three contents, you can directly search in the details page of the model. Taking Deployment as an example, you can find more detailed information in the documentation.

deployment

Based on the documentation, we can write the following deployment config in the main.k file:

import k8s.api.apps.v1 as d

deployment = d.Deployment {
metadata.name = "web-app-deployment"
spec = {
selector.matchLabels = {app = "web-app"}
template = {
metadata.labels = {app = "web-app"}
spec.containers = [{
name = "web-app"
image = "ghcr.io/test/my-web-app:1.0"
ports = [{containerPort = 80}]
}]
}
}
}

For the Service section, we can find related documentation.

service

The corresponding service content is as follows:

import k8s.api.core.v1 as s
service = s.Service {
metadata.name = "web-app-service"
spec.selector = {"app": "web-app"}
spec.ports = [{
port: 80
targetPort: 8080
}]
}

About Ingress, you can find related documentation.

ingress

The corresponding ingress content is as follows:

import k8s.api.networking.v1 as i

ingress = i.Ingress {
metadata.name = "web-app-ingress"
spec.rules = [
{
host: "web-app.example.com"
http.paths = [
{
path: "/"
pathType: "Prefix"
backend.service.name: "web-app-service"
backend.service.port: {
number: 8080
}
}
]
}
]
}

Finally, add the following content to the main.k file to render the deployment, service, and ingress as the YAML stream format.

manifests.yaml_stream([
deployment
service
ingress
])

You can compile the corresponding KCL program and apply it to the cluster using the following command.

kcl run main.k | kubectl apply -f -

At this point, we have successfully deployed the application my-web-app with the help of ArtifactHub. Finally, let's take a look at the deployment effect through kubectl port forwarding. Use the following command to forward the port.

kubectl port-forward service/web-app-service 8080:80

Then use the following command or directly access http://localhost:8080 in the browser to access our deployed application.

$ curl http://localhost:8080

Welcome to my web application!

Summary

In this blog, we demonstrated how to select the appropriate KCL third-party library on ArtifactHub through a simple application deployment. Currently, there are more than 150+ KCL third-party libraries available on ArtifactHub for you to choose from. Come and check if there is a KCL model you need!

In the example provided in this blog, we used KCL to write the program for deploying the application. In future updates, we will further abstract the KCL program in this blog with features such as dynamic parameters and introduce more application models such as Open Application Model (OAM), and package them into a separate module for release on ArtifactHub. If you also have KCL modules that you want to share with others, we will continue to update detailed steps and guides to help you successfully publish your package. Stay tuned!

· 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, 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 (10.26 - 10.08 2023). Here is an overview of the key content:

🔧 Language and Toolchain Updates

  • KCL IDE Updates - Supports for symbol find-references and rename; Optimized the formatting output for import statements and union types; Fixed the bug where file changes caused the language server to crash.

  • KCL Package Management Tool KPM Updates - kpm is integrating with ArtifactHub, enabling KCL packages publishing to ArtifactHub.

  • KCL Language Updates - Optimized error messages for mismatched parameter types in methods, providing clearer indications of the mismatch.

  • Unified Interface of KCL Command-Line - Redesigned the command-line interface and workflow for KCL tools to achieve a unified experience.

  • KCL IDE Update - More intelligent configuration value completion, property list completion, function parameter completion, built-in package reference completion, and docstring completion, etc.

  • KCL Package Manage Tool Update - More smooth workflow for creating and publishing KCL packages: supports automated processes for package updates and releases based on versioning systems; additionally, custom configuration of metadata for KCL packages is now allowed.

  • KCL Module Update - Out-of-the-box inclusion of over 120 KCL models: https://github.com/kcl-lang/artifacthub

  • KCL Language Update - Improved error messages for schema attribute type mismatches, support for lambda type annotations, and fixes for individual compilation issues and more system libraries support e.g., validation, serialization, and deserialization of JSON/YAML strings.

  • KCL Import Tool Release: Supports one-click generation of KCL configurations/models from YAML/JSON/CRD/Terraform Schema, enabling automated migration.

Special Thanks

The following are listed in no particular order:

IDE Extension Updates

The KCL IDE extension has added a large number of completion suggestions, focusing on the core aspect of configuration definition, simplifying the mental process of users when writing configurations based on models, and improving the efficiency of configuration editing. Additionally, parameter completion when invoking built-in functions has been enhanced. Talk is cheap, let's directly see the results:

For the model design phase, quick generation of document strings has also been added, reducing the need for manual boilerplate typing:

KCL Package Manager Updates

The package management tool has now interconnected the core workflow of KCL package creation, update, code review, and release. Based on this, over 120+ out-of-the-box KCL model packages have been added.

KCL Language Updates

The optimization of error message output in the KCL compilation command continues to progress, aiming to provide clear and understandable guidance to help developers quickly locate and fix issues and write correct code. Recently, KCL has optimized the error messages for schema field type mismatches:

  • before:

  • after:

Additionally, support has been added for adding type annotations in lambda expressions, and system libraries now support validation, serialization, and deserialization of JSON/YAML strings. The following issues have been fixed: cache invalidation for KCL programs with third-party libraries, path conflicts when compiling imported files across kcl.mod, and semantic checks for default value of KCL functions.

KCL Import Tool

Support for one-click generation of KCL configurations/models from YAML/JSON/CRD/Terraform Schema enables automated migration. Please refer to the One-click Migration from Kubernetes Ecosystem to KCL guide for more information.

Resources

❤️ Thanks to all KCL users and community members for their valuable feedback and suggestions in the community.

For more resources, please refer to