Skip to content

Commit 6d850cd

Browse files
zetxqxkfswain
authored andcommitted
feat: generate crd with version annotation. (kubernetes-sigs#1134)
* generate crd with version annotation. * remove unneeded make manifests in MakeFile. * update version. * regeneration. * remove manifests dep. * update Makefile. * update Makefile. * resolve conflict. * resolve conflict. * fix typos and use version/version.go * remove consts * typos and format. * regeneration. * renamve to version pkg. * update release tool as well. * version update. * version update. * unstage * version * version * version
1 parent 2d9bfa8 commit 6d850cd

File tree

8 files changed

+150
-16
lines changed

8 files changed

+150
-16
lines changed

Makefile

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,8 @@ help: ## Display this help.
9595

9696
##@ Development
9797

98-
.PHONY: manifests
99-
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
100-
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
101-
10298
.PHONY: generate
103-
generate: controller-gen code-generator manifests ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
99+
generate: controller-gen code-generator ## Generate WebhookConfiguration, ClusterRole, CustomResourceDefinition objects, code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
104100
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate/boilerplate.generatego.txt" paths="./..."
105101
./hack/update-codegen.sh
106102

@@ -132,7 +128,7 @@ vet: ## Run go vet against code.
132128
go vet ./...
133129

134130
.PHONY: test
135-
test: manifests generate fmt vet envtest image-build verify-crds ## Run tests.
131+
test: generate fmt vet envtest image-build verify-crds ## Run tests.
136132
CGO_ENABLED=1 KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e | grep -v /conformance) -race -coverprofile cover.out
137133

138134
.PHONY: test-unit
@@ -160,7 +156,7 @@ ci-lint: golangci-lint
160156
$(GOLANGCI_LINT) run --timeout 15m0s
161157

162158
.PHONY: verify
163-
verify: vet fmt-verify manifests generate ci-lint verify-all
159+
verify: vet fmt-verify generate ci-lint verify-all
164160
git --no-pager diff --exit-code config api client-go
165161

166162
.PHONY: verify-crds
@@ -311,11 +307,11 @@ ifndef ignore-not-found
311307
endif
312308

313309
.PHONY: install
314-
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
310+
install: generate kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
315311
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
316312

317313
.PHONY: uninstall
318-
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
314+
uninstall: generate kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
319315
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
320316

321317
##@ Helm

RELEASE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ The Kubernetes Template Project is released on an as-needed basis. The process i
77
1. All [OWNERS](OWNERS) must LGTM this release
88
1. An OWNER runs `git tag -s $VERSION` and inserts the changelog and pushes the tag with `git push $VERSION`
99
1. The release issue is closed
10-
1. An announcement email is sent to `[email protected]` with the subject `[ANNOUNCE] kubernetes-template-project $VERSION is released`
10+
1. An announcement email is sent to `[email protected]` with the subject `[ANNOUNCE] kubernetes-template-project $VERSION is released`

config/crd/bases/inference.networking.x-k8s.io_inferencemodels.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
---
21
apiVersion: apiextensions.k8s.io/v1
32
kind: CustomResourceDefinition
43
metadata:
54
annotations:
6-
controller-gen.kubebuilder.io/version: v0.16.1
5+
inference.networking.k8s.io/bundle-version: v1.0.0-dev
6+
creationTimestamp: null
77
name: inferencemodels.inference.networking.x-k8s.io
88
spec:
99
group: inference.networking.x-k8s.io
@@ -254,3 +254,9 @@ spec:
254254
storage: true
255255
subresources:
256256
status: {}
257+
status:
258+
acceptedNames:
259+
kind: ""
260+
plural: ""
261+
conditions: null
262+
storedVersions: null

config/crd/bases/inference.networking.x-k8s.io_inferencepools.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
---
21
apiVersion: apiextensions.k8s.io/v1
32
kind: CustomResourceDefinition
43
metadata:
54
annotations:
6-
controller-gen.kubebuilder.io/version: v0.16.1
5+
inference.networking.k8s.io/bundle-version: v1.0.0-dev
6+
creationTimestamp: null
77
name: inferencepools.inference.networking.x-k8s.io
88
spec:
99
group: inference.networking.x-k8s.io
@@ -283,3 +283,9 @@ spec:
283283
storage: true
284284
subresources:
285285
status: {}
286+
status:
287+
acceptedNames:
288+
kind: ""
289+
plural: ""
290+
conditions: null
291+
storedVersions: null

hack/release-quickstart.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ echo "Using vLLM GPU image version: ${VLLM_GPU}"
4242
echo "Using vLLM CPU image version: ${VLLM_CPU}"
4343
echo "Using vLLM Simulator image version: ${VLLM_SIM}"
4444

