Skip to content

Commit 5affe38

Browse files
committed
update resources builder logic
1 parent 35a3c4f commit 5affe38

File tree

6 files changed

+77
-65
lines changed

6 files changed

+77
-65
lines changed

internal/resources/database_statefulset.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"regexp"
99
"strconv"
10+
"strings"
1011

1112
appsv1 "k8s.io/api/apps/v1"
1213
corev1 "k8s.io/api/core/v1"
@@ -16,6 +17,7 @@ import (
1617
"sigs.k8s.io/controller-runtime/pkg/client"
1718

1819
api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
20+
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
1921
"github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
2022
"github.com/ydb-platform/ydb-kubernetes-operator/internal/ptr"
2123
)
@@ -63,9 +65,11 @@ func (b *DatabaseStatefulSetBuilder) Build(obj client.Object) error {
6365
Template: b.buildPodTemplateSpec(),
6466
}
6567

66-
if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateStrategyOnDelete]; ok && value == api.AnnotationValueTrue {
67-
sts.Spec.UpdateStrategy = appsv1.StatefulSetUpdateStrategy{
68-
Type: "OnDelete",
68+
if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateStrategyOnDelete]; ok {
69+
if isTrue, _ := strconv.ParseBool(value); isTrue {
70+
sts.Spec.UpdateStrategy = appsv1.StatefulSetUpdateStrategy{
71+
Type: "OnDelete",
72+
}
6973
}
7074
}
7175

@@ -129,12 +133,10 @@ func (b *DatabaseStatefulSetBuilder) buildPodTemplateSpec() corev1.PodTemplateSp
129133
}
130134

131135
if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateDNSPolicy]; ok {
132-
switch value {
133-
case string(corev1.DNSClusterFirstWithHostNet), string(corev1.DNSClusterFirst), string(corev1.DNSDefault), string(corev1.DNSNone):
134-
podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value)
135-
case "":
136-
podTemplate.Spec.DNSPolicy = corev1.DNSClusterFirst
137-
default:
136+
for _, acceptedPolicy := range annotations.AcceptedDNSPolicy {
137+
if value == acceptedPolicy {
138+
podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value)
139+
}
138140
}
139141
}
140142

@@ -412,13 +414,17 @@ func (b *DatabaseStatefulSetBuilder) buildContainer() corev1.Container {
412414
},
413415
}
414416

