/
reconcile.go
269 lines (235 loc) · 8.03 KB
/
reconcile.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
Copyright 2020 The Flux 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.
Original version: https://github.com/fluxcd/flux2/blob/437a94367784541695fa68deba7a52b188d97ea8/cmd/flux/reconcile.go
*/
package flux
import (
"context"
"time"
"github.com/bombsimon/logrusr/v4"
helmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
kustomizationv1 "github.com/fluxcd/kustomize-controller/api/v1"
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
sourcev1beta2 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/sirupsen/logrus"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/retry"
kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
type reconcileCommand struct {
kind string
groupVersion schema.GroupVersion
object reconcilable
}
type reconcilable interface {
asClientObject() client.Object
isSuspended() bool
lastHandledReconcileRequest() string
successMessage() string
}
func NewReconcileCommand(resource string) *reconcileCommand {
switch resource {
case "kustomization":
return &reconcileCommand{
object: kustomizationAdapter{&kustomizationv1.Kustomization{}},
groupVersion: kustomizationv1.GroupVersion,
kind: kustomizationv1.KustomizationKind,
}
case "helmrelease":
return &reconcileCommand{
object: helmReleaseAdapter{&helmv2beta1.HelmRelease{}},
groupVersion: helmv2beta1.GroupVersion,
kind: helmv2beta1.HelmReleaseKind,
}
case sourcev1.GitRepositoryKind:
return &reconcileCommand{
object: gitRepositoryAdapter{&sourcev1.GitRepository{}},
groupVersion: sourcev1.GroupVersion,
kind: sourcev1.GitRepositoryKind,
}
case sourcev1beta2.OCIRepositoryKind:
return &reconcileCommand{
object: ociRepositoryAdapter{&sourcev1beta2.OCIRepository{}},
groupVersion: sourcev1beta2.GroupVersion,
kind: sourcev1beta2.OCIRepositoryKind,
}
case sourcev1beta2.BucketKind:
return &reconcileCommand{
object: bucketAdapter{&sourcev1beta2.Bucket{}},
groupVersion: sourcev1beta2.GroupVersion,
kind: sourcev1beta2.BucketKind,
}
case sourcev1beta2.HelmRepositoryKind:
return &reconcileCommand{
object: helmRepositoryAdapter{&sourcev1beta2.HelmRepository{}},
groupVersion: sourcev1beta2.GroupVersion,
kind: sourcev1beta2.HelmRepositoryKind,
}
case sourcev1beta2.HelmChartKind:
return &reconcileCommand{
object: helmChartAdapter{&sourcev1beta2.HelmChart{}},
groupVersion: sourcev1beta2.GroupVersion,
kind: sourcev1beta2.HelmChartKind,
}
}
return nil
}
func (r *reconcileCommand) Run(config *rest.Config, namespace, name string) {
scheme := runtime.NewScheme()
sourcev1.AddToScheme(scheme)
sourcev1beta2.AddToScheme(scheme)
kustomizationv1.AddToScheme(scheme)
helmv2beta1.AddToScheme(scheme)
log := logrusr.New(logrus.New())
logf.SetLogger(log)
kubeClient, err := client.NewWithWatch(config, client.Options{
Scheme: scheme,
})
if err != nil {
logrus.Errorf("kubernetes client initialization failed: %s", err)
return
}
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: name,
}
err = kubeClient.Get(context.TODO(), namespacedName, r.object.asClientObject())
if err != nil {
logrus.Error(err)
return
}
if r.object.isSuspended() {
logrus.Errorf("resource is suspended")
return
}
lastHandledReconcileAt := r.object.lastHandledReconcileRequest()
logrus.Infof("annotating %s %s in %s namespace", r.kind, name, namespace)
if err := requestReconciliation(context.TODO(), kubeClient, namespacedName,
r.groupVersion.WithKind(r.kind)); err != nil {
logrus.Error(err)
return
}
logrus.Infof("%s annotated", r.kind)
logrus.Infof("waiting for %s reconciliation", r.kind)
if err := wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, 5*time.Minute, true,
reconciliationHandled(kubeClient, namespacedName, r.object, lastHandledReconcileAt)); err != nil {
logrus.Error(err)
return
}
readyCond := apimeta.FindStatusCondition(reconcilableConditions(r.object), meta.ReadyCondition)
if readyCond == nil {
logrus.Errorf("status can't be determined")
return
}
if readyCond.Status != metav1.ConditionTrue {
logrus.Errorf("%s reconciliation failed: %s", r.kind, readyCond.Message)
return
}
logrus.Infof(r.object.successMessage())
}
// oldConditions represents the deprecated API which is sunsetting.
type oldConditions interface {
// this is usually implemented by GOTK API objects because it's used by pkg/apis/meta
GetStatusConditions() *[]metav1.Condition
}
func reconcilableConditions(object reconcilable) []metav1.Condition {
if s, ok := object.(meta.ObjectWithConditions); ok {
return s.GetConditions()
}
if s, ok := object.(oldConditions); ok {
return *s.GetStatusConditions()
}
return []metav1.Condition{}
}
func requestReconciliation(
ctx context.Context,
kubeClient client.Client,
namespacedName types.NamespacedName,
gvk schema.GroupVersionKind,
) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) {
object := &metav1.PartialObjectMetadata{}
object.SetGroupVersionKind(gvk)
object.SetName(namespacedName.Name)
object.SetNamespace(namespacedName.Namespace)
if err := kubeClient.Get(ctx, namespacedName, object); err != nil {
return err
}
patch := client.MergeFrom(object.DeepCopy())
// Add a timestamp annotation to trigger a reconciliation.
ts := time.Now().Format(time.RFC3339Nano)
annotations := object.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string, 1)
}
annotations[meta.ReconcileRequestAnnotation] = ts
// HelmRelease specific annotations to force a release.
if gvk.Kind == helmv2beta1.HelmReleaseKind {
annotations["reconcile.fluxcd.io/forceAt"] = ts
}
object.SetAnnotations(annotations)
return kubeClient.Patch(ctx, object, patch)
})
}
func reconciliationHandled(
kubeClient client.Client,
namespacedName types.NamespacedName,
obj reconcilable,
lastHandledReconcileAt string,
) wait.ConditionWithContextFunc {
return func(ctx context.Context) (bool, error) {
err := kubeClient.Get(ctx, namespacedName, obj.asClientObject())
if err != nil {
return false, err
}
if obj.lastHandledReconcileRequest() == lastHandledReconcileAt {
return false, nil
}
result, err := kstatusCompute(obj.asClientObject())
if err != nil {
return false, err
}
return result.Status == kstatus.CurrentStatus, nil
}
}
// kstatusCompute returns the kstatus computed result of a given object.
func kstatusCompute(obj client.Object) (result *kstatus.Result, err error) {
u, err := toUnstructured(obj)
if err != nil {
return result, err
}
return kstatus.Compute(u)
}
// ToUnstructured converts a runtime.Object into an Unstructured object.
func toUnstructured(obj runtime.Object) (*unstructured.Unstructured, error) {
// If the incoming object is already unstructured, perform a deep copy first
// otherwise DefaultUnstructuredConverter ends up returning the inner map without
// making a copy.
if _, ok := obj.(runtime.Unstructured); ok {
obj = obj.DeepCopyObject()
}
rawMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}
return &unstructured.Unstructured{Object: rawMap}, nil
}