diff --git a/ray-operator/api/raycluster/v1alpha1/doc.go b/ray-operator/api/raycluster/v1alpha1/doc.go new file mode 100644 index 00000000000..5027bb804fd --- /dev/null +++ b/ray-operator/api/raycluster/v1alpha1/doc.go @@ -0,0 +1,2 @@ +// +groupName=ray.io +package v1alpha1 diff --git a/ray-operator/api/raycluster/v1alpha1/raycluster_types.go b/ray-operator/api/raycluster/v1alpha1/raycluster_types.go index 19c45e12b82..9f9b1690bb3 100644 --- a/ray-operator/api/raycluster/v1alpha1/raycluster_types.go +++ b/ray-operator/api/raycluster/v1alpha1/raycluster_types.go @@ -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"` diff --git a/ray-operator/api/raycluster/v1alpha1/register.go b/ray-operator/api/raycluster/v1alpha1/register.go new file mode 100644 index 00000000000..2dfd631ebb5 --- /dev/null +++ b/ray-operator/api/raycluster/v1alpha1/register.go @@ -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() +} diff --git a/ray-operator/go.mod b/ray-operator/go.mod index 426a6347558..ee787f55f8e 100644 --- a/ray-operator/go.mod +++ b/ray-operator/go.mod @@ -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 ) diff --git a/ray-operator/go.sum b/ray-operator/go.sum index 0c3fceb8090..a0efa9754c7 100644 --- a/ray-operator/go.sum +++ b/ray-operator/go.sum @@ -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= diff --git a/ray-operator/hack/tools.go b/ray-operator/hack/tools.go new file mode 100644 index 00000000000..95d87f442b4 --- /dev/null +++ b/ray-operator/hack/tools.go @@ -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" +) diff --git a/ray-operator/hack/update-codegen.sh b/ray-operator/hack/update-codegen.sh new file mode 100755 index 00000000000..de712979fb9 --- /dev/null +++ b/ray-operator/hack/update-codegen.sh @@ -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}/" diff --git a/ray-operator/hack/verify-codegen.sh b/ray-operator/hack/verify-codegen.sh new file mode 100644 index 00000000000..9f5082dd717 --- /dev/null +++ b/ray-operator/hack/verify-codegen.sh @@ -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