1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

Update generate scripts to use latest code_gen functions

Rewrite the generate.sh into update_codegen inspired in
k8s.io/code-generator documentation.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
This commit is contained in:
Carlos Eduardo Arango Gutierrez 2024-03-08 19:44:42 +01:00
parent 8553265d04
commit dd24e8bc97
No known key found for this signature in database
GPG key ID: 42D9CB42F300A852
8 changed files with 99 additions and 42 deletions

View file

@ -21,3 +21,7 @@ RUN curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v25.3
# Expect to be working with nfd
WORKDIR /go/node-feature-discovery
# We need to set the /go/node-feature-discovery directory as a safe directory.
# This allows git commands to run in the container on MacOS systems.
RUN git config --file=/.gitconfig --add safe.directory /go/node-feature-discovery

View file

@ -160,7 +160,7 @@ generate: .generator.image.stamp
-v "`go env GOMODCACHE`:/go/pkg/mod" \
--user=`id -u`:`id -g`\
nfd-generator \
./hack/generate.sh
./hack/update_codegen.sh
gofmt:
@$(GO_FMT) -w -l $$(find . -name '*.go')

4
go.mod
View file

@ -29,12 +29,14 @@ require (
k8s.io/apiextensions-apiserver v0.29.0
k8s.io/apimachinery v0.29.0
k8s.io/client-go v0.29.0
k8s.io/code-generator v0.29.0
k8s.io/klog/v2 v2.110.1
k8s.io/kubectl v0.29.0
k8s.io/kubelet v0.29.0
k8s.io/kubernetes v1.29.0
k8s.io/pod-security-admission v0.29.0
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/structured-merge-diff/v4 v4.4.1
sigs.k8s.io/yaml v1.3.0
)
@ -190,6 +192,7 @@ require (
k8s.io/cri-api v0.29.0 // indirect
k8s.io/csi-translation-lib v0.29.0 // indirect
k8s.io/dynamic-resource-allocation v0.29.0 // indirect
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 // indirect
k8s.io/kms v0.29.0 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/kube-scheduler v0.0.0 // indirect
@ -197,7 +200,6 @@ require (
k8s.io/mount-utils v0.25.0 // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.28.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
// The k8s "sub-"packages do not have 'semver' compatible versions. Thus, we

2
go.sum
View file

@ -2174,6 +2174,7 @@ k8s.io/cloud-provider v0.29.0 h1:Qgk/jHsSKGRk/ltTlN6e7eaNuuamLROOzVBd0RPp94M=
k8s.io/cloud-provider v0.29.0/go.mod h1:gBCt7YYKFV4oUcJ/0xF9lS/9il4MxKunJ+ZKvh39WGo=
k8s.io/cluster-bootstrap v0.29.0 h1:zCYdZ+LWDj4O86FB5tDKckIEsf2qBHjcp78xtjOzD3A=
k8s.io/cluster-bootstrap v0.29.0/go.mod h1:PDk+ouXC6bM2FFdo4u53IlWK3ZMWebH4TeE5BGmHWRw=
k8s.io/code-generator v0.29.0 h1:2LQfayGDhaIlaamXjIjEQlCMy4JNCH9lrzas4DNW1GQ=
k8s.io/code-generator v0.29.0/go.mod h1:5bqIZoCxs2zTRKMWNYqyQWW/bajc+ah4rh0tMY8zdGA=
k8s.io/component-base v0.29.0 h1:T7rjd5wvLnPBV1vC4zWd/iWRbV8Mdxs+nGaoaFzGw3s=
k8s.io/component-base v0.29.0/go.mod h1:sADonFTQ9Zc9yFLghpDpmNXEdHyQmFIGbiuZbqAXQ1M=
@ -2187,6 +2188,7 @@ k8s.io/csi-translation-lib v0.29.0 h1:we4X1yUlDikvm5Rv0dwMuPHNw6KwjwsQiAuOPWXha8
k8s.io/csi-translation-lib v0.29.0/go.mod h1:Cp6t3CNBSm1dXS17V8IImUjkqfIB6KCj8Fs8wf6uyTA=
k8s.io/dynamic-resource-allocation v0.29.0 h1:JQW5erdoOsvhst7DxMfEpnXhrfm9SmNTnvyaXdqTLAE=
k8s.io/dynamic-resource-allocation v0.29.0/go.mod h1:4T9Fg4J8B2SdRVVj5/1hM7hGAUrBqzyEUIGFY+wQl4Q=
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 h1:pWEwq4Asjm4vjW7vcsmijwBhOr1/shsbSYiWXmNGlks=
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=

View file

@ -1,39 +0,0 @@
#!/bin/bash -e
set -o pipefail
# Default path for code-generator repo
K8S_CODE_GENERATOR=${K8S_CODE_GENERATOR:-../code-generator}
# Omit version control information (buildvcs)
export GOENV="/go/env"
go env -w GOFLAGS="-buildvcs=false"
go mod vendor
go generate ./cmd/... ./pkg/... ./source/...
rm -rf vendor/
controller-gen object crd output:crd:stdout paths=./pkg/apis/... > deployment/base/nfd-crds/nfd-api-crds.yaml
mkdir -p deployment/helm/node-feature-discovery/crds
cp deployment/base/nfd-crds/nfd-api-crds.yaml deployment/helm/node-feature-discovery/crds
rm -rf sigs.k8s.io
${K8S_CODE_GENERATOR}/generate-groups.sh client,informer,lister \
sigs.k8s.io/node-feature-discovery/pkg/generated \
sigs.k8s.io/node-feature-discovery/pkg/apis \
"nfd:v1alpha1" --output-base=. \
--go-header-file hack/boilerplate.go.txt
rm -rf pkg/generated
mv sigs.k8s.io/node-feature-discovery/pkg/generated pkg/
# HACK: manually patching the auto-generated code as code-generator cannot
# properly handle deepcopy of MatchExpressionSet.
sed s'/out = new(map\[string\]\*MatchExpression)/out = new(MatchExpressionSet)/' -i pkg/apis/nfd/v1alpha1/zz_generated.deepcopy.go
rm -rf sigs.k8s.io

20
hack/tools.go Normal file
View file

@ -0,0 +1,20 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This package imports things required by build scripts, to force `go mod` to see them as dependencies
package tools
import _ "k8s.io/code-generator"

68
hack/update_codegen.sh Executable file
View file

@ -0,0 +1,68 @@
#!/usr/bin/env bash
# Copyright 2024 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
GO_CMD=${1:-go}
NFD_ROOT=$(realpath $(dirname ${BASH_SOURCE[0]})/..)
go mod vendor
CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${NFD_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)}
cd $(dirname ${BASH_SOURCE[0]})/..
source "${CODEGEN_PKG}/kube_codegen.sh"
# TODO: remove the workaround when the issue is solved in the code-generator
# (https://github.com/kubernetes/code-generator/issues/165).
# Here, we create the soft link named "sigs.k8s.io" to the parent directory of
# node-feature-discovery to ensure the layout required by the kube_codegen.sh script.
ln -s .. sigs.k8s.io
trap "rm sigs.k8s.io" EXIT
CODEGEN_PKG=${CODEGEN_PKG:-$(ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)}
source "${CODEGEN_PKG}/kube_codegen.sh"
go generate ./cmd/... ./pkg/... ./source/...
controller-gen object crd output:crd:stdout paths=./pkg/apis/... > deployment/base/nfd-crds/nfd-api-crds.yaml
mkdir -p deployment/helm/node-feature-discovery/crds
cp deployment/base/nfd-crds/nfd-api-crds.yaml deployment/helm/node-feature-discovery/crds
# Generating conversion and defaults functions
kube::codegen::gen_helpers \
--input-pkg-root sigs.k8s.io/node-feature-discovery/apis \
--output-base "${NFD_ROOT}" \
--boilerplate ${NFD_ROOT}/hack/boilerplate.go.txt
kube::codegen::gen_client \
--input-pkg-root sigs.k8s.io/node-feature-discovery/pkg/apis \
--output-pkg-root sigs.k8s.io/node-feature-discovery/pkg/generated \
--output-base "${NFD_ROOT}" \
--boilerplate ${NFD_ROOT}/hack/boilerplate.go.txt \
--with-watch
# HACK: manually patching the auto-generated code as code-generator cannot
# properly handle deepcopy of MatchExpressionSet.
sed s'/out = new(map\[string\]\*MatchExpression)/out = new(MatchExpressionSet)/' -i pkg/apis/nfd/v1alpha1/zz_generated.deepcopy.go
# We need to clean up the go.mod file since code-generator adds temporary library to the go.mod file.
"${GO_CMD}" mod tidy

View file

@ -34,8 +34,8 @@ type NodeFeatureList struct {
// NodeFeature resource holds the features discovered for one node in the
// cluster.
// +kubebuilder:object:root=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type NodeFeature struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`