Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: add encoding/cue to encode/decode cue code to/from a string #1852

Open
Somefive opened this issue Aug 10, 2022 · 5 comments
Open
Labels
FeatureRequest New feature or request Proposal x/user/KubeVela Experimental CUE-user-based labels

Comments

@Somefive
Copy link

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

In KubeVela, we use pure CUE file to write and organize render logics but the code is embedded as YAML object while deployed into Kubernetes cluster.

For example, the following CUE file will be converted into the following YAML object.

scaler: {
	type: "trait"
	annotations: {}
	labels: {}
	description: "Manually scale K8s pod for your workload which follows the pod spec in path 'spec.template'."
	attributes: {
		podDisruptive: false
		appliesToWorkloads: ["deployments.apps", "statefulsets.apps"]
	}
}
template: {
	parameter: {
		// +usage=Specify the number of workload
		replicas: *1 | int
	}
	// +patchStrategy=retainKeys
	patch: spec: replicas: parameter.replicas
}
apiVersion: core.oam.dev/v1beta1
kind: TraitDefinition
metadata:
  annotations:
    definition.oam.dev/description: Manually scale K8s pod for your workload which follows the pod spec in path 'spec.template'.
  name: scaler
  namespace: {{ include "systemDefinitionNamespace" . }}
spec:
  appliesToWorkloads:
    - deployments.apps
    - statefulsets.apps
  podDisruptive: false
  schematic:
    cue:
      template: |
        parameter: {
        	// +usage=Specify the number of workload
        	replicas: *1 | int
        }
        // +patchStrategy=retainKeys
        patch: spec: replicas: parameter.replicas

Reference:

Currently, we use vela def render command to do the conversion. The conversion logic includes:

  • Find out CUE in template field and convert into string and fill it into spec.schematic.cue.template.
  • Parse the rest struct into metadata or other fields.

Therefore, although the YAML object can be rendered from the CUE file, the CUE file itself is not actually cue native, as the fields in the template field are actually isolated from other fields.

Describe the solution you'd like
A clear and concise description of what you want to happen.

If we have some internal function like cue.Marshal from encoding/cue, just like encoding/json and json.Marshal, we can make the CUE rendering process more elegant. For example, with this function we can write,

// scaler.cue
import (
  "encoding/cue"
)

parameter: {
        name: "scaler"
	type: "trait"
	annotations: {}
	labels: {}
	description: "Manually scale K8s pod for your workload which follows the pod spec in path 'spec.template'."
	attributes: {
		podDisruptive: false
		appliesToWorkloads: ["deployments.apps", "statefulsets.apps"]
	}
        template: {
	        parameter: {
		        // +usage=Specify the number of workload
		        replicas: *1 | int
	        }
	        // +patchStrategy=retainKeys
	        patch: spec: replicas: parameter.replicas
        }
}

output: {
        apiVersion: "core.oam.dev/v1beta1"
        kind: strings.ToTitle(parameter.type) + "Definition"
        metadata: {
                name: parameter.name
                annotations: {
                        parameter.annotations
                        "definition.oam.dev/description": parameter.description
                }
                labels: parameter.labels
        }
        spec: {
                parameter.attributes
                schematic: cue: template: cue.Marshal(parameter.template)
        }
}

And then we could use cue eval scaler.cue -e output -d yaml | kubectl apply -f -n vela-system - to get the rendered YAML object and apply it into Kubernetes cluster directly.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

@Somefive Somefive added FeatureRequest New feature or request Triage Requires triage/attention labels Aug 10, 2022
@myitcv myitcv added x/user/KubeVela Experimental CUE-user-based labels and removed Triage Requires triage/attention labels Aug 11, 2022
@myitcv myitcv changed the title Proposal: add cue.Marshal to encode cue code into string Proposal: add encoding/cue to encode/decode cue code to/from a string Aug 11, 2022
@myitcv
Copy link
Member

myitcv commented Aug 11, 2022

Thanks, @Somefive.

Noting per our discussion that this is something that has been discussed before, although I can't find a good issue/discussion that captures that discussion. So thanks for creating this issue.

Noting some of the main challenges we have identified in these conversations:

  • What API would encoding/cue provide?
  • Does the API end up looking similar to the CLI API surface via cue (def|eval|export)?
  • ...

@myitcv myitcv changed the title Proposal: add encoding/cue to encode/decode cue code to/from a string proposal: add encoding/cue to encode/decode cue code to/from a string Sep 9, 2022
@myitcv myitcv added the Proposal label Sep 9, 2022
@roman-mazur
Copy link

Does the API end up looking similar to the CLI API surface via cue (def|eval|export)?

The export does not make much sense since you already have the other encoding packages (encoding/yaml, encoding/json). I would start with having something that is equivalent to cue eval to get the "minimized" form of the input serialized.

@sdboyer
Copy link

sdboyer commented Nov 8, 2022

This would be quite useful for Thema, as well.

@FogDong
Copy link

FogDong commented Feb 9, 2023

This one's priority is medium and we have some workaround. But the workarounds need to use syntax, and #2258 blocks the workaround.

Not user-facing.

@myitcv myitcv added the zGarden label Jun 13, 2023
@mvdan
Copy link
Member

mvdan commented Nov 14, 2023

Noting per our discussion that this is something that has been discussed before, although I can't find a good issue/discussion that captures that discussion. So thanks for creating this issue.

I believe that might be #423, although given its inactivity, I think we should close that one as a duplicate of this one even though this one is newer.

@mvdan mvdan removed the zGarden label Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest New feature or request Proposal x/user/KubeVela Experimental CUE-user-based labels
Projects
None yet
Development

No branches or pull requests

6 participants