-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathoptions.go
143 lines (121 loc) · 4.02 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2022 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 golang
import (
"path"
"sigs.k8s.io/kubebuilder/v4/pkg/config"
"sigs.k8s.io/kubebuilder/v4/pkg/model/resource"
)
var (
coreGroups = map[string]string{
"admission": "k8s.io",
"admissionregistration": "k8s.io",
"apps": "",
"auditregistration": "k8s.io",
"apiextensions": "k8s.io",
"authentication": "k8s.io",
"authorization": "k8s.io",
"autoscaling": "",
"batch": "",
"certificates": "k8s.io",
"coordination": "k8s.io",
"core": "",
"events": "k8s.io",
"extensions": "",
"imagepolicy": "k8s.io",
"networking": "k8s.io",
"node": "k8s.io",
"metrics": "k8s.io",
"policy": "",
"rbac.authorization": "k8s.io",
"scheduling": "k8s.io",
"setting": "k8s.io",
"storage": "k8s.io",
}
)
// Options contains the information required to build a new resource.Resource.
type Options struct {
// Plural is the resource's kind plural form.
Plural string
// ExternalAPIPath allows to inform a path for APIs not defined in the project
ExternalAPIPath string
// ExternalAPIPath allows to inform the resource domain to build the Qualified Group
// to generate the RBAC markers
ExternalAPIDomain string
// Namespaced is true if the resource should be namespaced.
Namespaced bool
// Flags that define which parts should be scaffolded
DoAPI bool
DoController bool
DoDefaulting bool
DoValidation bool
DoConversion bool
// Spoke versions for conversion webhook
Spoke []string
}
// UpdateResource updates the provided resource with the options
func (opts Options) UpdateResource(res *resource.Resource, c config.Config) {
if opts.Plural != "" {
res.Plural = opts.Plural
}
if opts.DoAPI {
res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
res.API = &resource.API{
CRDVersion: "v1",
Namespaced: opts.Namespaced,
}
}
if opts.DoController {
res.Controller = true
}
if opts.DoDefaulting || opts.DoValidation || opts.DoConversion {
res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
res.Webhooks.WebhookVersion = "v1"
if opts.DoDefaulting {
res.Webhooks.Defaulting = true
}
if opts.DoValidation {
res.Webhooks.Validation = true
}
if opts.DoConversion {
res.Webhooks.Conversion = true
res.Webhooks.Spoke = opts.Spoke
}
}
if len(opts.ExternalAPIPath) > 0 {
res.External = true
}
// domain and path may need to be changed in case we are referring to a builtin core resource:
// - Check if we are scaffolding the resource now => project resource
// - Check if we already scaffolded the resource => project resource
// - Check if the resource group is a well-known core group => builtin core resource
// - In any other case, default to => project resource
if !opts.DoAPI {
var alreadyHasAPI bool
loadedRes, err := c.GetResource(res.GVK)
alreadyHasAPI = err == nil && loadedRes.HasAPI()
if !alreadyHasAPI {
if res.External {
res.Path = opts.ExternalAPIPath
res.Domain = opts.ExternalAPIDomain
} else {
// Handle core types
if domain, found := coreGroups[res.Group]; found {
res.Core = true
res.Domain = domain
res.Path = path.Join("k8s.io", "api", res.Group, res.Version)
}
}
}
}
}