Skip to content

Commit b5aab6a

Browse files
committed
crossplane: only generate hooks.go once
The crossplane code generation was generating a `zz_hooks.go` file in each resource subpackage. This was incorrect in two ways: first, the file should not have been prefixed with `zz_`. Secondly, there only needs to be a single `pkg/controller/$SERVICE/$RESOURCE/hooks.go` file generated, not one per resource. This PR simply moves the generation of the `hooks.go` template out of the loop over CRDs inside the crossplane TemplateSet constructor and ensures that a single hooks.go file is generated from the first CRD's structure. Also removes unncessary templates that crossplane doesn't use and corrects a mistake in the hooks.go.tpl for the ReadMany code path's isUpToDate() function.
1 parent 66085c9 commit b5aab6a

File tree

11 files changed

+20
-218
lines changed

11 files changed

+20
-218
lines changed

cmd/ack-generate/command/crossplane.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
9999
return err
100100
}
101101
}
102-
apiPath := filepath.Join(providerDir, "apis", optGenVersion)
103-
controllerPath := filepath.Join(providerDir, "pkg", "controller")
102+
apiPath := filepath.Join(providerDir, "apis", svcAlias, optGenVersion)
103+
controllerPath := filepath.Join(providerDir, "pkg", "controller", svcAlias)
104104
// TODO(muvaf): goimports don't allow to be included as a library. Make sure
105105
// goimports binary exists.
106106
if err := exec.Command("goimports", "-w", apiPath, controllerPath).Run(); err != nil {

pkg/generate/crossplane/crossplane.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ func Crossplane(
152152
targets := []string{
153153
"controller.go.tpl",
154154
"conversions.go.tpl",
155-
"hooks.go.tpl",
156155
}
157156
for _, crd := range crds {
158157
for _, target := range targets {
@@ -169,5 +168,21 @@ func Crossplane(
169168
}
170169
}
171170
}
171+
172+
// Finally, generate the single hooks.go file from one of the CRDs (doesn't
173+
// matter which since the hooks.go is generic for all CRs)
174+
crd := crds[0]
175+
outPath := filepath.Join(
176+
"pkg", "controller", metaVars.ServiceIDClean, crd.Names.Snake,
177+
"hooks.go",
178+
)
179+
crdVars := &templateCRDVars{
180+
metaVars,
181+
crd,
182+
}
183+
if err = ts.Add(outPath, "pkg/hooks.go.tpl", crdVars); err != nil {
184+
return nil, err
185+
}
186+
172187
return ts, nil
173188
}

templates/crossplane/boilerplate.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

templates/crossplane/boilerplate_hash.go.tpl

Lines changed: 0 additions & 15 deletions
This file was deleted.

templates/crossplane/cmd/controller/main.go.tpl

Lines changed: 0 additions & 96 deletions
This file was deleted.

templates/crossplane/config/controller/deployment.yaml.tpl

Lines changed: 0 additions & 44 deletions
This file was deleted.

templates/crossplane/config/controller/kustomization.yaml.tpl

Lines changed: 0 additions & 8 deletions
This file was deleted.

templates/crossplane/config/default/kustomization.yaml.tpl

Lines changed: 0 additions & 20 deletions
This file was deleted.

templates/crossplane/config/rbac/cluster-role-binding.yaml.tpl

Lines changed: 0 additions & 12 deletions
This file was deleted.

templates/crossplane/config/rbac/kustomization.yaml.tpl

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)