Skip to content

Commit 15d1c10

Browse files
committed
ignore CREATE event from owns objects
1 parent 5c82a99 commit 15d1c10

File tree

14 files changed

+200
-114
lines changed

14 files changed

+200
-114
lines changed

deploy/ydb-operator/Chart.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type: application
1515
# This is the chart version. This version number should be incremented each time you make changes
1616
# to the chart and its templates, including the app version.
1717
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: 0.5.17
18+
version: 0.5.18
1919

2020
# This is the version number of the application being deployed. This version number should be
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
2323
# It is recommended to use it with quotes.
24-
appVersion: "0.5.17"
24+
appVersion: "0.5.18"

internal/controllers/database/controller.go

+23-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"k8s.io/client-go/rest"
1414
"k8s.io/client-go/tools/record"
1515
ctrl "sigs.k8s.io/controller-runtime"
16+
"sigs.k8s.io/controller-runtime/pkg/builder"
1617
"sigs.k8s.io/controller-runtime/pkg/client"
1718
"sigs.k8s.io/controller-runtime/pkg/handler"
1819
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -69,12 +70,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
6970
r.Log.Error(err, "unexpected Get error")
7071
return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err
7172
}
72-
result, err := r.Sync(ctx, resource)
73-
if err != nil {
74-
r.Log.Error(err, "unexpected Sync error")
75-
}
7673

77-
return result, err
74+
return r.Sync(ctx, resource)
7875
}
7976

8077
// Create FieldIndexer to usage for List requests in Reconcile
@@ -152,23 +149,31 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
152149
}
153150

154151
return controller.
155-
For(&v1alpha1.Database{}).
156-
Owns(&v1alpha1.RemoteDatabaseNodeSet{}).
157-
Owns(&v1alpha1.DatabaseNodeSet{}).
158-
Owns(&appsv1.StatefulSet{}).
159-
Owns(&corev1.ConfigMap{}).
160-
Owns(&corev1.Service{}).
152+
For(&v1alpha1.Database{},
153+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
154+
).
155+
Owns(&v1alpha1.RemoteDatabaseNodeSet{},
156+
builder.WithPredicates(resources.LastAppliedAnnotationPredicate()), // TODO: YDBOPS-9194
157+
).
158+
Owns(&v1alpha1.DatabaseNodeSet{},
159+
builder.WithPredicates(resources.LastAppliedAnnotationPredicate()), // TODO: YDBOPS-9194
160+
).
161+
Owns(&appsv1.StatefulSet{},
162+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
163+
).
164+
Owns(&corev1.ConfigMap{},
165+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
166+
).
167+
Owns(&corev1.Service{},
168+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
169+
).
161170
Watches(
162171
&source.Kind{Type: &corev1.Secret{}},
163172
handler.EnqueueRequestsFromMapFunc(r.findDatabasesForSecret),
173+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
164174
).
165-
WithEventFilter(predicate.Or(
166-
predicate.GenerationChangedPredicate{},
167-
resources.LastAppliedAnnotationPredicate(),
168-
resources.IsServicePredicate(),
169-
resources.IsSecretPredicate(),
170-
)).
171-
WithEventFilter(resources.IgnoreDeletetionPredicate()).
175+
WithEventFilter(resources.IsDatabaseCreatePredicate()).
176+
WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()).
172177
Complete(r)
173178
}
174179

internal/controllers/databasenodeset/controller.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"k8s.io/client-go/rest"
1111
"k8s.io/client-go/tools/record"
1212
ctrl "sigs.k8s.io/controller-runtime"
13+
"sigs.k8s.io/controller-runtime/pkg/builder"
1314
"sigs.k8s.io/controller-runtime/pkg/client"
1415
"sigs.k8s.io/controller-runtime/pkg/log"
1516
"sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -66,12 +67,13 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
6667
controller := ctrl.NewControllerManagedBy(mgr)
6768

6869
return controller.
69-
For(&v1alpha1.DatabaseNodeSet{}).
70-
Owns(&appsv1.StatefulSet{}).
71-
WithEventFilter(predicate.Or(
72-
predicate.GenerationChangedPredicate{},
73-
resources.LastAppliedAnnotationPredicate()),
70+
For(&v1alpha1.DatabaseNodeSet{},
71+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
7472
).
75-
WithEventFilter(resources.IgnoreDeletetionPredicate()).
73+
Owns(&appsv1.StatefulSet{},
74+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
75+
).
76+
WithEventFilter(resources.IsDatabaseNodeSetCreatePredicate()).
77+
WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()).
7678
Complete(r)
7779
}

