-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
clusterresourceset_scope.go
186 lines (155 loc) · 5.48 KB
/
clusterresourceset_scope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
)
// resourceReconcileScope contains the scope for a CRS's resource
// reconciliation request.
type resourceReconcileScope interface {
// needsApply determines if a resource needs to be applied to the target cluster
// based on the strategy.
needsApply() bool
// apply reconciles all objects defined by the resource following the proper strategy for the CRS.
apply(ctx context.Context, c client.Client) error
// hash returns a computed hash of the defined objects in the resource. It is consistent
// between runs.
hash() string
}
func reconcileScopeForResource(
crs *addonsv1.ClusterResourceSet,
resourceRef addonsv1.ResourceRef,
resourceSetBinding *addonsv1.ResourceSetBinding,
resource *unstructured.Unstructured,
) (resourceReconcileScope, error) {
normalizedData, err := normalizeData(resource)
if err != nil {
return nil, err
}
objs, err := objsFromYamlData(normalizedData)
if err != nil {
return nil, err
}
return newResourceReconcileScope(crs, resourceRef, resourceSetBinding, normalizedData, objs), nil
}
func newResourceReconcileScope(
clusterResourceSet *addonsv1.ClusterResourceSet,
resourceRef addonsv1.ResourceRef,
resourceSetBinding *addonsv1.ResourceSetBinding,
normalizedData [][]byte,
objs []unstructured.Unstructured,
) resourceReconcileScope {
base := baseResourceReconcileScope{
clusterResourceSet: clusterResourceSet,
resourceRef: resourceRef,
resourceSetBinding: resourceSetBinding,
data: normalizedData,
normalizedObjs: objs,
computedHash: computeHash(normalizedData),
}
switch addonsv1.ClusterResourceSetStrategy(clusterResourceSet.Spec.Strategy) {
case addonsv1.ClusterResourceSetStrategyApplyOnce:
return &reconcileApplyOnceScope{base}
case addonsv1.ClusterResourceSetStrategyReconcile:
return &reconcileStrategyScope{base}
default:
return nil
}
}
type baseResourceReconcileScope struct {
clusterResourceSet *addonsv1.ClusterResourceSet
resourceRef addonsv1.ResourceRef
resourceSetBinding *addonsv1.ResourceSetBinding
normalizedObjs []unstructured.Unstructured
data [][]byte
computedHash string
}
func (b baseResourceReconcileScope) objs() []unstructured.Unstructured {
return b.normalizedObjs
}
func (b baseResourceReconcileScope) hash() string {
return b.computedHash
}
type reconcileStrategyScope struct {
baseResourceReconcileScope
}
func (r *reconcileStrategyScope) needsApply() bool {
resourceBinding := r.resourceSetBinding.GetResource(r.resourceRef)
return resourceBinding == nil || !resourceBinding.Applied || resourceBinding.Hash != r.computedHash
}
func (r *reconcileStrategyScope) apply(ctx context.Context, c client.Client) error {
return apply(ctx, c, r.applyObj, r.objs())
}
func (r *reconcileStrategyScope) applyObj(ctx context.Context, c client.Client, obj *unstructured.Unstructured) error {
currentObj := &unstructured.Unstructured{}
currentObj.SetAPIVersion(obj.GetAPIVersion())
currentObj.SetKind(obj.GetKind())
err := c.Get(ctx, client.ObjectKeyFromObject(obj), currentObj)
if apierrors.IsNotFound(err) {
return createUnstructured(ctx, c, obj)
}
if err != nil {
return errors.Wrapf(
err,
"reading object %s %s",
obj.GroupVersionKind(),
klog.KObj(obj),
)
}
patch := client.MergeFrom(currentObj.DeepCopy())
obj.SetResourceVersion(currentObj.GetResourceVersion())
if err = c.Patch(ctx, obj, patch); err != nil {
return errors.Wrapf(
err,
"patching object %s %s",
obj.GroupVersionKind(),
klog.KObj(obj),
)
}
return nil
}
type reconcileApplyOnceScope struct {
baseResourceReconcileScope
}
func (r *reconcileApplyOnceScope) needsApply() bool {
return !r.resourceSetBinding.IsApplied(r.resourceRef)
}
func (r *reconcileApplyOnceScope) apply(ctx context.Context, c client.Client) error {
return apply(ctx, c, r.applyObj, r.objs())
}
func (r *reconcileApplyOnceScope) applyObj(ctx context.Context, c client.Client, obj *unstructured.Unstructured) error {
// The create call is idempotent, so if the object already exists
// then do not consider it to be an error.
if err := createUnstructured(ctx, c, obj); err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
type applyObj func(ctx context.Context, c client.Client, obj *unstructured.Unstructured) error
// apply reconciles unstructured objects using applyObj and aggreates the error if present.
func apply(ctx context.Context, c client.Client, applyObj applyObj, objs []unstructured.Unstructured) error {
errList := []error{}
for i := range objs {
if err := applyObj(ctx, c, &objs[i]); err != nil {
errList = append(errList, err)
}
}
return kerrors.NewAggregate(errList)
}