Skip to content

Commit

Permalink
Merge pull request #338 from droot/feature/add-attribution
Browse files Browse the repository at this point in the history
✨ add controller-gen attribution info to CRD
  • Loading branch information
k8s-ci-robot authored Oct 21, 2019
2 parents 674e40c + 07f8086 commit d556e7d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 11 additions & 0 deletions pkg/crd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sigs.k8s.io/controller-tools/pkg/genall"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
"sigs.k8s.io/controller-tools/pkg/version"
)

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

// addAttribution adds attribution info to indicate controller-gen tool was used
// to generate this CRD definition along with the version info.
func addAttribution(crd *apiext.CustomResourceDefinition) {
if crd.ObjectMeta.Annotations == nil {
crd.ObjectMeta.Annotations = map[string]string{}
}
crd.ObjectMeta.Annotations["controller-gen.kubebuilder.io/version"] = version.Version()
}

// FindMetav1 locates the actual package representing metav1 amongst
// the imports of the roots.
func FindMetav1(roots []*loader.Package) *loader.Package {
Expand Down
21 changes: 18 additions & 3 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
/*
Copyright 2019 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 version

import (
"fmt"
"runtime/debug"
)

// version returns the version of the main module
func version() string {
// Version returns the version of the main module
func Version() string {
info, ok := debug.ReadBuildInfo()
if !ok {
// binary has not been built with module support
Expand All @@ -30,5 +45,5 @@ func version() string {
//
// - "Version: (unknown)" when not using go modules.
func Print() {
fmt.Printf("Version: %s\n", version())
fmt.Printf("Version: %s\n", Version())
}

0 comments on commit d556e7d

Please sign in to comment.