internal/controllers/remotedatabasenodeset/controller.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -165,31 +165,30 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C
165165
Watches(
166166
source.NewKindWithCache(&v1alpha1.RemoteDatabaseNodeSet{}, cluster.GetCache()),
167167
&handler.EnqueueRequestForObject{},
168-
builder.WithPredicates(predicate.Or(
169-
predicate.GenerationChangedPredicate{},
170-
resources.LastAppliedAnnotationPredicate(),
171-
)),
168+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
172169
).
173170
Watches(
174171
&source.Kind{Type: &v1alpha1.DatabaseNodeSet{}},
175172
&handler.EnqueueRequestForObject{},
176-
builder.WithPredicates(
177-
resources.LabelExistsPredicate(isNodeSetFromMgmt),
178-
),
173+
builder.WithPredicates(resources.LabelExistsPredicate(isNodeSetFromMgmt)),
179174
).
180175
Watches(
181176
&source.Kind{Type: &corev1.Service{}},
182177
handler.EnqueueRequestsFromMapFunc(annotationFilter),
178+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
183179
).
184180
Watches(
185-
&source.Kind{Type: &corev1.Secret{}},
181+
&source.Kind{Type: &corev1.ConfigMap{}},
186182
handler.EnqueueRequestsFromMapFunc(annotationFilter),
183+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
187184
).
188185
Watches(
189-
&source.Kind{Type: &corev1.ConfigMap{}},
186+
&source.Kind{Type: &corev1.Secret{}},
190187
handler.EnqueueRequestsFromMapFunc(annotationFilter),
188+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
191189
).
192-
WithEventFilter(resources.IgnoreDeletetionPredicate()).
190+
WithEventFilter(resources.IsRemoteDatabaseNodeSetCreatePredicate()).
191+
WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()).
193192
Complete(r)
194193
}
195194

internal/controllers/remotestoragenodeset/controller.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -165,31 +165,30 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C
165165
Watches(
166166
source.NewKindWithCache(&v1alpha1.RemoteStorageNodeSet{}, cluster.GetCache()),
167167
&handler.EnqueueRequestForObject{},
168-
builder.WithPredicates(predicate.Or(
169-
predicate.GenerationChangedPredicate{},
170-
resources.LastAppliedAnnotationPredicate(),
171-
)),
168+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
172169
).
173170
Watches(
174171
&source.Kind{Type: &v1alpha1.StorageNodeSet{}},
175172
&handler.EnqueueRequestForObject{},
176-
builder.WithPredicates(
177-
resources.LabelExistsPredicate(isNodeSetFromMgmt),
178-
),
173+
builder.WithPredicates(resources.LabelExistsPredicate(isNodeSetFromMgmt)),
179174
).
180175
Watches(
181176
&source.Kind{Type: &corev1.Service{}},
182177
handler.EnqueueRequestsFromMapFunc(annotationFilter),
178+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
183179
).
184180
Watches(
185-
&source.Kind{Type: &corev1.Secret{}},
181+
&source.Kind{Type: &corev1.ConfigMap{}},
186182
handler.EnqueueRequestsFromMapFunc(annotationFilter),
183+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
187184
).
188185
Watches(
189-
&source.Kind{Type: &corev1.ConfigMap{}},
186+
&source.Kind{Type: &corev1.Secret{}},
190187
handler.EnqueueRequestsFromMapFunc(annotationFilter),
188+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
191189
).
192-
WithEventFilter(resources.IgnoreDeletetionPredicate()).
190+
WithEventFilter(resources.IsRemoteStorageNodeSetCreatePredicate()).
191+
WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()).
193192
Complete(r)
194193
}
195194

internal/controllers/storage/controller.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"k8s.io/client-go/rest"
1515
"k8s.io/client-go/tools/record"
1616
ctrl "sigs.k8s.io/controller-runtime"
17+
"sigs.k8s.io/controller-runtime/pkg/builder"
1718
"sigs.k8s.io/controller-runtime/pkg/client"
1819
"sigs.k8s.io/controller-runtime/pkg/handler"
1920
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -160,23 +161,31 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
160161
}
161162

