Skip to main content
Version: 0.10

Adapt From Kubernetes

Introduction

KCL provides many out of the box support for Kubernetes configuration. Through KCL tools, we can integrate Kubernetes manifests and types into KCL. This section will introduce how to adapt from Kubernetes.

Prerequisite

Quick Start

Convert Kubernetes Manifests to KCL

1. Get the Example

Firstly, let's get the example.

git clone https://github.com/kcl-lang/kcl-lang.io.git/
cd ./kcl-lang.io/examples/kubernetes/from-kubernetes

We can run the following command to show the config.

cat deployment.yaml

The output is

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

Transform the Kubernetes YAML code to KCL

kcl import -f deployment.yaml

The file deployment.k will be generated.

"""
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
}
]
}
]
}
}
}

We can run the following command to show YAML using KCL.

kcl deployment.k

The output is

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

Generate Types in KCL for Custom Resource

KCL supports extracting and generating KCL schemas from Kubernetes OpenAPI/Custom Resource Definition (CRD). the KCL OpenAPI Spec defines the mapping between the OpenAPI specification and the KCL language features.

If you developed CRDs, you can generate the KCL version of the CRD schemas and declare CRs based on that. Here we take the example CronTab CRD specified in the Kubernetes documentation.

  • Generate KCL Schema from CRD
# Convert the CRD YAML to KCL Schema
kcl import -m crd -s -f crd.yaml
# Init a new KCL project.
rm -rf kcl.mod && kcl mod init
# Add the crd models dependency
kcl mod add ./models --rename crd
  • Define CR (cr.k) based on CRDs in KCL
import crd.v1

v1.CronTab {
metadata.name = "my-new-cron-object",
spec: {
cronSpec = "* * * * */5",
image = "my-awesome-cron-image",
replicas = 3,
}
}

We can run the following command to show the CR YAML

kcl cr.k

The output is

apiVersion: stable.example.com/v1
kind: CronTab
metadata:
name: my-new-cron-object
spec:
cronSpec: "* * * * */5"
image: my-awesome-cron-image
replicas: 3

Summary

This section explains how to use the kcl import tool to migrate JSON, YAML, Kubernetes CRDs, and more to KCL. The quick start guide helps with the migration or integration from Kubernetes.