Skip to content

Commit 55fe843

Browse files
pepovtarokkk
authored andcommitted
stabilize configchecks
- do not create new configcheck if one is still pending and running - sort all logging items before generating the config to get a consistent result
1 parent e9a3944 commit 55fe843

File tree

4 files changed

+183
-20
lines changed

4 files changed

+183
-20
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
apiVersion: logging.banzaicloud.io/v1beta1
2+
kind: Output
3+
metadata:
4+
name: output-sample
5+
spec:
6+
nullout: {}
7+
---
8+
apiVersion: logging.banzaicloud.io/v1beta1
9+
kind: Flow
10+
metadata:
11+
name: sample1
12+
spec:
13+
selectors:
14+
sampleKey: sampleValue1
15+
filters:
16+
- tag_normaliser: {}
17+
- stdout: {}
18+
outputRefs:
19+
- "output-sample"
20+
---
21+
apiVersion: logging.banzaicloud.io/v1beta1
22+
kind: Flow
23+
metadata:
24+
name: sample2
25+
spec:
26+
selectors:
27+
sampleKey: sampleValue2
28+
filters:
29+
- tag_normaliser: {}
30+
- stdout: {}
31+
outputRefs:
32+
- "output-sample"
33+
---
34+
apiVersion: logging.banzaicloud.io/v1beta1
35+
kind: Flow
36+
metadata:
37+
name: sample3
38+
spec:
39+
selectors:
40+
sampleKey: sampleValue3
41+
filters:
42+
- tag_normaliser: {}
43+
- stdout: {}
44+
outputRefs:
45+
- "output-sample"
46+
---
47+
apiVersion: logging.banzaicloud.io/v1beta1
48+
kind: Flow
49+
metadata:
50+
name: sample4
51+
spec:
52+
selectors:
53+
sampleKey: sampleValue4
54+
filters:
55+
- tag_normaliser: {}
56+
- stdout: {}
57+
outputRefs:
58+
- "output-sample"
59+
---
60+
apiVersion: logging.banzaicloud.io/v1beta1
61+
kind: Flow
62+
metadata:
63+
name: sample5
64+
spec:
65+
selectors:
66+
sampleKey: sampleValue5
67+
filters:
68+
- tag_normaliser: {}
69+
- stdout: {}
70+
outputRefs:
71+
- "output-sample"
72+
---

controllers/logging_controller.go

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"context"
2020
"regexp"
21+
"sort"
2122

