55 "errors"
66 "os"
77 "path/filepath"
8+ "slices"
9+ "sort"
810 "strings"
911 "text/template"
1012)
@@ -23,6 +25,21 @@ var adapter_template string
2325//go:embed data/controller.template
2426var controller_template string
2527
28+ //go:embed data/PROJECT.template
29+ var project_template string
30+
31+ //go:embed data/kuttl-test.yaml.template
32+ var kuttl_test_template string
33+
34+ //go:embed data/config-crd-kustomization.yaml.template
35+ var crd_kustomization_template string
36+
37+ //go:embed data/config-samples-kustomization.yaml.template
38+ var samples_kustomization_template string
39+
40+ //go:embed data/internal-osclients-mock-doc.go.template
41+ var mock_doc_template string
42+
2643type specExtraValidation struct {
2744 Rule string
2845 Message string
@@ -45,22 +62,15 @@ type templateFields struct {
4562 StatusExtraType string
4663 SpecExtraValidations []specExtraValidation
4764 AdditionalPrintColumns []additionalPrintColumn
65+ // NOTE: this is temporary until we migrate the controllers to use a dedicated client
66+ // New controllers should not set this field
67+ ExistingOSClient bool
4868}
4969
50- var allResources []templateFields = []templateFields {
70+ var resources []templateFields = []templateFields {
5171 {
52- Name : "Image" ,
53- SpecExtraValidations : []specExtraValidation {
54- {
55- Rule : "!has(self.__import__) ? has(self.resource.content) : true" ,
56- Message : "resource content must be specified when not importing" ,
57- },
58- },
59- StatusExtraType : "ImageStatusExtra" ,
60- },
61- {
62- Name : "Flavor" ,
63- APIVersion : "v1alpha1" ,
72+ Name : "Flavor" ,
73+ ExistingOSClient : true ,
6474 },
6575 {
6676 Name : "FloatingIP" ,
@@ -72,17 +82,23 @@ var allResources []templateFields = []templateFields{
7282 Description : "Allocated IP address" ,
7383 },
7484 },
75- IsNotNamed : true , // FloatingIP is not named in OpenStack
76- },
77- {
78- Name : "Network" ,
79- APIVersion : "v1alpha1" ,
85+ IsNotNamed : true , // FloatingIP is not named in OpenStack
86+ ExistingOSClient : true ,
8087 },
8188 {
82- Name : "Subnet" ,
89+ Name : "Image" ,
90+ SpecExtraValidations : []specExtraValidation {
91+ {
92+ Rule : "!has(self.__import__) ? has(self.resource.content) : true" ,
93+ Message : "resource content must be specified when not importing" ,
94+ },
95+ },
96+ StatusExtraType : "ImageStatusExtra" ,
97+ ExistingOSClient : true ,
8398 },
8499 {
85- Name : "Router" ,
100+ Name : "Network" ,
101+ ExistingOSClient : true ,
86102 },
87103 {
88104 Name : "Port" ,
@@ -94,46 +110,65 @@ var allResources []templateFields = []templateFields{
94110 Description : "Allocated IP addresses" ,
95111 },
96112 },
113+ ExistingOSClient : true ,
114+ },
115+ {
116+ Name : "Project" ,
117+ ExistingOSClient : true ,
97118 },
98119 {
99- Name : "SecurityGroup" ,
120+ Name : "Router" ,
121+ ExistingOSClient : true ,
100122 },
101123 {
102- Name : "Server" ,
124+ Name : "SecurityGroup" ,
125+ ExistingOSClient : true ,
103126 },
104127 {
105- Name : "ServerGroup" ,
128+ Name : "Server" ,
129+ ExistingOSClient : true ,
106130 },
107131 {
108- Name : "Project" ,
132+ Name : "ServerGroup" ,
133+ ExistingOSClient : true ,
134+ },
135+ {
136+ Name : "Subnet" ,
137+ ExistingOSClient : true ,
138+ },
139+ }
140+
141+ // These resources won't be generated
142+ var specialResources []templateFields = []templateFields {
143+ {
144+ Name : "RouterInterface" ,
145+ ExistingOSClient : true ,
109146 },
110147}
111148
112149func main () {
113150 apiTemplate := template .Must (template .New ("api" ).Parse (api_template ))
114151 adapterTemplate := template .Must (template .New ("adapter" ).Parse (adapter_template ))
115152 controllerTemplate := template .Must (template .New ("controller" ).Parse (controller_template ))
153+ projectTemplate := template .Must (template .New ("project" ).Parse (project_template ))
154+ kuttlTestTemplate := template .Must (template .New ("kuttl-test" ).Parse (kuttl_test_template ))
155+ crdKustomizationTemplate := template .Must (template .New ("crd-kustomization" ).Parse (crd_kustomization_template ))
156+ samplesKustomizationTemplate := template .Must (
157+ template .New ("samples-kustomization" ).Parse (samples_kustomization_template ))
158+ mockDocTemplate := template .Must (template .New ("mock-doc" ).Parse (mock_doc_template ))
116159
117- for i := range allResources {
118- resource := & allResources [ i ]
160+ addDefaults ( resources )
161+ addDefaults ( specialResources )
119162
120- if resource .Year == "" {
121- resource .Year = defaultYear
122- }
163+ for i := range resources {
164+ resource := & resources [i ]
123165
124- if resource .APIVersion == "" {
125- resource .APIVersion = defaultAPIVersion
126- }
127-
128- resourceLower := strings .ToLower (resource .Name )
129- resource .NameLower = resourceLower
130-
131- apiPath := filepath .Join ("api" , resource .APIVersion , "zz_generated." + resourceLower + "-resource.go" )
166+ apiPath := filepath .Join ("api" , resource .APIVersion , "zz_generated." + resource .NameLower + "-resource.go" )
132167 if err := writeTemplate (apiPath , apiTemplate , resource ); err != nil {
133168 panic (err )
134169 }
135170
136- controllerDirPath := filepath .Join ("internal" , "controllers" , resourceLower )
171+ controllerDirPath := filepath .Join ("internal" , "controllers" , resource . NameLower )
137172 if _ , err := os .Stat (controllerDirPath ); os .IsNotExist (err ) {
138173 err = os .Mkdir (controllerDirPath , 0755 )
139174 if err != nil {
@@ -151,9 +186,59 @@ func main() {
151186 panic (err )
152187 }
153188 }
189+
190+ // NOTE: some resources needs special handling.
191+ // Let's add them now and sort the resulting slice alphabetically by resource name.
192+ allResources := slices .Concat (resources , specialResources )
193+ sort .Slice (allResources , func (i , j int ) bool {
194+ return allResources [i ].Name < allResources [j ].Name
195+ })
196+
197+ if err := writeTemplate ("PROJECT" , projectTemplate , allResources ); err != nil {
198+ panic (err )
199+ }
200+
201+ if err := writeTemplate ("kuttl-test.yaml" , kuttlTestTemplate , allResources ); err != nil {
202+ panic (err )
203+ }
204+
205+ crdKustomizationPath := filepath .Join ("config" , "crd" , "kustomization.yaml" )
206+ if err := writeTemplate (crdKustomizationPath , crdKustomizationTemplate , allResources ); err != nil {
207+ panic (err )
208+ }
209+
210+ samplesKustomizationPath := filepath .Join ("config" , "samples" , "kustomization.yaml" )
211+ if err := writeTemplate (samplesKustomizationPath , samplesKustomizationTemplate , allResources ); err != nil {
212+ panic (err )
213+ }
214+
215+ mockDocPath := filepath .Join ("internal" , "osclients" , "mock" , "doc.go" )
216+ if err := writeTemplate (mockDocPath , mockDocTemplate , allResources ); err != nil {
217+ panic (err )
218+ }
154219}
155220
156- func writeTemplate (path string , template * template.Template , resource * templateFields ) (err error ) {
221+ func addDefaults (resources []templateFields ) {
222+ for i := range resources {
223+ resource := & resources [i ]
224+
225+ if resource .Year == "" {
226+ resource .Year = defaultYear
227+ }
228+
229+ if resource .APIVersion == "" {
230+ resource .APIVersion = defaultAPIVersion
231+ }
232+
233+ resource .NameLower = strings .ToLower (resource .Name )
234+ }
235+ }
236+
237+ type ResourceType interface {
238+ * templateFields | []templateFields
239+ }
240+
241+ func writeTemplate [T ResourceType ](path string , tmpl * template.Template , resource T ) (err error ) {
157242 file , err := os .Create (path )
158243 if err != nil {
159244 return err
@@ -167,11 +252,23 @@ func writeTemplate(path string, template *template.Template, resource *templateF
167252 return err
168253 }
169254
170- return template .Execute (file , resource )
255+ return tmpl .Execute (file , resource )
171256}
172257
173258func writeAutogeneratedHeader (f * os.File ) error {
174- _ , err := f .WriteString ("// Code generated by resource-generator. DO NOT EDIT.\n " )
259+ var commentPrefix string
260+
261+ switch filepath .Ext (f .Name ()) {
262+ case ".go" :
263+ commentPrefix = "//"
264+ case ".yaml" , ".yml" :
265+ commentPrefix = "#"
266+ default :
267+ commentPrefix = "#"
268+ }
269+
270+ header := commentPrefix + " Code generated by resource-generator. DO NOT EDIT.\n "
271+ _ , err := f .WriteString (header )
175272
176273 return err
177274}
0 commit comments