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

Truncate CRD Description Fields to 50 Characters #204

Merged

Conversation

ayushsatyam146
Copy link
Contributor

@ayushsatyam146 ayushsatyam146 commented Apr 23, 2024

Changes

Adding a TruncateField transformer and using it to truncate CRD description fields from the kodata/release.yaml manifest. The CRDs for Shipwright Build objects are very large, resulting in a validation error from Kubernetes when we try to create the CRDs with Manifestival.

Partially addresses #184

Submitter Checklist

  • Includes tests if functionality changed/was added
  • Includes docs if changes are user-facing
  • Set a kind label on this PR
  • Release notes block has been filled in, or marked NONE

See the contributor guide
for details on coding conventions, github and prow interactions, and the code review process.

Release Notes

Tructate descriptions in Shipwright custom resource definitions to 50 characters.

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 23, 2024
@openshift-ci openshift-ci bot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Apr 23, 2024
Copy link
Member

@adambkaplan adambkaplan left a comment

Choose a reason for hiding this comment

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

Good start! Most of my comments are related to code style, though I think I did find a bug in the implementation.

Please also add unit tests for TruncateField and the transformer function it returns. This is especially important because the function uses recursion - we need to ensure this function doesn't get stuck in an infinite loop and handles a set of expected inputs.

Comment on lines 90 to 91
// Recursively truncates the given "field" from CustomResourceDefinition to 50 characters.
func truncateFieldRecursively(data map[string]interface{}, maxLength int, field string) {
Copy link
Member

Choose a reason for hiding this comment

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

Use Go Doc style conventions - start the sentence with the name of the function.

Suggested change
// Recursively truncates the given "field" from CustomResourceDefinition to 50 characters.
func truncateFieldRecursively(data map[string]interface{}, maxLength int, field string) {
// truncateFieldRecursively truncates the named "field" from the given data object and its sub-objects to 50 characters.
func truncateFieldRecursively(data map[string]interface{}, maxLength int, field string) {

Comment on lines 94 to 96
if str, ok := value.(string); ok && len(str) > maxLength {
data[key] = str[:maxLength]
}
Copy link
Member

Choose a reason for hiding this comment

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

This appears to be truncating to the maxLength - good!

continue
}
if subObj, ok := value.(map[string]interface{}); ok {
truncateFieldRecursively(subObj, 50, field)
Copy link
Member

Choose a reason for hiding this comment

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

Why not pass maxLength here?

Suggested change
truncateFieldRecursively(subObj, 50, field)
truncateFieldRecursively(subObj, maxLength, field)

if subObjs, ok := value.([]interface{}); ok {
for _, subObj := range subObjs {
if subObjMap, ok := subObj.(map[string]interface{}); ok {
truncateFieldRecursively(subObjMap, 50, field)
Copy link
Member

Choose a reason for hiding this comment

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

Ditto - pass maxLength here?

Suggested change
truncateFieldRecursively(subObjMap, 50, field)
truncateFieldRecursively(subObjMap, maxLength, field)

Comment on lines 112 to 113
// truncates the given "field" from CustomResourceDefinition to 50 characters.
func TruncateField(field string, targetLength int) manifestival.Transformer {
Copy link
Member

Choose a reason for hiding this comment

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

Since this has special behavior for CRDs and implements the Transformer interface, I recommend updating the function name to make this more clear. I also updated the length variable to maxLength so it is consistent.

Suggested change
// truncates the given "field" from CustomResourceDefinition to 50 characters.
func TruncateField(field string, targetLength int) manifestival.Transformer {
// TruncateCRDFieldTransformer returns a manifestival.Transformer that truncates the value of the given field within a CRD spec to the provided max length.
func TruncateCRDFieldTransformer(field string, maxLength int) manifestival.Transformer {

return nil
}
data := u.Object
truncateFieldRecursively(data, targetLength, field)
Copy link
Member

Choose a reason for hiding this comment

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

Continuing from above:

Suggested change
truncateFieldRecursively(data, targetLength, field)
truncateFieldRecursively(data, maxLength, field)

@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 30, 2024
Comment on lines 149 to 150
yaml.Unmarshal([]byte(crdYaml), &u.Object)
TruncateCRDFieldTransformer("description", 10)(u)
Copy link
Member

Choose a reason for hiding this comment

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

lint errors: Need to handle &/or check the return values for yaml.Unmarshal (returns err) & TruncateCRDFieldTransformer (returns manifestival.Transformer).

Copy link
Member

@adambkaplan adambkaplan left a comment

Choose a reason for hiding this comment

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

/approve

Code changes look good - I suggested a few nit fixes that shouldn't block merge.

Need to investigate why the lint check bombed out (may re-test without the cache).

pkg/common/util_test.go Outdated Show resolved Hide resolved
pkg/common/util_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

openshift-ci bot commented May 3, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: adambkaplan

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

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 3, 2024
@adambkaplan adambkaplan changed the title truncating description fields to max length of 50 to avoid unit test failure Truncate CRD Description Fields to 50 Characters May 3, 2024
@openshift-ci openshift-ci bot added release-note and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels May 3, 2024
@adambkaplan
Copy link
Member

/kind bug

@openshift-ci openshift-ci bot added the kind/bug Categorizes issue or PR as related to a bug. label May 3, 2024
@adambkaplan adambkaplan added this to the release-v0.13.0 milestone May 3, 2024
Adding a TruncateField transformer and using it to truncate CRD
description fields from the kodata/release.yaml manifest.
The CRDs for Shipwright Build objects are very large, resulting
in a validation error from Kubernetes when we try to create the
CRDs with Manifestival.
@adambkaplan
Copy link
Member

/lgtm

🎉 Thank you!

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label May 3, 2024
@openshift-merge-bot openshift-merge-bot bot merged commit d26c155 into shipwright-io:main May 3, 2024
4 checks passed
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. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. release-note size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants