Skip to content

Commit 6c656f8

Browse files
committed
Implement top loevel apply methoid in resource scopes
1 parent c033f00 commit 6c656f8

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

exp/addons/internal/controllers/clusterresourceset_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
320320
// Apply all values in the key-value pair of the resource to the cluster.
321321
// As there can be multiple key-value pairs in a resource, each value may have multiple objects in it.
322322
isSuccessful := true
323-
if err := apply(ctx, remoteClient, resourceScope); err != nil {
323+
if err := resourceScope.apply(ctx, remoteClient); err != nil {
324324
isSuccessful = false
325325
log.Error(err, "failed to apply ClusterResourceSet resource", "Resource kind", resource.Kind, "Resource name", resource.Name)
326326
conditions.MarkFalse(clusterResourceSet, addonsv1.ResourcesAppliedCondition, addonsv1.ApplyFailedReason, clusterv1.ConditionSeverityWarning, err.Error())

exp/addons/internal/controllers/clusterresourceset_helpers.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3434
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3535
"k8s.io/apimachinery/pkg/types"
36-
kerrors "k8s.io/apimachinery/pkg/util/errors"
3736
"k8s.io/klog/v2"
3837
"sigs.k8s.io/controller-runtime/pkg/client"
3938

@@ -94,19 +93,6 @@ func isJSONList(data []byte) (bool, error) {
9493
return bytes.HasPrefix(trim, jsonListPrefix), nil
9594
}
9695

97-
// apply reconciles all objects defined by a resource from a ClusterResourceSet
98-
// delegating on the reconcileScope for the applying strategy.
99-
func apply(ctx context.Context, c client.Client, scope resourceReconcileScope) error {
100-
errList := []error{}
101-
objs := scope.objs()
102-
for i := range objs {
103-
if err := scope.apply(ctx, c, &objs[i]); err != nil {
104-
errList = append(errList, err)
105-
}
106-
}
107-
return kerrors.NewAggregate(errList)
108-
}
109-
11096
func createUnstructured(ctx context.Context, c client.Client, obj *unstructured.Unstructured) error {
11197
if err := c.Create(ctx, obj); err != nil {
11298
return errors.Wrapf(

exp/addons/internal/controllers/clusterresourceset_scope.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/pkg/errors"
2323
apierrors "k8s.io/apimachinery/pkg/api/errors"
2424
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
kerrors "k8s.io/apimachinery/pkg/util/errors"
2526
"k8s.io/klog/v2"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728

@@ -34,11 +35,8 @@ type resourceReconcileScope interface {
3435
// needsApply determines if a resource needs to be applied to the target cluster
3536
// based on the strategy.
3637
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
4240
// hash returns a computed hash of the defined objects in the resource. It is consistent
4341
// between runs.
4442
hash() string
@@ -116,7 +114,11 @@ func (r *reconcileStrategyScope) needsApply() bool {
116114
return resourceBinding == nil || !resourceBinding.Applied || resourceBinding.Hash != r.computedHash
117115
}
118116

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 {
120122
currentObj := &unstructured.Unstructured{}
121123
currentObj.SetAPIVersion(obj.GetAPIVersion())
122124
currentObj.SetKind(obj.GetKind())
@@ -156,11 +158,28 @@ func (r *reconcileApplyOnceScope) needsApply() bool {
156158
return !r.resourceSetBinding.IsApplied(r.resourceRef)
157159
}
158160

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 {
160166
// The create call is idempotent, so if the object already exists
161167
// then do not consider it to be an error.
162168
if err := createUnstructured(ctx, c, obj); !apierrors.IsAlreadyExists(err) {
163169
return err
164170
}
165171
return nil
166172
}
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+
}

exp/addons/internal/controllers/clusterresourceset_scope_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424
corev1 "k8s.io/api/core/v1"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27-
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
2827
"sigs.k8s.io/controller-runtime/pkg/client"
2928
"sigs.k8s.io/controller-runtime/pkg/client/fake"
29+
30+
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
3031
)
3132

3233
func TestReconcileStrategyScopeNeedsApply(t *testing.T) {
@@ -191,7 +192,7 @@ func TestReconcileApplyOnceScopeNeedsApply(t *testing.T) {
191192
}
192193
}
193194

194-
func TestReconcileApplyOnceScopeApply(t *testing.T) {
195+
func TestReconcileApplyOnceScopeApplyObj(t *testing.T) {
195196
tests := []struct {
196197
name string
197198
existingObjs []client.Object
@@ -239,7 +240,7 @@ func TestReconcileApplyOnceScopeApply(t *testing.T) {
239240
ctx := context.Background()
240241
client := fake.NewClientBuilder().WithObjects(tt.existingObjs...).Build()
241242
scope := &reconcileApplyOnceScope{}
242-
err := scope.apply(ctx, client, tt.obj)
243+
err := scope.applyObj(ctx, client, tt.obj)
243244
if tt.wantErr == "" {
244245
gs.Expect(err).NotTo(HaveOccurred())
245246
} else {

0 commit comments

Comments
 (0)