-
Notifications
You must be signed in to change notification settings - Fork 17
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
Truncate CRD Description Fields to 50 Characters #204
Conversation
There was a problem hiding this 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.
pkg/common/util.go
Outdated
// Recursively truncates the given "field" from CustomResourceDefinition to 50 characters. | ||
func truncateFieldRecursively(data map[string]interface{}, maxLength int, field string) { |
There was a problem hiding this comment.
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.
// 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) { |
pkg/common/util.go
Outdated
if str, ok := value.(string); ok && len(str) > maxLength { | ||
data[key] = str[:maxLength] | ||
} |
There was a problem hiding this comment.
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!
pkg/common/util.go
Outdated
continue | ||
} | ||
if subObj, ok := value.(map[string]interface{}); ok { | ||
truncateFieldRecursively(subObj, 50, field) |
There was a problem hiding this comment.
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?
truncateFieldRecursively(subObj, 50, field) | |
truncateFieldRecursively(subObj, maxLength, field) |
pkg/common/util.go
Outdated
if subObjs, ok := value.([]interface{}); ok { | ||
for _, subObj := range subObjs { | ||
if subObjMap, ok := subObj.(map[string]interface{}); ok { | ||
truncateFieldRecursively(subObjMap, 50, field) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto - pass maxLength
here?
truncateFieldRecursively(subObjMap, 50, field) | |
truncateFieldRecursively(subObjMap, maxLength, field) |
pkg/common/util.go
Outdated
// truncates the given "field" from CustomResourceDefinition to 50 characters. | ||
func TruncateField(field string, targetLength int) manifestival.Transformer { |
There was a problem hiding this comment.
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.
// 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 { |
pkg/common/util.go
Outdated
return nil | ||
} | ||
data := u.Object | ||
truncateFieldRecursively(data, targetLength, field) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Continuing from above:
truncateFieldRecursively(data, targetLength, field) | |
truncateFieldRecursively(data, maxLength, field) |
135ec29
to
4ebe224
Compare
pkg/common/util_test.go
Outdated
yaml.Unmarshal([]byte(crdYaml), &u.Object) | ||
TruncateCRDFieldTransformer("description", 10)(u) |
There was a problem hiding this comment.
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).
545c0d9
to
8f57b3b
Compare
There was a problem hiding this 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).
[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 |
/kind bug |
0531353
to
2e8cffc
Compare
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.
2e8cffc
to
9290711
Compare
/lgtm 🎉 Thank you! |
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
See the contributor guide
for details on coding conventions, github and prow interactions, and the code review process.
Release Notes