generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 180
feat: generate crd with version annotation. #1134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
da52eb7
generate crd with version annotation.
zetxqx 919ace5
remove unneeded make manifests in MakeFile.
zetxqx 42e6716
update version.
zetxqx 9b2f879
regeneration.
zetxqx 599b79a
remove manifests dep.
zetxqx b7c256e
update Makefile.
zetxqx 1757d35
update Makefile.
zetxqx 65a007d
resolve conflict.
zetxqx 3cf163a
resolve conflict.
zetxqx af65dab
fix typos and use version/version.go
zetxqx f6f04dd
remove consts
zetxqx 308eaef
typos and format.
zetxqx 31ca81d
regeneration.
zetxqx 9b4b3ed
renamve to version pkg.
zetxqx afadb6a
update release tool as well.
zetxqx 4b232e5
version update.
zetxqx c0c7213
version update.
zetxqx 09c8d22
unstage
zetxqx c07d3cb
version
zetxqx 50e95f3
version
zetxqx 6f7c774
version
zetxqx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Copyright 2025 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. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"sigs.k8s.io/controller-tools/pkg/crd" | ||
"sigs.k8s.io/controller-tools/pkg/loader" | ||
"sigs.k8s.io/controller-tools/pkg/markers" | ||
"sigs.k8s.io/gateway-api-inference-extension/version" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// This generation code is largely copied from | ||
// github.com/kubernetes-sigs/gateway-api/blob/main/pkg/generator/main.go | ||
func main() { | ||
zetxqx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
roots, err := loader.LoadRoots( | ||
zetxqx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"k8s.io/apimachinery/pkg/runtime/schema", // Needed to parse generated register functions. | ||
"sigs.k8s.io/gateway-api-inference-extension/api/v1alpha2", | ||
) | ||
if err != nil { | ||
log.Fatalf("failed to load package roots: %s", err) | ||
} | ||
|
||
generator := &crd.Generator{} | ||
|
||
parser := &crd.Parser{ | ||
Collector: &markers.Collector{Registry: &markers.Registry{}}, | ||
Checker: &loader.TypeChecker{ | ||
NodeFilters: []loader.NodeFilter{generator.CheckFilter()}, | ||
}, | ||
} | ||
|
||
err = generator.RegisterMarkers(parser.Collector.Registry) | ||
if err != nil { | ||
log.Fatalf("failed to register markers: %s", err) | ||
} | ||
|
||
crd.AddKnownTypes(parser) | ||
for _, r := range roots { | ||
parser.NeedPackage(r) | ||
} | ||
|
||
metav1Pkg := crd.FindMetav1(roots) | ||
if metav1Pkg == nil { | ||
log.Fatalf("no objects in the roots, since nothing imported metav1") | ||
} | ||
|
||
kubeKinds := crd.FindKubeKinds(parser, metav1Pkg) | ||
if len(kubeKinds) == 0 { | ||
log.Fatalf("no objects in the roots") | ||
} | ||
|
||
for _, groupKind := range kubeKinds { | ||
|
||
log.Printf("generating CRD for %v\n", groupKind) | ||
|
||
parser.NeedCRDFor(groupKind, nil) | ||
crdRaw := parser.CustomResourceDefinitions[groupKind] | ||
|
||
// Inline version of "addAttribution(&crdRaw)" ... | ||
if crdRaw.ObjectMeta.Annotations == nil { | ||
crdRaw.ObjectMeta.Annotations = map[string]string{} | ||
} | ||
crdRaw.ObjectMeta.Annotations[version.BundleVersionAnnotation] = version.BundleVersion | ||
|
||
// Prevent the top level metadata for the CRD to be generated regardless of the intention in the arguments | ||
crd.FixTopLevelMetadata(crdRaw) | ||
|
||
conv, err := crd.AsVersion(*crdRaw.DeepCopy(), apiext.SchemeGroupVersion) | ||
if err != nil { | ||
log.Fatalf("failed to convert CRD: %s", err) | ||
} | ||
|
||
out, err := yaml.Marshal(conv) | ||
if err != nil { | ||
log.Fatalf("failed to marshal CRD: %s", err) | ||
} | ||
|
||
fileName := fmt.Sprintf("config/crd/bases/%s_%s.yaml", crdRaw.Spec.Group, crdRaw.Spec.Names.Plural) | ||
err = os.WriteFile(fileName, out, 0o600) | ||
zetxqx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
log.Fatalf("failed to write CRD: %s", err) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kfswain @nirrozenbaum Do you need anything beyond CRDs that could have been generated here? (I'm assuming not, certainly not a webhook, and probably not RBAC, but just want to make sure I'm not missing some EPP dependencies here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nothing that I'm aware of.
sorry for the delayed response, missed the question.