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

Codegen: a new script for subprojects to use #117262

Merged

Conversation

thockin
Copy link
Member

@thockin thockin commented Apr 13, 2023

Add a new way for subprojects to do codegen

This new script (k8s.io/code-generator/kube_codegen.sh) should be somewhat easier to understand and maintain. This PR includes commits which convert all of the internal subprojects to use it.

/kind cleanup

Projects which use k8s.io/code-generator and invoke `generate-groups` or `generate-internal-groups.sh` have a new, simpler script (`kube_codegen.sh`) they can use.  The old scripts are deprecated but remain intact.

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. area/code-generation labels Apr 13, 2023
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 13, 2023
@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. 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. sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Apr 13, 2023
@thockin
Copy link
Member Author

thockin commented Apr 13, 2023

/retest

@fedebongio
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 Apr 18, 2023
Copy link
Member

@BenTheElder BenTheElder left a comment

Choose a reason for hiding this comment

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

this is complicated enough to make me reach for: https://google.github.io/styleguide/shellguide.html#when-to-use-shell

defaulter-gen
)
# shellcheck disable=2046 # printf word-splitting is intentional
GO111MODULE=on go install $(printf "k8s.io/code-generator/cmd/%s " "${BINS[@]}")
Copy link
Member

Choose a reason for hiding this comment

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

is the whitespace after %s within the quotes intentional here?

also is the printf + word splitting necessary? we could just put k8s.io/code-generator/cmd/ in front of the three BINS entries

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, the space is needed to produce the right output.

As you raised above, there is a POTENTIAL whitespace issue, but not an ACTUAL issue - all the places we do this are controlled inputs which do not include the user-owned path. They are all either Go package names (as returned by go list - no spaces) or binary names enumerated immediately before. So One option is to leave it as printf.

Exploring:

$ # foo just prints info about its args
$ function foo() { local i=1; for arg; do echo "\$$i: $arg"; i=$((i+1)); done; }

$ foo a b "c d e"
$1: a
$2: b
$3: c d e

$ X=("a" "b" "c d e")

$ printf -- "--arg=\"%s\" " "${X[@]}"; echo;
--arg="a" --arg="b" --arg="c d e" 

That looks OK, but...

$ foo $(printf -- "--arg=\"%s\" " "${X[@]}")
$1: --arg="a"
$2: --arg="b"
$3: --arg="c
$4: d
$5: e"

The shell evaluation of quoted strings doesn't survive. We could do:

$ eval foo $(printf -- "--arg=\"%s\" " "${X[@]}")
$1: --arg=a
$2: --arg=b
$3: --arg=c d e

...but that is easy to misuse or just forget about. Bash arrays and functions are a mess, which is why we have things like read-array which is deeeeep dark magic

or else we take it into our own hands:

$ A=(); for arg in "${X[@]}"; do A+=("--arg=\"$arg\""); done

$ foo "${A[@]}"
$1: --arg="a"
$2: --arg="b"
$3: --arg="c d e"

It's not so bad, but it can't be a function.

Another option is to use the IFS trick ((IFS=,; foo "${X[*]}") - which, while terse, is also a bit brittle (IFS has to be set on a distinct command and [@] does not work). This passes a single --input-dirs=foo,bar,bat rather than --input-dirs=foo --input-dirs=bar --input-dirs=bat.

I'll add commits in a couple styles - you tell me which you think make sense, and then I will back-integrate :)

staging/src/k8s.io/code-generator/kube_codegen.sh Outdated Show resolved Hide resolved
Copy link
Member Author

@thockin thockin left a comment

Choose a reason for hiding this comment

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

pushed with WIP commits as the only changes.

staging/src/k8s.io/code-generator/kube_codegen.sh Outdated Show resolved Hide resolved
defaulter-gen
)
# shellcheck disable=2046 # printf word-splitting is intentional
GO111MODULE=on go install $(printf "k8s.io/code-generator/cmd/%s " "${BINS[@]}")
Copy link
Member Author

Choose a reason for hiding this comment

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

yes, the space is needed to produce the right output.

