diff --git a/Dockerfile_generator b/Dockerfile_generator index 3a4e4bedc..ffddd0287 100644 --- a/Dockerfile_generator +++ b/Dockerfile_generator @@ -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 diff --git a/Makefile b/Makefile index d90db4a4a..f77e7a9a9 100644 --- a/Makefile +++ b/Makefile @@ -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') diff --git a/go.mod b/go.mod index 67746c810..375476f91 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 70cbe6584..03bd53edc 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/hack/generate.sh b/hack/generate.sh deleted file mode 100755 index 71ceedba0..000000000 --- a/hack/generate.sh +++ /dev/null @@ -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 - diff --git a/hack/tools.go b/hack/tools.go new file mode 100644 index 000000000..e38e6c643 --- /dev/null +++ b/hack/tools.go @@ -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" diff --git a/hack/update_codegen.sh b/hack/update_codegen.sh new file mode 100755 index 000000000..77a530d0b --- /dev/null +++ b/hack/update_codegen.sh @@ -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 \ No newline at end of file diff --git a/pkg/apis/nfd/v1alpha1/types.go b/pkg/apis/nfd/v1alpha1/types.go index f5bfcb7ec..66122ba97 100644 --- a/pkg/apis/nfd/v1alpha1/types.go +++ b/pkg/apis/nfd/v1alpha1/types.go @@ -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"`