Skip to content

Commit

Permalink
Merge pull request #106 from pmorie/category-support
Browse files Browse the repository at this point in the history
Category support
  • Loading branch information
droot authored May 2, 2018
2 parents 3bbb86b + 51111c0 commit 2d1d976
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
24 changes: 20 additions & 4 deletions cmd/internal/codegen/parse/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ limitations under the License.
package parse

import (
"bytes"
"encoding/json"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"text/template"

"bytes"
"encoding/json"
"github.com/pkg/errors"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/gengo/types"
"strconv"
"text/template"
)

// parseCRDs populates the CRD field of each Group.Version.Resource,
Expand Down Expand Up @@ -74,6 +75,11 @@ func (b *APIs) parseCRDs() {
resource.CRD.Spec.Scope = "Namespaced"
}

if HasCategories(resource.Type) {
categoriesTag := getCategoriesTag(resource.Type)
resource.CRD.Spec.Names.Categories = strings.Split(categoriesTag, ",")
}

if len(resource.ShortName) > 0 {
resource.CRD.Spec.Names.ShortNames = []string{resource.ShortName}
}
Expand Down Expand Up @@ -457,3 +463,13 @@ func (b *APIs) getMembers(t *types.Type, found sets.String) (map[string]v1beta1.
defer found.Delete(t.Name.String())
return members, result
}

// getCategoriesTag returns the value of the +kubebuilder:categories tags
func getCategoriesTag(c *types.Type) string {
comments := Comments(c.CommentLines)
resource := comments.getTag("kubebuilder:categories", "=")
if len(resource) == 0 {
panic(errors.Errorf("Must specify +kubebuilder:categories comment for type %v", c.Name))
}
return resource
}
15 changes: 15 additions & 0 deletions cmd/internal/codegen/parse/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ func HasSubresource(t *types.Type) bool {
return false
}

// HasCategories returns true if t is an APIResource annotated with
// +kubebuilder:categories.
func HasCategories(t *types.Type) bool {
if !IsAPIResource(t) {
return false
}

for _, c := range t.CommentLines {
if strings.Contains(c, "+kubebuilder:categories") {
return true
}
}
return false
}

func IsUnversioned(t *types.Type, group string) bool {
return IsApisDir(filepath.Base(filepath.Dir(t.Name.Package))) && GetGroup(t) == group
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/gen/apis/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ package apis
// Resource annotates a type as a resource
const Resource = "// +kubebuilder:resource:path="

// Categories annotates a type as belonging to a comma-delimited list of
// categories
const Categories = "// +kubebuilder:categories="

// Maximum annotates a go struct field for CRD validation
const Maximum = "// +kubebuilder:validation:Maximum="

Expand Down
1 change: 1 addition & 0 deletions pkg/gen/apis/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Example() {
// Foo
// +k8s:openapi-gen=true
// +kubebuilder:resource:path=foos
// +kubebuilder:categories=foo,bar,baz
type Foo struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
8 changes: 7 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ function generate_crd_resources {
kubebuilder init repo --domain sample.kubernetes.io
kubebuilder create resource --group insect --version v1beta1 --kind Bee

header_text "generating CRD definition"
header_text "editing generated files to simulate a user"
sed -i pkg/apis/insect/v1beta1/bee_types.go -e "s|type Bee struct|// +kubebuilder:categories=foo,bar\ntype Bee struct|"

header_text "generating and testing CRD definition"
kubebuilder create config --crds --output crd.yaml

# Test for the expected generated CRD definition
Expand All @@ -167,6 +170,9 @@ metadata:
spec:
group: insect.sample.kubernetes.io
names:
categories:
- foo
- bar
kind: Bee
plural: bees
scope: Namespaced
Expand Down

0 comments on commit 2d1d976

Please sign in to comment.