As you raised above, there is a POTENTIAL whitespace issue, but not an ACTUAL issue - all the places we do this are controlled inputs which do not include the user-owned path. They are all either Go package names (as returned by go list - no spaces) or binary names enumerated immediately before. So One option is to leave it as printf.

Exploring:

$ # foo just prints info about its args
$ function foo() { local i=1; for arg; do echo "\$$i: $arg"; i=$((i+1)); done; }

$ foo a b "c d e"
$1: a
$2: b
$3: c d e

$ X=("a" "b" "c d e")

$ printf -- "--arg=\"%s\" " "${X[@]}"; echo;
--arg="a" --arg="b" --arg="c d e" 

That looks OK, but...

$ foo $(printf -- "--arg=\"%s\" " "${X[@]}")
$1: --arg="a"
$2: --arg="b"
$3: --arg="c
$4: d
$5: e"

The shell evaluation of quoted strings doesn't survive. We could do:

$ eval foo $(printf -- "--arg=\"%s\" " "${X[@]}")
$1: --arg=a
$2: --arg=b
$3: --arg=c d e

...but that is easy to misuse or just forget about. Bash arrays and functions are a mess, which is why we have things like read-array which is deeeeep dark magic

or else we take it into our own hands:

$ A=(); for arg in "${X[@]}"; do A+=("--arg=\"$arg\""); done

$ foo "${A[@]}"
$1: --arg="a"
$2: --arg="b"
$3: --arg="c d e"

It's not so bad, but it can't be a function.

Another option is to use the IFS trick ((IFS=,; foo "${X[*]}") - which, while terse, is also a bit brittle (IFS has to be set on a distinct command and [@] does not work). This passes a single --input-dirs=foo,bar,bat rather than --input-dirs=foo --input-dirs=bar --input-dirs=bat.

I'll add commits in a couple styles - you tell me which you think make sense, and then I will back-integrate :)

@thockin
Copy link
Member Author

thockin commented May 6, 2023

ping this - I have more stacked up behind it and it feeds into my go workspaces branch :)

Sorry!!

Copy link
Member

@BenTheElder BenTheElder left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve
/hold

@k8s-ci-robot k8s-ci-robot added 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. labels May 8, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 24d964bc91620bf8d944a80e389c3fcb3bc26c72

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: BenTheElder, thockin

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

@thockin
Copy link
Member Author

thockin commented May 8, 2023

Do you have a pref on #1 (a485af4) vs #2 (b678f13) ?

#1 is shorter, I guess.

@liggitt
Copy link
Member

liggitt commented May 8, 2023

One question on the generation order, no other comments from me.

I didn't review the script changes closely, will defer to @BenTheElder's review

@BenTheElder
Copy link
Member

2) is probably slightly easier for most contributors to read and modify?

I don't have a super strong preference though, they're both reasonable enough.

This script (k8s.io/code-generator/kube_codegen.sh) should be somewhat
easier to understand and maintain.

This commit converts one caller (sample-apiserver) to use it, which
exercises all 3 functions.  More to follow.
This caused some of the generated code to be sorted where it wasn't
before.
@thockin thockin force-pushed the codegen_new_script_for_subprojects branch from b678f13 to fb4d015 Compare May 8, 2023 21:37
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 8, 2023
@thockin
Copy link
Member Author

thockin commented May 8, 2023

So say we all.

Applied as fixups (the net result was just reordering and squashing commits, no code changes)

Copy link
Member

@BenTheElder BenTheElder left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 8, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 7f5b83957bef8e4b0310ef2640ee4f701d7b1bf4

@thockin thockin removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 9, 2023
@k8s-ci-robot k8s-ci-robot merged commit 7cdf67e into kubernetes:master May 9, 2023
12 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.28 milestone May 9, 2023
@cardil cardil mentioned this pull request Jul 20, 2023
15 tasks
@thockin thockin deleted the codegen_new_script_for_subprojects branch March 10, 2024 01:03
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. area/code-generation cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. size/XL Denotes a PR that changes 500-999 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.

None yet

5 participants