Skip to content

Commit 3ca4ff8

Browse files
authored
[codegen] Fix package import path in generated manifest go file (#828)
Fix discrepency where the package imported by the manifest go file for a group is based on the manifest's full group's first component, but the package path determined by the kind codegen uses the manifest short group from the CUE (which is a variant of the appName). Use the kind codegen's package name in the manifest go file. Prior to this, the group used in the manifest go import statement was just the first dot-separated component of the full manifest group, which was fine in most cases where the full group was auto-created from the "short" group which is just `LOWER(REPLACE(appName, "-", ""))`, and the full group was just that short group + `ext.grafana.(com|app)`. However, if `groupOverride` is used and the full group is set to something which does not begin with the short group (appName) (for example, and appName `foo` and the `groupOverride` being `bar.foo.ext.grafana.com`), the generated path use in the manifest go file would be incorrect (`bar` instead of `foo` in our example). This is noted [here](grafana/grafana#106111 (comment)) as an issue when generating code for grafana's `apps/alerting/notifications`, as the app is `alerting`, but the fullgroup is `notifications.alerting.grafana.app`.
1 parent 38fcbfc commit 3ca4ff8

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

codegen/jennies/manifest.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,12 @@ func (g *ManifestGoGenerator) Generate(appManifest codegen.AppManifest) (codejen
104104

105105
buf := bytes.Buffer{}
106106
err = templates.WriteManifestGoFile(templates.ManifestGoFileMetadata{
107-
Package: g.Package,
108-
Repo: g.ProjectRepo,
109-
CodegenPath: g.CodegenPath,
110-
KindsAreGrouped: !g.GroupByKind,
111-
ManifestData: *manifestData,
107+
Package: g.Package,
108+
Repo: g.ProjectRepo,
109+
CodegenPath: g.CodegenPath,
110+
KindsAreGrouped: !g.GroupByKind,
111+
ManifestData: *manifestData,
112+
CodegenManifestGroup: appManifest.Properties().Group,
112113
}, &buf)
113114
if err != nil {
114115
return nil, err

codegen/templates/templates.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,12 @@ func WriteOperatorConfig(out io.Writer) error {
390390
}
391391

392392
type ManifestGoFileMetadata struct {
393-
Package string
394-
Repo string
395-
CodegenPath string
396-
KindsAreGrouped bool
397-
ManifestData app.ManifestData
393+
Package string
394+
Repo string
395+
CodegenPath string
396+
KindsAreGrouped bool
397+
ManifestData app.ManifestData
398+
CodegenManifestGroup string
398399
}
399400

400401
func (ManifestGoFileMetadata) ToAdmissionOperationName(input app.AdmissionOperation) string {
@@ -449,7 +450,7 @@ func (m ManifestGoFileMetadata) Packages() []string {
449450
gvs := make(map[string]string)
450451
for _, k := range m.ManifestData.Kinds {
451452
for _, v := range k.Versions {
452-
gvs[fmt.Sprintf("%s/%s", m.GroupToPackageName(m.ManifestData.Group), ToPackageName(v.Name))] = ToPackageName(v.Name)
453+
gvs[fmt.Sprintf("%s/%s", m.GroupToPackageName(m.CodegenManifestGroup), ToPackageName(v.Name))] = ToPackageName(v.Name)
453454
}
454455
}
455456
for pkg, alias := range gvs {

0 commit comments

Comments
 (0)