-
Notifications
You must be signed in to change notification settings - Fork 27
/
nebulacluster_component.go
324 lines (276 loc) · 8.51 KB
/
nebulacluster_component.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
Copyright 2021 Vesoft Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/pointer"
)
// +k8s:deepcopy-gen=false
type ComponentAccessor interface {
Replicas() int32
PodImage() string
Resources() *corev1.ResourceRequirements
PodLabels() map[string]string
PodAnnotations() map[string]string
PodEnvVars() []corev1.EnvVar
NodeSelector() map[string]string
Affinity() *corev1.Affinity
Tolerations() []corev1.Toleration
TopologySpreadConstraints(labels map[string]string) []corev1.TopologySpreadConstraint
SecurityContext() *corev1.SecurityContext
InitContainers() []corev1.Container
SidecarContainers() []corev1.Container
Volumes() []corev1.Volume
VolumeMounts() []corev1.VolumeMount
ReadinessProbe() *corev1.Probe
LivenessProbe() *corev1.Probe
}
var _ ComponentAccessor = &componentAccessor{}
// +k8s:deepcopy-gen=false
type componentAccessor struct {
nodeSelector map[string]string
affinity *corev1.Affinity
tolerations []corev1.Toleration
topologySpreadConstraints []TopologySpreadConstraint
componentSpec *ComponentSpec
}
func (a *componentAccessor) Replicas() int32 {
if a.componentSpec == nil {
return 0
}
return pointer.Int32Deref(a.componentSpec.Replicas, 0)
}
func (a *componentAccessor) PodImage() string {
if a.componentSpec == nil {
return ""
}
return fmt.Sprintf("%s:%s", a.componentSpec.Image, a.componentSpec.Version)
}
func (a *componentAccessor) Resources() *corev1.ResourceRequirements {
if a.componentSpec == nil {
return nil
}
return getResources(a.componentSpec.Resources)
}
func (a *componentAccessor) PodLabels() map[string]string {
if a.componentSpec == nil {
return nil
}
return a.componentSpec.Labels
}
func (a *componentAccessor) PodAnnotations() map[string]string {
if a.componentSpec == nil {
return nil
}
return a.componentSpec.Annotations
}
func (a *componentAccessor) PodEnvVars() []corev1.EnvVar {
if a.componentSpec == nil {
return nil
}
return a.componentSpec.EnvVars
}
func (a *componentAccessor) NodeSelector() map[string]string {
selector := map[string]string{}
for k, v := range a.nodeSelector {
selector[k] = v
}
if a.componentSpec != nil {
for k, v := range a.componentSpec.NodeSelector {
selector[k] = v
}
}
return selector
}
func (a *componentAccessor) Affinity() *corev1.Affinity {
if a.componentSpec == nil || a.componentSpec.Affinity == nil {
return a.affinity
}
return a.componentSpec.Affinity
}
func (a *componentAccessor) Tolerations() []corev1.Toleration {
if a.componentSpec == nil || len(a.componentSpec.Tolerations) == 0 {
return a.tolerations
}
return a.componentSpec.Tolerations
}
func (a *componentAccessor) TopologySpreadConstraints(labels map[string]string) []corev1.TopologySpreadConstraint {
tscs := a.topologySpreadConstraints
if a.componentSpec != nil && len(a.componentSpec.TopologySpreadConstraints) > 0 {
tscs = a.componentSpec.TopologySpreadConstraints
}
if len(tscs) == 0 {
return nil
}
return getTopologySpreadConstraints(tscs, labels)
}
func (a *componentAccessor) SecurityContext() *corev1.SecurityContext {
if a.componentSpec == nil {
return nil
}
return a.componentSpec.SecurityContext
}
func (a *componentAccessor) InitContainers() []corev1.Container {
if a.componentSpec == nil {
return nil
}
return a.componentSpec.InitContainers
}
func (a *componentAccessor) SidecarContainers() []corev1.Container {
if a.componentSpec == nil {
return nil
}
return a.componentSpec.SidecarContainers
}
func (a *componentAccessor) Volumes() []corev1.Volume {
if a.componentSpec == nil {
return nil
}
return a.componentSpec.Volumes
}
func (a *componentAccessor) VolumeMounts() []corev1.VolumeMount {
if a.componentSpec == nil {
return nil
}
return a.componentSpec.VolumeMounts
}
func (a *componentAccessor) ReadinessProbe() *corev1.Probe {
if a.componentSpec == nil {
return nil
}
return a.componentSpec.ReadinessProbe
}
func (a *componentAccessor) LivenessProbe() *corev1.Probe {
if a.componentSpec == nil {
return nil
}
return a.componentSpec.LivenessProbe
}
// NebulaClusterComponent is the interface for component
// +k8s:deepcopy-gen=false
type NebulaClusterComponent interface {
BaseComponent
GetLogStorageResources() *corev1.ResourceRequirements
GetDataStorageResources() (*corev1.ResourceRequirements, error)
GetConfig() map[string]string
GetConfigMapKey() string
IsSSLEnabled() bool
GetServiceSpec() *ServiceSpec
GetHeadlessServiceName() string
GetServiceFQDN() string
GetPodFQDN(ordinal int32) string
GetPort(portName string) int32
GetConnAddress(portName string) string
GetEndpoints(portName string) []string
GenerateLabels() map[string]string
GenerateContainerPorts() []corev1.ContainerPort
GenerateVolumeMounts() []corev1.VolumeMount
GenerateVolumes() []corev1.Volume
GenerateVolumeClaim() ([]corev1.PersistentVolumeClaim, error)
GenerateWorkload(gvk schema.GroupVersionKind, cm *corev1.ConfigMap) (*unstructured.Unstructured, error)
GenerateService() *corev1.Service
GenerateHeadlessService() *corev1.Service
GenerateConfigMap() *corev1.ConfigMap
IsReady() bool
GetUpdateRevision() string
SetPhase(phase ComponentPhase)
GetPhase() ComponentPhase
IsSuspending() bool
IsSuspended() bool
IsAutoFailovering() bool
SetWorkloadStatus(status *WorkloadStatus)
SetVolumeStatus(status *VolumeStatus)
UpdateComponentStatus(status *ComponentStatus)
}
// +k8s:deepcopy-gen=false
type BaseComponent interface {
GraphdComponent() NebulaClusterComponent
MetadComponent() NebulaClusterComponent
StoragedComponent() NebulaClusterComponent
ComponentSpec() ComponentAccessor
ComponentType() ComponentType
GetNebulaCluster() *NebulaCluster
GetClusterName() string
GetNamespace() string
GetName() string
GetPodName(ordinal int32) string
GenerateOwnerReferences() []metav1.OwnerReference
}
var _ BaseComponent = &baseComponent{}
// ComponentType is the type of NebulaCluster Component: graphd, metad or storaged
// +k8s:deepcopy-gen=false
type ComponentType string
func (typ ComponentType) String() string {
return string(typ)
}
// +k8s:deepcopy-gen=false
type baseComponent struct {
nc *NebulaCluster
typ ComponentType
}
func (c *baseComponent) ComponentSpec() ComponentAccessor {
var spec *ComponentSpec
if c.typ == GraphdComponentType && c.nc.Spec.Graphd != nil {
spec = &c.nc.Spec.Graphd.ComponentSpec
}
if c.typ == MetadComponentType && c.nc.Spec.Metad != nil {
spec = &c.nc.Spec.Metad.ComponentSpec
}
if c.typ == StoragedComponentType && c.nc.Spec.Storaged != nil {
spec = &c.nc.Spec.Storaged.ComponentSpec
}
return buildComponentAccessor(c.nc, spec)
}
func (c *baseComponent) GraphdComponent() NebulaClusterComponent {
return newGraphdComponent(c.nc)
}
func (c *baseComponent) MetadComponent() NebulaClusterComponent {
return newMetadComponent(c.nc)
}
func (c *baseComponent) StoragedComponent() NebulaClusterComponent {
return newStoragedComponent(c.nc)
}
func (c *baseComponent) ComponentType() ComponentType {
return c.typ
}
func (c *baseComponent) GetNebulaCluster() *NebulaCluster {
return c.nc
}
func (c *baseComponent) GetClusterName() string {
return c.nc.GetClusterName()
}
func (c *baseComponent) GetNamespace() string {
return c.nc.Namespace
}
func (c *baseComponent) GetName() string {
return getComponentName(c.GetClusterName(), c.ComponentType())
}
func (c *baseComponent) GetPodName(ordinal int32) string {
return getPodName(c.GetName(), ordinal)
}
func (c *baseComponent) GenerateOwnerReferences() []metav1.OwnerReference {
return c.nc.GenerateOwnerReferences()
}
func buildComponentAccessor(nc *NebulaCluster, componentSpec *ComponentSpec) ComponentAccessor {
return &componentAccessor{
nodeSelector: nc.Spec.NodeSelector,
affinity: nc.Spec.Affinity,
tolerations: nc.Spec.Tolerations,
topologySpreadConstraints: nc.Spec.TopologySpreadConstraints,
componentSpec: componentSpec,
}
}