162163
return controller.
163-
For(&v1alpha1.Storage{}).
164-
Owns(&v1alpha1.RemoteStorageNodeSet{}).
165-
Owns(&v1alpha1.StorageNodeSet{}).
166-
Owns(&appsv1.StatefulSet{}).
167-
Owns(&corev1.ConfigMap{}).
168-
Owns(&corev1.Service{}).
164+
For(&v1alpha1.Storage{},
165+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
166+
).
167+
Owns(&v1alpha1.RemoteStorageNodeSet{},
168+
builder.WithPredicates(resources.LastAppliedAnnotationPredicate()), // TODO: YDBOPS-9194
169+
).
170+
Owns(&v1alpha1.StorageNodeSet{},
171+
builder.WithPredicates(resources.LastAppliedAnnotationPredicate()), // TODO: YDBOPS-9194
172+
).
173+
Owns(&appsv1.StatefulSet{},
174+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
175+
).
176+
Owns(&corev1.ConfigMap{},
177+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
178+
).
179+
Owns(&corev1.Service{},
180+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
181+
).
169182
Watches(
170183
&source.Kind{Type: &corev1.Secret{}},
171184
handler.EnqueueRequestsFromMapFunc(r.findStoragesForSecret),
185+
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
172186
).
173-
WithEventFilter(predicate.Or(
174-
predicate.GenerationChangedPredicate{},
175-
resources.LastAppliedAnnotationPredicate(),
176-
resources.IsServicePredicate(),
177-
resources.IsSecretPredicate(),
178-
)).
179-
WithEventFilter(resources.IgnoreDeletetionPredicate()).
187+
WithEventFilter(resources.IsStorageCreatePredicate()).
188+
WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()).
180189
Complete(r)
181190
}
182191

internal/controllers/storage/init.go

+12
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ func (r *Reconciler) initializeBlobstorage(
119119
return Stop, ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err
120120
}
121121

122+
r.Recorder.Event(
123+
storage,
124+
corev1.EventTypeNormal,
125+
"InitializingStorage",
126+
fmt.Sprintf("Successfuly created Job %s", fmt.Sprintf(resources.InitJobNameFormat, storage.Name)),
127+
)
122128
return Stop, ctrl.Result{RequeueAfter: StorageInitializationRequeueDelay}, nil
123129
}
124130

@@ -207,6 +213,12 @@ func (r *Reconciler) initializeBlobstorage(
207213
return r.updateStatus(ctx, storage, StatusUpdateRequeueDelay)
208214
}
209215

216+
r.Recorder.Event(
217+
storage,
218+
corev1.EventTypeNormal,
219+
"InitializingStorage",
220+
fmt.Sprintf("Waiting for Job %s status update", initJob.Name),
221+
)
210222
return Stop, ctrl.Result{RequeueAfter: StorageInitializationRequeueDelay}, nil
211223
}
212224

internal/controllers/storagenodeset/controller.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"k8s.io/client-go/rest"
1111
"k8s.io/client-go/tools/record"
1212
ctrl "sigs.k8s.io/controller-runtime"
13+
"sigs.k8s.io/controller-runtime/pkg/builder"
1314
"sigs.k8s.io/controller-runtime/pkg/client"
1415
"sigs.k8s.io/controller-runtime/pkg/log"
1516
"sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -65,12 +66,13 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
6566
controller := ctrl.NewControllerManagedBy(mgr)
6667

6768
return controller.
68-
For(&v1alpha1.StorageNodeSet{}).
69-
Owns(&appsv1.StatefulSet{}).
70-
WithEventFilter(predicate.Or(
71-
predicate.GenerationChangedPredicate{},
72-
resources.LastAppliedAnnotationPredicate()),
69+
For(&v1alpha1.StorageNodeSet{},
70+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
7371
).
74-
WithEventFilter(resources.IgnoreDeletetionPredicate()).
72+
Owns(&appsv1.StatefulSet{},
73+
builder.WithPredicates(predicate.GenerationChangedPredicate{}),
74+
).
75+
WithEventFilter(resources.IsStorageNodeSetCreatePredicate()).
76+
WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()).
7577
Complete(r)
7678
}

