carvel-dev / kapp-controller

Continuous delivery and package management for Kubernetes.
https://carvel.dev/kapp-controller
Apache License 2.0
267 stars 102 forks source link

Generate template files from kctrl package init #1182

Open liangyuanpeng opened 1 year ago

liangyuanpeng commented 1 year ago

Describe the problem/challenge you have

I want to generate a template carvel package from command of kctrl package init for quickly to know carvel package.

Describe the solution you'd like

Auto generate files (from https://github.com/carvel-dev/ytt/tree/develop/examples/playground/basics/example-demo) :

defaults.yml

#@data/values
---
echos:
- name: first
- name: second
  port: 8081
  text: "Hello #ytt World on 8081!"

demo.yml

#@ load("@ytt:data", "data")

#@ def labels():
app: echo
org: test
#@ end

#@ def name(echo):
#@   return "echo-"+echo.name
#@ end

kind: Pod
apiVersion: v1
metadata:
  name: echo-app
  labels: #@ labels()
spec:
  containers:
  #@ for/end echo in data.values.echos:
  - name: #@ name(echo)
    image: hashicorp/http-echo
    args:
    - #@ "-listen=:" + str(echo.port)
    - #@ "-text=" + echo.text

#@ if/end data.values.service.enabled:
---
kind: Service
apiVersion: v1
metadata:
  name: echo-service
spec:
  selector: #@ labels()
  ports:
  #@ for/end echo in data.values.echos:
  - name: #@ name(echo)
    port: #@ echo.port

schema.yml

#@data/values-schema
---
echos:
- name: ""
  port: 8080
  text: "Hello #ytt World on 8080!"
service:
  enabled: true

Anything else you would like to add:


Vote on this request

This is an invitation to the community to vote on issues, to help us prioritize our backlog. Use the "smiley face" up to the right of this comment to vote.

👍 "I would like to see this addressed as soon as possible" 👎 "There are other more important things to focus on right now"

We are also happy to receive and review Pull Requests if you want to help working on this issue.

100mik commented 1 year ago

@joostvdg shared very similar thoughts with us recently. I think it would be valuable to have a basic ytt example in place when folks use the "local" flow.

What I want to think about is, since this like essentially a flow which requires no user input at all, would it be valuable to have this as sort of a "quick init" option exposed via a flag?

This would ideally error out if your directory already has config and generated files in it as we would not want to overwrite existing art without having context. I do agree that this is definitely something we would like to have for someone who wants to play around with the idea of what an ideal Carvel package looks like!

Another thing to think about is if we would want to call out all good things ytt does with the boilerplate we generate. And is the example this issue calls out does it. (I think it does, but I am wondering if we could improve it)

(@joostvdg it would be super cool to hear your thoughts on this since you had a very similar thought)

joostvdg commented 1 year ago

Sure, let me share some thoughts:

  1. I firmly believe in never giving the User something empty to start with. So when you run kctrl init, there should be a package you could directly build into a package that does something.
  2. What is generated should give the user insight into best practices. Although there isn't one best practice to rule them all, it will likely be something that will at least be oke and possibly best.
  3. Continuing on point 2, this would probably be something like a file you want to template, a schema file, and a values file, what do you think?
  4. Not everyyone will use it for Kubernetes? So maybe the default is generic YAML/YTT?
  5. Continuing point 4, use the flag that controls the init generation with some provided "enum" options. Let's say something like --generate=default, --generate=kubernetes, --generate=disabled something like that.
  6. Thinking long term. People might want build further on this with something aking to Tanzu Application Platform's Accelerators/Maven Archetypes, you could provide a way to specify a template to use. Something like using a URL with the generate flag. Like --generate=<URL>, so people can share package templates. The is also avoids people from requesting more and more things to be included in the provided "starter" templates.

That's on the top of my head what I think about what would be nice with kctrl init. I'm curious to know what you think @100mik ?