- Create CRD and controller with
kubebuilderlink - Create clientset, informer, etc with
code-generatorlink - Solve conflicts of two generators
- Modify
Makefileto preventkubebuilderscan the folder generated bycode-generator.kubebuilderwon't work with the.gofiles generated bycode-generator. - Add folder created by
code-generatortoDockerfilegenerated bykubebuilder. Otherwise,*.gofiles won't compile in the docker build process. PodInfois actually a poor choice of CRD name.kubebuilderthink its plural form ispodinfoeswhilecode-generatorthinks it should bepodinfos. This will cause a permission error.
- Modify
MODULE=fluxframework.io/jgfoperator
go mod init $MODULE
kubebuilder init --domain fluxframework.io
kubebuilder edit --multigroup=truekubebuilder create api --group flux --version v1 --kind PodInfo
Create Resource[y/n]y
Create Controller[y/n]y
- Do some editing to the type definition.
make manifests
make docker-buildmake docker-push-local
make deploy-localkubebuilder create api --group core --version v1 --kind Pod
Create Resource[y/n]n
Create Controller[y/n]ymake manifestsModify pod_controller.go
- It seems that
make docker-buildwill automatically run the following cmd
go get k8s.io/api/core/v1@v0.22.1-
MODULE and go.mod bring into correspondence with API_PKG=apis, consistent with the API directory OUTPUT_PKG=generated/flux, the group specified when generating the Resource is the same GROUP_VERSION=flux:v1 Corresponds to the group version specified when the Resource is generated
hack/tools.go
// +build tools
package tools
import _ "k8s.io/code-generator"update-codegen.sh
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
# corresponding to go mod init <module>
MODULE=fluxframework.io/jgfoperator
# api package
APIS_PKG=apis
# generated output package
OUTPUT_PKG=generated/flux
# group-version such as foo:v1alpha1
GROUP_VERSION=flux:v1
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)}
# generate the code with:
# --output-base because this script should also be able to run inside the vendor dir of
# k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
# instead of the $GOPATH directly. For normal projects this can be dropped.
bash "${CODEGEN_PKG}"/generate-groups.sh "client,lister,informer" \
${MODULE}/${OUTPUT_PKG} ${MODULE}/${APIS_PKG} \
${GROUP_VERSION} \
--go-header-file "${SCRIPT_ROOT}"/hack/boilerplate.go.txt \
--output-base "${SCRIPT_ROOT}"
# --output-base "${SCRIPT_ROOT}/../../.." \verify-codegen.sh
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
OUTPUT_PKG=generated/flux
MODULE=fluxframework.io/jgfoperator
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
DIFFROOT="${SCRIPT_ROOT}/${OUTPUT_PKG}"
TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/${OUTPUT_PKG}"
_tmp="${SCRIPT_ROOT}/_tmp"
cleanup() {
rm -rf "${_tmp}"
}
trap "cleanup" EXIT SIGINT
cleanup
mkdir -p "${TMP_DIFFROOT}"
cp -a "${DIFFROOT}"/* "${TMP_DIFFROOT}"
"${SCRIPT_ROOT}/hack/update-codegen.sh"
echo "copying generated ${SCRIPT_ROOT}/${MODULE}/${OUTPUT_PKG} to ${DIFFROOT}"
cp -r "${SCRIPT_ROOT}/${MODULE}/${OUTPUT_PKG}"/* "${DIFFROOT}"
echo "diffing ${DIFFROOT} against freshly generated codegen"
ret=0
diff -Naupr "${DIFFROOT}" "${TMP_DIFFROOT}" || ret=$?
cp -a "${TMP_DIFFROOT}"/* "${DIFFROOT}"
if [[ $ret -eq 0 ]]
then
echo "${DIFFROOT} up to date."
else
echo "${DIFFROOT} is out of date. Please run hack/update-codegen.sh"
exit 1
fiK8S_VERSION=v0.22.1
go get k8s.io/code-generator@$K8S_VERSION
go mod vendorchmod +x vendor/k8s.io/code-generator/generate-groups.sh- add
// +clientgento types.go - add
doc.goandregister.goaccording to reference
./hack/update-codegen.sh- move
generatedfolder to project root- don't forget to add cmd to copy
generatedfolder in Dockerfile
- don't forget to add cmd to copy
- Notes on debug
-
The
Makefilegeneratedkubebuilderwill scan all the paths as defined inpaths=./.... Butcontroller-genwon't work with*.gofiles generated bycode-generator. So in order to ignore folder that generated bycode-generator, argumentpaths=./...is changed topaths=./apis/...andpaths=./controllers/... -
PodInfois a terrible choice ofKindname. Sincekubebuilderthinks it's plural form ispodinfoes, whilecode-generatorthinks it's plural form ispodinfos. As a result,clientsetgenerated bycode-generatorcomplains it doesn't have permission of resourcepodinfos, while the actual resource declared bykubebuilderispodinfoes. I decided to manually find and replace"podinfos"to"podinfoes"in thecode-generator-generated code since I think this part will have less changes after the type is defined. -
After the above step, don't forget to add permission to pod controller. Oneway to check if a new permission is added is that, after
make manifests, openrole.yamlto see if there are new changes.
-
- Initialize Kind local cluster with local docker registry
make undeploy./deploy-local.sh- deploy pi-test
kubectl get podinfoes- command to check the log
kubectl logs -n jgfoperator-system $(kubectl get pods -n jgfoperator-system -o jsonpath="{.items[0].metadata.name}") manager