-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathstoragenodeset.go
116 lines (87 loc) · 3.07 KB
/
storagenodeset.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
package resources
import (
"errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
)
type StorageNodeSetBuilder struct {
client.Object
Name string
Labels map[string]string
Annotations map[string]string
StorageNodeSetSpec api.StorageNodeSetSpec
}
func (b *StorageNodeSetBuilder) Build(obj client.Object) error {
sns, ok := obj.(*api.StorageNodeSet)
if !ok {
return errors.New("failed to cast to StorageNodeSet object")
}
if sns.ObjectMeta.Name == "" {
sns.ObjectMeta.Name = b.Name
}
sns.ObjectMeta.Namespace = b.GetNamespace()
sns.ObjectMeta.Labels = b.Labels
sns.ObjectMeta.Annotations = b.Annotations
sns.Spec = b.StorageNodeSetSpec
return nil
}
func (b *StorageNodeSetBuilder) Placeholder(cr client.Object) client.Object {
return &api.StorageNodeSet{
ObjectMeta: metav1.ObjectMeta{
Name: b.Name,
Namespace: cr.GetNamespace(),
},
}
}
type StorageNodeSetResource struct {
*api.StorageNodeSet
}
func (b *StorageNodeSetResource) NewLabels() labels.Labels {
l := labels.Common(b.Name, b.Labels)
l.Merge(map[string]string{labels.ComponentKey: labels.StorageComponent})
storageNodeSetName := b.Labels[labels.StorageNodeSetComponent]
l.Merge(map[string]string{labels.StorageNodeSetComponent: storageNodeSetName})
if remoteCluster, exist := b.Labels[labels.RemoteClusterKey]; exist {
l.Merge(map[string]string{labels.RemoteClusterKey: remoteCluster})
}
return l
}
func (b *StorageNodeSetResource) NewAnnotations() ydbannotations.Annotations {
annotations := ydbannotations.Common(b.Annotations)
return annotations
}
func (b *StorageNodeSetResource) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder {
nodeSetLabels := b.NewLabels()
nodeSetAnnotations := b.NewAnnotations()
statefulSetLabels := nodeSetLabels.Copy()
statefulSetLabels.Merge(b.Spec.AdditionalLabels)
statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name})
statefulSetAnnotations := nodeSetAnnotations.Copy()
statefulSetAnnotations.Merge(b.Spec.AdditionalAnnotations)
var resourceBuilders []ResourceBuilder
resourceBuilders = append(
resourceBuilders,
&StorageStatefulSetBuilder{
Storage: api.RecastStorageNodeSet(b.StorageNodeSet),
RestConfig: restConfig,
Name: b.Name,
Labels: statefulSetLabels,
Annotations: statefulSetAnnotations,
},
)
return resourceBuilders
}
func NewStorageNodeSet(storageNodeSet *api.StorageNodeSet) StorageNodeSetResource {
crStorageNodeSet := storageNodeSet.DeepCopy()
if crStorageNodeSet.Spec.Service.Status.TLSConfiguration == nil {
crStorageNodeSet.Spec.Service.Status.TLSConfiguration = &api.TLSConfiguration{Enabled: false}
}
return StorageNodeSetResource{StorageNodeSet: crStorageNodeSet}
}
func (b *StorageNodeSetResource) Unwrap() *api.StorageNodeSet {
return b.DeepCopy()
}