Skip to content

Commit 90850ad

Browse files
authored
Merge pull request #1059 from johnsonj/addon-replacements
plugins/addon: perform replacements for type and controller
2 parents 4f53daf + bb4132d commit 90850ad

File tree

6 files changed

+31
-16
lines changed

6 files changed

+31
-16
lines changed

pkg/scaffold/api.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,13 @@ func (api *API) scaffoldV2() error {
228228
if api.DoController {
229229
fmt.Println(filepath.Join("controllers", fmt.Sprintf("%s_controller.go", strings.ToLower(r.Kind))))
230230

231+
scaffold := &Scaffold{
232+
Plugins: api.Plugins,
233+
}
234+
231235
ctrlScaffolder := &resourcev2.Controller{Resource: r}
232236
testsuiteScaffolder := &resourcev2.ControllerSuiteTest{Resource: r}
233-
err := (&Scaffold{}).Execute(
237+
err := scaffold.Execute(
234238
api.buildUniverse(),
235239
input.Options{},
236240
testsuiteScaffolder,

plugins/addon/channel.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ func ExampleChannel(u *model.Universe) error {
3232
m := &model.File{
3333
Path: filepath.Join("channels", "stable"),
3434
Contents: exampleChannel,
35-
IfExistsAction: input.Error,
35+
IfExistsAction: input.Skip,
3636
}
3737

38-
return AddFile(u, m)
38+
_, err := AddFile(u, m)
39+
return err
3940
}

plugins/addon/controller.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,23 @@ var controllerTemplate = `{{ .Boilerplate }}
3333
package controllers
3434
3535
import (
36-
"context"
37-
38-
ctrl "sigs.k8s.io/controller-runtime"
39-
"sigs.k8s.io/controller-runtime/pkg/client"
40-
"github.com/go-logr/logr"
36+
"sigs.k8s.io/controller-runtime/pkg/controller"
37+
"sigs.k8s.io/controller-runtime/pkg/handler"
38+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
39+
"sigs.k8s.io/controller-runtime/pkg/source"
4140
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon"
4241
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/status"
4342
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
4443
44+
"github.com/go-logr/logr"
45+
ctrl "sigs.k8s.io/controller-runtime"
46+
"sigs.k8s.io/controller-runtime/pkg/client"
47+
4548
api "{{ .Resource.GoPackage }}/{{ .Resource.Version }}"
4649
)
4750
51+
var _ reconcile.Reconciler = &{{ .Resource.Kind }}Reconciler{}
52+
4853
// {{ .Resource.Kind }}Reconciler reconciles a {{ .Resource.Kind }} object
4954
type {{ .Resource.Kind }}Reconciler struct {
5055
client.Client

plugins/addon/helpers.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@ import (
1616

1717
type PluginFunc func(u *model.Universe) error
1818

19-
// AddFile adds the specified file to the model, returning a file if the file already exists
20-
func AddFile(u *model.Universe, add *model.File) error {
19+
// AddFile adds the specified file to the model.
20+
// If the file exists the function returns false and does not modify the Universe
21+
// If the file does not exist, the function returns true and adds the file to the Universe
22+
// If there is a problem with the file the function returns an error
23+
func AddFile(u *model.Universe, add *model.File) (bool, error) {
2124
p := add.Path
2225
if p == "" {
23-
return fmt.Errorf("path must be set")
26+
return false, fmt.Errorf("path must be set")
2427
}
2528

2629
for _, f := range u.Files {
2730
if f.Path == p {
28-
return fmt.Errorf("file already exists at path %q", p)
31+
return false, nil
2932
}
3033
}
3134

3235
u.Files = append(u.Files, add)
33-
return nil
36+
return true, nil
3437
}
3538

3639
// ReplaceFileIfExists replaces the specified file in the model by path

plugins/addon/manifest.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ func ExampleManifest(u *model.Universe) error {
3535
m := &model.File{
3636
Path: filepath.Join("channels", "packages", packageName, exampleManifestVersion, "manifest.yaml"),
3737
Contents: exampleManifestContents,
38-
IfExistsAction: input.Error,
38+
IfExistsAction: input.Skip,
3939
}
4040

41-
return AddFile(u, m)
41+
_, err := AddFile(u, m)
42+
43+
return err
4244
}
4345

4446
// getPackageName returns the (default) name of the declarative package

plugins/addon/type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func ReplaceTypes(u *model.Universe) error {
1919
}
2020

2121
m := &model.File{
22-
Path: filepath.Join("controllers", strings.ToLower(u.Resource.Kind)+"_controller.go"),
22+
Path: filepath.Join("api", u.Resource.Version, strings.ToLower(u.Resource.Kind)+"_types.go"),
2323
Contents: contents,
2424
IfExistsAction: input.Error,
2525
}

0 commit comments

Comments
 (0)