Repository navigation
friction report: developing a simple generator function #4825
howardjohn
started this conversation in
Ideas
Replies: 3 comments
|
Thanks for this detailed friction log! |
0 replies
|
Some of the low-hanging fruits has been addressed in Q3. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This is not a bug really; rather a documentation of my experience trying to write a simple generator function. I have always found friction reports super helpful for me, but feel free to close this immediately.
Goal: write a generator that takes in a config (declaratively) with two values: revision and id, and fetch the grafana dashboard (ie https://grafana.com/api/dashboards/9614/revisions/1/download) into a ConfigMap local yaml file.
Timeline: about 3 hours
I first started digging into https://kpt.dev/book/05-developing-functions/02-developing-in-Go and the github.com/GoogleContainerTools/kpt-functions-catalog for some real examples. Right off the bat I ran into issues; the
framework.Commandmethod no longer exists in newkyamlversions, and kpt-functions-catalog uses a newer version (v0.10.21) than the docs. I don't know the context, but no deprecation period and a breaking change in a patch release seems not very user friendly.Once I got a hello world compiling, I quickly found it challenging to debug at all to get a feel of the inputs/outputs I was expecting. Documented more in #2434.
Next I started looking for some examples of generators; however, almost everything in kpt-functions-catalog is just doing transformations on existing objects or validation. UpsertResource was somewhat similar but a bit complex to follow, so I was somewhat on my own. Luckily the task was trivial - I just needed to create a single configmap.
I started out just trying to do that, and not worry about getting the actual data into the configmap yet. I see I need to create an RNode and insert it into Items... should be simple enough.
RNodehas very somewhat minimal documentation on how to actually create one, so I dug through the ~100 methods it has and found a few promising ones:ConvertJSONToYamlNodeandFromMap. I also found I could just yaml.Unmarshal into one (I think?).I first attempt to create a
v1.ConfigMapobject, marshal to yaml string, then unmarshal into RNode. However,yaml.Marshalis actually outputting a completely incorrect format for these (missing type data, keys are all lowercase likeresourceversion, etc), so this didn't work. I then attempted to use the stdlibjson.Marshal, but ran into the fact that the type metadata is not auto populated then. I started trying to find some examples of actually doing this with schemes, decoders, etc but gave up after a few minutes, figuring this must not be the recommended approach to do this or it would be easier+documented.From the looks of things, all examples are using yaml.Parse with
Sprintf, or go templating. I would prefer strongly typedv1.ConfigMap, but was willing to compromise with go template, especially sinceTemplateProcessorseemed to handle a bunch of things.Because my code wasn't strictly as straightforward as plumbing some data from input to output, I had created my own
Processorthat then calledTemplateProcessor{}.Process(resourceList). This turned out to not be right;TemplateProcessor{}.Processonly works when used directly, as TemplateData will get set to theresourceList.FunctionConfig. Makes sense after running through a debugger, but got me stuck for 10 minutes or so. ReplacingProcesswithFiltergot things rolling again, and I finally got configmap written to a file.However, I didn't like the filename. There was no obvious method on RNode to set the filename, but I stumbled upon a
config.kubernetes.io/pathat some point in my debugging so tried it out and it worked; it appears nowhere in the kpt.dev documentation though.Next, I just needed to get the grafana dashboard downloaded and into the configmap. The download part was simple enough, but then I ran into issues with my previous choice to use go templating -- because the data is json (ie multiline, has quotes, etc), we have escaping issues just sticking it into the configmap like
Because
TemplateProcessordoesn't add in sprig functions, the usual fun tricks ofhelm(which I am using kpt to avoid using 🙂 ) like|ident 4won't work either. There didn't seem to be a way to add functions toTemplateProcessoreither.After trying a few variations of the template, I still couldn't get the escaping to work.
My end code:
Details
Next I tried to backtrack, and instead use the typed
ConfigMap. I realized I can usesigs.k8s.io/yamlinstead ofsigs.k8s.io/kustomize/kyaml/yamland get proper marshaling; that worked. Now just needed to unmarshal it into a nodeRan into an interesting quirk of the library
This panics with obscure error:
This works:
Once that got moving, I found I was actually wrong -
sigs.k8s.io/yamldoes NOT automatically work, and I got an error that my output is not a KRM format. Oops. Since my function is so simple, I guess I will bite the bullet and just manually specify the type meta...:Next all I needed to do was get the merging
TemplateProcessorwas previously giving me for free. A quick look through the code led me to theMergeFilter. This might cause issues since I really want a replace, not a merge, but should be good enough for now:And finally things are working!
End code:
Details
Overall, there was no one thing that was broken/bad/hard - but rather at most steps I encountered small barriers that overtime led to a more frustrating experience than I expected. I hope over time as the ecosystem matures a bit this will get streamlined a bit - I think a large function catalog would be great for the project
All reactions