Skip to content

Commit 2493093

Browse files
committed
refactoring
1 parent 5eb5c48 commit 2493093

File tree

6 files changed

+215
-158
lines changed

6 files changed

+215
-158
lines changed

pkg/component/component.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
// Instantiate given Component type T; panics unless T is a pointer type.
18-
func newComponent[T Component]() Component {
18+
func newComponent[T Component]() T {
1919
var component T
2020
v := reflect.ValueOf(&component).Elem()
2121
v.Set(reflect.New(v.Type().Elem()))

pkg/component/reconcile.go

Lines changed: 202 additions & 139 deletions
Large diffs are not rendered by default.

pkg/component/types.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import (
1313
)
1414

1515
// Component is the central interface that component operators have to implement.
16-
// Besides being a conroller-runtime client.Object, the implementing type has to include
17-
// the Spec and Status structs defined in this package, and has to define according accessor methods,
18-
// called GetComponentSpec() and GetComponentStatus(). In addition it has to expose its whole spec and status
19-
// as Unstructurable objects, via methods GetSpec() and GetStatus().
16+
// Besides being a conroller-runtime client.Object, the implementing type has to expose accessor
17+
// methods for the deployment's target namespace and name, and for the components's spec and status.
18+
// via methods GetSpec() and GetStatus().
2019
type Component interface {
2120
client.Object
2221
// Return target namespace for the component deployment.
@@ -26,10 +25,10 @@ type Component interface {
2625
// Return target name for the component deployment.
2726
// This is the value that will be passed to Generator.Generator() as name.
2827
GetDeploymentName() string
29-
// Return a pointer accessor to the component's spec.
28+
// Return a read-only accessor to the component's spec.
3029
// Which, as a consequence, obviously has to implement the types.Unstructurable interface.
3130
GetSpec() types.Unstructurable
32-
// Return a pointer accessor to the component's status,
31+
// Return a read-write (usually a pointer) accessor to the component's status,
3332
// resp. to the corresponding sub-struct if the status extends component.Status.
3433
GetStatus() *Status
3534
}

pkg/operator/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ type Operator interface {
2020
InitFlags(flagset *flag.FlagSet)
2121
ValidateFlags() error
2222
GetUncacheableTypes() []client.Object
23+
// Deprecation warning: the parameter discoveryClient will be removed;
24+
// implementations should not use it, and build a discoveryClient from mgr.GetConfig() and mgr.GetHTTPClient() instead.
2325
Setup(mgr ctrl.Manager, discoveryClient discovery.DiscoveryInterface) error
2426
}

scaffold/templates/main.go.tpl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3737
"k8s.io/apimachinery/pkg/runtime"
3838
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
39-
"k8s.io/client-go/discovery"
4039
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
4140
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
4241
ctrl "sigs.k8s.io/controller-runtime"
@@ -170,13 +169,7 @@ func main() {
170169
// connect to the conversion endpoint.
171170
// mgr.GetWebhookServer().Register("/convert", conversion.NewWebhookHandler(mgr.GetScheme()))
172171

173-
discoveryClient, err := discovery.NewDiscoveryClientForConfig(mgr.GetConfig())
174-
if err != nil {
175-
setupLog.Error(err, "error creating discovery client")
176-
os.Exit(1)
177-
}
178-
179-
if err := operator.Setup(mgr, discoveryClient); err != nil {
172+
if err := operator.Setup(mgr, nil); err != nil {
180173
setupLog.Error(err, "error registering controller with manager")
181174
os.Exit(1)
182175
}

scaffold/templates/pkg/operator/operator.go.tpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ func (o *Operator) Setup(mgr ctrl.Manager, discoveryClient discovery.DiscoveryIn
118118

119119
if err := component.NewReconciler[*operator{{ .groupVersion }}.{{ .kind }}](
120120
o.options.Name,
121-
mgr.GetClient(),
122-
discoveryClient,
123-
mgr.GetEventRecorderFor(o.options.Name),
124-
mgr.GetScheme(),
121+
nil,
122+
nil,
123+
nil,
124+
nil,
125125
resourceGenerator,
126126
).SetupWithManager(mgr); err != nil {
127127
return errors.Wrapf(err, "unable to create controller")

0 commit comments

Comments
 (0)