2223
"emperror.dev/errors"
2324
"github.com/banzaicloud/logging-operator/pkg/resources"
@@ -257,8 +258,19 @@ func (r *LoggingReconciler) GetResources(logging *loggingv1beta1.Logging) (*mode
257258
if err != nil {
258259
return nil, err
259260
}
261+
260262
if len(clusterFlows.Items) > 0 {
261-
for _, i := range clusterFlows.Items {
263+
items := clusterFlows.Items
264+
sort.Slice(items, func(i, j int) bool {
265+
if items[i].GetNamespace() < items[j].GetNamespace() {
266+
return true
267+
}
268+
if items[i].GetNamespace() == items[j].GetNamespace() {
269+
return items[i].GetName() < items[j].GetName()
270+
}
271+
return false
272+
})
273+
for _, i := range items {
262274
if i.Spec.LoggingRef == logging.Spec.LoggingRef {
263275
loggingResources.ClusterFlows = append(loggingResources.ClusterFlows, i)
264276
}
@@ -270,8 +282,19 @@ func (r *LoggingReconciler) GetResources(logging *loggingv1beta1.Logging) (*mode
270282
if err != nil {
271283
return nil, err
272284
}
285+
273286
if len(clusterOutputs.Items) > 0 {
274-
for _, i := range clusterOutputs.Items {
287+
items := clusterOutputs.Items
288+
sort.Slice(items, func(i, j int) bool {
289+
if items[i].GetNamespace() < items[j].GetNamespace() {
290+
return true
291+
}
292+
if items[i].GetNamespace() == items[j].GetNamespace() {
293+
return items[i].GetName() < items[j].GetName()
294+
}
295+
return false
296+
})
297+
for _, i := range items {
275298
if i.Spec.LoggingRef == logging.Spec.LoggingRef {
276299
loggingResources.ClusterOutputs = append(loggingResources.ClusterOutputs, i)
277300
}
@@ -286,7 +309,17 @@ func (r *LoggingReconciler) GetResources(logging *loggingv1beta1.Logging) (*mode
286309
if err != nil {
287310
return nil, errors.WrapIf(err, "failed to list all namespaces")
288311
}
289-
for _, ns := range nsList.Items {
312+
items := nsList.Items
313+
sort.Slice(items, func(i, j int) bool {
314+
if items[i].GetNamespace() < items[j].GetNamespace() {
315+
return true
316+
}
317+
if items[i].GetNamespace() == items[j].GetNamespace() {
318+
return items[i].GetName() < items[j].GetName()
319+
}
320+
return false
321+
})
322+
for _, ns := range items {
290323
watchNamespaces = append(watchNamespaces, ns.Name)
291324
}
292325
}
@@ -297,20 +330,42 @@ func (r *LoggingReconciler) GetResources(logging *loggingv1beta1.Logging) (*mode
297330
if err != nil {
298331
return nil, err
299332
}
333+
300334
if len(flows.Items) > 0 {
301-
for _, i := range flows.Items {
335+
items := flows.Items
336+
sort.Slice(items, func(i, j int) bool {
337+
if items[i].GetNamespace() < items[j].GetNamespace() {
338+
return true
339+
}
340+
if items[i].GetNamespace() == items[j].GetNamespace() {
341+
return items[i].GetName() < items[j].GetName()
342+
}
343+
return false
344+
})
345+
for _, i := range items {
302346
if i.Spec.LoggingRef == logging.Spec.LoggingRef {
303347
loggingResources.Flows = append(loggingResources.Flows, i)
304348
}
305349
}
306350
}
351+
307352
outputs := &loggingv1beta1.OutputList{}
308353
err = r.List(context.TODO(), outputs, client.InNamespace(ns))
309354
if err != nil {
310355
return nil, err
311356
}
312357
if len(outputs.Items) > 0 {
313-
for _, i := range outputs.Items {
358+
items := outputs.Items
359+
sort.Slice(items, func(i, j int) bool {
360+
if items[i].GetNamespace() < items[j].GetNamespace() {
361+
return true
362+
}
363+
if items[i].GetNamespace() == items[j].GetNamespace() {
364+
return items[i].GetName() < items[j].GetName()
365+
}
366+
return false
367+
})
368+
for _, i := range items {
314369
if i.Spec.LoggingRef == logging.Spec.LoggingRef {
315370
loggingResources.Outputs = append(loggingResources.Outputs, i)
316371
}

pkg/resources/fluentd/appconfigmap.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ import (
2626
apierrors "k8s.io/apimachinery/pkg/api/errors"
2727
"k8s.io/apimachinery/pkg/runtime"
2828
"k8s.io/apimachinery/pkg/types"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
2930
)
3031

3132
type ConfigCheckResult struct {
32-
Valid bool
33-
Ready bool
33+
Valid bool
34+
Ready bool
35+
Message string
3436
}
3537

3638
const ConfigKey = "generated.conf"
@@ -58,8 +60,37 @@ func (r *Reconciler) configCheck() (*ConfigCheckResult, error) {
5860
if err != nil {
5961
return nil, err
6062
}
63+
6164
pod := r.newCheckPod(hashKey)
6265

66+
existingPods := &v1.PodList{}
67+
err = r.Client.List(context.TODO(), existingPods, client.MatchingLabels(pod.Labels))
68+
if err != nil {
69+
return nil, errors.WrapIf(err, "failed to list existing configcheck pods")
70+
}
71+
72+
podsByPhase := make(map[v1.PodPhase]int)
73+
for _, p := range existingPods.Items {
74+
if actual, ok := podsByPhase[p.Status.Phase]; ok {
75+
podsByPhase[p.Status.Phase] = actual + 1
76+
} else {
77+
podsByPhase[p.Status.Phase] = 1
78+
}
79+
}
80+
81+
if podsByPhase[v1.PodPending] > 0 {
82+
return &ConfigCheckResult{
83+
Ready: false,
84+
Message: "there are pending configcheck pods, need to back off",
85+
}, nil
86+
}
87+
if podsByPhase[v1.PodRunning] > 0 {
88+
return &ConfigCheckResult{
89+
Ready: false,
90+
Message: "there are running configcheck pods, need to back off",
91+
}, nil
92+
}
93+
6394
err = r.Client.Get(context.TODO(), types.NamespacedName{Namespace: pod.Namespace, Name: pod.Name}, pod)
6495
if err == nil {
6596
// check pod status and write into the configmap
@@ -151,7 +182,7 @@ func (r *Reconciler) configCheckCleanup(currentHash string) ([]string, error) {
151182

152183
func (r *Reconciler) newCheckSecret(hashKey string) *v1.Secret {
153184
return &v1.Secret{
154-
ObjectMeta: r.FluentdObjectMeta(fmt.Sprintf("fluentd-configcheck-%s", hashKey), ComponentFluentd),
185+
ObjectMeta: r.FluentdObjectMeta(fmt.Sprintf("fluentd-configcheck-%s", hashKey), ComponentConfigCheck),
155186
Data: map[string][]byte{
156187
ConfigKey: []byte(*r.config),
157188
},
@@ -164,7 +195,7 @@ func (r *Reconciler) newCheckOutputSecret(hashKey string) (*v1.Secret, error) {
164195
return nil, err
165196
}
166197
if secret, ok := obj.(*v1.Secret); ok {
167-
secret.ObjectMeta = r.FluentdObjectMeta(fmt.Sprintf("fluentd-configcheck-output-%s", hashKey), ComponentFluentd)
198+
secret.ObjectMeta = r.FluentdObjectMeta(fmt.Sprintf("fluentd-configcheck-output-%s", hashKey), ComponentConfigCheck)
168199
return secret, nil
169200
}
170201
return nil, errors.New("output secret is invalid, unable to create output secret for config check")

pkg/resources/fluentd/fluentd.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,17 @@ func (r *Reconciler) Reconcile() (*reconcile.Result, error) {
110110
var removedHashes []string
111111
if removedHashes, err = r.configCheckCleanup(hash); err != nil {
112112
r.Log.Error(err, "failed to cleanup resources")
113-
}
114-
if len(removedHashes) > 0 {
115-
for _, removedHash := range removedHashes {
116-
delete(r.Logging.Status.ConfigCheckResults, removedHash)
117-
}
118-
if err := r.Client.Status().Update(context.TODO(), r.Logging); err != nil {
119-
return nil, errors.WrapWithDetails(err, "failed to update status", "logging", r.Logging)
120-
} else {
121-
// explicitly ask for a requeue to short circuit the controller loop after the status update
122-
return &reconcile.Result{Requeue: true}, nil
113+
} else {
114+
if len(removedHashes) > 0 {
115+
for _, removedHash := range removedHashes {
116+
delete(r.Logging.Status.ConfigCheckResults, removedHash)
117+
}
118+
if err := r.Client.Status().Update(context.TODO(), r.Logging); err != nil {
119+
return nil, errors.WrapWithDetails(err, "failed to update status", "logging", r.Logging)
120+
} else {
121+
// explicitly ask for a requeue to short circuit the controller loop after the status update
122+
return &reconcile.Result{Requeue: true}, nil
123+
}
123124
}
124125
}
125126
} else {
@@ -139,7 +140,11 @@ func (r *Reconciler) Reconcile() (*reconcile.Result, error) {
139140
return &reconcile.Result{Requeue: true}, nil
140141
}
141142
} else {
142-
r.Log.Info("still waiting for the configcheck result...")
143+
if result.Message != "" {
144+
r.Log.Info(result.Message)
145+
} else {
146+
r.Log.Info("still waiting for the configcheck result...")
147+
}
143148
return &reconcile.Result{RequeueAfter: time.Second}, nil
144149
}
145150
}

0 commit comments

Comments
 (0)