Skip to content

chore: Replace gojsontoyaml with gojq #2660

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

mrueg
Copy link
Member

@mrueg mrueg commented Apr 23, 2025

What this PR does / why we need it:
https://github.com/brancz/gojsontoyaml hasn't had a commit since 4 years +, replace it with a maintained alternative.

How does this change affect the cardinality of KSM: (increases, decreases or does not change cardinality)
None
Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 23, 2025
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Apr 23, 2025
verbs:
- list
- watch
- apiGroups:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should it have this space?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the way lists are treated with gojq is different from gojsontoyaml, it should still apply though, doesn't it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find most YAMLs in k/k repo use gojsontoyaml style. I would prefer gojsontoyaml style a little.

https://github.com/search?q=repo%3Akubernetes%2Fkubernetes+yaml+language%3AYAML&type=code&l=YAML

cc @dgrisonnet to share more opinions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a very strong opinion. I was checking if we could somehow get rid of that conversion, and it turns we might be able to. I tried with the following patch and it was generating valid yaml files:

diff --git a/Makefile b/Makefile
index 47948b1e7..f5c0d9700 100644
--- a/Makefile
+++ b/Makefile
@@ -158,8 +158,7 @@ examples: examples/standard examples/autosharding examples/daemonsetsharding mix
 
 examples/standard: jsonnet $(shell find jsonnet | grep ".libsonnet") scripts/standard.jsonnet scripts/vendor
        mkdir -p examples/standard
-       ${JSONNET_CLI}} -J scripts/vendor -m examples/standard --ext-str version="$(VERSION)" scripts/standard.jsonnet | xargs -I{} sh -c 'cat {} | ${GOJSONTOYAML_CLI} > `echo {} | sed "s/\(.\)\([A-Z]\)/\1-\2/g" | tr "[:upper:]" "[:lower:]"`.yaml' -- {}
-       find examples -type f ! -name '*.yaml' -delete
+       ${JSONNET_CLI} -J scripts/vendor -m examples/standard --ext-str version="$(VERSION)" -S scripts/standard.jsonnet 
 
 examples/autosharding: jsonnet $(shell find jsonnet | grep ".libsonnet") scripts/autosharding.jsonnet scripts/vendor
        mkdir -p examples/autosharding
diff --git a/scripts/standard.jsonnet b/scripts/standard.jsonnet
index 0839cecf5..24b5df25b 100644
--- a/scripts/standard.jsonnet
+++ b/scripts/standard.jsonnet
@@ -1,9 +1,17 @@
 local ksm = import 'kube-state-metrics/kube-state-metrics.libsonnet';
 local version = std.extVar('version');
 
-ksm {
-  name:: 'kube-state-metrics',
-  namespace:: 'kube-system',
-  version:: version,
-  image:: 'registry.k8s.io/kube-state-metrics/kube-state-metrics:v' + version,
+{
+  local k = ksm {
+     name:: 'kube-state-metrics',
+       namespace:: 'kube-system',
+       version:: version,
+       image:: 'registry.k8s.io/kube-state-metrics/kube-state-metrics:v' + version,
+  },
+
+  'cluster-role-binding.yaml': std.manifestYamlDoc(k.clusterRoleBinding),
+  'cluster-role.yaml': std.manifestYamlDoc(k.clusterRole),
+  'deployment.yaml': std.manifestYamlDoc(k.deployment),
+  'service-account.yaml': std.manifestYamlDoc(k.serviceAccount),
+  'service.yaml': std.manifestYamlDoc(k.service),
 }

But I haven't looked at any jsonnet code in a while so that might be incorrect

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know jsonnet and my interest in learning it is limited (I'm also fine with dropping jsonnet completely and see if we have anyone interested in maintaining it). My primary goal is to remove an unmaintained dependency here.

@rexagod
Copy link
Member

rexagod commented Apr 29, 2025

/lgtm
/hold

To give time to other discussion(s) here, feel free to unhold.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 29, 2025
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 29, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mrueg, rexagod

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@richabanker
Copy link
Contributor

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants