Skip to content

Commit d556e7d

Browse files
authored
Merge pull request #338 from droot/feature/add-attribution
✨ add controller-gen attribution info to CRD
2 parents 674e40c + 07f8086 commit d556e7d

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

pkg/crd/gen.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"sigs.k8s.io/controller-tools/pkg/genall"
2828
"sigs.k8s.io/controller-tools/pkg/loader"
2929
"sigs.k8s.io/controller-tools/pkg/markers"
30+
"sigs.k8s.io/controller-tools/pkg/version"
3031
)
3132

3233
// +controllertools:marker:generateHelp
@@ -81,6 +82,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
8182
if g.TrivialVersions {
8283
toTrivialVersions(&crd)
8384
}
85+
addAttribution(&crd)
8486
fileName := fmt.Sprintf("%s_%s.yaml", crd.Spec.Group, crd.Spec.Names.Plural)
8587
if err := ctx.WriteYAML(fileName, crd); err != nil {
8688
return err
@@ -116,6 +118,15 @@ func toTrivialVersions(crd *apiext.CustomResourceDefinition) {
116118
crd.Spec.AdditionalPrinterColumns = canonicalColumns
117119
}
118120

121+
// addAttribution adds attribution info to indicate controller-gen tool was used
122+
// to generate this CRD definition along with the version info.
123+
func addAttribution(crd *apiext.CustomResourceDefinition) {
124+
if crd.ObjectMeta.Annotations == nil {
125+
crd.ObjectMeta.Annotations = map[string]string{}
126+
}
127+
crd.ObjectMeta.Annotations["controller-gen.kubebuilder.io/version"] = version.Version()
128+
}
129+
119130
// FindMetav1 locates the actual package representing metav1 amongst
120131
// the imports of the roots.
121132
func FindMetav1(roots []*loader.Package) *loader.Package {

pkg/version/version.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1+
/*
2+
Copyright 2019 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+
*/
116
package version
217

318
import (
419
"fmt"
520
"runtime/debug"
621
)
722

8-
// version returns the version of the main module
9-
func version() string {
23+
// Version returns the version of the main module
24+
func Version() string {
1025
info, ok := debug.ReadBuildInfo()
1126
if !ok {
1227
// binary has not been built with module support
@@ -30,5 +45,5 @@ func version() string {
3045
//
3146
// - "Version: (unknown)" when not using go modules.
3247
func Print() {
33-
fmt.Printf("Version: %s\n", version())
48+
fmt.Printf("Version: %s\n", Version())
3449
}

0 commit comments

Comments
 (0)