Skip to main content
Version: v0.11

How Kusion Works?

Kusion is the platform engineering engine of KusionStack. It delivers intentions described with Kusion Modules defined in Catalog to Kubernetes, Clouds and On-Prem infrastructures.

arch

Overview

The workflow of KusionStack is illustrated in the diagram above, and it consists of three steps. The first step is Write, where platform engineers provide Kusion Modules and application developers write AppConfigurations based on the Kusion Modules to describe their operational intent.

The second step is the Generate process, which results in the creation of the SSoT (Single Source of Truth), also known as the Spec of the current operational task. If you need version management of the SSoT, we recommend you manage the Spec with a VCS (Version Control System) tool like git.

The third step is Apply which makes the Spec effective. Kusion parses the operational intent based on the Spec produced in the previous step. Before applying the intent, Kusion will execute the Preview command (you can also execute this command manually) which will use a three-way diff algorithm to preview changes and prompt users to make sure all changes meet expectations; the Apply command will then actualize the operational intent onto various infrastructure platforms. Currently, it supports three runtimes: Terraform, Kubernetes, and on-prem infrastructures.

As a user of Kusion, if you prefer not to be conscious of so many steps, you can simply use kusion apply, and Kusion will automatically execute all the aforementioned steps for you.

Platform Developer’s Workflow

Design Kusion Modules

Kusion Module is a reusable building block designed by platform engineers and contains two components: an application developer-oriented schema and a Kusion module generator. When platform engineers have developed a Kusion module, they can push it to a catalog repository to make it into a KCL package.

Given a database Kusion module as an example, the schema definition is shown below and the generator logic can be found here.

schema MySQL: 
""" MySQL describes the attributes to locally deploy or create a cloud provider
managed mysql database instance for the workload.

Attributes
----------
type: "local" | "cloud", defaults to Undefined, required.
Type defines whether the mysql database is deployed locally or provided by
cloud vendor.
version: str, defaults to Undefined, required.
Version defines the mysql version to use.

Examples
--------
Instantiate a local mysql database with version of 5.7.

import catalog.models.schema.v1.accessories.mysql

accessories: {
"mysql": mysql.MySQL {
type: "local"
version: "8.0"
}
}
"""

# The deployment mode of the mysql database.
type: "local" | "cloud"

# The mysql database version to use.
version: str

Instantiate and Set Up Workspaces

Each workspace includes a corresponding Platform config file maintained by platform engineers. Platform engineers should instantiate all workspaces and fulfill all fields with platform default values. Kusion will merge the workspace configuration with AppConfiguration in the Stack of the same name. An example is as follows.

# MySQL configurations for AWS RDS
modules:
kusionstack/mysql@0.1.0:
default:
cloud: aws
size: 20
instanceType: db.t3.micro
securityIPs:
- 0.0.0.0/0
suffix: "-mysql"

The mysql block represents a Kusion module. The fields inside are parts of the inputs for the Kusion module generator. For more details about the workspace, please refer to the workspace section.

Application Developer’s Workflow

Instantiate AppConfiguration and Apply

Application developers choose Kusion modules they need and instantiate them in the AppConfiguration to describe their operation intentions. We have built some built-in Kusion modules in the repository Catalog and we warmly welcome you to join us in building this ecosystem together.

main.k is the only configuration maintained by application developers and schemas in this file are defined from the application developer's perspective to reduce their cognitive load. An example is as follows.

import kam.v1.app_configuration as ac
import kam.v1.workload as wl
import kam.v1.workload.container as c
import network as n
import mysql

wordpress: ac.AppConfiguration {
   workload: wl.Service {
       containers: {
           wordpress: c.Container {
               image: "wordpress:6.3"
               env: {
                   "WORDPRESS_DB_HOST": "$(KUSION_DB_HOST_WORDPRESS_MYSQL)"
                   "WORDPRESS_DB_USER": "$(KUSION_DB_USERNAME_WORDPRESS_MYSQL)"
                   "WORDPRESS_DB_PASSWORD": "$(KUSION_DB_PASSWORD_WORDPRESS_MYSQL)"
                   "WORDPRESS_DB_NAME": "mysql"
               }
               resources: {
                   "cpu": "500m"
                   "memory": "512Mi"
               }
           }
       }
       replicas: 1
   }
   accessories: {
       "network": n.Network {
           ports: [
             n.Port {
                 port: 80
             }
           ]
       }
       "mysql": mysql.MySQL {
           type: "cloud"
           version: "8.0"
       }
   }
}

workload and database are both Kusion modules provided by platform engineers and Kusion will convert them into actual infrastructure API calls eventually.

Finally, application developers can deliver their operational intent to infrastructures with one command kusion apply.