Skip to content

Commit 2d1d976

Browse files
authored
Merge pull request #106 from pmorie/category-support
Category support
2 parents 3bbb86b + 51111c0 commit 2d1d976

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

cmd/internal/codegen/parse/crd.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@ limitations under the License.
1717
package parse
1818

1919
import (
20+
"bytes"
21+
"encoding/json"
2022
"fmt"
2123
"log"
2224
"regexp"
25+
"strconv"
2326
"strings"
27+
"text/template"
2428

25-
"bytes"
26-
"encoding/json"
29+
"github.com/pkg/errors"
2730
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
2831
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2932
"k8s.io/apimachinery/pkg/util/sets"
3033
"k8s.io/gengo/types"
31-
"strconv"
32-
"text/template"
3334
)
3435

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

78+
if HasCategories(resource.Type) {
79+
categoriesTag := getCategoriesTag(resource.Type)
80+
resource.CRD.Spec.Names.Categories = strings.Split(categoriesTag, ",")
81+
}
82+
7783
if len(resource.ShortName) > 0 {
7884
resource.CRD.Spec.Names.ShortNames = []string{resource.ShortName}
7985
}
@@ -457,3 +463,13 @@ func (b *APIs) getMembers(t *types.Type, found sets.String) (map[string]v1beta1.
457463
defer found.Delete(t.Name.String())
458464
return members, result
459465
}
466+
467+
// getCategoriesTag returns the value of the +kubebuilder:categories tags
468+
func getCategoriesTag(c *types.Type) string {
469+
comments := Comments(c.CommentLines)
470+
resource := comments.getTag("kubebuilder:categories", "=")
471+
if len(resource) == 0 {
472+
panic(errors.Errorf("Must specify +kubebuilder:categories comment for type %v", c.Name))
473+
}
474+
return resource
475+
}

cmd/internal/codegen/parse/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ func HasSubresource(t *types.Type) bool {
110110
return false
111111
}
112112

113+
// HasCategories returns true if t is an APIResource annotated with
114+
// +kubebuilder:categories.
115+
func HasCategories(t *types.Type) bool {
116+
if !IsAPIResource(t) {
117+
return false
118+
}
119+
120+
for _, c := range t.CommentLines {
121+
if strings.Contains(c, "+kubebuilder:categories") {
122+
return true
123+
}
124+
}
125+
return false
126+
}
127+
113128
func IsUnversioned(t *types.Type, group string) bool {
114129
return IsApisDir(filepath.Base(filepath.Dir(t.Name.Package))) && GetGroup(t) == group
115130
}

pkg/gen/apis/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ package apis
2222
// Resource annotates a type as a resource
2323
const Resource = "// +kubebuilder:resource:path="
2424

25+
// Categories annotates a type as belonging to a comma-delimited list of
26+
// categories
27+
const Categories = "// +kubebuilder:categories="
28+
2529
// Maximum annotates a go struct field for CRD validation
2630
const Maximum = "// +kubebuilder:validation:Maximum="
2731

pkg/gen/apis/example_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func Example() {
3737
// Foo
3838
// +k8s:openapi-gen=true
3939
// +kubebuilder:resource:path=foos
40+
// +kubebuilder:categories=foo,bar,baz
4041
type Foo struct {
4142
metav1.TypeMeta `json:",inline"`
4243
metav1.ObjectMeta `json:"metadata,omitempty"`

test.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ function generate_crd_resources {
147147
kubebuilder init repo --domain sample.kubernetes.io
148148
kubebuilder create resource --group insect --version v1beta1 --kind Bee
149149

150-
header_text "generating CRD definition"
150+
header_text "editing generated files to simulate a user"
151+
sed -i pkg/apis/insect/v1beta1/bee_types.go -e "s|type Bee struct|// +kubebuilder:categories=foo,bar\ntype Bee struct|"
152+
153+
header_text "generating and testing CRD definition"
151154
kubebuilder create config --crds --output crd.yaml
152155

153156
# Test for the expected generated CRD definition
@@ -167,6 +170,9 @@ metadata:
167170
spec:
168171
group: insect.sample.kubernetes.io
169172
names:
173+
categories:
174+
- foo
175+
- bar
170176
kind: Bee
171177
plural: bees
172178
scope: Namespaced

0 commit comments

Comments
 (0)