-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgenerators.go
More file actions
196 lines (179 loc) · 7.21 KB
/
generators.go
File metadata and controls
196 lines (179 loc) · 7.21 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package cuekind
import (
"encoding/json"
"path/filepath"
"strings"
"github.com/grafana/codejen"
"sigs.k8s.io/yaml"
"github.com/grafana/grafana-app-sdk/codegen"
"github.com/grafana/grafana-app-sdk/codegen/jennies"
"github.com/grafana/grafana-app-sdk/codegen/templates"
"github.com/grafana/grafana-app-sdk/k8s/apiserver"
)
// CRDGenerator returns a Generator which will create a CRD file
func CRDGenerator(outputEncoder jennies.CRDOutputEncoder, outputExtension string) *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(jennies.CRDGenerator(outputEncoder, outputExtension))
return g
}
// ResourceGenerator returns a collection of jennies which generate backend resource code from kinds.
// The `versioned` parameter governs whether to generate all versions where codegen.backend == true,
// or just generate code for the current version.
// If `groupKinds` is true, kinds within the same group will exist in the same package.
// When combined with `versioned`, each version package will contain all kinds in the group
// which have a schema for that version.
func ResourceGenerator(projectRepo, generatedAPIPath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(
&jennies.GoTypes{
Depth: 1,
AddKubernetesCodegen: true,
GroupByKind: !groupKinds,
AnyAsInterface: true, // This is for compatibility with kube openAPI generator, which has issues with map[string]any
ExcludeFields: []string{"metadata"}, // We don't want an object generated for the metadata, as we use the k8s metadata objects
OpenAPINamer: func(info jennies.OpenAPINamerInfo) string {
path := filepath.Join(projectRepo, generatedAPIPath, jennies.GetGeneratedGoTypePath(!groupKinds, info.ShortGroup, info.Version, strings.ToLower(info.Kind)), info.TypeName)
return apiserver.ToOpenAPIName(path)
},
},
&jennies.ResourceObjectGenerator{
SubresourceTypesArePrefixed: groupKinds,
GroupByKind: !groupKinds,
OpenAPINamer: func(info jennies.OpenAPINamerInfo) string {
path := filepath.Join(projectRepo, generatedAPIPath, jennies.GetGeneratedGoTypePath(!groupKinds, info.ShortGroup, info.Version, strings.ToLower(info.Kind)), info.TypeName)
return apiserver.ToOpenAPIName(path)
},
},
&jennies.SchemaGenerator{
GroupByKind: !groupKinds,
},
&jennies.CodecGenerator{
GroupByKind: !groupKinds,
},
&jennies.Constants{
GroupByKind: !groupKinds,
},
)
return g
}
// BackendPluginGenerator returns a Generator which will produce boilerplate backend plugin code
func BackendPluginGenerator(projectRepo, generatedAPIPath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
pluginSecurePkgFiles, _ := templates.GetBackendPluginSecurePackageFiles()
g := codejen.JennyListWithNamer(namerFuncManifest)
g.Append(
jennies.RouterHandlerCodeGenerator(projectRepo, generatedAPIPath, !groupKinds),
jennies.StaticManyToOneGenerator[codegen.AppManifest](codejen.File{
RelativePath: "plugin/secure/data.go",
Data: pluginSecurePkgFiles["data.go"],
}),
jennies.StaticManyToOneGenerator[codegen.AppManifest](codejen.File{
RelativePath: "plugin/secure/middleware.go",
Data: pluginSecurePkgFiles["middleware.go"],
}),
jennies.StaticManyToOneGenerator[codegen.AppManifest](codejen.File{
RelativePath: "plugin/secure/retriever.go",
Data: pluginSecurePkgFiles["retriever.go"],
}),
jennies.RouterCodeGenerator(projectRepo),
jennies.BackendPluginMainGenerator(projectRepo, generatedAPIPath, !groupKinds),
)
return g
}
// TypeScriptResourceGenerator returns a Generator which generates TypeScript resource code.
func TypeScriptResourceGenerator() *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(&jennies.TypeScriptTypes{
Depth: 1,
}, &jennies.TypeScriptResourceTypes{})
return g
}
// OperatorGenerator returns a Generator which will build out watcher boilerplate for each resource,
// and a main func to run an operator for the watchers.
func OperatorGenerator(projectRepo, codegenPath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(
&jennies.OperatorKubeConfigJenny{},
jennies.OperatorMainJenny(projectRepo, codegenPath, !groupKinds),
&jennies.OperatorConfigJenny{},
)
return g
}
func AppGenerator(projectRepo, codegenPath string, manifestGoFilePath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
parts := strings.Split(projectRepo, "/")
if len(parts) == 0 {
parts = []string{""}
}
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(
jennies.WatcherJenny(projectRepo, codegenPath, !groupKinds),
&jennies.AppGenerator{
GroupByKind: !groupKinds,
ProjectRepo: projectRepo,
ProjectName: parts[len(parts)-1],
CodegenPath: codegenPath,
ManifestPackagePath: manifestGoFilePath,
},
)
return g
}
func PostResourceGenerationGenerator(projectRepo, goGenPath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(&jennies.OpenAPI{
GoModName: projectRepo,
GoGenPath: goGenPath,
GroupByKind: !groupKinds,
})
return g
}
func ManifestGenerator(extension string, includeSchemas bool, version string) *codejen.JennyList[codegen.AppManifest] {
generator := &jennies.ManifestGenerator{
Encoder: jsonEncoder,
FileExtension: extension,
IncludeSchemas: includeSchemas,
ManifestVersion: version,
}
switch extension {
case "json":
generator.Encoder = jsonEncoder
case "yaml":
generator.Encoder = yaml.Marshal
default:
// defaults to json
}
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(generator)
return g
}
func ManifestGoGenerator(pkg string, includeSchemas bool, projectRepo, goGenPath string, manifestGoFilePath string, groupKinds bool) *codejen.JennyList[codegen.AppManifest] {
g := codejen.JennyListWithNamer[codegen.AppManifest](namerFuncManifest)
g.Append(&jennies.ManifestGoGenerator{
Package: pkg,
IncludeSchemas: includeSchemas,
ProjectRepo: projectRepo,
CodegenPath: goGenPath,
GroupByKind: !groupKinds,
DestinationPath: manifestGoFilePath,
},
&jennies.CustomRouteGoTypesJenny{
AddKubernetesCodegen: true,
GroupByKind: !groupKinds,
AnyAsInterface: true, // This is for compatibility with kube openAPI generator, which has issues with map[string]any
OpenAPINamer: func(info jennies.OpenAPINamerInfo) string {
path := filepath.Join(projectRepo, goGenPath, jennies.GetGeneratedGoTypePath(!groupKinds, info.ShortGroup, info.Version, strings.ToLower(info.Kind)), info.TypeName)
return apiserver.ToOpenAPIName(path)
},
},
&jennies.ClientJenny{
GroupByKind: !groupKinds,
})
return g
}
func namerFuncManifest(m codegen.AppManifest) string {
if m == nil {
return "nil"
}
return m.Name()
}
var jsonEncoder = func(v any) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
}