Skip to content

Commit

Permalink
Add codegen scripts and make required api changes (ray-project#96)
Browse files Browse the repository at this point in the history
* Update go modules to include code-generator

* Add scripts to generate clients

* Clean up code-generator dependency

Signed-off-by: Jiaxin Shan <[email protected]>

* Update codegen scripts and make required api changes

1. Add update-codegen.sh and verify-codegen.sh
2. Add required changes to register.go and doc.go and //+genclient tag in raycluster_types.go

Signed-off-by: Jiaxin Shan <[email protected]>

Co-authored-by: Jiaxin Shan <[email protected]>
  • Loading branch information
harryge00 and Jeffwan authored Nov 3, 2021
1 parent 2f9c2a2 commit 4dc5b92
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ray-operator/api/raycluster/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// +groupName=ray.io
package v1alpha1
1 change: 1 addition & 0 deletions ray-operator/api/raycluster/v1alpha1/raycluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const (
// RayCluster is the Schema for the RayClusters API
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+genclient
type RayCluster struct {
// Standard object metadata.
metav1.TypeMeta `json:",inline"`
Expand Down
12 changes: 12 additions & 0 deletions ray-operator/api/raycluster/v1alpha1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
)

// SchemeGroupVersion is group version used to register these objects.
var SchemeGroupVersion = GroupVersion

func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}
1 change: 1 addition & 0 deletions ray-operator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ require (
k8s.io/client-go v0.19.14
k8s.io/utils v0.0.0-20200912215256-4140de9c8800
sigs.k8s.io/controller-runtime v0.7.2
k8s.io/code-generator v0.19.14
)
2 changes: 2 additions & 0 deletions ray-operator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ k8s.io/client-go v0.19.2/go.mod h1:S5wPhCqyDNAlzM9CnEdgTGV4OqhsW3jGO1UM1epwfJA=
k8s.io/client-go v0.19.14 h1:D3V4mC6lQN3ZaABhOznxZXSsxMR5cdChjeMP4/pY4H8=
k8s.io/client-go v0.19.14/go.mod h1:iWwXK/etTTPyssCycvgdQ9d6i0k93HXLtksUQ6A67WE=
k8s.io/code-generator v0.19.2/go.mod h1:moqLn7w0t9cMs4+5CQyxnfA/HV8MF6aAVENF+WZZhgk=
k8s.io/code-generator v0.19.14 h1:EFU42Hj0+FBZciWedyRtFHsPu75IaWca0slGB1LlBqk=
k8s.io/code-generator v0.19.14/go.mod h1:ADrDvaUQWGn4a8lX0ONtzb7uFmDRQOMSYIMk1qWIAx8=
k8s.io/component-base v0.19.2 h1:jW5Y9RcZTb79liEhW3XDVTW7MuvEGP0tQZnfSX6/+gs=
k8s.io/component-base v0.19.2/go.mod h1:g5LrsiTiabMLZ40AR6Hl45f088DevyGY+cCE2agEIVo=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
Expand Down
15 changes: 15 additions & 0 deletions ray-operator/hack/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build tools
// +build tools

package tools

// This package imports things required by this repository, to force `go mod` to see them as dependencies
import (
_ "k8s.io/code-generator"
_ "k8s.io/code-generator/cmd/client-gen"
_ "k8s.io/code-generator/cmd/deepcopy-gen"
_ "k8s.io/code-generator/cmd/defaulter-gen"
_ "k8s.io/code-generator/cmd/informer-gen"
_ "k8s.io/code-generator/cmd/lister-gen"
_ "k8s.io/code-generator/cmd/openapi-gen"
)
54 changes: 54 additions & 0 deletions ray-operator/hack/update-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# This shell is used to auto generate some useful tools for k8s, such as clientset, lister, informer and so on.
# We don't use this tool to generate deepcopy because kubebuilder (controller-tools) has coverred that part.

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
ROOT_PKG=github.com/ray-project/kuberay/ray-operator

# Grab code-generator version from go.sum
CODEGEN_VERSION=$(grep 'k8s.io/code-generator' go.sum | awk '{print $2}' | sed 's/\/go.mod//g' | head -1)
CODEGEN_PKG=$(echo `go env GOPATH`"/pkg/mod/k8s.io/code-generator@${CODEGEN_VERSION}")

if [[ ! -d ${CODEGEN_PKG} ]]; then
echo "${CODEGEN_PKG} is missing. Running 'go mod download'."
go mod download
fi

echo ">> Using ${CODEGEN_PKG}"

# code-generator does work with go.mod but makes assumptions about
# the project living in `$GOPATH/src`. To work around this and support
# any location; create a temporary directory, use this as an output
# base, and copy everything back once generated.
TEMP_DIR=$(mktemp -d)
cleanup() {
echo ">> Removing ${TEMP_DIR}"
# rm -rf ${TEMP_DIR}
}
trap "cleanup" EXIT SIGINT

echo ">> Temporary output directory ${TEMP_DIR}"

# Ensure we can execute.
chmod +x ${CODEGEN_PKG}/generate-groups.sh

# 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.
#
cd ${SCRIPT_ROOT}
${CODEGEN_PKG}/generate-groups.sh "client,informer,lister" \
github.com/ray-project/kuberay/ray-operator/pkg/client github.com/ray-project/kuberay/ray-operator/api \
raycluster:v1alpha1 \
--output-base "${TEMP_DIR}" \
--go-header-file hack/boilerplate.go.txt


# Copy everything back.
cp -a "${TEMP_DIR}/${ROOT_PKG}/." "${SCRIPT_ROOT}/"
34 changes: 34 additions & 0 deletions ray-operator/hack/verify-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..

DIFFROOT="${SCRIPT_ROOT}/pkg"
TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/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 "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
fi

0 comments on commit 4dc5b92

Please sign in to comment.