Skip to content

Support dynamic imports of packages #4234

@deepbrook

Description

@deepbrook

I'm currently building a template for k8s resources:

manifests: [API_GROUP=string]: [API_VERSION=string]: [KIND=string]: [NAME=string]: { ... }

The idea was to provide a template to remove the need for import cue.dev/x/k8s.io import statements and manually adding the unification with the respective schema.

(please ignore the glaring issue with apiVersion and the string splitting required to map it to an appropriate package path for now)

Describe the solution you'd like
I want to be able to dynamically access the package in a module I need.

For example, like I would access any other struct:

import k8s "cue.dev/x/k8s.io"
manifests: [API_GROUP=string]: [API_VERSION=string]: [KIND=string]: [NAME=string]:  {
    k8s.api[API_GROUP][API_VERSION]["#\(KIND)]
    metadata: name: NAME
}

Alternatively, perhaps a loader pkg exposing functions like so:

import (
    k8s "cue.dev/x/k8s.io"
    "loader"
)
manifests: [API_GROUP=string]: [API_VERSION=string]: [KIND=string]: [NAME=string]:  {
    _pkg: loader.package(k8s, "api/[API_GROUP]/[API_VERSION]")
    _pkg["#\(KIND)"]
    metadata: name: NAME
}

Some expected arguments for loader.package would be the module to import from (which must have already been imported) and the path to the package to load.

Describe alternatives you've considered
I could declare all the k8s.io package imports in the template .cue file and add them as field validators (I'm currently actually doing this). But it does require maintenance which, well. Nobody likes maintenance work.

Alternatively, having the module provide a struct which does the loading making available of schemas would be better for the end-user, since the maintenance effort then shifts to the author.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions