Skip to content

Commit 35a3c4f

Browse files
committed
move annotations to separate package
1 parent 9af2269 commit 35a3c4f

File tree

9 files changed

+182
-57
lines changed

9 files changed

+182
-57
lines changed

api/v1alpha1/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const (
5151
AnnotationNodeHost = "ydb.tech/node-host"
5252
AnnotationNodeDomain = "ydb.tech/node-domain"
5353

54-
AnnotationValueTrue = "true"
54+
FinalizerRemote = "ydb.tech/remote-finalizer"
5555

5656
legacyTenantNameFormat = "/%s/%s"
5757
)

internal/annotations/annotations.go

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,85 @@
11
package annotations
22

3+
import (
4+
"github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
5+
corev1 "k8s.io/api/core/v1"
6+
)
7+
38
const (
4-
PrimaryResourceStorageAnnotation = "ydb.tech/primary-resource-storage"
5-
PrimaryResourceDatabaseAnnotation = "ydb.tech/primary-resource-database"
6-
RemoteResourceVersionAnnotation = "ydb.tech/remote-resource-version"
7-
ConfigurationChecksum = "ydb.tech/configuration-checksum"
8-
RemoteFinalizerKey = "ydb.tech/remote-finalizer"
9-
LastAppliedAnnotation = "ydb.tech/last-applied"
9+
PrimaryResourceStorage = "ydb.tech/primary-resource-storage"
10+
PrimaryResourceDatabase = "ydb.tech/primary-resource-database"
11+
RemoteResourceVersion = "ydb.tech/remote-resource-version"
12+
ConfigurationChecksum = "ydb.tech/configuration-checksum"
13+
LastApplied = "ydb.tech/last-applied"
14+
)
15+
16+
var (
17+
AcceptedDNSPolicy = []string{
18+
string(corev1.DNSClusterFirstWithHostNet),
19+
string(corev1.DNSClusterFirst),
20+
string(corev1.DNSDefault),
21+
string(corev1.DNSNone),
22+
}
23+
UserAnnotations = map[string]struct{}{
24+
v1alpha1.AnnotationSkipInitialization: struct{}{},
25+
v1alpha1.AnnotationUpdateStrategyOnDelete: struct{}{},
26+
v1alpha1.AnnotationUpdateDNSPolicy: struct{}{},
27+
v1alpha1.AnnotationDisableLivenessProbe: struct{}{},
28+
v1alpha1.AnnotationDataCenter: struct{}{},
29+
v1alpha1.AnnotationGRPCPublicHost: struct{}{},
30+
v1alpha1.AnnotationNodeHost: struct{}{},
31+
v1alpha1.AnnotationNodeDomain: struct{}{},
32+
}
1033
)
1134

35+
type Annotations map[string]string
36+
37+
func Common(objAnnotations Annotations) Annotations {
38+
an := Annotations{}
39+
40+
an.Merge(getUserAnnotations(objAnnotations))
41+
42+
return an
43+
}
44+
45+
func (an Annotations) Merge(other map[string]string) map[string]string {
46+
if other == nil {
47+
return an
48+
}
49+
50+
for k, v := range other {
51+
an[k] = v
52+
}
53+
54+
return an
55+
}
56+
57+
func (an Annotations) AsMap() map[string]string {
58+
return an
59+
}
60+
61+
func (an Annotations) Copy() Annotations {
62+
res := Annotations{}
63+
64+
for k, v := range an {
65+
res[k] = v
66+
}
67+
68+
return res
69+
}
70+
71+
func getUserAnnotations(annotations map[string]string) map[string]string {
72+
common := make(map[string]string)
73+
74+
for key, value := range annotations {
75+
if _, exists := UserAnnotations[key]; exists {
76+
common[key] = value
77+
}
78+
}
79+
80+
return common
81+
}
82+
1283
func CompareLastAppliedAnnotation(map1, map2 map[string]string) bool {
1384
value1 := getLastAppliedAnnotation(map1)
1485
value2 := getLastAppliedAnnotation(map2)
@@ -17,7 +88,7 @@ func CompareLastAppliedAnnotation(map1, map2 map[string]string) bool {
1788

1889
func getLastAppliedAnnotation(annotations map[string]string) string {
1990
for key, value := range annotations {
20-
if key == LastAppliedAnnotation {
91+
if key == LastApplied {
2192
return value
2293
}
2394
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package annotations_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
9+
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
10+
)
11+
12+
func TestLabels(t *testing.T) {
13+
RegisterFailHandler(Fail)
14+
RunSpecs(t, "Label suite")
15+
}
16+
17+
var _ = Describe("Testing annotations", func() {
18+
It("merges two sets of annotations", func() {
19+
fstLabels := annotations.Annotations{
20+
"a": "a",
21+
"b": "b",
22+
}
23+
24+
sndLabels := annotations.Annotations{
25+
"c": "c",
26+
"d": "d",
27+
}
28+
29+
Expect(fstLabels.Merge(sndLabels)).To(BeEquivalentTo(map[string]string{
30+
"a": "a",
31+
"b": "b",
32+
"c": "c",
33+
"d": "d",
34+
}))
35+
})
36+
37+
It("sets correct defaults", func() {
38+
Expect(annotations.Common(map[string]string{
39+
"ydb.tech/skip-initialization": "true",
40+
"ydb.tech/node-host": "ydb-testing.k8s-c.yandex.net",
41+
"ydb.tech/last-applied": "some-body",
42+
"sample-annotation": "test",
43+
})).To(BeEquivalentTo(map[string]string{
44+
"ydb.tech/skip-initialization": "true",
45+
"ydb.tech/node-host": "ydb-testing.k8s-c.yandex.net",
46+
}))
47+
})
48+
})

internal/controllers/database/init.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database
33
import (
44
"context"
55
"fmt"
6+
"strconv"
67

78
"github.com/ydb-platform/ydb-go-sdk/v3"
89
corev1 "k8s.io/api/core/v1"
@@ -34,15 +35,17 @@ func (r *Reconciler) setInitPipelineStatus(
3435
}
3536

3637
// This block is special internal logic that skips all Database initialization.
37-
if value, ok := database.Annotations[v1alpha1.AnnotationSkipInitialization]; ok && value == v1alpha1.AnnotationValueTrue {
38-
r.Log.Info("Database initialization disabled (with annotation), proceed with caution")
39-
r.Recorder.Event(
40-
database,
41-
corev1.EventTypeWarning,
42-
"SkippingInit",
43-
"Skipping initialization due to skip annotation present, be careful!",
44-
)
45-
return r.setInitDatabaseCompleted(ctx, database, "Database initialization not performed because initialization is skipped")
38+
if value, ok := database.Annotations[v1alpha1.AnnotationSkipInitialization]; ok {
39+
if isTrue, _ := strconv.ParseBool(value); isTrue {
40+
r.Log.Info("Database initialization disabled (with annotation), proceed with caution")
41+
r.Recorder.Event(
42+
database,
43+
corev1.EventTypeWarning,
44+
"SkippingInit",
45+
"Skipping initialization due to skip annotation present, be careful!",
46+
)
47+
return r.setInitDatabaseCompleted(ctx, database, "Database initialization not performed because initialization is skipped")
48+
}
4649
}
4750

4851
if meta.IsStatusConditionTrue(database.Status.Conditions, OldDatabaseInitializedCondition) {

internal/controllers/remotedatabasenodeset/controller.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/go-logr/logr"
77
corev1 "k8s.io/api/core/v1"
88
apierrors "k8s.io/apimachinery/pkg/api/errors"
9-
"k8s.io/apimachinery/pkg/labels"
9+
apilabels "k8s.io/apimachinery/pkg/labels"
1010
"k8s.io/apimachinery/pkg/runtime"
1111
"k8s.io/apimachinery/pkg/selection"
1212
"k8s.io/apimachinery/pkg/types"
@@ -23,9 +23,9 @@ import (
2323
"sigs.k8s.io/controller-runtime/pkg/source"
2424

2525
"github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
26-
ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
26+
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
2727
. "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck
28-
ydblabels "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
28+
"github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
2929
"github.com/ydb-platform/ydb-kubernetes-operator/internal/resources"
3030
)
3131

@@ -72,15 +72,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
7272
// The object is not being deleted, so if it does not have our finalizer,
7373
// then lets add the finalizer and update the object. This is equivalent
7474
// to registering our finalizer.
75-
if !controllerutil.ContainsFinalizer(remoteDatabaseNodeSet, ydbannotations.RemoteFinalizerKey) {
76-
controllerutil.AddFinalizer(remoteDatabaseNodeSet, ydbannotations.RemoteFinalizerKey)
75+
if !controllerutil.ContainsFinalizer(remoteDatabaseNodeSet, v1alpha1.FinalizerRemote) {
76+
controllerutil.AddFinalizer(remoteDatabaseNodeSet, v1alpha1.FinalizerRemote)
7777
if err := r.RemoteClient.Update(ctx, remoteDatabaseNodeSet); err != nil {
7878
return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err
7979
}
8080
}
8181
} else {
8282
// The object is being deleted
83-
if controllerutil.ContainsFinalizer(remoteDatabaseNodeSet, ydbannotations.RemoteFinalizerKey) {
83+
if controllerutil.ContainsFinalizer(remoteDatabaseNodeSet, v1alpha1.FinalizerRemote) {
8484
// our finalizer is present, so lets handle any external dependency
8585
if err := r.deleteExternalResources(ctx, remoteDatabaseNodeSet); err != nil {
8686
// if fail to delete the external dependency here, return with error
@@ -89,7 +89,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
8989
}
9090

9191
// remove our finalizer from the list and update it.
92-
controllerutil.RemoveFinalizer(remoteDatabaseNodeSet, ydbannotations.RemoteFinalizerKey)
92+
controllerutil.RemoveFinalizer(remoteDatabaseNodeSet, v1alpha1.FinalizerRemote)
9393
if err := r.RemoteClient.Update(ctx, remoteDatabaseNodeSet); err != nil {
9494
return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err
9595
}
@@ -118,8 +118,8 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C
118118
annotationFilter := func(mapObj client.Object) []reconcile.Request {
119119
requests := make([]reconcile.Request, 0)
120120

121-
annotations := mapObj.GetAnnotations()
122-
primaryResourceName, exist := annotations[ydbannotations.PrimaryResourceDatabaseAnnotation]
121+
an := mapObj.GetAnnotations()
122+
primaryResourceName, exist := an[annotations.PrimaryResourceDatabase]
123123
if exist {
124124
databaseNodeSets := &v1alpha1.DatabaseNodeSetList{}
125125
if err := r.Client.List(
@@ -192,32 +192,32 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C
192192
Complete(r)
193193
}
194194

195-
func buildLocalSelector() (labels.Selector, error) {
196-
labelRequirements := []labels.Requirement{}
197-
localClusterRequirement, err := labels.NewRequirement(
198-
ydblabels.RemoteClusterKey,
195+
func buildLocalSelector() (apilabels.Selector, error) {
196+
labelRequirements := []apilabels.Requirement{}
197+
localClusterRequirement, err := apilabels.NewRequirement(
198+
labels.RemoteClusterKey,
199199
selection.Exists,
200200
[]string{},
201201
)
202202
if err != nil {
203203
return nil, err
204204
}
205205
labelRequirements = append(labelRequirements, *localClusterRequirement)
206-
return labels.NewSelector().Add(labelRequirements...), nil
206+
return apilabels.NewSelector().Add(labelRequirements...), nil
207207
}
208208

209-
func BuildRemoteSelector(remoteCluster string) (labels.Selector, error) {
210-
labelRequirements := []labels.Requirement{}
211-
remoteClusterRequirement, err := labels.NewRequirement(
212-
ydblabels.RemoteClusterKey,
209+
func BuildRemoteSelector(remoteCluster string) (apilabels.Selector, error) {
210+
labelRequirements := []apilabels.Requirement{}
211+
remoteClusterRequirement, err := apilabels.NewRequirement(
212+
labels.RemoteClusterKey,
213213
selection.Equals,
214214
[]string{remoteCluster},
215215
)
216216
if err != nil {
217217
return nil, err
218218
}
219219
labelRequirements = append(labelRequirements, *remoteClusterRequirement)
220-
return labels.NewSelector().Add(labelRequirements...), nil
220+
return apilabels.NewSelector().Add(labelRequirements...), nil
221221
}
222222

223223
func (r *Reconciler) deleteExternalResources(

internal/controllers/remotedatabasenodeset/remote_objects.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (r *Reconciler) removeUnusedRemoteObjects(
278278
// Remove annotation if no one another DatabaseNodeSet
279279
if !existInDatabase {
280280
// Try to update existing object in local cluster by rawPatch
281-
patch := []byte(fmt.Sprintf(`{"metadata": {"annotations": {"%s": null}}}`, ydbannotations.PrimaryResourceStorageAnnotation))
281+
patch := []byte(fmt.Sprintf(`{"metadata": {"annotations": {"%s": null}}}`, ydbannotations.PrimaryResourceStorage))
282282
updateErr := r.Client.Patch(ctx, localObj, client.RawPatch(types.StrategicMergePatchType, patch))
283283
if updateErr != nil {
284284
r.Recorder.Event(
@@ -298,7 +298,7 @@ func (r *Reconciler) removeUnusedRemoteObjects(
298298
}
299299

300300
// Delete resource if annotation `ydb.tech/primary-resource-storage` does not exist
301-
_, existInStorage := localObj.GetAnnotations()[ydbannotations.PrimaryResourceStorageAnnotation]
301+
_, existInStorage := localObj.GetAnnotations()[ydbannotations.PrimaryResourceStorage]
302302
if !existInStorage {
303303
// Try to delete unused resource from local cluster
304304
deleteErr := r.Client.Delete(ctx, localObj)

internal/controllers/remotestoragenodeset/controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"sigs.k8s.io/controller-runtime/pkg/source"
2424

2525
"github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
26-
ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
26+
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
2727
. "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck
2828
ydblabels "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
2929
"github.com/ydb-platform/ydb-kubernetes-operator/internal/resources"
@@ -72,15 +72,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
7272
// The object is not being deleted, so if it does not have our finalizer,
7373
// then lets add the finalizer and update the object. This is equivalent
7474
// to registering our finalizer.
75-
if !controllerutil.ContainsFinalizer(remoteStorageNodeSet, ydbannotations.RemoteFinalizerKey) {
76-
controllerutil.AddFinalizer(remoteStorageNodeSet, ydbannotations.RemoteFinalizerKey)
75+
if !controllerutil.ContainsFinalizer(remoteStorageNodeSet, v1alpha1.FinalizerRemote) {
76+
controllerutil.AddFinalizer(remoteStorageNodeSet, v1alpha1.FinalizerRemote)
7777
if err := r.RemoteClient.Update(ctx, remoteStorageNodeSet); err != nil {
7878
return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err
7979
}
8080
}
8181
} else {
8282
// The object is being deleted
83-
if controllerutil.ContainsFinalizer(remoteStorageNodeSet, ydbannotations.RemoteFinalizerKey) {
83+
if controllerutil.ContainsFinalizer(remoteStorageNodeSet, v1alpha1.FinalizerRemote) {
8484
// our finalizer is present, so lets handle any external dependency
8585
if err := r.deleteExternalResources(ctx, remoteStorageNodeSet); err != nil {
8686
// if fail to delete the external dependency here, return with error
@@ -89,7 +89,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
8989
}
9090

9191
// remove our finalizer from the list and update it.
92-
controllerutil.RemoveFinalizer(remoteStorageNodeSet, ydbannotations.RemoteFinalizerKey)
92+
controllerutil.RemoveFinalizer(remoteStorageNodeSet, v1alpha1.FinalizerRemote)
9393
if err := r.RemoteClient.Update(ctx, remoteStorageNodeSet); err != nil {
9494
return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err
9595
}
@@ -118,8 +118,8 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C
118118
annotationFilter := func(mapObj client.Object) []reconcile.Request {
119119
requests := make([]reconcile.Request, 0)
120120

121-
annotations := mapObj.GetAnnotations()
122-
primaryResourceName, exist := annotations[ydbannotations.PrimaryResourceStorageAnnotation]
121+
an := mapObj.GetAnnotations()
122+
primaryResourceName, exist := an[annotations.PrimaryResourceStorage]
123123
if exist {
124124
storageNodeSets := &v1alpha1.StorageNodeSetList{}
125125
if err := r.Client.List(

internal/controllers/remotestoragenodeset/remote_objects.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"sigs.k8s.io/controller-runtime/pkg/client"
1717

1818
"github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
19-
ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
19+
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
2020
. "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck
2121
ydblabels "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
2222
"github.com/ydb-platform/ydb-kubernetes-operator/internal/resources"
@@ -277,7 +277,7 @@ func (r *Reconciler) removeUnusedRemoteObjects(
277277

278278
// Remove annotation if no one another StorageNodeSet
279279
if !existInStorage {
280-
patch := []byte(fmt.Sprintf(`{"metadata": {"annotations": {"%s": null}}}`, ydbannotations.PrimaryResourceStorageAnnotation))
280+
patch := []byte(fmt.Sprintf(`{"metadata": {"annotations": {"%s": null}}}`, annotations.PrimaryResourceStorage))
281281
updateErr := r.Client.Patch(ctx, localObj, client.RawPatch(types.StrategicMergePatchType, patch))
282282
if updateErr != nil {
283283
r.Recorder.Event(
@@ -297,7 +297,7 @@ func (r *Reconciler) removeUnusedRemoteObjects(
297297
}
298298

299299
// Delete resource if annotation `ydb.tech/primary-resource-database` does not exist
300-
_, existInDatabase := localObj.GetAnnotations()[ydbannotations.PrimaryResourceDatabaseAnnotation]
300+
_, existInDatabase := localObj.GetAnnotations()[annotations.PrimaryResourceDatabase]
301301
if !existInDatabase {
302302
// Try to delete unused resource from local cluster
303303
deleteErr := r.Client.Delete(ctx, localObj)

0 commit comments

Comments
 (0)