internal/resources/databasenodeset.go

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func (b *DatabaseNodeSetResource) GetResourceBuilders(restConfig *rest.Config) [
8282
func NewDatabaseNodeSet(databaseNodeSet *api.DatabaseNodeSet) DatabaseNodeSetResource {
8383
crDatabaseNodeSet := databaseNodeSet.DeepCopy()
8484

85+
if crDatabaseNodeSet.Spec.Service.Status.TLSConfiguration == nil {
86+
crDatabaseNodeSet.Spec.Service.Status.TLSConfiguration = &api.TLSConfiguration{Enabled: false}
87+
}
88+
8589
return DatabaseNodeSetResource{DatabaseNodeSet: crDatabaseNodeSet}
8690
}
8791

internal/resources/predicate.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package resources
2+
3+
import (
4+
"k8s.io/apimachinery/pkg/labels"
5+
"sigs.k8s.io/controller-runtime/pkg/client"
6+
"sigs.k8s.io/controller-runtime/pkg/event"
7+
"sigs.k8s.io/controller-runtime/pkg/predicate"
8+
9+
api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
10+
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
11+
)
12+
13+
func LastAppliedAnnotationPredicate() predicate.Predicate {
14+
return predicate.Funcs{
15+
UpdateFunc: func(e event.UpdateEvent) bool {
16+
return !annotations.CompareYdbTechAnnotations(
17+
e.ObjectOld.GetAnnotations(),
18+
e.ObjectNew.GetAnnotations(),
19+
)
20+
},
21+
}
22+
}
23+
24+
func IsStorageCreatePredicate() predicate.Predicate {
25+
return predicate.Funcs{
26+
CreateFunc: func(e event.CreateEvent) bool {
27+
_, isStorage := e.Object.(*api.Storage)
28+
return isStorage
29+
},
30+
}
31+
}
32+
33+
func IsStorageNodeSetCreatePredicate() predicate.Predicate {
34+
return predicate.Funcs{
35+
CreateFunc: func(e event.CreateEvent) bool {
36+
_, isStorageNodeSet := e.Object.(*api.StorageNodeSet)
37+
return isStorageNodeSet
38+
},
39+
}
40+
}
41+
42+
func IsRemoteStorageNodeSetCreatePredicate() predicate.Predicate {
43+
return predicate.Funcs{
44+
CreateFunc: func(e event.CreateEvent) bool {
45+
_, isRemoteStorageNodeSet := e.Object.(*api.RemoteStorageNodeSet)
46+
return isRemoteStorageNodeSet
47+
},
48+
}
49+
}
50+
51+
func IsDatabaseCreatePredicate() predicate.Predicate {
52+
return predicate.Funcs{
53+
CreateFunc: func(e event.CreateEvent) bool {
54+
_, isDatabase := e.Object.(*api.Database)
55+
return isDatabase
56+
},
57+
}
58+
}
59+
60+
func IsDatabaseNodeSetCreatePredicate() predicate.Predicate {
61+
return predicate.Funcs{
62+
CreateFunc: func(e event.CreateEvent) bool {
63+
_, isDatabaseNodeSet := e.Object.(*api.DatabaseNodeSet)
64+
return isDatabaseNodeSet
65+
},
66+
}
67+
}
68+
69+
func IsRemoteDatabaseNodeSetCreatePredicate() predicate.Predicate {
70+
return predicate.Funcs{
71+
CreateFunc: func(e event.CreateEvent) bool {
72+
_, isRemoteDatabaseNodeSet := e.Object.(*api.RemoteDatabaseNodeSet)
73+
return isRemoteDatabaseNodeSet
74+
},
75+
}
76+
}
77+
78+
func IgnoreDeleteStateUnknownPredicate() predicate.Predicate {
79+
return predicate.Funcs{
80+
DeleteFunc: func(e event.DeleteEvent) bool {
81+
// Evaluates to false if the object has been confirmed deleted.
82+
return !e.DeleteStateUnknown
83+
},
84+
}
85+
}
86+
87+
func LabelExistsPredicate(selector labels.Selector) predicate.Predicate {
88+
return predicate.NewPredicateFuncs(func(o client.Object) bool {
89+
return selector.Matches(labels.Set(o.GetLabels()))
90+
})
91+
}

0 commit comments

Comments
 (0)