45+
# -----------------------------------------------------------------------------
46+
# Update version/version.go and generating CRDs with new version annotations
47+
# -----------------------------------------------------------------------------
48+
VERSION_FILE="version/version.go"
49+
echo "Updating ${VERSION_FILE} ..."
50+
51+
# Replace bundleVersion in version.go
52+
# This regex finds the line with "BundleVersion" and replaces the string within the quotes.
53+
sed -i.bak -E "s|( *BundleVersion = \")[^\"]+(\")|\1${RELEASE_TAG}\2|g" "$VERSION_FILE"
54+
55+
UPDATED_CRD="config/crd/"
56+
echo "Generating CRDs with new annotations in $UPDATED_CRD"
57+
go run ./pkg/generator
58+
echo "Generated CRDs with new annotations in $UPDATED_CRD"
59+
4560
# -----------------------------------------------------------------------------
4661
# Update pkg/README.md
4762
# -----------------------------------------------------------------------------
@@ -115,8 +130,8 @@ sed -i.bak '/llm-d\/llm-d-inference-sim/{n;s/Always/IfNotPresent/;}' "$VLLM_SIM_
115130
# -----------------------------------------------------------------------------
116131
# Stage the changes
117132
# -----------------------------------------------------------------------------
118-
echo "Staging $README $EPP $EPP_HELM $BBR_HELM $CONFORMANCE_MANIFESTS $VLLM_GPU_DEPLOY $VLLM_CPU_DEPLOY $VLLM_SIM_DEPLOY files..."
119-
git add $README $EPP $EPP_HELM $BBR_HELM $CONFORMANCE_MANIFESTS $VLLM_GPU_DEPLOY $VLLM_CPU_DEPLOY $VLLM_SIM_DEPLOY
133+
echo "Staging $VERSION_FILE $UPDATED_CRD $README $EPP $EPP_HELM $BBR_HELM $CONFORMANCE_MANIFESTS $VLLM_GPU_DEPLOY $VLLM_CPU_DEPLOY $VLLM_SIM_DEPLOY files..."
134+
git add $VERSION_FILE $UPDATED_CRD $README $EPP $EPP_HELM $BBR_HELM $CONFORMANCE_MANIFESTS $VLLM_GPU_DEPLOY $VLLM_CPU_DEPLOY $VLLM_SIM_DEPLOY
120135

121136
# -----------------------------------------------------------------------------
122137
# Cleanup backup files and finish

hack/update-codegen.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ set -o errexit
1818
set -o nounset
1919
set -o pipefail
2020

21+
echo "Generating CRDs"
22+
go run ./pkg/generator
23+
2124
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
2225
echo "$SCRIPT_ROOT script"
2326
CODEGEN_PKG=${2:-bin}

pkg/generator/main.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"log"
22+
"os"
23+
24+
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
25+
"sigs.k8s.io/controller-tools/pkg/crd"
26+
"sigs.k8s.io/controller-tools/pkg/loader"
27+
"sigs.k8s.io/controller-tools/pkg/markers"
28+
"sigs.k8s.io/gateway-api-inference-extension/version"
29+
"sigs.k8s.io/yaml"
30+
)
31+
32+
// This generation code is largely copied from
33+
// github.com/kubernetes-sigs/gateway-api/blob/main/pkg/generator/main.go
34+
func main() {
35+
roots, err := loader.LoadRoots(
36+
"k8s.io/apimachinery/pkg/runtime/schema", // Needed to parse generated register functions.
37+
"sigs.k8s.io/gateway-api-inference-extension/api/v1alpha2",
38+
)
39+
if err != nil {
40+
log.Fatalf("failed to load package roots: %s", err)
41+
}
42+
43+
generator := &crd.Generator{}
44+
45+
parser := &crd.Parser{
46+
Collector: &markers.Collector{Registry: &markers.Registry{}},
47+
Checker: &loader.TypeChecker{
48+
NodeFilters: []loader.NodeFilter{generator.CheckFilter()},
49+
},
50+
}
51+
52+
err = generator.RegisterMarkers(parser.Collector.Registry)
53+
if err != nil {
54+
log.Fatalf("failed to register markers: %s", err)
55+
}
56+
57+
crd.AddKnownTypes(parser)
58+
for _, r := range roots {
59+
parser.NeedPackage(r)
60+
}
61+
62+
metav1Pkg := crd.FindMetav1(roots)
63+
if metav1Pkg == nil {
64+
log.Fatalf("no objects in the roots, since nothing imported metav1")
65+
}
66+
67+
kubeKinds := crd.FindKubeKinds(parser, metav1Pkg)
68+
if len(kubeKinds) == 0 {
69+
log.Fatalf("no objects in the roots")
70+
}
71+
72+
for _, groupKind := range kubeKinds {
73+
74+
log.Printf("generating CRD for %v\n", groupKind)
75+
76+
parser.NeedCRDFor(groupKind, nil)
77+
crdRaw := parser.CustomResourceDefinitions[groupKind]
78+
79+
// Inline version of "addAttribution(&crdRaw)" ...
80+
if crdRaw.ObjectMeta.Annotations == nil {
81+
crdRaw.ObjectMeta.Annotations = map[string]string{}
82+
}
83+
crdRaw.ObjectMeta.Annotations[version.BundleVersionAnnotation] = version.BundleVersion
84+
85+
// Prevent the top level metadata for the CRD to be generated regardless of the intention in the arguments
86+
crd.FixTopLevelMetadata(crdRaw)
87+
88+
conv, err := crd.AsVersion(*crdRaw.DeepCopy(), apiext.SchemeGroupVersion)
89+
if err != nil {
90+
log.Fatalf("failed to convert CRD: %s", err)
91+
}
92+
93+
out, err := yaml.Marshal(conv)
94+
if err != nil {
95+
log.Fatalf("failed to marshal CRD: %s", err)
96+
}
97+
98+
fileName := fmt.Sprintf("config/crd/bases/%s_%s.yaml", crdRaw.Spec.Group, crdRaw.Spec.Names.Plural)
99+
err = os.WriteFile(fileName, out, 0o600)
100+
if err != nil {
101+
log.Fatalf("failed to write CRD: %s", err)
102+
}
103+
}
104+
}

version/version.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ limitations under the License.
1717
package version
1818

1919
const (
20+
// BundleVersionAnnotation is the annotation key used in the Gateway API inference extension CRDs to specify
21+
// the installed Gateway API inference extension version.
22+
BundleVersionAnnotation = "inference.networking.k8s.io/bundle-version"
23+
2024
// BundleVersion is the value used for labeling the version of the gateway-api-inference-extension.
2125
BundleVersion = "v0.5.1"
2226
)

0 commit comments

Comments
 (0)