Skip to content

Commit e81705f

Browse files
committed
Rename application package to builder
1 parent a06b1dd commit e81705f

File tree

6 files changed

+90
-78
lines changed

6 files changed

+90
-78
lines changed

pkg/patterns/application/application.go renamed to pkg/builder/build.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package application
17+
package builder
1818

1919
import (
2020
"fmt"
@@ -52,13 +52,18 @@ type Builder struct {
5252
predicates []predicate.Predicate
5353
managedObjects []runtime.Object
5454
config *rest.Config
55-
reconcile reconcile.Reconciler
5655
ctrl controller.Controller
5756
}
5857

59-
// BuilderFor returns a new Builder for and BuilderFor
60-
func BuilderFor(apiType runtime.Object) *Builder {
61-
return &Builder{apiType: apiType}
58+
// SimpleController returns a new Builder
59+
func SimpleController() *Builder {
60+
return &Builder{}
61+
}
62+
63+
// ForType sets the ForType that generates other types
64+
func (b *Builder) ForType(apiType runtime.Object) *Builder {
65+
b.apiType = apiType
66+
return b
6267
}
6368

6469
// Owns configures the Application Controller to respond to create / delete / update events for objects it managedObjects
@@ -88,16 +93,9 @@ func (b *Builder) WithEventFilter(p predicate.Predicate) *Builder {
8893
return b
8994
}
9095

91-
// WithReconciler sets the Reconcile called in response to create / update / delete events for the
92-
// BuilderFor type or Created object types.
93-
func (b *Builder) WithReconciler(r reconcile.Reconciler) *Builder {
94-
b.reconcile = r
95-
return b
96-
}
97-
9896
// Build builds the Application Controller and returns the Manager used to start it.
99-
func (b *Builder) Build() (manager.Manager, error) {
100-
if b.reconcile == nil {
97+
func (b *Builder) Build(r reconcile.Reconciler) (manager.Manager, error) {
98+
if r == nil {
10199
return nil, fmt.Errorf("must call WithReconciler to set Reconciler")
102100
}
103101

@@ -112,7 +110,7 @@ func (b *Builder) Build() (manager.Manager, error) {
112110
}
113111

114112
// Set the Controller
115-
if err := b.doController(); err != nil {
113+
if err := b.doController(r); err != nil {
116114
return nil, err
117115
}
118116

@@ -168,11 +166,11 @@ func (b *Builder) getControllerName() (string, error) {
168166
return name, nil
169167
}
170168

171-
func (b *Builder) doController() error {
169+
func (b *Builder) doController(r reconcile.Reconciler) error {
172170
name, err := b.getControllerName()
173171
if err != nil {
174172
return err
175173
}
176-
b.ctrl, err = newController(name, b.mgr, controller.Options{Reconciler: b.reconcile})
174+
b.ctrl, err = newController(name, b.mgr, controller.Options{Reconciler: r})
177175
return err
178176
}

pkg/patterns/application/application_test.go renamed to pkg/builder/build_test.go

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package application
1+
package builder
22

33
import (
44
"context"
@@ -38,36 +38,51 @@ var _ = Describe("application", func() {
3838

3939
Describe("New", func() {
4040
It("should return success if given valid objects", func() {
41-
instance, err := BuilderFor(&appsv1.ReplicaSet{}).Owns(&appsv1.ReplicaSet{}).WithReconciler(noop).Build()
41+
instance, err := SimpleController().
42+
ForType(&appsv1.ReplicaSet{}).
43+
Owns(&appsv1.ReplicaSet{}).
44+
Build(noop)
4245
Expect(err).NotTo(HaveOccurred())
4346
Expect(instance).NotTo(BeNil())
4447
})
4548

4649
It("should return an error if the Config is invalid", func() {
4750
getConfig = func() (*rest.Config, error) { return cfg, fmt.Errorf("expected error") }
48-
instance, err := BuilderFor(&appsv1.ReplicaSet{}).Owns(&appsv1.ReplicaSet{}).WithReconciler(noop).Build()
51+
instance, err := SimpleController().
52+
ForType(&appsv1.ReplicaSet{}).
53+
Owns(&appsv1.ReplicaSet{}).
54+
Build(noop)
4955
Expect(err).To(HaveOccurred())
5056
Expect(err.Error()).To(ContainSubstring("expected error"))
5157
Expect(instance).To(BeNil())
5258
})
5359

5460
It("should return an error if there is no GVK for an object", func() {
55-
instance, err := BuilderFor(fakeType("")).Owns(&appsv1.ReplicaSet{}).WithReconciler(noop).Build()
61+
instance, err := SimpleController().
62+
ForType(&fakeType{}).
63+
Owns(&appsv1.ReplicaSet{}).
64+
Build(noop)
5665
Expect(err).To(HaveOccurred())
57-
Expect(err.Error()).To(ContainSubstring("expected pointer, but got application.fakeType"))
66+
Expect(err.Error()).To(ContainSubstring("no kind is registered for the type builder.fakeType"))
5867
Expect(instance).To(BeNil())
5968

60-
instance, err = BuilderFor(&appsv1.ReplicaSet{}).Owns(fakeType("")).WithReconciler(noop).Build()
69+
instance, err = SimpleController().
70+
ForType(&appsv1.ReplicaSet{}).
71+
Owns(&fakeType{}).
72+
Build(noop)
6173
Expect(err).To(HaveOccurred())
62-
Expect(err.Error()).To(ContainSubstring("expected pointer, but got application.fakeType"))
74+
Expect(err.Error()).To(ContainSubstring("no kind is registered for the type builder.fakeType"))
6375
Expect(instance).To(BeNil())
6476
})
6577

6678
It("should return an error if it cannot create the manager", func() {
6779
newManager = func(config *rest.Config, options manager.Options) (manager.Manager, error) {
6880
return nil, fmt.Errorf("expected error")
6981
}
70-
instance, err := BuilderFor(&appsv1.ReplicaSet{}).Owns(&appsv1.ReplicaSet{}).WithReconciler(noop).Build()
82+
instance, err := SimpleController().
83+
ForType(&appsv1.ReplicaSet{}).
84+
Owns(&appsv1.ReplicaSet{}).
85+
Build(noop)
7186
Expect(err).To(HaveOccurred())
7287
Expect(err.Error()).To(ContainSubstring("expected error"))
7388
Expect(instance).To(BeNil())
@@ -78,34 +93,14 @@ var _ = Describe("application", func() {
7893
controller.Controller, error) {
7994
return nil, fmt.Errorf("expected error")
8095
}
81-
instance, err := BuilderFor(&appsv1.ReplicaSet{}).Owns(&appsv1.ReplicaSet{}).WithReconciler(noop).Build()
96+
instance, err := SimpleController().
97+
ForType(&appsv1.ReplicaSet{}).
98+
Owns(&appsv1.ReplicaSet{}).
99+
Build(noop)
82100
Expect(err).To(HaveOccurred())
83101
Expect(err.Error()).To(ContainSubstring("expected error"))
84102
Expect(instance).To(BeNil())
85103
})
86-
87-
It("should return an error if there is no Reconciler", func() {
88-
// Prevent getGVK from failing so watch fails
89-
getGvk = func(obj runtime.Object, scheme *runtime.Scheme) (schema.GroupVersionKind, error) {
90-
return schema.GroupVersionKind{Kind: "foo"}, nil
91-
}
92-
instance, err := BuilderFor(&appsv1.ReplicaSet{}).Owns(&appsv1.ReplicaSet{}).Build()
93-
Expect(err).To(HaveOccurred())
94-
Expect(err.Error()).To(ContainSubstring("must call WithReconciler to set Reconciler"))
95-
Expect(instance).To(BeNil())
96-
})
97-
98-
It("should use the provide config", func() {
99-
100-
})
101-
102-
It("should use the provide manager", func() {
103-
104-
})
105-
106-
It("should use the provide filters", func() {
107-
108-
})
109104
})
110105

111106
Describe("Start", func() {
@@ -118,11 +113,10 @@ var _ = Describe("application", func() {
118113
return reconcile.Result{}, nil
119114
})
120115

121-
instance, err := BuilderFor(&appsv1.Deployment{}).
116+
instance, err := SimpleController().ForType(&appsv1.Deployment{}).
122117
WithConfig(cfg).
123118
Owns(&appsv1.ReplicaSet{}).
124-
WithReconciler(fn).
125-
Build()
119+
Build(fn)
126120
Expect(err).NotTo(HaveOccurred())
127121

128122
By("Starting the application")
@@ -198,9 +192,9 @@ var _ = Describe("application", func() {
198192
})
199193
})
200194

201-
var _ runtime.Object = fakeType("")
195+
var _ runtime.Object = &fakeType{}
202196

203-
type fakeType string
197+
type fakeType struct{}
204198

205-
func (fakeType) GetObjectKind() schema.ObjectKind { return nil }
206-
func (fakeType) DeepCopyObject() runtime.Object { return nil }
199+
func (*fakeType) GetObjectKind() schema.ObjectKind { return nil }
200+
func (*fakeType) DeepCopyObject() runtime.Object { return nil }

pkg/patterns/application/application_suite_test.go renamed to pkg/builder/builder_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package application
1+
package builder
22

33
import (
44
"testing"

pkg/builder/doc.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package builder provides wraps other controller-runtime libraries and exposes simple
18+
// patterns for building common Controllers.
19+
//
20+
// Projects built with the builder package can trivially be rebased on top of the underlying
21+
// packages if the project requires more customized behavior in the future.
22+
package builder

pkg/patterns/application/example_test.go renamed to pkg/builder/example_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package application_test
17+
package builder_test
1818

1919
import (
2020
"context"
@@ -23,33 +23,32 @@ import (
2323

2424
appsv1 "k8s.io/api/apps/v1"
2525
corev1 "k8s.io/api/core/v1"
26+
"sigs.k8s.io/controller-runtime/pkg/builder"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
27-
"sigs.k8s.io/controller-runtime/pkg/patterns/application"
2828
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2929
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
3030
)
3131

3232
// This example creates a simple application Controller that is configured for ReplicaSets and Pods.
3333
//
3434
// * Create a new application for ReplicaSets that manages Pods owned by the ReplicaSet and calls into
35-
// ReplicaSetController.
35+
// ReplicaSetReconciler.
3636
//
3737
// * Start the application.
3838
func ExampleBuilder() {
39-
a, err := application.
40-
BuilderFor(&appsv1.ReplicaSet{}). // ReplicaSet is the Application API
41-
Owns(&corev1.Pod{}). // ReplicaSet owns Pods created by it
42-
WithReconciler(&ReplicaSetController{}). // ReplicaSet and Pod events (create/update/del) trigger the handler
43-
Build() // Build
39+
rs, err := builder.SimpleController().
40+
ForType(&appsv1.ReplicaSet{}). // ReplicaSet is the Application API
41+
Owns(&corev1.Pod{}). // ReplicaSet owns Pods created by it
42+
Build(&ReplicaSetReconciler{}) // Build
4443
if err != nil {
4544
log.Fatal(err)
4645
}
4746

48-
log.Fatal(a.Start(signals.SetupSignalHandler()))
47+
log.Fatal(rs.Start(signals.SetupSignalHandler()))
4948
}
5049

51-
// ReplicaSetController is a simple Controller example implementation.
52-
type ReplicaSetController struct {
50+
// ReplicaSetReconciler is a simple Controller example implementation.
51+
type ReplicaSetReconciler struct {
5352
client.Client
5453
}
5554

@@ -60,7 +59,7 @@ type ReplicaSetController struct {
6059
// * Read the ReplicaSet
6160
// * Read the Pods
6261
// * Set a Label on the ReplicaSet with the Pod count
63-
func (a *ReplicaSetController) Reconcile(req reconcile.Request) (reconcile.Result, error) {
62+
func (a *ReplicaSetReconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {
6463
// Read the ReplicaSet
6564
rs := &appsv1.ReplicaSet{}
6665
err := a.Get(context.TODO(), req.NamespacedName, rs)
@@ -85,7 +84,7 @@ func (a *ReplicaSetController) Reconcile(req reconcile.Request) (reconcile.Resul
8584
return reconcile.Result{}, nil
8685
}
8786

88-
func (a *ReplicaSetController) InjectClient(c client.Client) error {
87+
func (a *ReplicaSetReconciler) InjectClient(c client.Client) error {
8988
a.Client = c
9089
return nil
9190
}

pkg/patterns/application/doc.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Package application provides high-level porcelain wrapping the controller and manager libraries for
18-
// building Kubernetes APIs for simple applications (operators).
17+
// Package application documents patterns for building Controllers to manage specific applications.
1918
//
20-
// Note: Application is an alpha library and make have backwards compatibility breaking changes.
2119
//
22-
// Projects built with an application can trivially be rebased on top of the underlying Controller and Manager
23-
// packages if the project requires more customized behavior in the future.
20+
// An application is a Controller and Resource that together implement the operational logic for an application.
21+
// They are often used to take off-the-shelf OSS applications, and make them Kubernetes native.
2422
//
25-
// Application
23+
// A typical application Controller may use a new builder.SimpleController() to create a Controller
24+
// for a single API type that manages other objects it creates.
2625
//
27-
// An application is a Controller that implements the operational logic for an application. It is often used
28-
// to take off-the-shelf OSS applications, and make them Kubernetes native.
26+
// Application Controllers are most useful for stateful applications such as Cassandra, Etcd and MySQL
27+
// which contain operation logic for sharding, backup and restore, upgrade / downgrade, etc.
2928
package application

0 commit comments

Comments
 (0)