Skip to content

Commit 8fdcb4d

Browse files
committed
Improve Config API reference generator for resource types
The `types.go` file for some APIs may not have the anticipated `// +k8s:deepcopy-gen:...` line for any types. We have to allow the generator to identify the "exposed" resource types. For example, the `Summary` type from the kubernetes stats API does not have this comment line.
1 parent f59e9cf commit 8fdcb4d

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

genref/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ apis:
7474
title: Kubelet Stats (v1alpha1)
7575
package: k8s.io/kubelet
7676
path: pkg/apis/stats/v1alpha1
77+
resources:
78+
- Summary
7779
# skip: true
7880

7981
- name: kubelet-credentialprovider

genref/main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ type apiDefinition struct {
100100
// MainPackage is an override for API definitions that involves more
101101
// than one package.
102102
MainPackage string `json:"mainPackage"`
103+
104+
// Resource types manually specified
105+
Resources []string `json:"resources"`
103106
}
104107

105108
// Global vars
@@ -138,7 +141,7 @@ func init() {
138141
}
139142

140143
// processAPIPath processes a path for package enumeration and processing.
141-
func processAPIPath(path string, includes []string, title string, mainPkg string) ([]*apiPackage, error) {
144+
func processAPIPath(path string, includes []string, title string, mainPkg string, resources []string) ([]*apiPackage, error) {
142145
klog.V(0).Infof("Parsing go packages in %s", path)
143146
gopkgs, err := parseAPIPackages(path)
144147
if err != nil {
@@ -156,7 +159,7 @@ func processAPIPath(path string, includes []string, title string, mainPkg string
156159
gopkgs = append(gopkgs, extra...)
157160
}
158161

159-
pkgs, err := combineAPIPackages(gopkgs, title, mainPkg)
162+
pkgs, err := combineAPIPackages(gopkgs, title, mainPkg, resources)
160163
if err != nil {
161164
return nil, err
162165
}
@@ -217,7 +220,7 @@ func parseAPIPackages(dir string) ([]*types.Package, error) {
217220

218221
// combineAPIPackages groups the Go packages by the <apiGroup+apiVersion> they
219222
// offer, and combines the types in them.
220-
func combineAPIPackages(pkgs []*types.Package, title string, mainPkg string) ([]*apiPackage, error) {
223+
func combineAPIPackages(pkgs []*types.Package, title string, mainPkg string, resources []string) ([]*apiPackage, error) {
221224
pkgMap := make(map[string]*apiPackage)
222225
re := `^v\d+((alpha|beta)\d+)?$`
223226

@@ -242,13 +245,15 @@ func combineAPIPackages(pkgs []*types.Package, title string, mainPkg string) ([]
242245
if len(mainPkg) > 0 && group != mainPkg {
243246
isMain = false
244247
}
248+
245249
pkgMap[id] = &apiPackage{
246250
apiGroup: group,
247251
apiVersion: version,
248252
Types: typeList,
249253
GoPackages: []*types.Package{gopkg},
250254
Title: title,
251255
IsMain: isMain,
256+
Resources: resources,
252257
}
253258
} else {
254259
v.Types = append(v.Types, typeList...)
@@ -359,7 +364,7 @@ func main() {
359364
if len(pkgInclude) > 0 && !containsString(pkgInclude, item.Name) {
360365
continue
361366
}
362-
pkgs, err := processAPIPath(apiDir, item.Includes, item.Title, item.MainPackage)
367+
pkgs, err := processAPIPath(apiDir, item.Includes, item.Title, item.MainPackage, item.Resources)
363368
if err != nil {
364369
klog.ErrorS(err, "cannot process API path")
365370
continue

genref/types.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type apiPackage struct {
3636

3737
// IsMain is set if the package is the main one
3838
IsMain bool
39+
40+
// Resources is the customized resource type names
41+
Resources []string
3942
}
4043

4144
// DisplayName returns the full name of the API package
@@ -190,11 +193,18 @@ func (t *apiType) IsExported() bool {
190193
if strings.Contains(comments, "+genclient") {
191194
return true
192195
}
193-
194196
if strings.Contains(comments, "+k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object") {
195197
return true
196198
}
197199

200+
t1 := t.deref()
201+
202+
p := typePkgMap[t1.String()]
203+
for _, res := range p.Resources {
204+
if res == t.Name.Name {
205+
return true
206+
}
207+
}
198208
return false
199209
}
200210

0 commit comments

Comments
 (0)