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

Modified faqs to fix preserveUnknownFields issue from make bundle #5219

Merged
merged 4 commits into from
Sep 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions website/content/en/docs/faqs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,41 @@ For more information and examples, please see the type-specific docs:
[rbac]:https://kubernetes.io/docs/reference/access-authn-authz/rbac/
[scorecard-doc]: https://sdk.operatorframework.io/docs/testing-operators/scorecard/
[project-doc]: /docs/overview/project-layout

## Preserve the `preserveUnknownFields` in your CRDs

The [`preserveUnknownFields`](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#field-pruning) will be removed if set to false when running `make bundle`. Because of some underlying data structure changes and how yaml is unmarshalled, it is best to add them back in after they have been written.

You can use this script to post process the files to add the `preserveUnknownFields` back in.

```sh
function generate_preserveUnknownFieldsdata() {
laxmikantbpandhare marked this conversation as resolved.
Show resolved Hide resolved
for j in config/crd/patches/*.yaml ; do
if grep -qF "preserveUnknownFields" "$j";then
variable=`awk '/metadata/{flag=1} flag && /name:/{print $NF;flag=""}' "$j"`
for k in config/crd/bases/*.yaml ; do
if grep -qF "$variable" "$j";then
filename=`awk 'END{ var=FILENAME; split (var,a,/\//); print a[4]}' "$k"`
awk '/^spec:/{print;print " preserveUnknownFields: false";next}1' "bundle/manifests/$filename" > testfile.tmp && mv testfile.tmp "bundle/manifests/$filename"
fi
done
fi
done
}

generate_preserveUnknownFieldsdata
```

You can then modify the `bundle` target in your `Makefile` by adding a call to the script at the end of the target. See the example below:

```
.PHONY: bundle
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
operator-sdk generate kustomize manifests -q
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
operator-sdk bundle validate ./bundle | ./preserve_script.sh
```
laxmikantbpandhare marked this conversation as resolved.
Show resolved Hide resolved

Note:
Though this is a bug with controller-gen which is used by Operator SDK to generate CRD, this is a workaround from our end to enable users to preserve the field after controller-gen has run.