Skip to content

Commit 6edde34

Browse files
authored
carve out dependent object reconciler (#87)
1 parent afa6dff commit 6edde34

File tree

21 files changed

+1887
-1699
lines changed

21 files changed

+1887
-1699
lines changed

internal/cluster/types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"sigs.k8s.io/controller-runtime/pkg/client"
1212
)
1313

14-
// TODO: the Client interface should be public, because it is returned by component.GetClientFromContext()
1514
// TODO: should we add a Config() method, i.e. expose the rest.Config used by the client?
1615

1716
// The Client interface extends the controller-runtime client by discovery and event recording capabilities.

pkg/cluster/doc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and component-operator-runtime contributors
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/*
7+
Package cluster contains client logic to interact with Kubernetes clusters.
8+
*/
9+
package cluster

pkg/cluster/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and component-operator-runtime contributors
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package cluster
7+
8+
import "github.com/sap/component-operator-runtime/internal/cluster"
9+
10+
type Client cluster.Client

pkg/component/component.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ import (
1212
"time"
1313

1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15-
"k8s.io/apimachinery/pkg/runtime/schema"
1615

1716
"github.com/sap/component-operator-runtime/internal/walk"
18-
"github.com/sap/component-operator-runtime/pkg/types"
1917
)
2018

2119
// Instantiate given Component type T; panics unless T is a pointer type.
@@ -225,39 +223,3 @@ func (s *Status) SetState(state State, reason string, message string) {
225223
cond.Message = message
226224
s.State = state
227225
}
228-
229-
// Get inventory item's ObjectKind accessor.
230-
func (i *InventoryItem) GetObjectKind() schema.ObjectKind {
231-
return i
232-
}
233-
234-
// Get inventory item's GroupVersionKind.
235-
func (i InventoryItem) GroupVersionKind() schema.GroupVersionKind {
236-
return schema.GroupVersionKind(i.TypeInfo)
237-
}
238-
239-
// Set inventory item's GroupVersionKind.
240-
func (i *InventoryItem) SetGroupVersionKind(gvk schema.GroupVersionKind) {
241-
i.TypeInfo = TypeInfo(gvk)
242-
}
243-
244-
// Get inventory item's namespace.
245-
func (i InventoryItem) GetNamespace() string {
246-
return i.Namespace
247-
}
248-
249-
// Get inventory item's name.
250-
func (i InventoryItem) GetName() string {
251-
return i.Name
252-
}
253-
254-
// Check whether inventory item matches given ObjectKey, in the sense that Group, Kind, Namespace and Name are the same.
255-
// Note that this does not compare the group's Version.
256-
func (i InventoryItem) Matches(key types.ObjectKey) bool {
257-
return i.GroupVersionKind().GroupKind() == key.GetObjectKind().GroupVersionKind().GroupKind() && i.Namespace == key.GetNamespace() && i.Name == key.GetName()
258-
}
259-
260-
// Return a string representation of the inventory item; makes InventoryItem implement the Stringer interface.
261-
func (i InventoryItem) String() string {
262-
return types.ObjectKeyToString(&i)
263-
}

pkg/component/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"context"
1010
"fmt"
1111

12-
"github.com/sap/component-operator-runtime/internal/cluster"
12+
"github.com/sap/component-operator-runtime/pkg/cluster"
1313
)
1414

1515
type reconcilerNameContextKey struct{}
Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ import (
3232

3333
"github.com/sap/component-operator-runtime/internal/backoff"
3434
"github.com/sap/component-operator-runtime/internal/cluster"
35-
"github.com/sap/component-operator-runtime/internal/kstatus"
3635
"github.com/sap/component-operator-runtime/pkg/manifests"
36+
"github.com/sap/component-operator-runtime/pkg/reconciler"
37+
"github.com/sap/component-operator-runtime/pkg/status"
3738
"github.com/sap/component-operator-runtime/pkg/types"
3839
)
3940

