@@ -22,6 +22,7 @@ import (
22
22
"github.com/pkg/errors"
23
23
apierrors "k8s.io/apimachinery/pkg/api/errors"
24
24
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25
+ kerrors "k8s.io/apimachinery/pkg/util/errors"
25
26
"k8s.io/klog/v2"
26
27
"sigs.k8s.io/controller-runtime/pkg/client"
27
28
@@ -34,11 +35,8 @@ type resourceReconcileScope interface {
34
35
// needsApply determines if a resource needs to be applied to the target cluster
35
36
// based on the strategy.
36
37
needsApply () bool
37
- // apply reconciles the resource to the target cluster following a different process
38
- // depending on the strategy.
39
- apply (ctx context.Context , c client.Client , obj * unstructured.Unstructured ) error
40
- // objs returns all the Kubernetes objects defined in the resource.
41
- objs () []unstructured.Unstructured
38
+ // apply reconciles all objects defined by the resource following the proper strategy for the CRS.
39
+ apply (ctx context.Context , c client.Client ) error
42
40
// hash returns a computed hash of the defined objects in the resource. It is consistent
43
41
// between runs.
44
42
hash () string
@@ -116,7 +114,11 @@ func (r *reconcileStrategyScope) needsApply() bool {
116
114
return resourceBinding == nil || ! resourceBinding .Applied || resourceBinding .Hash != r .computedHash
117
115
}
118
116
119
- func (r * reconcileStrategyScope ) apply (ctx context.Context , c client.Client , obj * unstructured.Unstructured ) error {
117
+ func (r * reconcileStrategyScope ) apply (ctx context.Context , c client.Client ) error {
118
+ return apply (ctx , c , r .applyObj , r .objs ())
119
+ }
120
+
121
+ func (r * reconcileStrategyScope ) applyObj (ctx context.Context , c client.Client , obj * unstructured.Unstructured ) error {
120
122
currentObj := & unstructured.Unstructured {}
121
123
currentObj .SetAPIVersion (obj .GetAPIVersion ())
122
124
currentObj .SetKind (obj .GetKind ())
@@ -156,11 +158,28 @@ func (r *reconcileApplyOnceScope) needsApply() bool {
156
158
return ! r .resourceSetBinding .IsApplied (r .resourceRef )
157
159
}
158
160
159
- func (r * reconcileApplyOnceScope ) apply (ctx context.Context , c client.Client , obj * unstructured.Unstructured ) error {
161
+ func (r * reconcileApplyOnceScope ) apply (ctx context.Context , c client.Client ) error {
162
+ return apply (ctx , c , r .applyObj , r .objs ())
163
+ }
164
+
165
+ func (r * reconcileApplyOnceScope ) applyObj (ctx context.Context , c client.Client , obj * unstructured.Unstructured ) error {
160
166
// The create call is idempotent, so if the object already exists
161
167
// then do not consider it to be an error.
162
168
if err := createUnstructured (ctx , c , obj ); ! apierrors .IsAlreadyExists (err ) {
163
169
return err
164
170
}
165
171
return nil
166
172
}
173
+
174
+ type applyObj func (ctx context.Context , c client.Client , obj * unstructured.Unstructured ) error
175
+
176
+ // apply reconciles unstructured objects using applyObj and aggreates the error if present.
177
+ func apply (ctx context.Context , c client.Client , applyObj applyObj , objs []unstructured.Unstructured ) error {
178
+ errList := []error {}
179
+ for i := range objs {
180
+ if err := applyObj (ctx , c , & objs [i ]); err != nil {
181
+ errList = append (errList , err )
182
+ }
183
+ }
184
+ return kerrors .NewAggregate (errList )
185
+ }
0 commit comments