Skip to main content
Version: 0.4.5

CRD to KCL

To convert from models defined in the Kubernetes CRD file to KCL schema, we could run the following script:

kcl-openapi generate model --crd -f ${your_CRD.yaml} -t ${the_kcl_files_output_dir} --skip-validation

Example

  • There is the Kubernetes CRD file that we need to convert: test_crontab_CRD.yaml:

    # Deprecated in v1.16 in favor of apiextensions.k8s.io/v1
    apiVersion: apiextensions.k8s.io/v1beta1
    kind: CustomResourceDefinition
    metadata:
    # name must match the spec fields below, and be in the form: <plural>.<group>
    name: crontabs.stable.example.com
    spec:
    # group name to use for REST API: /apis/<group>/<version>
    group: stable.example.com
    # list of versions supported by this CustomResourceDefinition
    versions:
    - name: v1
    # Each version can be enabled/disabled by Served flag.
    served: true
    # One and only one version must be marked as the storage version.
    storage: true
    # either Namespaced or Cluster
    scope: Namespaced
    names:
    # plural name to be used in the URL: /apis/<group>/<version>/<plural>
    plural: crontabs
    # singular name to be used as an alias on the CLI and for display
    singular: crontab
    # kind is normally the CamelCased singular type. Your resource manifests use this.
    kind: CronTab
    # shortNames allow shorter string to match your resource on the CLI
    shortNames:
    - ct
    preserveUnknownFields: false
    validation:
    openAPIV3Schema:
    type: object
    properties:
    spec:
    type: object
    properties:
    cronSpec:
    type: string
    image:
    type: string
    replicas:
    type: integer
  • The script will be:

    kcl-openapi generate model -f test_crontab_CRD.yaml -t ~/ --skip-validation --crd
  • Then we can find the generated file here: ~/models/stable_example_com_v1_cron_tab.k

    """
    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.
    """
    import kusion_kubernetes.apimachinery.apis

schema CronTab: """stable example com v1 cron tab """

  apiVersion: "stable.example.com/v1" = "stable.example.com/v1"
""" APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
"""

kind: "CronTab" = "CronTab"
""" Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
"""

metadata?: apis.ObjectMeta
""" Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
"""

spec?: StableExampleComV1CronTabSpec
"""spec
"""

schema StableExampleComV1CronTabSpec: """stable example com v1 cron tab spec """

  cronSpec?: str
"""cron spec
"""

image?: str
"""image
"""

replicas?: int
"""replicas
"""