415-
if value, ok := b.ObjectMeta.Annotations[api.AnnotationDisableLivenessProbe]; !ok || value != api.AnnotationValueTrue {
416-
container.LivenessProbe = &corev1.Probe{
417-
ProbeHandler: corev1.ProbeHandler{
418-
TCPSocket: &corev1.TCPSocketAction{
419-
Port: intstr.FromInt(api.GRPCPort),
420-
},
417+
container.LivenessProbe = &corev1.Probe{
418+
ProbeHandler: corev1.ProbeHandler{
419+
TCPSocket: &corev1.TCPSocketAction{
420+
Port: intstr.FromInt(api.GRPCPort),
421421
},
422+
},
423+
}
424+
425+
if value, ok := b.ObjectMeta.Annotations[api.AnnotationDisableLivenessProbe]; ok {
426+
if isTrue, _ := strconv.ParseBool(value); isTrue {
427+
container.LivenessProbe = nil
422428
}
423429
}
424430

@@ -648,6 +654,9 @@ func (b *DatabaseStatefulSetBuilder) buildContainerArgs() ([]string, []string) {
648654
}
649655

650656
if value, ok := b.ObjectMeta.Annotations[api.AnnotationNodeHost]; ok {
657+
if !strings.HasPrefix(value, "$(NODE_NAME).") {
658+
value = fmt.Sprintf("%s.%s", "$(NODE_NAME)", value)
659+
}
651660
args = append(args,
652661
"--node-host",
653662
value,

internal/resources/remotedatabasenodeset.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
1313

1414
api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
15-
ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
15+
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
1616
. "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck
1717
)
1818

@@ -62,7 +62,7 @@ func (b *RemoteDatabaseNodeSetResource) GetResourceBuilders() []ResourceBuilder
6262
var resourceBuilders []ResourceBuilder
6363

6464
nodeSetAnnotations := CopyDict(b.Annotations)
65-
delete(nodeSetAnnotations, ydbannotations.LastAppliedAnnotation)
65+
delete(nodeSetAnnotations, annotations.LastApplied)
6666

6767
resourceBuilders = append(resourceBuilders,
6868
&DatabaseNodeSetBuilder{
@@ -164,26 +164,26 @@ func (b *RemoteDatabaseNodeSetResource) GetRemoteObjects(
164164
}
165165

166166
func (b *RemoteDatabaseNodeSetResource) SetPrimaryResourceAnnotations(obj client.Object) {
167-
annotations := make(map[string]string)
167+
an := make(map[string]string)
168168
for key, value := range obj.GetAnnotations() {
169-
annotations[key] = value
169+
an[key] = value
170170
}
171171

172-
if _, exist := annotations[ydbannotations.PrimaryResourceDatabaseAnnotation]; !exist {
173-
annotations[ydbannotations.PrimaryResourceDatabaseAnnotation] = b.Spec.DatabaseRef.Name
172+
if _, exist := an[annotations.PrimaryResourceDatabase]; !exist {
173+
an[annotations.PrimaryResourceDatabase] = b.Spec.DatabaseRef.Name
174174
}
175175

176-
obj.SetAnnotations(annotations)
176+
obj.SetAnnotations(an)
177177
}
178178

179179
func (b *RemoteDatabaseNodeSetResource) UnsetPrimaryResourceAnnotations(obj client.Object) {
180-
annotations := make(map[string]string)
180+
an := make(map[string]string)
181181
for key, value := range obj.GetAnnotations() {
182-
if key != annotations[ydbannotations.PrimaryResourceDatabaseAnnotation] {
183-
annotations[key] = value
182+
if key != an[annotations.PrimaryResourceDatabase] {
183+
an[key] = value
184184
}
185185
}
186-
obj.SetAnnotations(annotations)
186+
obj.SetAnnotations(an)
187187
}
188188

189189
func (b *RemoteDatabaseNodeSetResource) CreateRemoteResourceStatus(

internal/resources/remotestoragenodeset.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
1313

1414
api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
15-
ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
15+
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
1616
. "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck
1717
)
1818

@@ -62,7 +62,7 @@ func (b *RemoteStorageNodeSetResource) GetResourceBuilders() []ResourceBuilder {
6262
var resourceBuilders []ResourceBuilder
6363

6464
nodeSetAnnotations := CopyDict(b.Annotations)
65-
delete(nodeSetAnnotations, ydbannotations.LastAppliedAnnotation)
65+
delete(nodeSetAnnotations, annotations.LastApplied)
6666

6767
resourceBuilders = append(resourceBuilders,
6868
&StorageNodeSetBuilder{
@@ -145,26 +145,26 @@ func (b *RemoteStorageNodeSetResource) GetRemoteObjects(
145145
}
146146

147147
func (b *RemoteStorageNodeSetResource) SetPrimaryResourceAnnotations(obj client.Object) {
148-
annotations := make(map[string]string)
148+
an := make(map[string]string)
149149
for key, value := range obj.GetAnnotations() {
150-
annotations[key] = value
150+
an[key] = value
151151
}
152152

153-
if _, exist := annotations[ydbannotations.PrimaryResourceStorageAnnotation]; !exist {
154-
annotations[ydbannotations.PrimaryResourceStorageAnnotation] = b.Spec.StorageRef.Name
153+
if _, exist := an[annotations.PrimaryResourceStorage]; !exist {
154+
an[annotations.PrimaryResourceStorage] = b.Spec.StorageRef.Name
155155
}
156156

157-
obj.SetAnnotations(annotations)
157+
obj.SetAnnotations(an)
158158
}
159159

160160
func (b *RemoteStorageNodeSetResource) UnsetPrimaryResourceAnnotations(obj client.Object) {
161-
annotations := make(map[string]string)
161+
an := make(map[string]string)
162162
for key, value := range obj.GetAnnotations() {
163-
if key != annotations[ydbannotations.PrimaryResourceStorageAnnotation] {
164-
annotations[key] = value
163+
if key != an[annotations.PrimaryResourceStorage] {
164+
an[key] = value
165165
}
166166
}
167-
obj.SetAnnotations(annotations)
167+
obj.SetAnnotations(an)
168168
}
169169

170170
func (b *RemoteStorageNodeSetResource) CreateRemoteResourceStatus(remoteObj client.Object) {

internal/resources/resource.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2626

2727
api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
28-
ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
28+
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
2929
"github.com/ydb-platform/ydb-kubernetes-operator/internal/connection"
3030
)
3131

@@ -82,7 +82,7 @@ type ResourceBuilder interface {
8282
}
8383

8484
var (
85-
annotator = patch.NewAnnotator(ydbannotations.LastAppliedAnnotation)
85+
annotator = patch.NewAnnotator(annotations.LastApplied)
8686
patchMaker = patch.NewPatchMaker(annotator)
8787
)
8888

@@ -263,23 +263,23 @@ func UpdateResource(oldObj, newObj client.Object) client.Object {
263263
}
264264

265265
func CopyPrimaryResourceObjectAnnotation(obj client.Object, oldAnnotations map[string]string) {
266-
annotations := CopyDict(obj.GetAnnotations())
266+
an := CopyDict(obj.GetAnnotations())
267267
for key, value := range oldAnnotations {
268-
if key == ydbannotations.PrimaryResourceDatabaseAnnotation ||
269-
key == ydbannotations.PrimaryResourceStorageAnnotation {
270-
annotations[key] = value
268+
if key == annotations.PrimaryResourceDatabase ||
269+
key == annotations.PrimaryResourceStorage {
270+
an[key] = value
271271
}
272272
}
273-
obj.SetAnnotations(annotations)
273+
obj.SetAnnotations(an)
274274
}
275275

276276
func SetRemoteResourceVersionAnnotation(obj client.Object, resourceVersion string) {
277-
annotations := make(map[string]string)
277+
an := make(map[string]string)
278278
for key, value := range obj.GetAnnotations() {
279-
annotations[key] = value
279+
an[key] = value
280280
}
281-
annotations[ydbannotations.RemoteResourceVersionAnnotation] = resourceVersion
282-
obj.SetAnnotations(annotations)
281+
an[annotations.RemoteResourceVersion] = resourceVersion
282+
obj.SetAnnotations(an)
283283
}
284284

285285
func ConvertRemoteResourceToObject(remoteResource api.RemoteResource, namespace string) (client.Object, error) {

internal/resources/storage_init_job.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,10 @@ func (b *StorageInitJobBuilder) buildInitJobPodTemplateSpec() corev1.PodTemplate
134134
}
135135

136136
if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateDNSPolicy]; ok {
137-
switch value {
138-
case string(corev1.DNSClusterFirstWithHostNet), string(corev1.DNSClusterFirst), string(corev1.DNSDefault), string(corev1.DNSNone):
139-
podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value)
140-
case "":
141-
podTemplate.Spec.DNSPolicy = corev1.DNSClusterFirst
142-
default:
137+
for _, acceptedPolicy := range annotations.AcceptedDNSPolicy {
138+
if value == acceptedPolicy {
139+
podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value)
140+
}
143141
}
144142
}
145143

internal/resources/storage_statefulset.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ func (b *StorageStatefulSetBuilder) Build(obj client.Object) error {
8080
Template: b.buildPodTemplateSpec(),
8181
}
8282

83-
if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateStrategyOnDelete]; ok && value == api.AnnotationValueTrue {
84-
sts.Spec.UpdateStrategy = appsv1.StatefulSetUpdateStrategy{
85-
Type: "OnDelete",
83+
if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateStrategyOnDelete]; ok {
84+
if isTrue, _ := strconv.ParseBool(value); isTrue {
85+
sts.Spec.UpdateStrategy = appsv1.StatefulSetUpdateStrategy{
86+
Type: "OnDelete",
87+
}
8688
}
8789
}
8890

@@ -153,9 +155,8 @@ func (b *StorageStatefulSetBuilder) buildPodTemplateSpec() corev1.PodTemplateSpe
153155
switch value {
154156
case string(corev1.DNSClusterFirstWithHostNet), string(corev1.DNSClusterFirst), string(corev1.DNSDefault), string(corev1.DNSNone):
155157
podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value)
156-
case "":
157-
podTemplate.Spec.DNSPolicy = corev1.DNSClusterFirst
158158
default:
159+
podTemplate.Spec.DNSPolicy = corev1.DNSClusterFirst
159160
}
160161
}
161162

@@ -379,13 +380,17 @@ func (b *StorageStatefulSetBuilder) buildContainer() corev1.Container { // todo
379380
Resources: containerResources,
380381
}
381382

382-
if value, ok := b.ObjectMeta.Annotations[api.AnnotationDisableLivenessProbe]; !ok || value != api.AnnotationValueTrue {
383-
container.LivenessProbe = &corev1.Probe{
384-
ProbeHandler: corev1.ProbeHandler{
385-
TCPSocket: &corev1.TCPSocketAction{
386-
Port: intstr.FromInt(api.GRPCPort),
387-
},
383+
container.LivenessProbe = &corev1.Probe{
384+
ProbeHandler: corev1.ProbeHandler{
385+
TCPSocket: &corev1.TCPSocketAction{
386+
Port: intstr.FromInt(api.GRPCPort),
388387
},
388+
},
389+
}
390+
391+
if value, ok := b.ObjectMeta.Annotations[api.AnnotationDisableLivenessProbe]; ok {
392+
if isTrue, _ := strconv.ParseBool(value); isTrue {
393+
container.LivenessProbe = nil
389394
}
390395
}
391396

0 commit comments

Comments
 (0)