@@ -77,11 +78,11 @@ type ReconcilerOptions struct {
7778
// How to react if a dependent object exists but has no or a different owner.
7879
// If unspecified, AdoptionPolicyIfUnowned is assumed.
7980
// Can be overridden by annotation on object level.
80-
AdoptionPolicy *AdoptionPolicy
81+
AdoptionPolicy *reconciler.AdoptionPolicy
8182
// How to perform updates to dependent objects.
8283
// If unspecified, UpdatePolicyReplace is assumed.
8384
// Can be overridden by annotation on object level.
84-
UpdatePolicy *UpdatePolicy
85+
UpdatePolicy *reconciler.UpdatePolicy
8586
// Schemebuilder allows to define additional schemes to be made available in the
8687
// target client.
8788
SchemeBuilder types.SchemeBuilder
@@ -93,7 +94,7 @@ type Reconciler[T Component] struct {
9394
id string
9495
client cluster.Client
9596
resourceGenerator manifests.Generator
96-
statusAnalyzer kstatus.StatusAnalyzer
97+
statusAnalyzer status.StatusAnalyzer
9798
options ReconcilerOptions
9899
clients *cluster.ClientFactory
99100
backoff *backoff.Backoff
@@ -115,19 +116,21 @@ func NewReconciler[T Component](name string, resourceGenerator manifests.Generat
115116
options.CreateMissingNamespaces = ref(true)
116117
}
117118
if options.AdoptionPolicy == nil {
118-
options.AdoptionPolicy = ref(AdoptionPolicyIfUnowned)
119+
options.AdoptionPolicy = ref(reconciler.AdoptionPolicyIfUnowned)
119120
}
120121
if options.UpdatePolicy == nil {
121-
options.UpdatePolicy = ref(UpdatePolicyReplace)
122+
options.UpdatePolicy = ref(reconciler.UpdatePolicyReplace)
122123
}
123124

124125
return &Reconciler[T]{
125126
name: name,
126127
resourceGenerator: resourceGenerator,
127-
statusAnalyzer: kstatus.NewStatusAnalyzer(name),
128-
options: options,
129-
backoff: backoff.NewBackoff(10 * time.Second),
130-
postReadHooks: []HookFunc[T]{resolveReferences[T]},
128+
// TODO: make statusAnalyzer specifiable via options?
129+
statusAnalyzer: status.NewStatusAnalyzer(name),
130+
options: options,
131+
// TODO: make backoff configurable via options?
132+
backoff: backoff.NewBackoff(10 * time.Second),
133+
postReadHooks: []HookFunc[T]{resolveReferences[T]},
131134
}
132135
}
133136

@@ -248,7 +251,12 @@ func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (result
248251
if err != nil {
249252
return ctrl.Result{}, errors.Wrap(err, "error getting client for component")
250253
}
251-
target := newReconcileTarget[T](r.name, r.id, targetClient, r.resourceGenerator, r.statusAnalyzer, *r.options.CreateMissingNamespaces, *r.options.AdoptionPolicy, *r.options.UpdatePolicy)
254+
target := newReconcileTarget[T](r.name, r.id, targetClient, r.resourceGenerator, reconciler.ReconcilerOptions{
255+
CreateMissingNamespaces: r.options.CreateMissingNamespaces,
256+
AdoptionPolicy: r.options.AdoptionPolicy,
257+
UpdatePolicy: r.options.UpdatePolicy,
258+
StatusAnalyzer: r.statusAnalyzer,
259+
})
252260
hookCtx := newContext(ctx).WithClient(targetClient)
253261

254262
// do the reconciliation
@@ -271,7 +279,7 @@ func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (result
271279
return ctrl.Result{}, errors.Wrapf(err, "error running pre-reconcile hook (%d)", hookOrder)
272280
}
273281
}
274-
ok, err := target.Reconcile(ctx, component)
282+
ok, err := target.Apply(ctx, component)
275283
if err != nil {
276284
log.V(1).Info("error while reconciling dependent resources")
277285
return ctrl.Result{}, errors.Wrap(err, "error reconciling dependent resources")

0 commit comments

Comments
 (0)